test_operations.cpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #include "test_precomp.hpp"
  43. #include "opencv2/ts/ocl_test.hpp" // T-API like tests
  44. namespace opencv_test {
  45. namespace {
  46. class CV_OperationsTest : public cvtest::BaseTest
  47. {
  48. public:
  49. CV_OperationsTest();
  50. ~CV_OperationsTest();
  51. protected:
  52. void run(int);
  53. struct test_excep
  54. {
  55. test_excep(const string& _s=string("")) : s(_s) { }
  56. string s;
  57. };
  58. bool SomeMatFunctions();
  59. bool TestMat();
  60. template<typename _Tp> void TestType(Size sz, _Tp value);
  61. bool TestTemplateMat();
  62. bool TestMatND();
  63. bool TestSparseMat();
  64. bool TestVec();
  65. bool TestMatxMultiplication();
  66. bool TestMatxElementwiseDivison();
  67. bool TestDivisionByValue();
  68. bool TestInplaceDivisionByValue();
  69. bool TestMatMatxCastSum();
  70. bool TestSubMatAccess();
  71. bool TestExp();
  72. bool TestSVD();
  73. bool operations1();
  74. void checkDiff(const Mat& m1, const Mat& m2, const string& s)
  75. {
  76. if (cvtest::norm(m1, m2, NORM_INF) != 0) throw test_excep(s);
  77. }
  78. void checkDiffF(const Mat& m1, const Mat& m2, const string& s)
  79. {
  80. if (cvtest::norm(m1, m2, NORM_INF) > 1e-5) throw test_excep(s);
  81. }
  82. };
  83. CV_OperationsTest::CV_OperationsTest()
  84. {
  85. }
  86. CV_OperationsTest::~CV_OperationsTest() {}
  87. #define STR(a) STR2(a)
  88. #define STR2(a) #a
  89. #define CHECK_DIFF(a, b) checkDiff(a, b, "(" #a ") != (" #b ") at l." STR(__LINE__))
  90. #define CHECK_DIFF_FLT(a, b) checkDiffF(a, b, "(" #a ") !=(eps) (" #b ") at l." STR(__LINE__))
  91. #if defined _MSC_VER && _MSC_VER < 1400
  92. #define MSVC_OLD 1
  93. #else
  94. #define MSVC_OLD 0
  95. #endif
  96. template<typename _Tp> void CV_OperationsTest::TestType(Size sz, _Tp value)
  97. {
  98. cv::Mat_<_Tp> m(sz);
  99. CV_Assert(m.cols == sz.width && m.rows == sz.height && m.depth() == cv::traits::Depth<_Tp>::value &&
  100. m.channels() == DataType<_Tp>::channels &&
  101. m.elemSize() == sizeof(_Tp) && m.step == m.elemSize()*m.cols);
  102. for( int y = 0; y < sz.height; y++ )
  103. for( int x = 0; x < sz.width; x++ )
  104. {
  105. m(y,x) = value;
  106. }
  107. double s = sum(Mat(m).reshape(1))[0];
  108. CV_Assert( s == (double)sz.width*sz.height );
  109. }
  110. bool CV_OperationsTest::TestMat()
  111. {
  112. try
  113. {
  114. Mat one_3x1(3, 1, CV_32F, Scalar(1.0));
  115. Mat shi_3x1(3, 1, CV_32F, Scalar(1.2));
  116. Mat shi_2x1(2, 1, CV_32F, Scalar(-1));
  117. Scalar shift = Scalar::all(15);
  118. float data[] = { sqrt(2.f)/2, -sqrt(2.f)/2, 1.f, sqrt(2.f)/2, sqrt(2.f)/2, 10.f };
  119. Mat rot_2x3(2, 3, CV_32F, data);
  120. Mat res = one_3x1 + shi_3x1 + shi_3x1 + shi_3x1;
  121. res = Mat(Mat(2 * rot_2x3) * res - shi_2x1) + shift;
  122. Mat tmp, res2;
  123. cv::add(one_3x1, shi_3x1, tmp);
  124. cv::add(tmp, shi_3x1, tmp);
  125. cv::add(tmp, shi_3x1, tmp);
  126. cv::gemm(rot_2x3, tmp, 2, shi_2x1, -1, res2, 0);
  127. cv::add(res2, Mat(2, 1, CV_32F, shift), res2);
  128. CHECK_DIFF(res, res2);
  129. Mat mat4x4(4, 4, CV_32F);
  130. cv::randu(mat4x4, Scalar(0), Scalar(10));
  131. Mat roi1 = mat4x4(Rect(Point(1, 1), Size(2, 2)));
  132. Mat roi2 = mat4x4(Range(1, 3), Range(1, 3));
  133. CHECK_DIFF(roi1, roi2);
  134. CHECK_DIFF(mat4x4, mat4x4(Rect(Point(0,0), mat4x4.size())));
  135. Mat intMat10(3, 3, CV_32S, Scalar(10));
  136. Mat intMat11(3, 3, CV_32S, Scalar(11));
  137. Mat resMat(3, 3, CV_8U, Scalar(255));
  138. CHECK_DIFF(resMat, intMat10 == intMat10);
  139. CHECK_DIFF(resMat, intMat10 < intMat11);
  140. CHECK_DIFF(resMat, intMat11 > intMat10);
  141. CHECK_DIFF(resMat, intMat10 <= intMat11);
  142. CHECK_DIFF(resMat, intMat11 >= intMat10);
  143. CHECK_DIFF(resMat, intMat11 != intMat10);
  144. CHECK_DIFF(resMat, intMat10 == 10.0);
  145. CHECK_DIFF(resMat, 10.0 == intMat10);
  146. CHECK_DIFF(resMat, intMat10 < 11.0);
  147. CHECK_DIFF(resMat, 11.0 > intMat10);
  148. CHECK_DIFF(resMat, 10.0 < intMat11);
  149. CHECK_DIFF(resMat, 11.0 >= intMat10);
  150. CHECK_DIFF(resMat, 10.0 <= intMat11);
  151. CHECK_DIFF(resMat, 10.0 != intMat11);
  152. CHECK_DIFF(resMat, intMat11 != 10.0);
  153. Mat eye = Mat::eye(3, 3, CV_16S);
  154. Mat maskMat4(3, 3, CV_16S, Scalar(4));
  155. Mat maskMat1(3, 3, CV_16S, Scalar(1));
  156. Mat maskMat5(3, 3, CV_16S, Scalar(5));
  157. Mat maskMat0(3, 3, CV_16S, Scalar(0));
  158. CHECK_DIFF(maskMat0, maskMat4 & maskMat1);
  159. CHECK_DIFF(maskMat0, Scalar(1) & maskMat4);
  160. CHECK_DIFF(maskMat0, maskMat4 & Scalar(1));
  161. Mat m;
  162. m = maskMat4.clone(); m &= maskMat1; CHECK_DIFF(maskMat0, m);
  163. m = maskMat4.clone(); m &= maskMat1 | maskMat1; CHECK_DIFF(maskMat0, m);
  164. m = maskMat4.clone(); m &= (2* maskMat1 - maskMat1); CHECK_DIFF(maskMat0, m);
  165. m = maskMat4.clone(); m &= Scalar(1); CHECK_DIFF(maskMat0, m);
  166. m = maskMat4.clone(); m |= maskMat1; CHECK_DIFF(maskMat5, m);
  167. m = maskMat5.clone(); m ^= maskMat1; CHECK_DIFF(maskMat4, m);
  168. m = maskMat4.clone(); m |= (2* maskMat1 - maskMat1); CHECK_DIFF(maskMat5, m);
  169. m = maskMat5.clone(); m ^= (2* maskMat1 - maskMat1); CHECK_DIFF(maskMat4, m);
  170. m = maskMat4.clone(); m |= Scalar(1); CHECK_DIFF(maskMat5, m);
  171. m = maskMat5.clone(); m ^= Scalar(1); CHECK_DIFF(maskMat4, m);
  172. CHECK_DIFF(maskMat0, (maskMat4 | maskMat4) & (maskMat1 | maskMat1));
  173. CHECK_DIFF(maskMat0, (maskMat4 | maskMat4) & maskMat1);
  174. CHECK_DIFF(maskMat0, maskMat4 & (maskMat1 | maskMat1));
  175. CHECK_DIFF(maskMat0, (maskMat1 | maskMat1) & Scalar(4));
  176. CHECK_DIFF(maskMat0, Scalar(4) & (maskMat1 | maskMat1));
  177. CHECK_DIFF(maskMat0, maskMat5 ^ (maskMat4 | maskMat1));
  178. CHECK_DIFF(maskMat0, (maskMat4 | maskMat1) ^ maskMat5);
  179. CHECK_DIFF(maskMat0, (maskMat4 + maskMat1) ^ (maskMat4 + maskMat1));
  180. CHECK_DIFF(maskMat0, Scalar(5) ^ (maskMat4 | Scalar(1)));
  181. CHECK_DIFF(maskMat1, Scalar(5) ^ maskMat4);
  182. CHECK_DIFF(maskMat0, Scalar(5) ^ (maskMat4 + maskMat1));
  183. CHECK_DIFF(maskMat5, Scalar(5) | (maskMat4 + maskMat1));
  184. CHECK_DIFF(maskMat0, (maskMat4 + maskMat1) ^ Scalar(5));
  185. CHECK_DIFF(maskMat5, maskMat5 | (maskMat4 ^ maskMat1));
  186. CHECK_DIFF(maskMat5, (maskMat4 ^ maskMat1) | maskMat5);
  187. CHECK_DIFF(maskMat5, maskMat5 | (maskMat4 ^ Scalar(1)));
  188. CHECK_DIFF(maskMat5, (maskMat4 | maskMat4) | Scalar(1));
  189. CHECK_DIFF(maskMat5, Scalar(1) | (maskMat4 | maskMat4));
  190. CHECK_DIFF(maskMat5, Scalar(1) | maskMat4);
  191. CHECK_DIFF(maskMat5, (maskMat5 | maskMat5) | (maskMat4 ^ maskMat1));
  192. CHECK_DIFF(maskMat1, min(maskMat1, maskMat5));
  193. CHECK_DIFF(maskMat1, min(Mat(maskMat1 | maskMat1), maskMat5 | maskMat5));
  194. CHECK_DIFF(maskMat5, max(maskMat1, maskMat5));
  195. CHECK_DIFF(maskMat5, max(Mat(maskMat1 | maskMat1), maskMat5 | maskMat5));
  196. CHECK_DIFF(maskMat1, min(maskMat1, maskMat5 | maskMat5));
  197. CHECK_DIFF(maskMat1, min(maskMat1 | maskMat1, maskMat5));
  198. CHECK_DIFF(maskMat5, max(maskMat1 | maskMat1, maskMat5));
  199. CHECK_DIFF(maskMat5, max(maskMat1, maskMat5 | maskMat5));
  200. CHECK_DIFF(~maskMat1, maskMat1 ^ -1);
  201. CHECK_DIFF(~(maskMat1 | maskMat1), maskMat1 ^ -1);
  202. CHECK_DIFF(maskMat1, maskMat4/4.0);
  203. /////////////////////////////
  204. CHECK_DIFF(1.0 - (maskMat5 | maskMat5), -maskMat4);
  205. CHECK_DIFF((maskMat4 | maskMat4) * 1.0 + 1.0, maskMat5);
  206. CHECK_DIFF(1.0 + (maskMat4 | maskMat4) * 1.0, maskMat5);
  207. CHECK_DIFF((maskMat5 | maskMat5) * 1.0 - 1.0, maskMat4);
  208. CHECK_DIFF(5.0 - (maskMat4 | maskMat4) * 1.0, maskMat1);
  209. CHECK_DIFF((maskMat4 | maskMat4) * 1.0 + 0.5 + 0.5, maskMat5);
  210. CHECK_DIFF(0.5 + ((maskMat4 | maskMat4) * 1.0 + 0.5), maskMat5);
  211. CHECK_DIFF(((maskMat4 | maskMat4) * 1.0 + 2.0) - 1.0, maskMat5);
  212. CHECK_DIFF(5.0 - ((maskMat1 | maskMat1) * 1.0 + 3.0), maskMat1);
  213. CHECK_DIFF( ( (maskMat1 | maskMat1) * 2.0 + 2.0) * 1.25, maskMat5);
  214. CHECK_DIFF( 1.25 * ( (maskMat1 | maskMat1) * 2.0 + 2.0), maskMat5);
  215. CHECK_DIFF( -( (maskMat1 | maskMat1) * (-2.0) + 1.0), maskMat1);
  216. CHECK_DIFF( maskMat1 * 1.0 + maskMat4 * 0.5 + 2.0, maskMat5);
  217. CHECK_DIFF( 1.0 + (maskMat1 * 1.0 + maskMat4 * 0.5 + 1.0), maskMat5);
  218. CHECK_DIFF( (maskMat1 * 1.0 + maskMat4 * 0.5 + 2.0) - 1.0, maskMat4);
  219. CHECK_DIFF(5.0 - (maskMat1 * 1.0 + maskMat4 * 0.5 + 1.0), maskMat1);
  220. CHECK_DIFF((maskMat1 * 1.0 + maskMat4 * 0.5 + 1.0)*1.25, maskMat5);
  221. CHECK_DIFF(1.25 * (maskMat1 * 1.0 + maskMat4 * 0.5 + 1.0), maskMat5);
  222. CHECK_DIFF(-(maskMat1 * 2.0 + maskMat4 * (-1) + 1.0), maskMat1);
  223. CHECK_DIFF((maskMat1 * 1.0 + maskMat4), maskMat5);
  224. CHECK_DIFF((maskMat4 + maskMat1 * 1.0), maskMat5);
  225. CHECK_DIFF((maskMat1 * 3.0 + 1.0) + maskMat1, maskMat5);
  226. CHECK_DIFF(maskMat1 + (maskMat1 * 3.0 + 1.0), maskMat5);
  227. CHECK_DIFF(maskMat1*4.0 + (maskMat1 | maskMat1), maskMat5);
  228. CHECK_DIFF((maskMat1 | maskMat1) + maskMat1*4.0, maskMat5);
  229. CHECK_DIFF((maskMat1*3.0 + 1.0) + (maskMat1 | maskMat1), maskMat5);
  230. CHECK_DIFF((maskMat1 | maskMat1) + (maskMat1*3.0 + 1.0), maskMat5);
  231. CHECK_DIFF(maskMat1*4.0 + maskMat4*2.0, maskMat1 * 12);
  232. CHECK_DIFF((maskMat1*3.0 + 1.0) + maskMat4*2.0, maskMat1 * 12);
  233. CHECK_DIFF(maskMat4*2.0 + (maskMat1*3.0 + 1.0), maskMat1 * 12);
  234. CHECK_DIFF((maskMat1*3.0 + 1.0) + (maskMat1*2.0 + 2.0), maskMat1 * 8);
  235. CHECK_DIFF(maskMat5*1.0 - maskMat4, maskMat1);
  236. CHECK_DIFF(maskMat5 - maskMat1 * 4.0, maskMat1);
  237. CHECK_DIFF((maskMat4 * 1.0 + 4.0)- maskMat4, maskMat4);
  238. CHECK_DIFF(maskMat5 - (maskMat1 * 2.0 + 2.0), maskMat1);
  239. CHECK_DIFF(maskMat5*1.0 - (maskMat4 | maskMat4), maskMat1);
  240. CHECK_DIFF((maskMat5 | maskMat5) - maskMat1 * 4.0, maskMat1);
  241. CHECK_DIFF((maskMat4 * 1.0 + 4.0)- (maskMat4 | maskMat4), maskMat4);
  242. CHECK_DIFF((maskMat5 | maskMat5) - (maskMat1 * 2.0 + 2.0), maskMat1);
  243. CHECK_DIFF(maskMat1*5.0 - maskMat4 * 1.0, maskMat1);
  244. CHECK_DIFF((maskMat1*5.0 + 3.0)- maskMat4 * 1.0, maskMat4);
  245. CHECK_DIFF(maskMat4 * 2.0 - (maskMat1*4.0 + 3.0), maskMat1);
  246. CHECK_DIFF((maskMat1 * 2.0 + 3.0) - (maskMat1*3.0 + 1.0), maskMat1);
  247. CHECK_DIFF((maskMat5 - maskMat4)* 4.0, maskMat4);
  248. CHECK_DIFF(4.0 * (maskMat5 - maskMat4), maskMat4);
  249. CHECK_DIFF(-((maskMat4 | maskMat4) - (maskMat5 | maskMat5)), maskMat1);
  250. CHECK_DIFF(4.0 * (maskMat1 | maskMat1), maskMat4);
  251. CHECK_DIFF((maskMat4 | maskMat4)/4.0, maskMat1);
  252. #if !MSVC_OLD
  253. CHECK_DIFF(2.0 * (maskMat1 * 2.0) , maskMat4);
  254. #endif
  255. CHECK_DIFF((maskMat4 / 2.0) / 2.0 , maskMat1);
  256. CHECK_DIFF(-(maskMat4 - maskMat5) , maskMat1);
  257. CHECK_DIFF(-((maskMat4 - maskMat5) * 1.0), maskMat1);
  258. /////////////////////////////
  259. CHECK_DIFF(maskMat4 / maskMat4, maskMat1);
  260. ///// Element-wise multiplication
  261. CHECK_DIFF(maskMat4.mul(maskMat4, 0.25), maskMat4);
  262. CHECK_DIFF(maskMat4.mul(maskMat1 * 4, 0.25), maskMat4);
  263. CHECK_DIFF(maskMat4.mul(maskMat4 / 4), maskMat4);
  264. CHECK_DIFF(maskMat4.mul(maskMat4 / 4), maskMat4);
  265. CHECK_DIFF(maskMat4.mul(maskMat4) * 0.25, maskMat4);
  266. CHECK_DIFF(0.25 * maskMat4.mul(maskMat4), maskMat4);
  267. ////// Element-wise division
  268. CHECK_DIFF(maskMat4 / maskMat4, maskMat1);
  269. CHECK_DIFF((maskMat4 & maskMat4) / (maskMat1 * 4), maskMat1);
  270. CHECK_DIFF((maskMat4 & maskMat4) / maskMat4, maskMat1);
  271. CHECK_DIFF(maskMat4 / (maskMat4 & maskMat4), maskMat1);
  272. CHECK_DIFF((maskMat1 * 4) / maskMat4, maskMat1);
  273. CHECK_DIFF(maskMat4 / (maskMat1 * 4), maskMat1);
  274. CHECK_DIFF((maskMat4 * 0.5 )/ (maskMat1 * 2), maskMat1);
  275. CHECK_DIFF(maskMat4 / maskMat4.mul(maskMat1), maskMat1);
  276. CHECK_DIFF((maskMat4 & maskMat4) / maskMat4.mul(maskMat1), maskMat1);
  277. CHECK_DIFF(4.0 / maskMat4, maskMat1);
  278. CHECK_DIFF(4.0 / (maskMat4 | maskMat4), maskMat1);
  279. CHECK_DIFF(4.0 / (maskMat1 * 4.0), maskMat1);
  280. CHECK_DIFF(4.0 / (maskMat4 / maskMat1), maskMat1);
  281. m = maskMat4.clone(); m/=4.0; CHECK_DIFF(m, maskMat1);
  282. m = maskMat4.clone(); m/=maskMat4; CHECK_DIFF(m, maskMat1);
  283. m = maskMat4.clone(); m/=(maskMat1 * 4.0); CHECK_DIFF(m, maskMat1);
  284. m = maskMat4.clone(); m/=(maskMat4 / maskMat1); CHECK_DIFF(m, maskMat1);
  285. /////////////////////////////
  286. float matrix_data[] = { 3, 1, -4, -5, 1, 0, 0, 1.1f, 1.5f};
  287. Mat mt(3, 3, CV_32F, matrix_data);
  288. Mat mi = mt.inv();
  289. Mat d1 = Mat::eye(3, 3, CV_32F);
  290. Mat d2 = d1 * 2;
  291. MatExpr mt_tr = mt.t();
  292. MatExpr mi_tr = mi.t();
  293. Mat mi2 = mi * 2;
  294. CHECK_DIFF_FLT( mi2 * mt, d2 );
  295. CHECK_DIFF_FLT( mi * mt, d1 );
  296. CHECK_DIFF_FLT( mt_tr * mi_tr, d1 );
  297. m = mi.clone(); m*=mt; CHECK_DIFF_FLT(m, d1);
  298. m = mi.clone(); m*= (2 * mt - mt) ; CHECK_DIFF_FLT(m, d1);
  299. m = maskMat4.clone(); m+=(maskMat1 * 1.0); CHECK_DIFF(m, maskMat5);
  300. m = maskMat5.clone(); m-=(maskMat1 * 4.0); CHECK_DIFF(m, maskMat1);
  301. m = maskMat1.clone(); m+=(maskMat1 * 3.0 + 1.0); CHECK_DIFF(m, maskMat5);
  302. m = maskMat5.clone(); m-=(maskMat1 * 3.0 + 1.0); CHECK_DIFF(m, maskMat1);
  303. #if !MSVC_OLD
  304. m = mi.clone(); m+=(3.0 * mi * mt + d1); CHECK_DIFF_FLT(m, mi + d1 * 4);
  305. m = mi.clone(); m-=(3.0 * mi * mt + d1); CHECK_DIFF_FLT(m, mi - d1 * 4);
  306. m = mi.clone(); m*=(mt * 1.0); CHECK_DIFF_FLT(m, d1);
  307. m = mi.clone(); m*=(mt * 1.0 + Mat::eye(m.size(), m.type())); CHECK_DIFF_FLT(m, d1 + mi);
  308. m = mi.clone(); m*=mt_tr.t(); CHECK_DIFF_FLT(m, d1);
  309. CHECK_DIFF_FLT( (mi * 2) * mt, d2);
  310. CHECK_DIFF_FLT( mi * (2 * mt), d2);
  311. CHECK_DIFF_FLT( mt.t() * mi_tr, d1 );
  312. CHECK_DIFF_FLT( mt_tr * mi.t(), d1 );
  313. CHECK_DIFF_FLT( (mi * 0.4) * (mt * 5), d2);
  314. CHECK_DIFF_FLT( mt.t() * (mi_tr * 2), d2 );
  315. CHECK_DIFF_FLT( (mt_tr * 2) * mi.t(), d2 );
  316. CHECK_DIFF_FLT(mt.t() * mi.t(), d1);
  317. CHECK_DIFF_FLT( (mi * mt) * 2.0, d2);
  318. CHECK_DIFF_FLT( 2.0 * (mi * mt), d2);
  319. CHECK_DIFF_FLT( -(mi * mt), -d1);
  320. CHECK_DIFF_FLT( (mi * mt) / 2.0, d1 / 2);
  321. Mat mt_mul_2_plus_1;
  322. gemm(mt, d1, 2, Mat::ones(3, 3, CV_32F), 1, mt_mul_2_plus_1);
  323. CHECK_DIFF( (mt * 2.0 + 1.0) * mi, mt_mul_2_plus_1 * mi); // (A*alpha + beta)*B
  324. CHECK_DIFF( mi * (mt * 2.0 + 1.0), mi * mt_mul_2_plus_1); // A*(B*alpha + beta)
  325. CHECK_DIFF( (mt * 2.0 + 1.0) * (mi * 2), mt_mul_2_plus_1 * mi2); // (A*alpha + beta)*(B*gamma)
  326. CHECK_DIFF( (mi *2)* (mt * 2.0 + 1.0), mi2 * mt_mul_2_plus_1); // (A*gamma)*(B*alpha + beta)
  327. CHECK_DIFF_FLT( (mt * 2.0 + 1.0) * mi.t(), mt_mul_2_plus_1 * mi_tr); // (A*alpha + beta)*B^t
  328. CHECK_DIFF_FLT( mi.t() * (mt * 2.0 + 1.0), mi_tr * mt_mul_2_plus_1); // A^t*(B*alpha + beta)
  329. CHECK_DIFF_FLT( (mi * mt + d2)*5, d1 * 3 * 5);
  330. CHECK_DIFF_FLT( mi * mt + d2, d1 * 3);
  331. CHECK_DIFF_FLT( -(mi * mt) + d2, d1);
  332. CHECK_DIFF_FLT( (mi * mt) + d1, d2);
  333. CHECK_DIFF_FLT( d1 + (mi * mt), d2);
  334. CHECK_DIFF_FLT( (mi * mt) - d2, -d1);
  335. CHECK_DIFF_FLT( d2 - (mi * mt), d1);
  336. CHECK_DIFF_FLT( (mi * mt) + d2 * 0.5, d2);
  337. CHECK_DIFF_FLT( d2 * 0.5 + (mi * mt), d2);
  338. CHECK_DIFF_FLT( (mi * mt) - d1 * 2, -d1);
  339. CHECK_DIFF_FLT( d1 * 2 - (mi * mt), d1);
  340. CHECK_DIFF_FLT( (mi * mt) + mi.t(), mi_tr + d1);
  341. CHECK_DIFF_FLT( mi.t() + (mi * mt), mi_tr + d1);
  342. CHECK_DIFF_FLT( (mi * mt) - mi.t(), d1 - mi_tr);
  343. CHECK_DIFF_FLT( mi.t() - (mi * mt), mi_tr - d1);
  344. CHECK_DIFF_FLT( 2.0 *(mi * mt + d2), d1 * 6);
  345. CHECK_DIFF_FLT( -(mi * mt + d2), d1 * -3);
  346. CHECK_DIFF_FLT(mt.inv() * mt, d1);
  347. CHECK_DIFF_FLT(mt.inv() * (2*mt - mt), d1);
  348. #endif
  349. }
  350. catch (const test_excep& e)
  351. {
  352. ts->printf(cvtest::TS::LOG, "%s\n", e.s.c_str());
  353. ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
  354. return false;
  355. }
  356. return true;
  357. }
  358. bool CV_OperationsTest::SomeMatFunctions()
  359. {
  360. try
  361. {
  362. Mat rgba( 10, 10, CV_8UC4, Scalar(1,2,3,4) );
  363. Mat bgr( rgba.rows, rgba.cols, CV_8UC3 );
  364. Mat alpha( rgba.rows, rgba.cols, CV_8UC1 );
  365. Mat out[] = { bgr, alpha };
  366. // rgba[0] -> bgr[2], rgba[1] -> bgr[1],
  367. // rgba[2] -> bgr[0], rgba[3] -> alpha[0]
  368. int from_to[] = { 0,2, 1,1, 2,0, 3,3 };
  369. mixChannels( &rgba, 1, out, 2, from_to, 4 );
  370. Mat bgr_exp( rgba.size(), CV_8UC3, Scalar(3,2,1));
  371. Mat alpha_exp( rgba.size(), CV_8UC1, Scalar(4));
  372. CHECK_DIFF(bgr_exp, bgr);
  373. CHECK_DIFF(alpha_exp, alpha);
  374. }
  375. catch (const test_excep& e)
  376. {
  377. ts->printf(cvtest::TS::LOG, "%s\n", e.s.c_str());
  378. ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
  379. return false;
  380. }
  381. return true;
  382. }
  383. bool CV_OperationsTest::TestSubMatAccess()
  384. {
  385. try
  386. {
  387. Mat_<float> T_bs(4,4);
  388. Vec3f cdir(1.f, 1.f, 0.f);
  389. Vec3f ydir(1.f, 0.f, 1.f);
  390. Vec3f fpt(0.1f, 0.7f, 0.2f);
  391. T_bs.setTo(0);
  392. T_bs(Range(0,3),Range(2,3)) = 1.0*Mat(cdir); // weird OpenCV stuff, need to do multiply
  393. T_bs(Range(0,3),Range(1,2)) = 1.0*Mat(ydir);
  394. T_bs(Range(0,3),Range(0,1)) = 1.0*Mat(cdir.cross(ydir));
  395. T_bs(Range(0,3),Range(3,4)) = 1.0*Mat(fpt);
  396. T_bs(3,3) = 1.0;
  397. //std::cout << "[Nav Grok] S frame =" << std::endl << T_bs << std::endl;
  398. // set up display coords, really just the S frame
  399. std::vector<float>coords;
  400. for (int i=0; i<16; i++)
  401. {
  402. coords.push_back(T_bs(i));
  403. //std::cout << T_bs1(i) << std::endl;
  404. }
  405. CV_Assert( cvtest::norm(coords, T_bs.reshape(1,1), NORM_INF) == 0 );
  406. }
  407. catch (const test_excep& e)
  408. {
  409. ts->printf(cvtest::TS::LOG, "%s\n", e.s.c_str());
  410. ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
  411. return false;
  412. }
  413. return true;
  414. }
  415. bool CV_OperationsTest::TestTemplateMat()
  416. {
  417. try
  418. {
  419. Mat_<float> one_3x1(3, 1, 1.0f);
  420. Mat_<float> shi_3x1(3, 1, 1.2f);
  421. Mat_<float> shi_2x1(2, 1, -2);
  422. Scalar shift = Scalar::all(15);
  423. float data[] = { sqrt(2.f)/2, -sqrt(2.f)/2, 1.f, sqrt(2.f)/2, sqrt(2.f)/2, 10.f };
  424. Mat_<float> rot_2x3(2, 3, data);
  425. Mat_<float> res = Mat(Mat(2 * rot_2x3) * Mat(one_3x1 + shi_3x1 + shi_3x1 + shi_3x1) - shi_2x1) + shift;
  426. Mat_<float> resS = rot_2x3 * one_3x1;
  427. Mat_<float> tmp, res2, resS2;
  428. cv::add(one_3x1, shi_3x1, tmp);
  429. cv::add(tmp, shi_3x1, tmp);
  430. cv::add(tmp, shi_3x1, tmp);
  431. cv::gemm(rot_2x3, tmp, 2, shi_2x1, -1, res2, 0);
  432. cv::add(res2, Mat(2, 1, CV_32F, shift), res2);
  433. cv::gemm(rot_2x3, one_3x1, 1, shi_2x1, 0, resS2, 0);
  434. CHECK_DIFF(res, res2);
  435. CHECK_DIFF(resS, resS2);
  436. Mat_<float> mat4x4(4, 4);
  437. cv::randu(mat4x4, Scalar(0), Scalar(10));
  438. Mat_<float> roi1 = mat4x4(Rect(Point(1, 1), Size(2, 2)));
  439. Mat_<float> roi2 = mat4x4(Range(1, 3), Range(1, 3));
  440. CHECK_DIFF(roi1, roi2);
  441. CHECK_DIFF(mat4x4, mat4x4(Rect(Point(0,0), mat4x4.size())));
  442. Mat_<int> intMat10(3, 3, 10);
  443. Mat_<int> intMat11(3, 3, 11);
  444. Mat_<uchar> resMat(3, 3, 255);
  445. CHECK_DIFF(resMat, intMat10 == intMat10);
  446. CHECK_DIFF(resMat, intMat10 < intMat11);
  447. CHECK_DIFF(resMat, intMat11 > intMat10);
  448. CHECK_DIFF(resMat, intMat10 <= intMat11);
  449. CHECK_DIFF(resMat, intMat11 >= intMat10);
  450. CHECK_DIFF(resMat, intMat10 == 10.0);
  451. CHECK_DIFF(resMat, intMat10 < 11.0);
  452. CHECK_DIFF(resMat, intMat11 > 10.0);
  453. CHECK_DIFF(resMat, intMat10 <= 11.0);
  454. CHECK_DIFF(resMat, intMat11 >= 10.0);
  455. Mat_<uchar> maskMat4(3, 3, 4);
  456. Mat_<uchar> maskMat1(3, 3, 1);
  457. Mat_<uchar> maskMat5(3, 3, 5);
  458. Mat_<uchar> maskMat0(3, 3, (uchar)0);
  459. CHECK_DIFF(maskMat0, maskMat4 & maskMat1);
  460. CHECK_DIFF(maskMat0, Scalar(1) & maskMat4);
  461. CHECK_DIFF(maskMat0, maskMat4 & Scalar(1));
  462. Mat_<uchar> m;
  463. m = maskMat4.clone(); m&=maskMat1; CHECK_DIFF(maskMat0, m);
  464. m = maskMat4.clone(); m&=Scalar(1); CHECK_DIFF(maskMat0, m);
  465. m = maskMat4.clone(); m|=maskMat1; CHECK_DIFF(maskMat5, m);
  466. m = maskMat4.clone(); m^=maskMat1; CHECK_DIFF(maskMat5, m);
  467. CHECK_DIFF(maskMat0, (maskMat4 | maskMat4) & (maskMat1 | maskMat1));
  468. CHECK_DIFF(maskMat0, (maskMat4 | maskMat4) & maskMat1);
  469. CHECK_DIFF(maskMat0, maskMat4 & (maskMat1 | maskMat1));
  470. CHECK_DIFF(maskMat0, maskMat5 ^ (maskMat4 | maskMat1));
  471. CHECK_DIFF(maskMat0, Scalar(5) ^ (maskMat4 | Scalar(1)));
  472. CHECK_DIFF(maskMat5, maskMat5 | (maskMat4 ^ maskMat1));
  473. CHECK_DIFF(maskMat5, maskMat5 | (maskMat4 ^ Scalar(1)));
  474. CHECK_DIFF(~maskMat1, maskMat1 ^ 0xFF);
  475. CHECK_DIFF(~(maskMat1 | maskMat1), maskMat1 ^ 0xFF);
  476. CHECK_DIFF(maskMat1 + maskMat4, maskMat5);
  477. CHECK_DIFF(maskMat1 + Scalar(4), maskMat5);
  478. CHECK_DIFF(Scalar(4) + maskMat1, maskMat5);
  479. CHECK_DIFF(Scalar(4) + (maskMat1 & maskMat1), maskMat5);
  480. CHECK_DIFF(maskMat1 + 4.0, maskMat5);
  481. CHECK_DIFF((maskMat1 & 0xFF) + 4.0, maskMat5);
  482. CHECK_DIFF(4.0 + maskMat1, maskMat5);
  483. m = maskMat4.clone(); m+=Scalar(1); CHECK_DIFF(m, maskMat5);
  484. m = maskMat4.clone(); m+=maskMat1; CHECK_DIFF(m, maskMat5);
  485. m = maskMat4.clone(); m+=(maskMat1 | maskMat1); CHECK_DIFF(m, maskMat5);
  486. CHECK_DIFF(maskMat5 - maskMat1, maskMat4);
  487. CHECK_DIFF(maskMat5 - Scalar(1), maskMat4);
  488. CHECK_DIFF((maskMat5 | maskMat5) - Scalar(1), maskMat4);
  489. CHECK_DIFF(maskMat5 - 1, maskMat4);
  490. CHECK_DIFF((maskMat5 | maskMat5) - 1, maskMat4);
  491. CHECK_DIFF((maskMat5 | maskMat5) - (maskMat1 | maskMat1), maskMat4);
  492. CHECK_DIFF(maskMat1, min(maskMat1, maskMat5));
  493. CHECK_DIFF(maskMat5, max(maskMat1, maskMat5));
  494. m = maskMat5.clone(); m-=Scalar(1); CHECK_DIFF(m, maskMat4);
  495. m = maskMat5.clone(); m-=maskMat1; CHECK_DIFF(m, maskMat4);
  496. m = maskMat5.clone(); m-=(maskMat1 | maskMat1); CHECK_DIFF(m, maskMat4);
  497. m = maskMat4.clone(); m |= Scalar(1); CHECK_DIFF(maskMat5, m);
  498. m = maskMat5.clone(); m ^= Scalar(1); CHECK_DIFF(maskMat4, m);
  499. CHECK_DIFF(maskMat1, maskMat4/4.0);
  500. Mat_<float> negf(3, 3, -3.0);
  501. Mat_<float> posf = -negf;
  502. Mat_<float> posf2 = posf * 2;
  503. Mat_<int> negi(3, 3, -3);
  504. CHECK_DIFF(abs(negf), -negf);
  505. CHECK_DIFF(abs(posf - posf2), -negf);
  506. CHECK_DIFF(abs(negi), -(negi & negi));
  507. CHECK_DIFF(5.0 - maskMat4, maskMat1);
  508. CHECK_DIFF(maskMat4.mul(maskMat4, 0.25), maskMat4);
  509. CHECK_DIFF(maskMat4.mul(maskMat1 * 4, 0.25), maskMat4);
  510. CHECK_DIFF(maskMat4.mul(maskMat4 / 4), maskMat4);
  511. ////// Element-wise division
  512. CHECK_DIFF(maskMat4 / maskMat4, maskMat1);
  513. CHECK_DIFF(4.0 / maskMat4, maskMat1);
  514. m = maskMat4.clone(); m/=4.0; CHECK_DIFF(m, maskMat1);
  515. ////////////////////////////////
  516. typedef Mat_<int> TestMat_t;
  517. const TestMat_t cnegi = negi.clone();
  518. TestMat_t::iterator beg = negi.begin();
  519. TestMat_t::iterator end = negi.end();
  520. TestMat_t::const_iterator cbeg = cnegi.begin();
  521. TestMat_t::const_iterator cend = cnegi.end();
  522. int sum = 0;
  523. for(; beg!=end; ++beg)
  524. sum+=*beg;
  525. for(; cbeg!=cend; ++cbeg)
  526. sum-=*cbeg;
  527. if (sum != 0) throw test_excep();
  528. CHECK_DIFF(negi.col(1), negi.col(2));
  529. CHECK_DIFF(negi.row(1), negi.row(2));
  530. CHECK_DIFF(negi.col(1), negi.diag());
  531. if (Mat_<Point2f>(1, 1).elemSize1() != sizeof(float)) throw test_excep();
  532. if (Mat_<Point2f>(1, 1).elemSize() != 2 * sizeof(float)) throw test_excep();
  533. if (Mat_<Point2f>(1, 1).depth() != CV_32F) throw test_excep();
  534. if (Mat_<float>(1, 1).depth() != CV_32F) throw test_excep();
  535. if (Mat_<int>(1, 1).depth() != CV_32S) throw test_excep();
  536. if (Mat_<double>(1, 1).depth() != CV_64F) throw test_excep();
  537. if (Mat_<Point3d>(1, 1).depth() != CV_64F) throw test_excep();
  538. if (Mat_<signed char>(1, 1).depth() != CV_8S) throw test_excep();
  539. if (Mat_<unsigned short>(1, 1).depth() != CV_16U) throw test_excep();
  540. if (Mat_<unsigned short>(1, 1).channels() != 1) throw test_excep();
  541. if (Mat_<Point2f>(1, 1).channels() != 2) throw test_excep();
  542. if (Mat_<Point3f>(1, 1).channels() != 3) throw test_excep();
  543. if (Mat_<Point3d>(1, 1).channels() != 3) throw test_excep();
  544. Mat_<uchar> eye = Mat_<uchar>::zeros(2, 2); CHECK_DIFF(Mat_<uchar>::zeros(Size(2, 2)), eye);
  545. eye.at<uchar>(Point(0,0)) = 1; eye.at<uchar>(1, 1) = 1;
  546. CHECK_DIFF(Mat_<uchar>::eye(2, 2), eye);
  547. CHECK_DIFF(eye, Mat_<uchar>::eye(Size(2,2)));
  548. Mat_<uchar> ones(2, 2, (uchar)1);
  549. CHECK_DIFF(ones, Mat_<uchar>::ones(Size(2,2)));
  550. CHECK_DIFF(Mat_<uchar>::ones(2, 2), ones);
  551. Mat_<Point2f> pntMat(2, 2, Point2f(1, 0));
  552. if(pntMat.stepT() != 2) throw test_excep();
  553. uchar uchar_data[] = {1, 0, 0, 1};
  554. Mat_<uchar> matFromData(1, 4, uchar_data);
  555. const Mat_<uchar> mat2 = matFromData.clone();
  556. CHECK_DIFF(matFromData, eye.reshape(1, 1));
  557. if (matFromData(Point(0,0)) != uchar_data[0])throw test_excep();
  558. if (mat2(Point(0,0)) != uchar_data[0]) throw test_excep();
  559. if (matFromData(0,0) != uchar_data[0])throw test_excep();
  560. if (mat2(0,0) != uchar_data[0]) throw test_excep();
  561. Mat_<uchar> rect(eye, Rect(0, 0, 1, 1));
  562. if (rect.cols != 1 || rect.rows != 1 || rect(0,0) != uchar_data[0]) throw test_excep();
  563. //cv::Mat_<_Tp>::adjustROI(int,int,int,int)
  564. //cv::Mat_<_Tp>::cross(const Mat_&) const
  565. //cv::Mat_<_Tp>::Mat_(const vector<_Tp>&,bool)
  566. //cv::Mat_<_Tp>::Mat_(int,int,_Tp*,size_t)
  567. //cv::Mat_<_Tp>::Mat_(int,int,const _Tp&)
  568. //cv::Mat_<_Tp>::Mat_(Size,const _Tp&)
  569. //cv::Mat_<_Tp>::mul(const Mat_<_Tp>&,double) const
  570. //cv::Mat_<_Tp>::mul(const MatExpr_<MatExpr_Op2_<Mat_<_Tp>,double,Mat_<_Tp>,MatOp_DivRS_<Mat> >,Mat_<_Tp> >&,double) const
  571. //cv::Mat_<_Tp>::mul(const MatExpr_<MatExpr_Op2_<Mat_<_Tp>,double,Mat_<_Tp>,MatOp_Scale_<Mat> >,Mat_<_Tp> >&,double) const
  572. //cv::Mat_<_Tp>::operator Mat_<T2>() const
  573. //cv::Mat_<_Tp>::operator MatExpr_<Mat_<_Tp>,Mat_<_Tp> >() const
  574. //cv::Mat_<_Tp>::operator()(const Range&,const Range&) const
  575. //cv::Mat_<_Tp>::operator()(const Rect&) const
  576. //cv::Mat_<_Tp>::operator=(const MatExpr_Base&)
  577. //cv::Mat_<_Tp>::operator[](int) const
  578. ///////////////////////////////
  579. float matrix_data[] = { 3, 1, -4, -5, 1, 0, 0, 1.1f, 1.5f};
  580. Mat_<float> mt(3, 3, matrix_data);
  581. Mat_<float> mi = mt.inv();
  582. Mat_<float> d1 = Mat_<float>::eye(3, 3);
  583. Mat_<float> d2 = d1 * 2;
  584. Mat_<float> mt_tr = mt.t();
  585. Mat_<float> mi_tr = mi.t();
  586. Mat_<float> mi2 = mi * 2;
  587. CHECK_DIFF_FLT( mi2 * mt, d2 );
  588. CHECK_DIFF_FLT( mi * mt, d1 );
  589. CHECK_DIFF_FLT( mt_tr * mi_tr, d1 );
  590. Mat_<float> mf;
  591. mf = mi.clone(); mf*=mt; CHECK_DIFF_FLT(mf, d1);
  592. ////// typedefs //////
  593. if (Mat1b(1, 1).elemSize() != sizeof(uchar)) throw test_excep();
  594. if (Mat2b(1, 1).elemSize() != 2 * sizeof(uchar)) throw test_excep();
  595. if (Mat3b(1, 1).elemSize() != 3 * sizeof(uchar)) throw test_excep();
  596. if (Mat1f(1, 1).elemSize() != sizeof(float)) throw test_excep();
  597. if (Mat2f(1, 1).elemSize() != 2 * sizeof(float)) throw test_excep();
  598. if (Mat3f(1, 1).elemSize() != 3 * sizeof(float)) throw test_excep();
  599. if (Mat1f(1, 1).depth() != CV_32F) throw test_excep();
  600. if (Mat3f(1, 1).depth() != CV_32F) throw test_excep();
  601. if (Mat3f(1, 1).type() != CV_32FC3) throw test_excep();
  602. if (Mat1i(1, 1).depth() != CV_32S) throw test_excep();
  603. if (Mat1d(1, 1).depth() != CV_64F) throw test_excep();
  604. if (Mat1b(1, 1).depth() != CV_8U) throw test_excep();
  605. if (Mat3b(1, 1).type() != CV_8UC3) throw test_excep();
  606. if (Mat1w(1, 1).depth() != CV_16U) throw test_excep();
  607. if (Mat1s(1, 1).depth() != CV_16S) throw test_excep();
  608. if (Mat1f(1, 1).channels() != 1) throw test_excep();
  609. if (Mat1b(1, 1).channels() != 1) throw test_excep();
  610. if (Mat1i(1, 1).channels() != 1) throw test_excep();
  611. if (Mat1w(1, 1).channels() != 1) throw test_excep();
  612. if (Mat1s(1, 1).channels() != 1) throw test_excep();
  613. if (Mat2f(1, 1).channels() != 2) throw test_excep();
  614. if (Mat2b(1, 1).channels() != 2) throw test_excep();
  615. if (Mat2i(1, 1).channels() != 2) throw test_excep();
  616. if (Mat2w(1, 1).channels() != 2) throw test_excep();
  617. if (Mat2s(1, 1).channels() != 2) throw test_excep();
  618. if (Mat3f(1, 1).channels() != 3) throw test_excep();
  619. if (Mat3b(1, 1).channels() != 3) throw test_excep();
  620. if (Mat3i(1, 1).channels() != 3) throw test_excep();
  621. if (Mat3w(1, 1).channels() != 3) throw test_excep();
  622. if (Mat3s(1, 1).channels() != 3) throw test_excep();
  623. vector<Mat_<float> > mvf, mvf2;
  624. Mat_<Vec2f> mf2;
  625. mvf.push_back(Mat_<float>::ones(4, 3));
  626. mvf.push_back(Mat_<float>::zeros(4, 3));
  627. merge(mvf, mf2);
  628. split(mf2, mvf2);
  629. CV_Assert( cvtest::norm(mvf2[0], mvf[0], CV_C) == 0 &&
  630. cvtest::norm(mvf2[1], mvf[1], CV_C) == 0 );
  631. {
  632. Mat a(2,2,CV_32F,1.f);
  633. Mat b(1,2,CV_32F,1.f);
  634. Mat c = (a*b.t()).t();
  635. CV_Assert( cvtest::norm(c, CV_L1) == 4. );
  636. }
  637. bool badarg_catched = false;
  638. try
  639. {
  640. Mat m1 = Mat::zeros(1, 10, CV_8UC1);
  641. Mat m2 = Mat::zeros(10, 10, CV_8UC3);
  642. m1.copyTo(m2.row(1));
  643. }
  644. catch(const Exception&)
  645. {
  646. badarg_catched = true;
  647. }
  648. CV_Assert( badarg_catched );
  649. Size size(2, 5);
  650. TestType<float>(size, 1.f);
  651. cv::Vec3f val1(1.f);
  652. TestType<cv::Vec3f>(size, val1);
  653. cv::Matx31f val2(1.f);
  654. TestType<cv::Matx31f>(size, val2);
  655. cv::Matx41f val3(1.f);
  656. TestType<cv::Matx41f>(size, val3);
  657. cv::Matx32f val4(1.f);
  658. TestType<cv::Matx32f>(size, val4);
  659. }
  660. catch (const test_excep& e)
  661. {
  662. ts->printf(cvtest::TS::LOG, "%s\n", e.s.c_str());
  663. ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
  664. return false;
  665. }
  666. return true;
  667. }
  668. bool CV_OperationsTest::TestMatND()
  669. {
  670. int sizes[] = { 3, 3, 3};
  671. cv::MatND nd(3, sizes, CV_32F);
  672. return true;
  673. }
  674. bool CV_OperationsTest::TestSparseMat()
  675. {
  676. try
  677. {
  678. int sizes[] = { 10, 10, 10};
  679. int dims = sizeof(sizes)/sizeof(sizes[0]);
  680. SparseMat mat(dims, sizes, CV_32FC2);
  681. if (mat.dims() != dims) throw test_excep();
  682. if (mat.channels() != 2) throw test_excep();
  683. if (mat.depth() != CV_32F) throw test_excep();
  684. SparseMat mat2 = mat.clone();
  685. }
  686. catch (const test_excep&)
  687. {
  688. ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
  689. return false;
  690. }
  691. return true;
  692. }
  693. bool CV_OperationsTest::TestMatxMultiplication()
  694. {
  695. try
  696. {
  697. Matx33f mat(1, 1, 1, 0, 1, 1, 0, 0, 1); // Identity matrix
  698. Point2f pt(3, 4);
  699. Point3f res = mat * pt; // Correctly assumes homogeneous coordinates
  700. Vec3f res2 = mat*Vec3f(res.x, res.y, res.z);
  701. if(res.x != 8.0) throw test_excep();
  702. if(res.y != 5.0) throw test_excep();
  703. if(res.z != 1.0) throw test_excep();
  704. if(res2[0] != 14.0) throw test_excep();
  705. if(res2[1] != 6.0) throw test_excep();
  706. if(res2[2] != 1.0) throw test_excep();
  707. Matx44f mat44f(1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1);
  708. Matx44d mat44d(1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1);
  709. Scalar s(4, 3, 2, 1);
  710. Scalar sf = mat44f*s;
  711. Scalar sd = mat44d*s;
  712. if(sf[0] != 10.0) throw test_excep();
  713. if(sf[1] != 6.0) throw test_excep();
  714. if(sf[2] != 3.0) throw test_excep();
  715. if(sf[3] != 1.0) throw test_excep();
  716. if(sd[0] != 10.0) throw test_excep();
  717. if(sd[1] != 6.0) throw test_excep();
  718. if(sd[2] != 3.0) throw test_excep();
  719. if(sd[3] != 1.0) throw test_excep();
  720. }
  721. catch(const test_excep&)
  722. {
  723. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
  724. return false;
  725. }
  726. return true;
  727. }
  728. bool CV_OperationsTest::TestMatMatxCastSum()
  729. {
  730. try
  731. {
  732. Mat ref1 = (Mat_<double>(3, 1) << 1, 2, 3);
  733. Mat ref2 = (Mat_<double>(3, 1) << 3, 4, 5);
  734. Mat ref3 = Mat::ones(3, 1, CV_64FC1);
  735. Mat mat = Mat::zeros(3, 1, CV_64FC1);
  736. Mat tst1 = ref1.clone();
  737. Mat_<double> tst2 = ref2.clone();
  738. Matx<double, 3, 1> tst3(1, 2, 3);
  739. Vec3d tst4(3, 4, 5);
  740. Scalar tst5(1, 2, 3);
  741. Mat res;
  742. res = mat + tst1;
  743. CHECK_DIFF_FLT(res, ref1);
  744. res = mat + tst2;
  745. CHECK_DIFF_FLT(res, ref2);
  746. res = mat + tst3;
  747. CHECK_DIFF_FLT(res, ref1);
  748. res = mat + tst4;
  749. CHECK_DIFF_FLT(res, ref2);
  750. res = mat + tst5;
  751. CHECK_DIFF_FLT(res, ref3);
  752. res = mat + 1;
  753. CHECK_DIFF_FLT(res, ref3);
  754. cv::add(mat, tst1, res);
  755. CHECK_DIFF_FLT(res, ref1);
  756. cv::add(mat, tst2, res);
  757. CHECK_DIFF_FLT(res, ref2);
  758. cv::add(mat, tst3, res);
  759. CHECK_DIFF_FLT(res, ref1);
  760. cv::add(mat, tst4, res);
  761. CHECK_DIFF_FLT(res, ref2);
  762. cv::add(mat, tst5, res);
  763. CHECK_DIFF_FLT(res, ref3);
  764. cv::add(mat, 1, res);
  765. CHECK_DIFF_FLT(res, ref3);
  766. res = mat.clone(); res += tst1;
  767. CHECK_DIFF_FLT(res, ref1);
  768. res = mat.clone(); res += tst2;
  769. CHECK_DIFF_FLT(res, ref2);
  770. res = mat.clone(); res += tst3;
  771. CHECK_DIFF_FLT(res, ref1);
  772. res = mat.clone(); res += tst4;
  773. CHECK_DIFF_FLT(res, ref2);
  774. res = mat.clone(); res += tst5;
  775. CHECK_DIFF_FLT(res, ref3);
  776. res = mat.clone(); res += 1;
  777. CHECK_DIFF_FLT(res, ref3);
  778. }
  779. catch (const test_excep& e)
  780. {
  781. ts->printf(cvtest::TS::LOG, "%s\n", e.s.c_str());
  782. ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
  783. return false;
  784. }
  785. return true;
  786. }
  787. bool CV_OperationsTest::TestMatxElementwiseDivison()
  788. {
  789. try
  790. {
  791. Matx22f mat(2, 4, 6, 8);
  792. Matx22f mat2(2, 2, 2, 2);
  793. Matx22f res = mat.div(mat2);
  794. if(res(0, 0) != 1.0) throw test_excep();
  795. if(res(0, 1) != 2.0) throw test_excep();
  796. if(res(1, 0) != 3.0) throw test_excep();
  797. if(res(1, 1) != 4.0) throw test_excep();
  798. }
  799. catch(const test_excep&)
  800. {
  801. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
  802. return false;
  803. }
  804. return true;
  805. }
  806. bool CV_OperationsTest::TestDivisionByValue()
  807. {
  808. try
  809. {
  810. Matx22f mat(2, 4, 6, 8);
  811. float alpha = 2.f;
  812. Matx22f res = mat / alpha;
  813. if(res(0, 0) != 1.0) throw test_excep();
  814. if(res(0, 1) != 2.0) throw test_excep();
  815. if(res(1, 0) != 3.0) throw test_excep();
  816. if(res(1, 1) != 4.0) throw test_excep();
  817. }
  818. catch(const test_excep&)
  819. {
  820. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
  821. return false;
  822. }
  823. return true;
  824. }
  825. bool CV_OperationsTest::TestInplaceDivisionByValue()
  826. {
  827. try
  828. {
  829. Matx22f mat(2, 4, 6, 8);
  830. float alpha = 2.f;
  831. mat /= alpha;
  832. if(mat(0, 0) != 1.0) throw test_excep();
  833. if(mat(0, 1) != 2.0) throw test_excep();
  834. if(mat(1, 0) != 3.0) throw test_excep();
  835. if(mat(1, 1) != 4.0) throw test_excep();
  836. }
  837. catch(const test_excep&)
  838. {
  839. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
  840. return false;
  841. }
  842. return true;
  843. }
  844. bool CV_OperationsTest::TestVec()
  845. {
  846. try
  847. {
  848. cv::Mat hsvImage_f(5, 5, CV_32FC3), hsvImage_b(5, 5, CV_8UC3);
  849. int i = 0,j = 0;
  850. cv::Vec3f a;
  851. //these compile
  852. cv::Vec3b b = a;
  853. hsvImage_f.at<cv::Vec3f>(i,j) = cv::Vec3f((float)i,0,1);
  854. hsvImage_b.at<cv::Vec3b>(i,j) = cv::Vec3b(cv::Vec3f((float)i,0,1));
  855. //these don't
  856. b = cv::Vec3f(1,0,0);
  857. cv::Vec3b c;
  858. c = cv::Vec3f(0,0,1);
  859. hsvImage_b.at<cv::Vec3b>(i,j) = cv::Vec3f((float)i,0,1);
  860. hsvImage_b.at<cv::Vec3b>(i,j) = a;
  861. hsvImage_b.at<cv::Vec3b>(i,j) = cv::Vec3f(1,2,3);
  862. }
  863. catch(const test_excep&)
  864. {
  865. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
  866. return false;
  867. }
  868. return true;
  869. }
  870. bool CV_OperationsTest::operations1()
  871. {
  872. try
  873. {
  874. Point3d p1(1, 1, 1), p2(2, 2, 2), p4(4, 4, 4);
  875. p1*=2;
  876. if (!(p1 == p2)) throw test_excep();
  877. if (!(p2 * 2 == p4)) throw test_excep();
  878. if (!(p2 * 2.f == p4)) throw test_excep();
  879. if (!(p2 * 2.f == p4)) throw test_excep();
  880. Point2d pi1(1, 1), pi2(2, 2), pi4(4, 4);
  881. pi1*=2;
  882. if (!(pi1 == pi2)) throw test_excep();
  883. if (!(pi2 * 2 == pi4)) throw test_excep();
  884. if (!(pi2 * 2.f == pi4)) throw test_excep();
  885. if (!(pi2 * 2.f == pi4)) throw test_excep();
  886. Vec2d v12(1, 1), v22(2, 2);
  887. v12*=2.0;
  888. if (!(v12 == v22)) throw test_excep();
  889. Vec3d v13(1, 1, 1), v23(2, 2, 2);
  890. v13*=2.0;
  891. if (!(v13 == v23)) throw test_excep();
  892. Vec4d v14(1, 1, 1, 1), v24(2, 2, 2, 2);
  893. v14*=2.0;
  894. if (!(v14 == v24)) throw test_excep();
  895. Size sz(10, 20);
  896. if (sz.area() != 200) throw test_excep();
  897. if (sz.width != 10 || sz.height != 20) throw test_excep();
  898. if (cvSize(sz).width != 10 || cvSize(sz).height != 20) throw test_excep();
  899. Rect r1(0, 0, 10, 20);
  900. Size sz1(5, 10);
  901. r1 -= sz1;
  902. if (r1.size().width != 5 || r1.size().height != 10) throw test_excep();
  903. Rect r2 = r1 - sz1;
  904. if (r2.size().width != 0 || r2.size().height != 0) throw test_excep();
  905. Vec<double, 5> v5d(1, 1, 1, 1, 1);
  906. Vec<double, 6> v6d(1, 1, 1, 1, 1, 1);
  907. Vec<double, 7> v7d(1, 1, 1, 1, 1, 1, 1);
  908. Vec<double, 8> v8d(1, 1, 1, 1, 1, 1, 1, 1);
  909. Vec<double, 9> v9d(1, 1, 1, 1, 1, 1, 1, 1, 1);
  910. Vec<double,10> v10d(1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
  911. Vec<double,10> v10dzero;
  912. for (int ii = 0; ii < 10; ++ii) {
  913. if (v10dzero[ii] != 0.0)
  914. throw test_excep();
  915. }
  916. Mat A(1, 32, CV_32F), B;
  917. for( int i = 0; i < A.cols; i++ )
  918. A.at<float>(i) = (float)(i <= 12 ? i : 24 - i);
  919. cv::transpose(A, B);
  920. int minidx[2] = {0, 0}, maxidx[2] = {0, 0};
  921. double minval = 0, maxval = 0;
  922. cv::minMaxIdx(A, &minval, &maxval, minidx, maxidx);
  923. if( !(minidx[0] == 0 && minidx[1] == 31 && maxidx[0] == 0 && maxidx[1] == 12 &&
  924. minval == -7 && maxval == 12))
  925. throw test_excep();
  926. cv::minMaxIdx(B, &minval, &maxval, minidx, maxidx);
  927. if( !(minidx[0] == 31 && minidx[1] == 0 && maxidx[0] == 12 && maxidx[1] == 0 &&
  928. minval == -7 && maxval == 12))
  929. throw test_excep();
  930. Matx33f b(1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f);
  931. Mat c;
  932. cv::add(Mat::zeros(3, 3, CV_32F), b, c);
  933. CV_Assert( cvtest::norm(b, c, CV_C) == 0 );
  934. cv::add(Mat::zeros(3, 3, CV_64F), b, c, noArray(), c.type());
  935. CV_Assert( cvtest::norm(b, c, CV_C) == 0 );
  936. cv::add(Mat::zeros(6, 1, CV_64F), 1, c, noArray(), c.type());
  937. CV_Assert( cvtest::norm(Matx61f(1.f, 1.f, 1.f, 1.f, 1.f, 1.f), c, CV_C) == 0 );
  938. vector<Point2f> pt2d(3);
  939. vector<Point3d> pt3d(2);
  940. CV_Assert( Mat(pt2d).checkVector(2) == 3 && Mat(pt2d).checkVector(3) < 0 &&
  941. Mat(pt3d).checkVector(2) < 0 && Mat(pt3d).checkVector(3) == 2 );
  942. Matx44f m44(0.8147f, 0.6324f, 0.9575f, 0.9572f,
  943. 0.9058f, 0.0975f, 0.9649f, 0.4854f,
  944. 0.1270f, 0.2785f, 0.1576f, 0.8003f,
  945. 0.9134f, 0.5469f, 0.9706f, 0.1419f);
  946. double d = cv::determinant(m44);
  947. CV_Assert( fabs(d - (-0.0262)) <= 0.001 );
  948. Cv32suf z;
  949. z.i = 0x80000000;
  950. CV_Assert( cvFloor(z.f) == 0 && cvCeil(z.f) == 0 && cvRound(z.f) == 0 );
  951. }
  952. catch(const test_excep&)
  953. {
  954. ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
  955. return false;
  956. }
  957. return true;
  958. }
  959. bool CV_OperationsTest::TestExp()
  960. {
  961. Mat1f tt = Mat1f::ones(4,2);
  962. Mat1f outs;
  963. exp(-tt, outs);
  964. Mat1f tt2 = Mat1f::ones(4,1), outs2;
  965. exp(-tt2, outs2);
  966. return true;
  967. }
  968. bool CV_OperationsTest::TestSVD()
  969. {
  970. try
  971. {
  972. Mat A = (Mat_<double>(3,4) << 1, 2, -1, 4, 2, 4, 3, 5, -1, -2, 6, 7);
  973. Mat x;
  974. SVD::solveZ(A,x);
  975. if( cvtest::norm(A*x, CV_C) > FLT_EPSILON )
  976. throw test_excep();
  977. SVD svd(A, SVD::FULL_UV);
  978. if( cvtest::norm(A*svd.vt.row(3).t(), CV_C) > FLT_EPSILON )
  979. throw test_excep();
  980. Mat Dp(3,3,CV_32FC1);
  981. Mat Dc(3,3,CV_32FC1);
  982. Mat Q(3,3,CV_32FC1);
  983. Mat U,Vt,R,T,W;
  984. Dp.at<float>(0,0)=0.86483884f; Dp.at<float>(0,1)= -0.3077251f; Dp.at<float>(0,2)=-0.55711365f;
  985. Dp.at<float>(1,0)=0.49294353f; Dp.at<float>(1,1)=-0.24209651f; Dp.at<float>(1,2)=-0.25084701f;
  986. Dp.at<float>(2,0)=0; Dp.at<float>(2,1)=0; Dp.at<float>(2,2)=0;
  987. Dc.at<float>(0,0)=0.75632739f; Dc.at<float>(0,1)= -0.38859656f; Dc.at<float>(0,2)=-0.36773083f;
  988. Dc.at<float>(1,0)=0.9699229f; Dc.at<float>(1,1)=-0.49858192f; Dc.at<float>(1,2)=-0.47134098f;
  989. Dc.at<float>(2,0)=0.10566688f; Dc.at<float>(2,1)=-0.060333252f; Dc.at<float>(2,2)=-0.045333147f;
  990. Q=Dp*Dc.t();
  991. SVD decomp;
  992. decomp=SVD(Q);
  993. U=decomp.u;
  994. Vt=decomp.vt;
  995. W=decomp.w;
  996. Mat I = Mat::eye(3, 3, CV_32F);
  997. if( cvtest::norm(U*U.t(), I, CV_C) > FLT_EPSILON ||
  998. cvtest::norm(Vt*Vt.t(), I, CV_C) > FLT_EPSILON ||
  999. W.at<float>(2) < 0 || W.at<float>(1) < W.at<float>(2) ||
  1000. W.at<float>(0) < W.at<float>(1) ||
  1001. cvtest::norm(U*Mat::diag(W)*Vt, Q, CV_C) > FLT_EPSILON )
  1002. throw test_excep();
  1003. }
  1004. catch(const test_excep&)
  1005. {
  1006. ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
  1007. return false;
  1008. }
  1009. return true;
  1010. }
  1011. void CV_OperationsTest::run( int /* start_from */)
  1012. {
  1013. if (!TestMat())
  1014. return;
  1015. if (!SomeMatFunctions())
  1016. return;
  1017. if (!TestTemplateMat())
  1018. return;
  1019. if (!TestMatND())
  1020. return;
  1021. if (!TestSparseMat())
  1022. return;
  1023. if (!TestVec())
  1024. return;
  1025. if (!TestMatxMultiplication())
  1026. return;
  1027. if (!TestMatxElementwiseDivison())
  1028. return;
  1029. if (!TestDivisionByValue())
  1030. return;
  1031. if (!TestInplaceDivisionByValue())
  1032. return;
  1033. if (!TestMatMatxCastSum())
  1034. return;
  1035. if (!TestSubMatAccess())
  1036. return;
  1037. if (!TestExp())
  1038. return;
  1039. if (!TestSVD())
  1040. return;
  1041. if (!operations1())
  1042. return;
  1043. ts->set_failed_test_info(cvtest::TS::OK);
  1044. }
  1045. TEST(Core_Array, expressions) { CV_OperationsTest test; test.safe_run(); }
  1046. class CV_SparseMatTest : public cvtest::BaseTest
  1047. {
  1048. public:
  1049. CV_SparseMatTest() {}
  1050. ~CV_SparseMatTest() {}
  1051. protected:
  1052. void run(int)
  1053. {
  1054. try
  1055. {
  1056. RNG& rng = theRNG();
  1057. const int MAX_DIM=3;
  1058. int sizes[MAX_DIM], idx[MAX_DIM];
  1059. for( int iter = 0; iter < 100; iter++ )
  1060. {
  1061. ts->printf(cvtest::TS::LOG, ".");
  1062. ts->update_context(this, iter, true);
  1063. int k, dims = rng.uniform(1, MAX_DIM+1), p = 1;
  1064. for( k = 0; k < dims; k++ )
  1065. {
  1066. sizes[k] = rng.uniform(1, 30);
  1067. p *= sizes[k];
  1068. }
  1069. int j, nz = rng.uniform(0, (p+2)/2), nz0 = 0;
  1070. SparseMat_<int> v(dims,sizes);
  1071. CV_Assert( (int)v.nzcount() == 0 );
  1072. SparseMatIterator_<int> it = v.begin();
  1073. SparseMatIterator_<int> it_end = v.end();
  1074. for( k = 0; it != it_end; ++it, ++k )
  1075. ;
  1076. CV_Assert( k == 0 );
  1077. int sum0 = 0, sum = 0;
  1078. for( j = 0; j < nz; j++ )
  1079. {
  1080. int val = rng.uniform(1, 100);
  1081. for( k = 0; k < dims; k++ )
  1082. idx[k] = rng.uniform(0, sizes[k]);
  1083. if( dims == 1 )
  1084. {
  1085. CV_Assert( v.ref(idx[0]) == v(idx[0]) );
  1086. }
  1087. else if( dims == 2 )
  1088. {
  1089. CV_Assert( v.ref(idx[0], idx[1]) == v(idx[0], idx[1]) );
  1090. }
  1091. else if( dims == 3 )
  1092. {
  1093. CV_Assert( v.ref(idx[0], idx[1], idx[2]) == v(idx[0], idx[1], idx[2]) );
  1094. }
  1095. CV_Assert( v.ref(idx) == v(idx) );
  1096. v.ref(idx) += val;
  1097. if( v(idx) == val )
  1098. nz0++;
  1099. sum0 += val;
  1100. }
  1101. CV_Assert( (int)v.nzcount() == nz0 );
  1102. it = v.begin();
  1103. it_end = v.end();
  1104. for( k = 0; it != it_end; ++it, ++k )
  1105. sum += *it;
  1106. CV_Assert( k == nz0 && sum == sum0 );
  1107. v.clear();
  1108. CV_Assert( (int)v.nzcount() == 0 );
  1109. it = v.begin();
  1110. it_end = v.end();
  1111. for( k = 0; it != it_end; ++it, ++k )
  1112. ;
  1113. CV_Assert( k == 0 );
  1114. }
  1115. }
  1116. catch(...)
  1117. {
  1118. ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
  1119. }
  1120. }
  1121. };
  1122. TEST(Core_SparseMat, iterations) { CV_SparseMatTest test; test.safe_run(); }
  1123. TEST(MatTestRoi, adjustRoiOverflow)
  1124. {
  1125. Mat m(15, 10, CV_32S);
  1126. Mat roi(m, cv::Range(2, 10), cv::Range(3,6));
  1127. int rowsInROI = roi.rows;
  1128. roi.adjustROI(1, 0, 0, 0);
  1129. ASSERT_EQ(roi.rows, rowsInROI + 1);
  1130. roi.adjustROI(-m.rows, -m.rows, 0, 0);
  1131. ASSERT_EQ(roi.rows, m.rows);
  1132. }
  1133. CV_ENUM(SortRowCol, SORT_EVERY_COLUMN, SORT_EVERY_ROW)
  1134. CV_ENUM(SortOrder, SORT_ASCENDING, SORT_DESCENDING)
  1135. PARAM_TEST_CASE(sortIdx, MatDepth, SortRowCol, SortOrder, Size, bool)
  1136. {
  1137. int type;
  1138. Size size;
  1139. int flags;
  1140. bool use_roi;
  1141. Mat src, src_roi;
  1142. Mat dst, dst_roi;
  1143. virtual void SetUp()
  1144. {
  1145. int depth = GET_PARAM(0);
  1146. int rowFlags = GET_PARAM(1);
  1147. int orderFlags = GET_PARAM(2);
  1148. size = GET_PARAM(3);
  1149. use_roi = GET_PARAM(4);
  1150. type = CV_MAKE_TYPE(depth, 1);
  1151. flags = rowFlags | orderFlags;
  1152. }
  1153. void generateTestData()
  1154. {
  1155. Border srcBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
  1156. randomSubMat(src, src_roi, size, srcBorder, type, -100, 100);
  1157. Border dstBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
  1158. randomSubMat(dst, dst_roi, size, dstBorder, CV_32S, 5, 16);
  1159. }
  1160. template<typename T>
  1161. void check_(const cv::Mat& values_, const cv::Mat_<int>& idx_)
  1162. {
  1163. cv::Mat_<T>& values = (cv::Mat_<T>&)values_;
  1164. cv::Mat_<int>& idx = (cv::Mat_<int>&)idx_;
  1165. size_t N = values.total();
  1166. std::vector<bool> processed(N, false);
  1167. int prevIdx = idx(0);
  1168. T prevValue = values(prevIdx);
  1169. processed[prevIdx] = true;
  1170. for (size_t i = 1; i < N; i++)
  1171. {
  1172. int nextIdx = idx((int)i);
  1173. T value = values(nextIdx);
  1174. ASSERT_EQ(false, processed[nextIdx]) << "Indexes must be unique. i=" << i << " idx=" << nextIdx << std::endl << idx;
  1175. processed[nextIdx] = true;
  1176. if ((flags & SORT_DESCENDING) == SORT_DESCENDING)
  1177. ASSERT_GE(prevValue, value) << "i=" << i << " prevIdx=" << prevIdx << " idx=" << nextIdx;
  1178. else
  1179. ASSERT_LE(prevValue, value) << "i=" << i << " prevIdx=" << prevIdx << " idx=" << nextIdx;
  1180. prevValue = value;
  1181. prevIdx = nextIdx;
  1182. }
  1183. }
  1184. void validate()
  1185. {
  1186. ASSERT_EQ(CV_32SC1, dst_roi.type());
  1187. ASSERT_EQ(size, dst_roi.size());
  1188. bool isColumn = (flags & SORT_EVERY_COLUMN) == SORT_EVERY_COLUMN;
  1189. size_t N = isColumn ? src_roi.cols : src_roi.rows;
  1190. Mat values_row((int)N, 1, type), idx_row((int)N, 1, CV_32S);
  1191. for (size_t i = 0; i < N; i++)
  1192. {
  1193. SCOPED_TRACE(cv::format("row/col=%d", (int)i));
  1194. if (isColumn)
  1195. {
  1196. src_roi.col((int)i).copyTo(values_row);
  1197. dst_roi.col((int)i).copyTo(idx_row);
  1198. }
  1199. else
  1200. {
  1201. src_roi.row((int)i).copyTo(values_row);
  1202. dst_roi.row((int)i).copyTo(idx_row);
  1203. }
  1204. switch(type)
  1205. {
  1206. case CV_8U: check_<uchar>(values_row, idx_row); break;
  1207. case CV_8S: check_<char>(values_row, idx_row); break;
  1208. case CV_16S: check_<short>(values_row, idx_row); break;
  1209. case CV_32S: check_<int>(values_row, idx_row); break;
  1210. case CV_32F: check_<float>(values_row, idx_row); break;
  1211. case CV_64F: check_<double>(values_row, idx_row); break;
  1212. default: ASSERT_FALSE(true) << "Unsupported type: " << type;
  1213. }
  1214. }
  1215. }
  1216. };
  1217. TEST_P(sortIdx, simple)
  1218. {
  1219. for (int j = 0; j < 5; j++)
  1220. {
  1221. generateTestData();
  1222. cv::sortIdx(src_roi, dst_roi, flags);
  1223. validate();
  1224. }
  1225. }
  1226. INSTANTIATE_TEST_CASE_P(Core, sortIdx, Combine(
  1227. Values(CV_8U, CV_8S, CV_16S, CV_32S, CV_32F, CV_64F), // depth
  1228. Values(SORT_EVERY_COLUMN, SORT_EVERY_ROW),
  1229. Values(SORT_ASCENDING, SORT_DESCENDING),
  1230. Values(Size(3, 3), Size(16, 8)),
  1231. ::testing::Bool()
  1232. ));
  1233. TEST(Core_sortIdx, regression_8941)
  1234. {
  1235. cv::Mat src = (cv::Mat_<int>(3, 3) <<
  1236. 1, 2, 3,
  1237. 0, 9, 5,
  1238. 8, 1, 6
  1239. );
  1240. cv::Mat expected = (cv::Mat_<int>(3, 1) <<
  1241. 1,
  1242. 0,
  1243. 2
  1244. );
  1245. cv::Mat result;
  1246. cv::sortIdx(src.col(0), result, CV_SORT_EVERY_COLUMN | CV_SORT_ASCENDING);
  1247. #if 0
  1248. std::cout << src.col(0) << std::endl;
  1249. std::cout << result << std::endl;
  1250. #endif
  1251. ASSERT_EQ(expected.size(), result.size());
  1252. EXPECT_EQ(0, cvtest::norm(expected, result, NORM_INF)) <<
  1253. "result=" << std::endl << result << std::endl <<
  1254. "expected=" << std::endl << expected;
  1255. }
  1256. TEST(Core_Mat, augmentation_operations_9688)
  1257. {
  1258. {
  1259. Mat x(1, 1, CV_64FC1, 1.0f);
  1260. Mat p(1, 4, CV_64FC1, 5.0f);
  1261. EXPECT_ANY_THROW(
  1262. x += p;
  1263. ) << x;
  1264. }
  1265. {
  1266. Mat x(1, 1, CV_64FC1, 1.0f);
  1267. Mat p(1, 4, CV_64FC1, 5.0f);
  1268. EXPECT_ANY_THROW(
  1269. x -= p;
  1270. ) << x;
  1271. }
  1272. }
  1273. //These tests guard regressions against running MatExpr
  1274. //operations on empty operands and giving bogus
  1275. //results.
  1276. TEST(Core_MatExpr, empty_check_15760)
  1277. {
  1278. EXPECT_THROW(Mat c = min(Mat(), Mat()), cv::Exception);
  1279. EXPECT_THROW(Mat c = abs(Mat()), cv::Exception);
  1280. EXPECT_THROW(Mat c = min(Mat(), Mat()), cv::Exception);
  1281. EXPECT_THROW(Mat c = Mat() | Mat(), cv::Exception);
  1282. EXPECT_THROW(Mat c = Mat() + Mat(), cv::Exception);
  1283. EXPECT_THROW(Mat c = Mat().t(), cv::Exception);
  1284. EXPECT_THROW(Mat c = Mat().cross(Mat()), cv::Exception);
  1285. }
  1286. TEST(Core_Arithm, scalar_handling_19599) // https://github.com/opencv/opencv/issues/19599 (OpenCV 4.x+ only)
  1287. {
  1288. Mat a(1, 1, CV_32F, Scalar::all(1));
  1289. Mat b(4, 1, CV_64F, Scalar::all(1)); // MatExpr may convert Scalar to Mat
  1290. Mat c;
  1291. EXPECT_NO_THROW(cv::multiply(a, b, c));
  1292. EXPECT_EQ(1, c.cols);
  1293. EXPECT_EQ(1, c.rows);
  1294. }
  1295. }} // namespace