test_operations.cpp 56 KB

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