test_arithm.cpp 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include "test_precomp.hpp"
  5. #include "ref_reduce_arg.impl.hpp"
  6. #include <algorithm>
  7. namespace opencv_test { namespace {
  8. const int ARITHM_NTESTS = 1000;
  9. const int ARITHM_RNG_SEED = -1;
  10. const int ARITHM_MAX_CHANNELS = 4;
  11. const int ARITHM_MAX_NDIMS = 4;
  12. const int ARITHM_MAX_SIZE_LOG = 10;
  13. struct BaseElemWiseOp
  14. {
  15. enum
  16. {
  17. FIX_ALPHA=1, FIX_BETA=2, FIX_GAMMA=4, REAL_GAMMA=8,
  18. SUPPORT_MASK=16, SCALAR_OUTPUT=32, SUPPORT_MULTICHANNELMASK=64,
  19. MIXED_TYPE=128
  20. };
  21. BaseElemWiseOp(int _ninputs, int _flags, double _alpha, double _beta,
  22. Scalar _gamma=Scalar::all(0), int _context=1)
  23. : ninputs(_ninputs), flags(_flags), alpha(_alpha), beta(_beta), gamma(_gamma), context(_context) {}
  24. BaseElemWiseOp() { flags = 0; alpha = beta = 0; gamma = Scalar::all(0); ninputs = 0; context = 1; }
  25. virtual ~BaseElemWiseOp() {}
  26. virtual void op(const vector<Mat>&, Mat&, const Mat&) {}
  27. virtual void refop(const vector<Mat>&, Mat&, const Mat&) {}
  28. virtual void getValueRange(int depth, double& minval, double& maxval)
  29. {
  30. minval = depth < CV_32S ? cvtest::getMinVal(depth) : depth == CV_32S ? -1000000 : -1000.;
  31. maxval = depth < CV_32S ? cvtest::getMaxVal(depth) : depth == CV_32S ? 1000000 : 1000.;
  32. }
  33. virtual void getRandomSize(RNG& rng, vector<int>& size)
  34. {
  35. cvtest::randomSize(rng, 2, ARITHM_MAX_NDIMS, ARITHM_MAX_SIZE_LOG, size);
  36. }
  37. virtual int getRandomType(RNG& rng)
  38. {
  39. return cvtest::randomType(rng, _OutputArray::DEPTH_MASK_ALL_BUT_8S, 1,
  40. ninputs > 1 ? ARITHM_MAX_CHANNELS : 4);
  41. }
  42. virtual double getMaxErr(int depth) { return depth < CV_32F ? 1 : depth == CV_32F ? 1e-5 : 1e-12; }
  43. virtual void generateScalars(int depth, RNG& rng)
  44. {
  45. const double m = 3.;
  46. if( !(flags & FIX_ALPHA) )
  47. {
  48. alpha = exp(rng.uniform(-0.5, 0.1)*m*2*CV_LOG2);
  49. alpha *= rng.uniform(0, 2) ? 1 : -1;
  50. }
  51. if( !(flags & FIX_BETA) )
  52. {
  53. beta = exp(rng.uniform(-0.5, 0.1)*m*2*CV_LOG2);
  54. beta *= rng.uniform(0, 2) ? 1 : -1;
  55. }
  56. if( !(flags & FIX_GAMMA) )
  57. {
  58. for( int i = 0; i < 4; i++ )
  59. {
  60. gamma[i] = exp(rng.uniform(-1, 6)*m*CV_LOG2);
  61. gamma[i] *= rng.uniform(0, 2) ? 1 : -1;
  62. }
  63. if( flags & REAL_GAMMA )
  64. gamma = Scalar::all(gamma[0]);
  65. }
  66. if( depth == CV_32F )
  67. {
  68. Mat fl, db;
  69. db = Mat(1, 1, CV_64F, &alpha);
  70. db.convertTo(fl, CV_32F);
  71. fl.convertTo(db, CV_64F);
  72. db = Mat(1, 1, CV_64F, &beta);
  73. db.convertTo(fl, CV_32F);
  74. fl.convertTo(db, CV_64F);
  75. db = Mat(1, 4, CV_64F, &gamma[0]);
  76. db.convertTo(fl, CV_32F);
  77. fl.convertTo(db, CV_64F);
  78. }
  79. }
  80. int ninputs;
  81. int flags;
  82. double alpha;
  83. double beta;
  84. Scalar gamma;
  85. int context;
  86. };
  87. struct BaseAddOp : public BaseElemWiseOp
  88. {
  89. BaseAddOp(int _ninputs, int _flags, double _alpha, double _beta, Scalar _gamma=Scalar::all(0))
  90. : BaseElemWiseOp(_ninputs, _flags, _alpha, _beta, _gamma) {}
  91. void refop(const vector<Mat>& src, Mat& dst, const Mat& mask)
  92. {
  93. int dstType = (flags & MIXED_TYPE) ? dst.type() : src[0].type();
  94. if( !mask.empty() )
  95. {
  96. Mat temp;
  97. cvtest::add(src[0], alpha, src.size() > 1 ? src[1] : Mat(), beta, gamma, temp, dstType);
  98. cvtest::copy(temp, dst, mask);
  99. }
  100. else
  101. cvtest::add(src[0], alpha, src.size() > 1 ? src[1] : Mat(), beta, gamma, dst, dstType);
  102. }
  103. };
  104. struct AddOp : public BaseAddOp
  105. {
  106. AddOp() : BaseAddOp(2, FIX_ALPHA+FIX_BETA+FIX_GAMMA+SUPPORT_MASK, 1, 1, Scalar::all(0)) {}
  107. void op(const vector<Mat>& src, Mat& dst, const Mat& mask)
  108. {
  109. int dtype = (flags & MIXED_TYPE) ? dst.type() : -1;
  110. cv::add(src[0], src[1], dst, mask, dtype);
  111. }
  112. };
  113. struct SubOp : public BaseAddOp
  114. {
  115. SubOp() : BaseAddOp(2, FIX_ALPHA+FIX_BETA+FIX_GAMMA+SUPPORT_MASK, 1, -1, Scalar::all(0)) {}
  116. void op(const vector<Mat>& src, Mat& dst, const Mat& mask)
  117. {
  118. int dtype = (flags & MIXED_TYPE) ? dst.type() : -1;
  119. cv::subtract(src[0], src[1], dst, mask, dtype);
  120. }
  121. };
  122. struct AddSOp : public BaseAddOp
  123. {
  124. AddSOp() : BaseAddOp(1, FIX_ALPHA+FIX_BETA+SUPPORT_MASK, 1, 0, Scalar::all(0)) {}
  125. void op(const vector<Mat>& src, Mat& dst, const Mat& mask)
  126. {
  127. int dtype = (flags & MIXED_TYPE) ? dst.type() : -1;
  128. cv::add(src[0], gamma, dst, mask, dtype);
  129. }
  130. };
  131. struct SubRSOp : public BaseAddOp
  132. {
  133. SubRSOp() : BaseAddOp(1, FIX_ALPHA+FIX_BETA+SUPPORT_MASK, -1, 0, Scalar::all(0)) {}
  134. void op(const vector<Mat>& src, Mat& dst, const Mat& mask)
  135. {
  136. int dtype = (flags & MIXED_TYPE) ? dst.type() : -1;
  137. cv::subtract(gamma, src[0], dst, mask, dtype);
  138. }
  139. };
  140. struct ScaleAddOp : public BaseAddOp
  141. {
  142. ScaleAddOp() : BaseAddOp(2, FIX_BETA+FIX_GAMMA, 1, 1, Scalar::all(0)) {}
  143. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  144. {
  145. cv::scaleAdd(src[0], alpha, src[1], dst);
  146. }
  147. double getMaxErr(int depth)
  148. {
  149. return depth < CV_32F ? 1 : depth == CV_32F ? 3e-5 : 1e-12;
  150. }
  151. };
  152. struct AddWeightedOp : public BaseAddOp
  153. {
  154. AddWeightedOp() : BaseAddOp(2, REAL_GAMMA, 1, 1, Scalar::all(0)) {}
  155. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  156. {
  157. int dtype = (flags & MIXED_TYPE) ? dst.type() : -1;
  158. cv::addWeighted(src[0], alpha, src[1], beta, gamma[0], dst, dtype);
  159. }
  160. };
  161. struct MulOp : public BaseElemWiseOp
  162. {
  163. MulOp() : BaseElemWiseOp(2, FIX_BETA+FIX_GAMMA, 1, 1, Scalar::all(0)) {}
  164. void getValueRange(int depth, double& minval, double& maxval)
  165. {
  166. minval = depth < CV_32S ? cvtest::getMinVal(depth) : depth == CV_32S ? -1000000 : -1000.;
  167. maxval = depth < CV_32S ? cvtest::getMaxVal(depth) : depth == CV_32S ? 1000000 : 1000.;
  168. minval = std::max(minval, -30000.);
  169. maxval = std::min(maxval, 30000.);
  170. }
  171. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  172. {
  173. int dtype = (flags & MIXED_TYPE) ? dst.type() : -1;
  174. cv::multiply(src[0], src[1], dst, alpha, dtype);
  175. }
  176. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  177. {
  178. int dtype = (flags & MIXED_TYPE) ? dst.type() : -1;
  179. cvtest::multiply(src[0], src[1], dst, alpha, dtype);
  180. }
  181. };
  182. struct MulSOp : public BaseElemWiseOp
  183. {
  184. MulSOp() : BaseElemWiseOp(1, FIX_BETA+FIX_GAMMA, 1, 1, Scalar::all(0)) {}
  185. void getValueRange(int depth, double& minval, double& maxval)
  186. {
  187. minval = depth < CV_32S ? cvtest::getMinVal(depth) : depth == CV_32S ? -1000000 : -1000.;
  188. maxval = depth < CV_32S ? cvtest::getMaxVal(depth) : depth == CV_32S ? 1000000 : 1000.;
  189. minval = std::max(minval, -30000.);
  190. maxval = std::min(maxval, 30000.);
  191. }
  192. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  193. {
  194. int dtype = (flags & MIXED_TYPE) ? dst.type() : -1;
  195. cv::multiply(src[0], alpha, dst, /* scale */ 1.0, dtype);
  196. }
  197. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  198. {
  199. int dtype = (flags & MIXED_TYPE) ? dst.type() : -1;
  200. cvtest::multiply(Mat(), src[0], dst, alpha, dtype);
  201. }
  202. };
  203. struct DivOp : public BaseElemWiseOp
  204. {
  205. DivOp() : BaseElemWiseOp(2, FIX_BETA+FIX_GAMMA, 1, 1, Scalar::all(0)) {}
  206. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  207. {
  208. int dtype = (flags & MIXED_TYPE) ? dst.type() : -1;
  209. cv::divide(src[0], src[1], dst, alpha, dtype);
  210. if (flags & MIXED_TYPE)
  211. {
  212. // div by zero result is implementation-defined
  213. // since it may involve conversions to/from intermediate format
  214. Mat zeroMask = src[1] == 0;
  215. dst.setTo(0, zeroMask);
  216. }
  217. }
  218. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  219. {
  220. int dtype = (flags & MIXED_TYPE) ? dst.type() : -1;
  221. cvtest::divide(src[0], src[1], dst, alpha, dtype);
  222. }
  223. };
  224. struct RecipOp : public BaseElemWiseOp
  225. {
  226. RecipOp() : BaseElemWiseOp(1, FIX_BETA+FIX_GAMMA, 1, 1, Scalar::all(0)) {}
  227. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  228. {
  229. int dtype = (flags & MIXED_TYPE) ? dst.type() : -1;
  230. cv::divide(alpha, src[0], dst, dtype);
  231. if (flags & MIXED_TYPE)
  232. {
  233. // div by zero result is implementation-defined
  234. // since it may involve conversions to/from intermediate format
  235. Mat zeroMask = src[0] == 0;
  236. dst.setTo(0, zeroMask);
  237. }
  238. }
  239. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  240. {
  241. int dtype = (flags & MIXED_TYPE) ? dst.type() : -1;
  242. cvtest::divide(Mat(), src[0], dst, alpha, dtype);
  243. }
  244. };
  245. struct AbsDiffOp : public BaseAddOp
  246. {
  247. AbsDiffOp() : BaseAddOp(2, FIX_ALPHA+FIX_BETA+FIX_GAMMA, 1, -1, Scalar::all(0)) {}
  248. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  249. {
  250. absdiff(src[0], src[1], dst);
  251. }
  252. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  253. {
  254. cvtest::add(src[0], 1, src[1], -1, Scalar::all(0), dst, src[0].type(), true);
  255. }
  256. };
  257. struct AbsDiffSOp : public BaseAddOp
  258. {
  259. AbsDiffSOp() : BaseAddOp(1, FIX_ALPHA+FIX_BETA, 1, 0, Scalar::all(0)) {}
  260. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  261. {
  262. absdiff(src[0], gamma, dst);
  263. }
  264. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  265. {
  266. cvtest::add(src[0], 1, Mat(), 0, -gamma, dst, src[0].type(), true);
  267. }
  268. };
  269. struct LogicOp : public BaseElemWiseOp
  270. {
  271. LogicOp(char _opcode) : BaseElemWiseOp(2, FIX_ALPHA+FIX_BETA+FIX_GAMMA+SUPPORT_MASK, 1, 1, Scalar::all(0)), opcode(_opcode) {}
  272. void op(const vector<Mat>& src, Mat& dst, const Mat& mask)
  273. {
  274. if( opcode == '&' )
  275. cv::bitwise_and(src[0], src[1], dst, mask);
  276. else if( opcode == '|' )
  277. cv::bitwise_or(src[0], src[1], dst, mask);
  278. else
  279. cv::bitwise_xor(src[0], src[1], dst, mask);
  280. }
  281. void refop(const vector<Mat>& src, Mat& dst, const Mat& mask)
  282. {
  283. Mat temp;
  284. if( !mask.empty() )
  285. {
  286. cvtest::logicOp(src[0], src[1], temp, opcode);
  287. cvtest::copy(temp, dst, mask);
  288. }
  289. else
  290. cvtest::logicOp(src[0], src[1], dst, opcode);
  291. }
  292. double getMaxErr(int)
  293. {
  294. return 0;
  295. }
  296. char opcode;
  297. };
  298. struct LogicSOp : public BaseElemWiseOp
  299. {
  300. LogicSOp(char _opcode)
  301. : BaseElemWiseOp(1, FIX_ALPHA+FIX_BETA+(_opcode != '~' ? SUPPORT_MASK : 0), 1, 1, Scalar::all(0)), opcode(_opcode) {}
  302. void op(const vector<Mat>& src, Mat& dst, const Mat& mask)
  303. {
  304. if( opcode == '&' )
  305. cv::bitwise_and(src[0], gamma, dst, mask);
  306. else if( opcode == '|' )
  307. cv::bitwise_or(src[0], gamma, dst, mask);
  308. else if( opcode == '^' )
  309. cv::bitwise_xor(src[0], gamma, dst, mask);
  310. else
  311. cv::bitwise_not(src[0], dst);
  312. }
  313. void refop(const vector<Mat>& src, Mat& dst, const Mat& mask)
  314. {
  315. Mat temp;
  316. if( !mask.empty() )
  317. {
  318. cvtest::logicOp(src[0], gamma, temp, opcode);
  319. cvtest::copy(temp, dst, mask);
  320. }
  321. else
  322. cvtest::logicOp(src[0], gamma, dst, opcode);
  323. }
  324. double getMaxErr(int)
  325. {
  326. return 0;
  327. }
  328. char opcode;
  329. };
  330. struct MinOp : public BaseElemWiseOp
  331. {
  332. MinOp() : BaseElemWiseOp(2, FIX_ALPHA+FIX_BETA+FIX_GAMMA, 1, 1, Scalar::all(0)) {}
  333. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  334. {
  335. cv::min(src[0], src[1], dst);
  336. }
  337. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  338. {
  339. cvtest::min(src[0], src[1], dst);
  340. }
  341. double getMaxErr(int)
  342. {
  343. return 0;
  344. }
  345. };
  346. struct MaxOp : public BaseElemWiseOp
  347. {
  348. MaxOp() : BaseElemWiseOp(2, FIX_ALPHA+FIX_BETA+FIX_GAMMA, 1, 1, Scalar::all(0)) {}
  349. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  350. {
  351. cv::max(src[0], src[1], dst);
  352. }
  353. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  354. {
  355. cvtest::max(src[0], src[1], dst);
  356. }
  357. double getMaxErr(int)
  358. {
  359. return 0;
  360. }
  361. };
  362. struct MinSOp : public BaseElemWiseOp
  363. {
  364. MinSOp() : BaseElemWiseOp(1, FIX_ALPHA+FIX_BETA+REAL_GAMMA, 1, 1, Scalar::all(0)) {}
  365. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  366. {
  367. cv::min(src[0], gamma[0], dst);
  368. }
  369. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  370. {
  371. cvtest::min(src[0], gamma[0], dst);
  372. }
  373. double getMaxErr(int)
  374. {
  375. return 0;
  376. }
  377. };
  378. struct MaxSOp : public BaseElemWiseOp
  379. {
  380. MaxSOp() : BaseElemWiseOp(1, FIX_ALPHA+FIX_BETA+REAL_GAMMA, 1, 1, Scalar::all(0)) {}
  381. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  382. {
  383. cv::max(src[0], gamma[0], dst);
  384. }
  385. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  386. {
  387. cvtest::max(src[0], gamma[0], dst);
  388. }
  389. double getMaxErr(int)
  390. {
  391. return 0;
  392. }
  393. };
  394. struct CmpOp : public BaseElemWiseOp
  395. {
  396. CmpOp() : BaseElemWiseOp(2, FIX_ALPHA+FIX_BETA+FIX_GAMMA, 1, 1, Scalar::all(0)) { cmpop = 0; }
  397. void generateScalars(int depth, RNG& rng)
  398. {
  399. BaseElemWiseOp::generateScalars(depth, rng);
  400. cmpop = rng.uniform(0, 6);
  401. }
  402. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  403. {
  404. cv::compare(src[0], src[1], dst, cmpop);
  405. }
  406. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  407. {
  408. cvtest::compare(src[0], src[1], dst, cmpop);
  409. }
  410. int getRandomType(RNG& rng)
  411. {
  412. return cvtest::randomType(rng, _OutputArray::DEPTH_MASK_ALL_BUT_8S, 1, 1);
  413. }
  414. double getMaxErr(int)
  415. {
  416. return 0;
  417. }
  418. int cmpop;
  419. };
  420. struct CmpSOp : public BaseElemWiseOp
  421. {
  422. CmpSOp() : BaseElemWiseOp(1, FIX_ALPHA+FIX_BETA+REAL_GAMMA, 1, 1, Scalar::all(0)) { cmpop = 0; }
  423. void generateScalars(int depth, RNG& rng)
  424. {
  425. BaseElemWiseOp::generateScalars(depth, rng);
  426. cmpop = rng.uniform(0, 6);
  427. if( depth < CV_32F )
  428. gamma[0] = cvRound(gamma[0]);
  429. }
  430. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  431. {
  432. cv::compare(src[0], gamma[0], dst, cmpop);
  433. }
  434. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  435. {
  436. cvtest::compare(src[0], gamma[0], dst, cmpop);
  437. }
  438. int getRandomType(RNG& rng)
  439. {
  440. return cvtest::randomType(rng, _OutputArray::DEPTH_MASK_ALL_BUT_8S, 1, 1);
  441. }
  442. double getMaxErr(int)
  443. {
  444. return 0;
  445. }
  446. int cmpop;
  447. };
  448. struct CopyOp : public BaseElemWiseOp
  449. {
  450. CopyOp() : BaseElemWiseOp(1, FIX_ALPHA+FIX_BETA+FIX_GAMMA+SUPPORT_MASK+SUPPORT_MULTICHANNELMASK, 1, 1, Scalar::all(0)) { }
  451. void op(const vector<Mat>& src, Mat& dst, const Mat& mask)
  452. {
  453. src[0].copyTo(dst, mask);
  454. }
  455. void refop(const vector<Mat>& src, Mat& dst, const Mat& mask)
  456. {
  457. cvtest::copy(src[0], dst, mask);
  458. }
  459. int getRandomType(RNG& rng)
  460. {
  461. return cvtest::randomType(rng, _OutputArray::DEPTH_MASK_ALL_16F, 1, ARITHM_MAX_CHANNELS);
  462. }
  463. double getMaxErr(int)
  464. {
  465. return 0;
  466. }
  467. };
  468. struct SetOp : public BaseElemWiseOp
  469. {
  470. SetOp() : BaseElemWiseOp(0, FIX_ALPHA+FIX_BETA+SUPPORT_MASK+SUPPORT_MULTICHANNELMASK, 1, 1, Scalar::all(0)) {}
  471. void op(const vector<Mat>&, Mat& dst, const Mat& mask)
  472. {
  473. dst.setTo(gamma, mask);
  474. }
  475. void refop(const vector<Mat>&, Mat& dst, const Mat& mask)
  476. {
  477. cvtest::set(dst, gamma, mask);
  478. }
  479. int getRandomType(RNG& rng)
  480. {
  481. return cvtest::randomType(rng, _OutputArray::DEPTH_MASK_ALL_16F, 1, ARITHM_MAX_CHANNELS);
  482. }
  483. double getMaxErr(int)
  484. {
  485. return 0;
  486. }
  487. };
  488. template<typename _Tp, typename _WTp> static void
  489. inRangeS_(const _Tp* src, const _WTp* a, const _WTp* b, uchar* dst, size_t total, int cn)
  490. {
  491. size_t i;
  492. int c;
  493. for( i = 0; i < total; i++ )
  494. {
  495. _Tp val = src[i*cn];
  496. dst[i] = (a[0] <= val && val <= b[0]) ? uchar(255) : 0;
  497. }
  498. for( c = 1; c < cn; c++ )
  499. {
  500. for( i = 0; i < total; i++ )
  501. {
  502. _Tp val = src[i*cn + c];
  503. dst[i] = a[c] <= val && val <= b[c] ? dst[i] : 0;
  504. }
  505. }
  506. }
  507. template<typename _Tp> static void inRange_(const _Tp* src, const _Tp* a, const _Tp* b, uchar* dst, size_t total, int cn)
  508. {
  509. size_t i;
  510. int c;
  511. for( i = 0; i < total; i++ )
  512. {
  513. _Tp val = src[i*cn];
  514. dst[i] = a[i*cn] <= val && val <= b[i*cn] ? 255 : 0;
  515. }
  516. for( c = 1; c < cn; c++ )
  517. {
  518. for( i = 0; i < total; i++ )
  519. {
  520. _Tp val = src[i*cn + c];
  521. dst[i] = a[i*cn + c] <= val && val <= b[i*cn + c] ? dst[i] : 0;
  522. }
  523. }
  524. }
  525. namespace reference {
  526. static void inRange(const Mat& src, const Mat& lb, const Mat& rb, Mat& dst)
  527. {
  528. CV_Assert( src.type() == lb.type() && src.type() == rb.type() &&
  529. src.size == lb.size && src.size == rb.size );
  530. dst.create( src.dims, &src.size[0], CV_8U );
  531. const Mat *arrays[]={&src, &lb, &rb, &dst, 0};
  532. Mat planes[4];
  533. NAryMatIterator it(arrays, planes);
  534. size_t total = planes[0].total();
  535. size_t i, nplanes = it.nplanes;
  536. int depth = src.depth(), cn = src.channels();
  537. for( i = 0; i < nplanes; i++, ++it )
  538. {
  539. const uchar* sptr = planes[0].ptr();
  540. const uchar* aptr = planes[1].ptr();
  541. const uchar* bptr = planes[2].ptr();
  542. uchar* dptr = planes[3].ptr();
  543. switch( depth )
  544. {
  545. case CV_8U:
  546. inRange_((const uchar*)sptr, (const uchar*)aptr, (const uchar*)bptr, dptr, total, cn);
  547. break;
  548. case CV_8S:
  549. inRange_((const schar*)sptr, (const schar*)aptr, (const schar*)bptr, dptr, total, cn);
  550. break;
  551. case CV_16U:
  552. inRange_((const ushort*)sptr, (const ushort*)aptr, (const ushort*)bptr, dptr, total, cn);
  553. break;
  554. case CV_16S:
  555. inRange_((const short*)sptr, (const short*)aptr, (const short*)bptr, dptr, total, cn);
  556. break;
  557. case CV_32S:
  558. inRange_((const int*)sptr, (const int*)aptr, (const int*)bptr, dptr, total, cn);
  559. break;
  560. case CV_32F:
  561. inRange_((const float*)sptr, (const float*)aptr, (const float*)bptr, dptr, total, cn);
  562. break;
  563. case CV_64F:
  564. inRange_((const double*)sptr, (const double*)aptr, (const double*)bptr, dptr, total, cn);
  565. break;
  566. default:
  567. CV_Error(cv::Error::StsUnsupportedFormat, "");
  568. }
  569. }
  570. }
  571. static void inRangeS(const Mat& src, const Scalar& lb, const Scalar& rb, Mat& dst)
  572. {
  573. dst.create( src.dims, &src.size[0], CV_8U );
  574. const Mat *arrays[]={&src, &dst, 0};
  575. Mat planes[2];
  576. NAryMatIterator it(arrays, planes);
  577. size_t total = planes[0].total();
  578. size_t i, nplanes = it.nplanes;
  579. int depth = src.depth(), cn = src.channels();
  580. union { double d[4]; float f[4]; int i[4];} lbuf, rbuf;
  581. int wtype = CV_MAKETYPE(depth <= CV_32S ? CV_32S : depth, cn);
  582. scalarToRawData(lb, lbuf.d, wtype, cn);
  583. scalarToRawData(rb, rbuf.d, wtype, cn);
  584. for( i = 0; i < nplanes; i++, ++it )
  585. {
  586. const uchar* sptr = planes[0].ptr();
  587. uchar* dptr = planes[1].ptr();
  588. switch( depth )
  589. {
  590. case CV_8U:
  591. inRangeS_((const uchar*)sptr, lbuf.i, rbuf.i, dptr, total, cn);
  592. break;
  593. case CV_8S:
  594. inRangeS_((const schar*)sptr, lbuf.i, rbuf.i, dptr, total, cn);
  595. break;
  596. case CV_16U:
  597. inRangeS_((const ushort*)sptr, lbuf.i, rbuf.i, dptr, total, cn);
  598. break;
  599. case CV_16S:
  600. inRangeS_((const short*)sptr, lbuf.i, rbuf.i, dptr, total, cn);
  601. break;
  602. case CV_32S:
  603. inRangeS_((const int*)sptr, lbuf.i, rbuf.i, dptr, total, cn);
  604. break;
  605. case CV_32F:
  606. inRangeS_((const float*)sptr, lbuf.f, rbuf.f, dptr, total, cn);
  607. break;
  608. case CV_64F:
  609. inRangeS_((const double*)sptr, lbuf.d, rbuf.d, dptr, total, cn);
  610. break;
  611. default:
  612. CV_Error(cv::Error::StsUnsupportedFormat, "");
  613. }
  614. }
  615. }
  616. } // namespace
  617. CVTEST_GUARD_SYMBOL(inRange)
  618. struct InRangeSOp : public BaseElemWiseOp
  619. {
  620. InRangeSOp() : BaseElemWiseOp(1, FIX_ALPHA+FIX_BETA, 1, 1, Scalar::all(0)) {}
  621. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  622. {
  623. cv::inRange(src[0], gamma, gamma1, dst);
  624. }
  625. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  626. {
  627. reference::inRangeS(src[0], gamma, gamma1, dst);
  628. }
  629. double getMaxErr(int)
  630. {
  631. return 0;
  632. }
  633. void generateScalars(int depth, RNG& rng)
  634. {
  635. BaseElemWiseOp::generateScalars(depth, rng);
  636. Scalar temp = gamma;
  637. BaseElemWiseOp::generateScalars(depth, rng);
  638. for( int i = 0; i < 4; i++ )
  639. {
  640. gamma1[i] = std::max(gamma[i], temp[i]);
  641. gamma[i] = std::min(gamma[i], temp[i]);
  642. }
  643. }
  644. Scalar gamma1;
  645. };
  646. struct InRangeOp : public BaseElemWiseOp
  647. {
  648. InRangeOp() : BaseElemWiseOp(3, FIX_ALPHA+FIX_BETA+FIX_GAMMA, 1, 1, Scalar::all(0)) {}
  649. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  650. {
  651. Mat lb, rb;
  652. cvtest::min(src[1], src[2], lb);
  653. cvtest::max(src[1], src[2], rb);
  654. cv::inRange(src[0], lb, rb, dst);
  655. }
  656. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  657. {
  658. Mat lb, rb;
  659. cvtest::min(src[1], src[2], lb);
  660. cvtest::max(src[1], src[2], rb);
  661. reference::inRange(src[0], lb, rb, dst);
  662. }
  663. double getMaxErr(int)
  664. {
  665. return 0;
  666. }
  667. };
  668. struct ConvertScaleOp : public BaseElemWiseOp
  669. {
  670. ConvertScaleOp() : BaseElemWiseOp(1, FIX_BETA+REAL_GAMMA, 1, 1, Scalar::all(0)), ddepth(0) { }
  671. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  672. {
  673. src[0].convertTo(dst, ddepth, alpha, gamma[0]);
  674. }
  675. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  676. {
  677. cvtest::convert(src[0], dst, CV_MAKETYPE(ddepth, src[0].channels()), alpha, gamma[0]);
  678. }
  679. int getRandomType(RNG& rng)
  680. {
  681. int srctype = cvtest::randomType(rng, _OutputArray::DEPTH_MASK_ALL, 1, ARITHM_MAX_CHANNELS);
  682. ddepth = cvtest::randomType(rng, _OutputArray::DEPTH_MASK_ALL, 1, 1);
  683. return srctype;
  684. }
  685. double getMaxErr(int)
  686. {
  687. return ddepth <= CV_32S ? 2 : ddepth < CV_64F ? 1e-3 : 1e-12;
  688. }
  689. void generateScalars(int depth, RNG& rng)
  690. {
  691. if( rng.uniform(0, 2) )
  692. BaseElemWiseOp::generateScalars(depth, rng);
  693. else
  694. {
  695. alpha = 1;
  696. gamma = Scalar::all(0);
  697. }
  698. }
  699. int ddepth;
  700. };
  701. struct ConvertScaleFp16Op : public BaseElemWiseOp
  702. {
  703. ConvertScaleFp16Op() : BaseElemWiseOp(1, FIX_BETA+REAL_GAMMA, 1, 1, Scalar::all(0)), nextRange(0) { }
  704. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  705. {
  706. Mat m;
  707. convertFp16(src[0], m);
  708. convertFp16(m, dst);
  709. }
  710. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  711. {
  712. cvtest::copy(src[0], dst);
  713. }
  714. int getRandomType(RNG&)
  715. {
  716. // 0: FP32 -> FP16 -> FP32
  717. // 1: FP16 -> FP32 -> FP16
  718. int srctype = (nextRange & 1) == 0 ? CV_32F : CV_16S;
  719. return srctype;
  720. }
  721. void getValueRange(int, double& minval, double& maxval)
  722. {
  723. // 0: FP32 -> FP16 -> FP32
  724. // 1: FP16 -> FP32 -> FP16
  725. if( (nextRange & 1) == 0 )
  726. {
  727. // largest integer number that fp16 can express exactly
  728. maxval = 2048.f;
  729. minval = -maxval;
  730. }
  731. else
  732. {
  733. // 0: positive number range
  734. // 1: negative number range
  735. if( (nextRange & 2) == 0 )
  736. {
  737. minval = 0; // 0x0000 +0
  738. maxval = 31744; // 0x7C00 +Inf
  739. }
  740. else
  741. {
  742. minval = -32768; // 0x8000 -0
  743. maxval = -1024; // 0xFC00 -Inf
  744. }
  745. }
  746. }
  747. double getMaxErr(int)
  748. {
  749. return 0.5f;
  750. }
  751. void generateScalars(int, RNG& rng)
  752. {
  753. nextRange = rng.next();
  754. }
  755. int nextRange;
  756. };
  757. struct ConvertScaleAbsOp : public BaseElemWiseOp
  758. {
  759. ConvertScaleAbsOp() : BaseElemWiseOp(1, FIX_BETA+REAL_GAMMA, 1, 1, Scalar::all(0)) {}
  760. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  761. {
  762. cv::convertScaleAbs(src[0], dst, alpha, gamma[0]);
  763. }
  764. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  765. {
  766. cvtest::add(src[0], alpha, Mat(), 0, Scalar::all(gamma[0]), dst, CV_8UC(src[0].channels()), true);
  767. }
  768. int getRandomType(RNG& rng)
  769. {
  770. return cvtest::randomType(rng, _OutputArray::DEPTH_MASK_ALL, 1,
  771. ninputs > 1 ? ARITHM_MAX_CHANNELS : 4);
  772. }
  773. double getMaxErr(int)
  774. {
  775. return 1;
  776. }
  777. void generateScalars(int depth, RNG& rng)
  778. {
  779. if( rng.uniform(0, 2) )
  780. BaseElemWiseOp::generateScalars(depth, rng);
  781. else
  782. {
  783. alpha = 1;
  784. gamma = Scalar::all(0);
  785. }
  786. }
  787. };
  788. namespace reference {
  789. // does not support inplace operation
  790. static void flip(const Mat& src, Mat& dst, int flipcode)
  791. {
  792. CV_Assert(src.dims == 2);
  793. dst.create(src.size(), src.type());
  794. int i, j, k, esz = (int)src.elemSize(), width = src.cols*esz;
  795. for( i = 0; i < dst.rows; i++ )
  796. {
  797. const uchar* sptr = src.ptr(flipcode == 1 ? i : dst.rows - i - 1);
  798. uchar* dptr = dst.ptr(i);
  799. if( flipcode == 0 )
  800. memcpy(dptr, sptr, width);
  801. else
  802. {
  803. for( j = 0; j < width; j += esz )
  804. for( k = 0; k < esz; k++ )
  805. dptr[j + k] = sptr[width - j - esz + k];
  806. }
  807. }
  808. }
  809. static void flip_inplace(Mat& dst, int flipcode)
  810. {
  811. Mat m;
  812. m.create(dst.size(), dst.type());
  813. reference::flip(dst, m, flipcode);
  814. memcpy(dst.ptr<uchar>(), m.ptr<uchar>(), dst.total() * dst.elemSize());
  815. }
  816. static void rotate(const Mat& src, Mat& dst, int rotateMode)
  817. {
  818. Mat tmp;
  819. switch (rotateMode)
  820. {
  821. case ROTATE_90_CLOCKWISE:
  822. cvtest::transpose(src, tmp);
  823. reference::flip(tmp, dst, 1);
  824. break;
  825. case ROTATE_180:
  826. reference::flip(src, dst, -1);
  827. break;
  828. case ROTATE_90_COUNTERCLOCKWISE:
  829. cvtest::transpose(src, tmp);
  830. reference::flip(tmp, dst, 0);
  831. break;
  832. default:
  833. break;
  834. }
  835. }
  836. static void setIdentity(Mat& dst, const Scalar& s)
  837. {
  838. CV_Assert( dst.dims == 2 && dst.channels() <= 4 );
  839. double buf[4];
  840. scalarToRawData(s, buf, dst.type(), 0);
  841. int i, k, esz = (int)dst.elemSize(), width = dst.cols*esz;
  842. for( i = 0; i < dst.rows; i++ )
  843. {
  844. uchar* dptr = dst.ptr(i);
  845. memset( dptr, 0, width );
  846. if( i < dst.cols )
  847. for( k = 0; k < esz; k++ )
  848. dptr[i*esz + k] = ((uchar*)buf)[k];
  849. }
  850. }
  851. } // namespace
  852. struct FlipOp : public BaseElemWiseOp
  853. {
  854. FlipOp() : BaseElemWiseOp(1, FIX_ALPHA+FIX_BETA+FIX_GAMMA, 1, 1, Scalar::all(0)) { flipcode = 0; }
  855. void getRandomSize(RNG& rng, vector<int>& size)
  856. {
  857. cvtest::randomSize(rng, 2, 2, ARITHM_MAX_SIZE_LOG, size);
  858. }
  859. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  860. {
  861. cv::flip(src[0], dst, flipcode);
  862. }
  863. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  864. {
  865. reference::flip(src[0], dst, flipcode);
  866. }
  867. void generateScalars(int, RNG& rng)
  868. {
  869. flipcode = rng.uniform(0, 3) - 1;
  870. }
  871. double getMaxErr(int)
  872. {
  873. return 0;
  874. }
  875. int flipcode;
  876. };
  877. struct FlipInplaceOp : public BaseElemWiseOp
  878. {
  879. FlipInplaceOp() : BaseElemWiseOp(1, FIX_ALPHA+FIX_BETA+FIX_GAMMA, 1, 1, Scalar::all(0)) { flipcode = 0; }
  880. void getRandomSize(RNG& rng, vector<int>& size)
  881. {
  882. cvtest::randomSize(rng, 2, 2, ARITHM_MAX_SIZE_LOG, size);
  883. }
  884. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  885. {
  886. dst.create(src[0].size(), src[0].type());
  887. memcpy(dst.ptr<uchar>(), src[0].ptr<uchar>(), src[0].total() * src[0].elemSize());
  888. cv::flip(dst, dst, flipcode);
  889. }
  890. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  891. {
  892. dst.create(src[0].size(), src[0].type());
  893. memcpy(dst.ptr<uchar>(), src[0].ptr<uchar>(), src[0].total() * src[0].elemSize());
  894. reference::flip_inplace(dst, flipcode);
  895. }
  896. void generateScalars(int, RNG& rng)
  897. {
  898. flipcode = rng.uniform(0, 3) - 1;
  899. }
  900. double getMaxErr(int)
  901. {
  902. return 0;
  903. }
  904. int flipcode;
  905. };
  906. struct RotateOp : public BaseElemWiseOp
  907. {
  908. RotateOp() : BaseElemWiseOp(1, FIX_ALPHA+FIX_BETA+FIX_GAMMA, 1, 1, Scalar::all(0)) { rotatecode = 0; }
  909. void getRandomSize(RNG& rng, vector<int>& size)
  910. {
  911. cvtest::randomSize(rng, 2, 2, ARITHM_MAX_SIZE_LOG, size);
  912. }
  913. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  914. {
  915. cv::rotate(src[0], dst, rotatecode);
  916. }
  917. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  918. {
  919. reference::rotate(src[0], dst, rotatecode);
  920. }
  921. void generateScalars(int, RNG& rng)
  922. {
  923. rotatecode = rng.uniform(0, 3);
  924. }
  925. double getMaxErr(int)
  926. {
  927. return 0;
  928. }
  929. int rotatecode;
  930. };
  931. struct TransposeOp : public BaseElemWiseOp
  932. {
  933. TransposeOp() : BaseElemWiseOp(1, FIX_ALPHA+FIX_BETA+FIX_GAMMA, 1, 1, Scalar::all(0)) {}
  934. void getRandomSize(RNG& rng, vector<int>& size)
  935. {
  936. cvtest::randomSize(rng, 2, 2, ARITHM_MAX_SIZE_LOG, size);
  937. }
  938. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  939. {
  940. cv::transpose(src[0], dst);
  941. }
  942. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  943. {
  944. cvtest::transpose(src[0], dst);
  945. }
  946. double getMaxErr(int)
  947. {
  948. return 0;
  949. }
  950. };
  951. struct SetIdentityOp : public BaseElemWiseOp
  952. {
  953. SetIdentityOp() : BaseElemWiseOp(0, FIX_ALPHA+FIX_BETA, 1, 1, Scalar::all(0)) {}
  954. void getRandomSize(RNG& rng, vector<int>& size)
  955. {
  956. cvtest::randomSize(rng, 2, 2, ARITHM_MAX_SIZE_LOG, size);
  957. }
  958. void op(const vector<Mat>&, Mat& dst, const Mat&)
  959. {
  960. cv::setIdentity(dst, gamma);
  961. }
  962. void refop(const vector<Mat>&, Mat& dst, const Mat&)
  963. {
  964. reference::setIdentity(dst, gamma);
  965. }
  966. double getMaxErr(int)
  967. {
  968. return 0;
  969. }
  970. };
  971. struct SetZeroOp : public BaseElemWiseOp
  972. {
  973. SetZeroOp() : BaseElemWiseOp(0, FIX_ALPHA+FIX_BETA+FIX_GAMMA, 1, 1, Scalar::all(0)) {}
  974. void op(const vector<Mat>&, Mat& dst, const Mat&)
  975. {
  976. dst = Scalar::all(0);
  977. }
  978. void refop(const vector<Mat>&, Mat& dst, const Mat&)
  979. {
  980. cvtest::set(dst, Scalar::all(0));
  981. }
  982. double getMaxErr(int)
  983. {
  984. return 0;
  985. }
  986. };
  987. namespace reference {
  988. static void exp(const Mat& src, Mat& dst)
  989. {
  990. dst.create( src.dims, &src.size[0], src.type() );
  991. const Mat *arrays[]={&src, &dst, 0};
  992. Mat planes[2];
  993. NAryMatIterator it(arrays, planes);
  994. size_t j, total = planes[0].total()*src.channels();
  995. size_t i, nplanes = it.nplanes;
  996. int depth = src.depth();
  997. for( i = 0; i < nplanes; i++, ++it )
  998. {
  999. const uchar* sptr = planes[0].ptr();
  1000. uchar* dptr = planes[1].ptr();
  1001. if( depth == CV_32F )
  1002. {
  1003. for( j = 0; j < total; j++ )
  1004. ((float*)dptr)[j] = std::exp(((const float*)sptr)[j]);
  1005. }
  1006. else if( depth == CV_64F )
  1007. {
  1008. for( j = 0; j < total; j++ )
  1009. ((double*)dptr)[j] = std::exp(((const double*)sptr)[j]);
  1010. }
  1011. }
  1012. }
  1013. static void log(const Mat& src, Mat& dst)
  1014. {
  1015. dst.create( src.dims, &src.size[0], src.type() );
  1016. const Mat *arrays[]={&src, &dst, 0};
  1017. Mat planes[2];
  1018. NAryMatIterator it(arrays, planes);
  1019. size_t j, total = planes[0].total()*src.channels();
  1020. size_t i, nplanes = it.nplanes;
  1021. int depth = src.depth();
  1022. for( i = 0; i < nplanes; i++, ++it )
  1023. {
  1024. const uchar* sptr = planes[0].ptr();
  1025. uchar* dptr = planes[1].ptr();
  1026. if( depth == CV_32F )
  1027. {
  1028. for( j = 0; j < total; j++ )
  1029. ((float*)dptr)[j] = (float)std::log(fabs(((const float*)sptr)[j]));
  1030. }
  1031. else if( depth == CV_64F )
  1032. {
  1033. for( j = 0; j < total; j++ )
  1034. ((double*)dptr)[j] = std::log(fabs(((const double*)sptr)[j]));
  1035. }
  1036. }
  1037. }
  1038. } // namespace
  1039. struct ExpOp : public BaseElemWiseOp
  1040. {
  1041. ExpOp() : BaseElemWiseOp(1, FIX_ALPHA+FIX_BETA+FIX_GAMMA, 1, 1, Scalar::all(0)) {}
  1042. int getRandomType(RNG& rng)
  1043. {
  1044. return cvtest::randomType(rng, _OutputArray::DEPTH_MASK_FLT, 1, ARITHM_MAX_CHANNELS);
  1045. }
  1046. void getValueRange(int depth, double& minval, double& maxval)
  1047. {
  1048. maxval = depth == CV_32F ? 80 : 700;
  1049. minval = -maxval;
  1050. }
  1051. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  1052. {
  1053. cv::exp(src[0], dst);
  1054. }
  1055. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  1056. {
  1057. reference::exp(src[0], dst);
  1058. }
  1059. double getMaxErr(int depth)
  1060. {
  1061. return depth == CV_32F ? 1e-5 : 1e-12;
  1062. }
  1063. };
  1064. struct LogOp : public BaseElemWiseOp
  1065. {
  1066. LogOp() : BaseElemWiseOp(1, FIX_ALPHA+FIX_BETA+FIX_GAMMA, 1, 1, Scalar::all(0)) {}
  1067. int getRandomType(RNG& rng)
  1068. {
  1069. return cvtest::randomType(rng, _OutputArray::DEPTH_MASK_FLT, 1, ARITHM_MAX_CHANNELS);
  1070. }
  1071. void getValueRange(int depth, double& minval, double& maxval)
  1072. {
  1073. maxval = depth == CV_32F ? 50 : 100;
  1074. minval = -maxval;
  1075. }
  1076. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  1077. {
  1078. Mat temp;
  1079. reference::exp(src[0], temp);
  1080. cv::log(temp, dst);
  1081. }
  1082. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  1083. {
  1084. Mat temp;
  1085. reference::exp(src[0], temp);
  1086. reference::log(temp, dst);
  1087. }
  1088. double getMaxErr(int depth)
  1089. {
  1090. return depth == CV_32F ? 1e-5 : 1e-12;
  1091. }
  1092. };
  1093. namespace reference {
  1094. static void cartToPolar(const Mat& mx, const Mat& my, Mat& mmag, Mat& mangle, bool angleInDegrees)
  1095. {
  1096. CV_Assert( (mx.type() == CV_32F || mx.type() == CV_64F) &&
  1097. mx.type() == my.type() && mx.size == my.size );
  1098. mmag.create( mx.dims, &mx.size[0], mx.type() );
  1099. mangle.create( mx.dims, &mx.size[0], mx.type() );
  1100. const Mat *arrays[]={&mx, &my, &mmag, &mangle, 0};
  1101. Mat planes[4];
  1102. NAryMatIterator it(arrays, planes);
  1103. size_t j, total = planes[0].total();
  1104. size_t i, nplanes = it.nplanes;
  1105. int depth = mx.depth();
  1106. double scale = angleInDegrees ? 180/CV_PI : 1;
  1107. for( i = 0; i < nplanes; i++, ++it )
  1108. {
  1109. if( depth == CV_32F )
  1110. {
  1111. const float* xptr = planes[0].ptr<float>();
  1112. const float* yptr = planes[1].ptr<float>();
  1113. float* mptr = planes[2].ptr<float>();
  1114. float* aptr = planes[3].ptr<float>();
  1115. for( j = 0; j < total; j++ )
  1116. {
  1117. mptr[j] = std::sqrt(xptr[j]*xptr[j] + yptr[j]*yptr[j]);
  1118. double a = atan2((double)yptr[j], (double)xptr[j]);
  1119. if( a < 0 ) a += CV_PI*2;
  1120. aptr[j] = (float)(a*scale);
  1121. }
  1122. }
  1123. else
  1124. {
  1125. const double* xptr = planes[0].ptr<double>();
  1126. const double* yptr = planes[1].ptr<double>();
  1127. double* mptr = planes[2].ptr<double>();
  1128. double* aptr = planes[3].ptr<double>();
  1129. for( j = 0; j < total; j++ )
  1130. {
  1131. mptr[j] = std::sqrt(xptr[j]*xptr[j] + yptr[j]*yptr[j]);
  1132. double a = atan2(yptr[j], xptr[j]);
  1133. if( a < 0 ) a += CV_PI*2;
  1134. aptr[j] = a*scale;
  1135. }
  1136. }
  1137. }
  1138. }
  1139. } // namespace
  1140. struct CartToPolarToCartOp : public BaseElemWiseOp
  1141. {
  1142. CartToPolarToCartOp() : BaseElemWiseOp(2, FIX_ALPHA+FIX_BETA+FIX_GAMMA, 1, 1, Scalar::all(0))
  1143. {
  1144. context = 3;
  1145. angleInDegrees = true;
  1146. }
  1147. int getRandomType(RNG& rng)
  1148. {
  1149. return cvtest::randomType(rng, _OutputArray::DEPTH_MASK_FLT, 1, 1);
  1150. }
  1151. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  1152. {
  1153. Mat mag, angle, x, y;
  1154. cv::cartToPolar(src[0], src[1], mag, angle, angleInDegrees);
  1155. cv::polarToCart(mag, angle, x, y, angleInDegrees);
  1156. Mat msrc[] = {mag, angle, x, y};
  1157. int pairs[] = {0, 0, 1, 1, 2, 2, 3, 3};
  1158. dst.create(src[0].dims, src[0].size, CV_MAKETYPE(src[0].depth(), 4));
  1159. cv::mixChannels(msrc, 4, &dst, 1, pairs, 4);
  1160. }
  1161. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  1162. {
  1163. Mat mag, angle;
  1164. reference::cartToPolar(src[0], src[1], mag, angle, angleInDegrees);
  1165. Mat msrc[] = {mag, angle, src[0], src[1]};
  1166. int pairs[] = {0, 0, 1, 1, 2, 2, 3, 3};
  1167. dst.create(src[0].dims, src[0].size, CV_MAKETYPE(src[0].depth(), 4));
  1168. cv::mixChannels(msrc, 4, &dst, 1, pairs, 4);
  1169. }
  1170. void generateScalars(int, RNG& rng)
  1171. {
  1172. angleInDegrees = rng.uniform(0, 2) != 0;
  1173. }
  1174. double getMaxErr(int)
  1175. {
  1176. return 1e-3;
  1177. }
  1178. bool angleInDegrees;
  1179. };
  1180. struct MeanOp : public BaseElemWiseOp
  1181. {
  1182. MeanOp() : BaseElemWiseOp(1, FIX_ALPHA+FIX_BETA+FIX_GAMMA+SUPPORT_MASK+SCALAR_OUTPUT, 1, 1, Scalar::all(0))
  1183. {
  1184. context = 3;
  1185. }
  1186. void op(const vector<Mat>& src, Mat& dst, const Mat& mask)
  1187. {
  1188. dst.create(1, 1, CV_64FC4);
  1189. dst.at<Scalar>(0,0) = cv::mean(src[0], mask);
  1190. }
  1191. void refop(const vector<Mat>& src, Mat& dst, const Mat& mask)
  1192. {
  1193. dst.create(1, 1, CV_64FC4);
  1194. dst.at<Scalar>(0,0) = cvtest::mean(src[0], mask);
  1195. }
  1196. double getMaxErr(int)
  1197. {
  1198. return 1e-5;
  1199. }
  1200. };
  1201. struct SumOp : public BaseElemWiseOp
  1202. {
  1203. SumOp() : BaseElemWiseOp(1, FIX_ALPHA+FIX_BETA+FIX_GAMMA+SCALAR_OUTPUT, 1, 1, Scalar::all(0))
  1204. {
  1205. context = 3;
  1206. }
  1207. void op(const vector<Mat>& src, Mat& dst, const Mat&)
  1208. {
  1209. dst.create(1, 1, CV_64FC4);
  1210. dst.at<Scalar>(0,0) = cv::sum(src[0]);
  1211. }
  1212. void refop(const vector<Mat>& src, Mat& dst, const Mat&)
  1213. {
  1214. dst.create(1, 1, CV_64FC4);
  1215. dst.at<Scalar>(0,0) = cvtest::mean(src[0])*(double)src[0].total();
  1216. }
  1217. double getMaxErr(int)
  1218. {
  1219. return 1e-5;
  1220. }
  1221. };
  1222. struct CountNonZeroOp : public BaseElemWiseOp
  1223. {
  1224. CountNonZeroOp() : BaseElemWiseOp(1, FIX_ALPHA+FIX_BETA+FIX_GAMMA+SCALAR_OUTPUT+SUPPORT_MASK, 1, 1, Scalar::all(0))
  1225. {}
  1226. int getRandomType(RNG& rng)
  1227. {
  1228. return cvtest::randomType(rng, _OutputArray::DEPTH_MASK_ALL, 1, 1);
  1229. }
  1230. void op(const vector<Mat>& src, Mat& dst, const Mat& mask)
  1231. {
  1232. Mat temp;
  1233. src[0].copyTo(temp);
  1234. if( !mask.empty() )
  1235. temp.setTo(Scalar::all(0), mask);
  1236. dst.create(1, 1, CV_32S);
  1237. dst.at<int>(0,0) = cv::countNonZero(temp);
  1238. }
  1239. void refop(const vector<Mat>& src, Mat& dst, const Mat& mask)
  1240. {
  1241. Mat temp;
  1242. cvtest::compare(src[0], 0, temp, CMP_NE);
  1243. if( !mask.empty() )
  1244. cvtest::set(temp, Scalar::all(0), mask);
  1245. dst.create(1, 1, CV_32S);
  1246. dst.at<int>(0,0) = saturate_cast<int>(cvtest::mean(temp)[0]/255*temp.total());
  1247. }
  1248. double getMaxErr(int)
  1249. {
  1250. return 0;
  1251. }
  1252. };
  1253. struct MeanStdDevOp : public BaseElemWiseOp
  1254. {
  1255. Scalar sqmeanRef;
  1256. int cn;
  1257. MeanStdDevOp() : BaseElemWiseOp(1, FIX_ALPHA+FIX_BETA+FIX_GAMMA+SUPPORT_MASK+SCALAR_OUTPUT, 1, 1, Scalar::all(0))
  1258. {
  1259. cn = 0;
  1260. context = 7;
  1261. }
  1262. void op(const vector<Mat>& src, Mat& dst, const Mat& mask)
  1263. {
  1264. dst.create(1, 2, CV_64FC4);
  1265. cv::meanStdDev(src[0], dst.at<Scalar>(0,0), dst.at<Scalar>(0,1), mask);
  1266. }
  1267. void refop(const vector<Mat>& src, Mat& dst, const Mat& mask)
  1268. {
  1269. Mat temp;
  1270. cvtest::convert(src[0], temp, CV_64F);
  1271. cvtest::multiply(temp, temp, temp);
  1272. Scalar mean = cvtest::mean(src[0], mask);
  1273. Scalar sqmean = cvtest::mean(temp, mask);
  1274. sqmeanRef = sqmean;
  1275. cn = temp.channels();
  1276. for( int c = 0; c < 4; c++ )
  1277. sqmean[c] = std::sqrt(std::max(sqmean[c] - mean[c]*mean[c], 0.));
  1278. dst.create(1, 2, CV_64FC4);
  1279. dst.at<Scalar>(0,0) = mean;
  1280. dst.at<Scalar>(0,1) = sqmean;
  1281. }
  1282. double getMaxErr(int)
  1283. {
  1284. CV_Assert(cn > 0);
  1285. double err = sqmeanRef[0];
  1286. for(int i = 1; i < cn; ++i)
  1287. err = std::max(err, sqmeanRef[i]);
  1288. return 3e-7 * err;
  1289. }
  1290. };
  1291. struct NormOp : public BaseElemWiseOp
  1292. {
  1293. NormOp() : BaseElemWiseOp(2, FIX_ALPHA+FIX_BETA+FIX_GAMMA+SUPPORT_MASK+SCALAR_OUTPUT, 1, 1, Scalar::all(0))
  1294. {
  1295. context = 1;
  1296. normType = 0;
  1297. }
  1298. int getRandomType(RNG& rng)
  1299. {
  1300. int type = cvtest::randomType(rng, _OutputArray::DEPTH_MASK_ALL_BUT_8S, 1, 4);
  1301. for(;;)
  1302. {
  1303. normType = rng.uniform(1, 8);
  1304. if( normType == NORM_INF || normType == NORM_L1 ||
  1305. normType == NORM_L2 || normType == NORM_L2SQR ||
  1306. normType == NORM_HAMMING || normType == NORM_HAMMING2 )
  1307. break;
  1308. }
  1309. if( normType == NORM_HAMMING || normType == NORM_HAMMING2 )
  1310. {
  1311. type = CV_8U;
  1312. }
  1313. return type;
  1314. }
  1315. void op(const vector<Mat>& src, Mat& dst, const Mat& mask)
  1316. {
  1317. dst.create(1, 2, CV_64FC1);
  1318. dst.at<double>(0,0) = cv::norm(src[0], normType, mask);
  1319. dst.at<double>(0,1) = cv::norm(src[0], src[1], normType, mask);
  1320. }
  1321. void refop(const vector<Mat>& src, Mat& dst, const Mat& mask)
  1322. {
  1323. dst.create(1, 2, CV_64FC1);
  1324. dst.at<double>(0,0) = cvtest::norm(src[0], normType, mask);
  1325. dst.at<double>(0,1) = cvtest::norm(src[0], src[1], normType, mask);
  1326. }
  1327. void generateScalars(int, RNG& /*rng*/)
  1328. {
  1329. }
  1330. double getMaxErr(int)
  1331. {
  1332. return 1e-6;
  1333. }
  1334. int normType;
  1335. };
  1336. struct MinMaxLocOp : public BaseElemWiseOp
  1337. {
  1338. MinMaxLocOp() : BaseElemWiseOp(1, FIX_ALPHA+FIX_BETA+FIX_GAMMA+SUPPORT_MASK+SCALAR_OUTPUT, 1, 1, Scalar::all(0))
  1339. {
  1340. context = ARITHM_MAX_NDIMS*2 + 2;
  1341. }
  1342. int getRandomType(RNG& rng)
  1343. {
  1344. return cvtest::randomType(rng, _OutputArray::DEPTH_MASK_ALL_BUT_8S, 1, 1);
  1345. }
  1346. void saveOutput(const vector<int>& minidx, const vector<int>& maxidx,
  1347. double minval, double maxval, Mat& dst)
  1348. {
  1349. int i, ndims = (int)minidx.size();
  1350. dst.create(1, ndims*2 + 2, CV_64FC1);
  1351. for( i = 0; i < ndims; i++ )
  1352. {
  1353. dst.at<double>(0,i) = minidx[i];
  1354. dst.at<double>(0,i+ndims) = maxidx[i];
  1355. }
  1356. dst.at<double>(0,ndims*2) = minval;
  1357. dst.at<double>(0,ndims*2+1) = maxval;
  1358. }
  1359. void op(const vector<Mat>& src, Mat& dst, const Mat& mask)
  1360. {
  1361. int ndims = src[0].dims;
  1362. vector<int> minidx(ndims), maxidx(ndims);
  1363. double minval=0, maxval=0;
  1364. cv::minMaxIdx(src[0], &minval, &maxval, &minidx[0], &maxidx[0], mask);
  1365. saveOutput(minidx, maxidx, minval, maxval, dst);
  1366. }
  1367. void refop(const vector<Mat>& src, Mat& dst, const Mat& mask)
  1368. {
  1369. int ndims=src[0].dims;
  1370. vector<int> minidx(ndims), maxidx(ndims);
  1371. double minval=0, maxval=0;
  1372. cvtest::minMaxLoc(src[0], &minval, &maxval, &minidx, &maxidx, mask);
  1373. saveOutput(minidx, maxidx, minval, maxval, dst);
  1374. }
  1375. double getMaxErr(int)
  1376. {
  1377. return 0;
  1378. }
  1379. };
  1380. struct reduceArgMinMaxOp : public BaseElemWiseOp
  1381. {
  1382. reduceArgMinMaxOp() : BaseElemWiseOp(1, FIX_ALPHA+FIX_BETA+FIX_GAMMA, 1, 1, Scalar::all(0)),
  1383. isLast(false), isMax(false), axis(0)
  1384. {
  1385. context = ARITHM_MAX_NDIMS*2 + 2;
  1386. }
  1387. int getRandomType(RNG& rng) override
  1388. {
  1389. return cvtest::randomType(rng, _OutputArray::DEPTH_MASK_ALL_BUT_8S, 1, 1);
  1390. }
  1391. void getRandomSize(RNG& rng, vector<int>& size) override
  1392. {
  1393. cvtest::randomSize(rng, 2, ARITHM_MAX_NDIMS, 6, size);
  1394. }
  1395. void generateScalars(int depth, RNG& rng) override
  1396. {
  1397. BaseElemWiseOp::generateScalars(depth, rng);
  1398. isLast = (randInt(rng) % 2 == 0);
  1399. isMax = (randInt(rng) % 2 == 0);
  1400. axis = randInt(rng);
  1401. }
  1402. int getAxis(const Mat& src) const
  1403. {
  1404. int dims = src.dims;
  1405. return static_cast<int>(axis % (2 * dims)) - dims; // [-dims; dims - 1]
  1406. }
  1407. void op(const vector<Mat>& src, Mat& dst, const Mat&) override
  1408. {
  1409. const Mat& inp = src[0];
  1410. const int axis_ = getAxis(inp);
  1411. if (isMax)
  1412. {
  1413. cv::reduceArgMax(inp, dst, axis_, isLast);
  1414. }
  1415. else
  1416. {
  1417. cv::reduceArgMin(inp, dst, axis_, isLast);
  1418. }
  1419. }
  1420. void refop(const vector<Mat>& src, Mat& dst, const Mat&) override
  1421. {
  1422. const Mat& inp = src[0];
  1423. const int axis_ = getAxis(inp);
  1424. if (!isLast && !isMax)
  1425. {
  1426. cvtest::MinMaxReducer<std::less>::reduce(inp, dst, axis_);
  1427. }
  1428. else if (!isLast && isMax)
  1429. {
  1430. cvtest::MinMaxReducer<std::greater>::reduce(inp, dst, axis_);
  1431. }
  1432. else if (isLast && !isMax)
  1433. {
  1434. cvtest::MinMaxReducer<std::less_equal>::reduce(inp, dst, axis_);
  1435. }
  1436. else
  1437. {
  1438. cvtest::MinMaxReducer<std::greater_equal>::reduce(inp, dst, axis_);
  1439. }
  1440. }
  1441. bool isLast;
  1442. bool isMax;
  1443. uint32_t axis;
  1444. };
  1445. typedef Ptr<BaseElemWiseOp> ElemWiseOpPtr;
  1446. class ElemWiseTest : public ::testing::TestWithParam<ElemWiseOpPtr> {};
  1447. TEST_P(ElemWiseTest, accuracy)
  1448. {
  1449. ElemWiseOpPtr op = GetParam();
  1450. int testIdx = 0;
  1451. RNG rng((uint64)ARITHM_RNG_SEED);
  1452. for( testIdx = 0; testIdx < ARITHM_NTESTS; testIdx++ )
  1453. {
  1454. vector<int> size;
  1455. op->getRandomSize(rng, size);
  1456. int type = op->getRandomType(rng);
  1457. int depth = CV_MAT_DEPTH(type);
  1458. bool haveMask = ((op->flags & BaseElemWiseOp::SUPPORT_MASK) != 0
  1459. || (op->flags & BaseElemWiseOp::SUPPORT_MULTICHANNELMASK) != 0) && rng.uniform(0, 4) == 0;
  1460. double minval=0, maxval=0;
  1461. op->getValueRange(depth, minval, maxval);
  1462. int i, ninputs = op->ninputs;
  1463. vector<Mat> src(ninputs);
  1464. for( i = 0; i < ninputs; i++ )
  1465. src[i] = cvtest::randomMat(rng, size, type, minval, maxval, true);
  1466. Mat dst0, dst, mask;
  1467. if( haveMask ) {
  1468. bool multiChannelMask = (op->flags & BaseElemWiseOp::SUPPORT_MULTICHANNELMASK) != 0
  1469. && rng.uniform(0, 2) == 0;
  1470. int masktype = CV_8UC(multiChannelMask ? CV_MAT_CN(type) : 1);
  1471. mask = cvtest::randomMat(rng, size, masktype, 0, 2, true);
  1472. }
  1473. if( (haveMask || ninputs == 0) && !(op->flags & BaseElemWiseOp::SCALAR_OUTPUT))
  1474. {
  1475. dst0 = cvtest::randomMat(rng, size, type, minval, maxval, false);
  1476. dst = cvtest::randomMat(rng, size, type, minval, maxval, true);
  1477. cvtest::copy(dst, dst0);
  1478. }
  1479. op->generateScalars(depth, rng);
  1480. op->refop(src, dst0, mask);
  1481. op->op(src, dst, mask);
  1482. double maxErr = op->getMaxErr(depth);
  1483. ASSERT_PRED_FORMAT2(cvtest::MatComparator(maxErr, op->context), dst0, dst) << "\nsrc[0] ~ " <<
  1484. cvtest::MatInfo(!src.empty() ? src[0] : Mat()) << "\ntestCase #" << testIdx << "\n";
  1485. }
  1486. }
  1487. INSTANTIATE_TEST_CASE_P(Core_Copy, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new CopyOp)));
  1488. INSTANTIATE_TEST_CASE_P(Core_Set, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new SetOp)));
  1489. INSTANTIATE_TEST_CASE_P(Core_SetZero, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new SetZeroOp)));
  1490. INSTANTIATE_TEST_CASE_P(Core_ConvertScale, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new ConvertScaleOp)));
  1491. INSTANTIATE_TEST_CASE_P(Core_ConvertScaleFp16, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new ConvertScaleFp16Op)));
  1492. INSTANTIATE_TEST_CASE_P(Core_ConvertScaleAbs, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new ConvertScaleAbsOp)));
  1493. INSTANTIATE_TEST_CASE_P(Core_Add, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new AddOp)));
  1494. INSTANTIATE_TEST_CASE_P(Core_Sub, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new SubOp)));
  1495. INSTANTIATE_TEST_CASE_P(Core_AddS, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new AddSOp)));
  1496. INSTANTIATE_TEST_CASE_P(Core_SubRS, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new SubRSOp)));
  1497. INSTANTIATE_TEST_CASE_P(Core_ScaleAdd, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new ScaleAddOp)));
  1498. INSTANTIATE_TEST_CASE_P(Core_AddWeighted, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new AddWeightedOp)));
  1499. INSTANTIATE_TEST_CASE_P(Core_AbsDiff, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new AbsDiffOp)));
  1500. INSTANTIATE_TEST_CASE_P(Core_AbsDiffS, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new AbsDiffSOp)));
  1501. INSTANTIATE_TEST_CASE_P(Core_And, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new LogicOp('&'))));
  1502. INSTANTIATE_TEST_CASE_P(Core_AndS, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new LogicSOp('&'))));
  1503. INSTANTIATE_TEST_CASE_P(Core_Or, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new LogicOp('|'))));
  1504. INSTANTIATE_TEST_CASE_P(Core_OrS, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new LogicSOp('|'))));
  1505. INSTANTIATE_TEST_CASE_P(Core_Xor, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new LogicOp('^'))));
  1506. INSTANTIATE_TEST_CASE_P(Core_XorS, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new LogicSOp('^'))));
  1507. INSTANTIATE_TEST_CASE_P(Core_Not, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new LogicSOp('~'))));
  1508. INSTANTIATE_TEST_CASE_P(Core_Max, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new MaxOp)));
  1509. INSTANTIATE_TEST_CASE_P(Core_MaxS, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new MaxSOp)));
  1510. INSTANTIATE_TEST_CASE_P(Core_Min, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new MinOp)));
  1511. INSTANTIATE_TEST_CASE_P(Core_MinS, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new MinSOp)));
  1512. INSTANTIATE_TEST_CASE_P(Core_Mul, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new MulOp)));
  1513. INSTANTIATE_TEST_CASE_P(Core_Div, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new DivOp)));
  1514. INSTANTIATE_TEST_CASE_P(Core_Recip, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new RecipOp)));
  1515. INSTANTIATE_TEST_CASE_P(Core_Cmp, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new CmpOp)));
  1516. INSTANTIATE_TEST_CASE_P(Core_CmpS, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new CmpSOp)));
  1517. INSTANTIATE_TEST_CASE_P(Core_InRangeS, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new InRangeSOp)));
  1518. INSTANTIATE_TEST_CASE_P(Core_InRange, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new InRangeOp)));
  1519. INSTANTIATE_TEST_CASE_P(Core_Flip, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new FlipOp)));
  1520. INSTANTIATE_TEST_CASE_P(Core_FlipInplace, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new FlipInplaceOp)));
  1521. INSTANTIATE_TEST_CASE_P(Core_Rotate, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new RotateOp)));
  1522. INSTANTIATE_TEST_CASE_P(Core_Transpose, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new TransposeOp)));
  1523. INSTANTIATE_TEST_CASE_P(Core_SetIdentity, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new SetIdentityOp)));
  1524. INSTANTIATE_TEST_CASE_P(Core_Exp, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new ExpOp)));
  1525. INSTANTIATE_TEST_CASE_P(Core_Log, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new LogOp)));
  1526. INSTANTIATE_TEST_CASE_P(Core_CountNonZero, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new CountNonZeroOp)));
  1527. INSTANTIATE_TEST_CASE_P(Core_Mean, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new MeanOp)));
  1528. INSTANTIATE_TEST_CASE_P(Core_MeanStdDev, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new MeanStdDevOp)));
  1529. INSTANTIATE_TEST_CASE_P(Core_Sum, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new SumOp)));
  1530. INSTANTIATE_TEST_CASE_P(Core_Norm, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new NormOp)));
  1531. INSTANTIATE_TEST_CASE_P(Core_MinMaxLoc, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new MinMaxLocOp)));
  1532. INSTANTIATE_TEST_CASE_P(Core_reduceArgMinMax, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new reduceArgMinMaxOp)));
  1533. INSTANTIATE_TEST_CASE_P(Core_CartToPolarToCart, ElemWiseTest, ::testing::Values(ElemWiseOpPtr(new CartToPolarToCartOp)));
  1534. // Mixed Type Arithmetic Operations
  1535. typedef std::tuple<ElemWiseOpPtr, std::tuple<cvtest::MatDepth, cvtest::MatDepth>, int> SomeType;
  1536. class ArithmMixedTest : public ::testing::TestWithParam<SomeType> {};
  1537. TEST_P(ArithmMixedTest, accuracy)
  1538. {
  1539. auto p = GetParam();
  1540. ElemWiseOpPtr op = std::get<0>(p);
  1541. int srcDepth = std::get<0>(std::get<1>(p));
  1542. int dstDepth = std::get<1>(std::get<1>(p));
  1543. int channels = std::get<2>(p);
  1544. int srcType = CV_MAKETYPE(srcDepth, channels);
  1545. int dstType = CV_MAKETYPE(dstDepth, channels);
  1546. op->flags |= BaseElemWiseOp::MIXED_TYPE;
  1547. int testIdx = 0;
  1548. RNG rng((uint64)ARITHM_RNG_SEED);
  1549. for( testIdx = 0; testIdx < ARITHM_NTESTS; testIdx++ )
  1550. {
  1551. vector<int> size;
  1552. op->getRandomSize(rng, size);
  1553. bool haveMask = ((op->flags & BaseElemWiseOp::SUPPORT_MASK) != 0) && rng.uniform(0, 4) == 0;
  1554. double minval=0, maxval=0;
  1555. op->getValueRange(srcDepth, minval, maxval);
  1556. int ninputs = op->ninputs;
  1557. vector<Mat> src(ninputs);
  1558. for(int i = 0; i < ninputs; i++ )
  1559. src[i] = cvtest::randomMat(rng, size, srcType, minval, maxval, true);
  1560. Mat dst0, dst, mask;
  1561. if( haveMask )
  1562. {
  1563. mask = cvtest::randomMat(rng, size, CV_8UC1, 0, 2, true);
  1564. }
  1565. dst0 = cvtest::randomMat(rng, size, dstType, minval, maxval, false);
  1566. dst = cvtest::randomMat(rng, size, dstType, minval, maxval, true);
  1567. cvtest::copy(dst, dst0);
  1568. op->generateScalars(dstDepth, rng);
  1569. op->refop(src, dst0, mask);
  1570. op->op(src, dst, mask);
  1571. double maxErr = op->getMaxErr(dstDepth);
  1572. ASSERT_PRED_FORMAT2(cvtest::MatComparator(maxErr, op->context), dst0, dst) << "\nsrc[0] ~ " <<
  1573. cvtest::MatInfo(!src.empty() ? src[0] : Mat()) << "\ntestCase #" << testIdx << "\n";
  1574. }
  1575. }
  1576. INSTANTIATE_TEST_CASE_P(Core_AddMixed, ArithmMixedTest,
  1577. ::testing::Combine(::testing::Values(ElemWiseOpPtr(new AddOp)),
  1578. ::testing::Values(std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8U, CV_16U},
  1579. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8S, CV_16S},
  1580. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8U, CV_32F},
  1581. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8S, CV_32F}),
  1582. ::testing::Values(1, 3, 4)));
  1583. INSTANTIATE_TEST_CASE_P(Core_AddScalarMixed, ArithmMixedTest,
  1584. ::testing::Combine(::testing::Values(ElemWiseOpPtr(new AddSOp)),
  1585. ::testing::Values(std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8U, CV_16U},
  1586. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8S, CV_16S},
  1587. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8U, CV_32F},
  1588. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8S, CV_32F}),
  1589. ::testing::Values(1, 3, 4)));
  1590. INSTANTIATE_TEST_CASE_P(Core_AddWeightedMixed, ArithmMixedTest,
  1591. ::testing::Combine(::testing::Values(ElemWiseOpPtr(new AddWeightedOp)),
  1592. ::testing::Values(std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8U, CV_16U},
  1593. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8S, CV_16S},
  1594. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8U, CV_32F},
  1595. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8S, CV_32F}),
  1596. ::testing::Values(1, 3, 4)));
  1597. INSTANTIATE_TEST_CASE_P(Core_SubMixed, ArithmMixedTest,
  1598. ::testing::Combine(::testing::Values(ElemWiseOpPtr(new SubOp)),
  1599. ::testing::Values(std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8U, CV_16U},
  1600. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8S, CV_16S},
  1601. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8U, CV_32F},
  1602. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8S, CV_32F}),
  1603. ::testing::Values(1, 3, 4)));
  1604. INSTANTIATE_TEST_CASE_P(Core_SubScalarMinusArgMixed, ArithmMixedTest,
  1605. ::testing::Combine(::testing::Values(ElemWiseOpPtr(new SubRSOp)),
  1606. ::testing::Values(std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8U, CV_16U},
  1607. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8S, CV_16S},
  1608. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8U, CV_32F},
  1609. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8S, CV_32F}),
  1610. ::testing::Values(1, 3, 4)));
  1611. INSTANTIATE_TEST_CASE_P(Core_MulMixed, ArithmMixedTest,
  1612. ::testing::Combine(::testing::Values(ElemWiseOpPtr(new MulOp)),
  1613. ::testing::Values(std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8U, CV_16U},
  1614. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8S, CV_16S},
  1615. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8U, CV_32F},
  1616. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8S, CV_32F}),
  1617. ::testing::Values(1, 3, 4)));
  1618. INSTANTIATE_TEST_CASE_P(Core_MulScalarMixed, ArithmMixedTest,
  1619. ::testing::Combine(::testing::Values(ElemWiseOpPtr(new MulSOp)),
  1620. ::testing::Values(std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8U, CV_16U},
  1621. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8S, CV_16S},
  1622. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8U, CV_32F},
  1623. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8S, CV_32F}),
  1624. ::testing::Values(1, 3, 4)));
  1625. INSTANTIATE_TEST_CASE_P(Core_DivMixed, ArithmMixedTest,
  1626. ::testing::Combine(::testing::Values(ElemWiseOpPtr(new DivOp)),
  1627. ::testing::Values(std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8U, CV_16U},
  1628. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8S, CV_16S},
  1629. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8U, CV_32F},
  1630. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8S, CV_32F}),
  1631. ::testing::Values(1, 3, 4)));
  1632. INSTANTIATE_TEST_CASE_P(Core_RecipMixed, ArithmMixedTest,
  1633. ::testing::Combine(::testing::Values(ElemWiseOpPtr(new RecipOp)),
  1634. ::testing::Values(std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8U, CV_16U},
  1635. std::tuple<cvtest::MatDepth, cvtest::MatDepth>{CV_8S, CV_32F}),
  1636. ::testing::Values(1, 3, 4)));
  1637. TEST(Core_ArithmMask, uninitialized)
  1638. {
  1639. RNG& rng = theRNG();
  1640. const int MAX_DIM=3;
  1641. int sizes[MAX_DIM];
  1642. for( int iter = 0; iter < 100; iter++ )
  1643. {
  1644. int dims = rng.uniform(1, MAX_DIM+1);
  1645. int depth = rng.uniform(CV_8U, CV_64F+1);
  1646. int cn = rng.uniform(1, 6);
  1647. int type = CV_MAKETYPE(depth, cn);
  1648. int op = rng.uniform(0, depth < CV_32F ? 5 : 2); // don't run binary operations between floating-point values
  1649. int depth1 = op <= 1 ? CV_64F : depth;
  1650. for (int k = 0; k < MAX_DIM; k++)
  1651. {
  1652. sizes[k] = k < dims ? rng.uniform(1, 30) : 0;
  1653. }
  1654. SCOPED_TRACE(cv::format("iter=%d dims=%d depth=%d cn=%d type=%d op=%d depth1=%d dims=[%d; %d; %d]",
  1655. iter, dims, depth, cn, type, op, depth1, sizes[0], sizes[1], sizes[2]));
  1656. Mat a(dims, sizes, type), a1;
  1657. Mat b(dims, sizes, type), b1;
  1658. Mat mask(dims, sizes, CV_8U);
  1659. Mat mask1;
  1660. Mat c, d;
  1661. rng.fill(a, RNG::UNIFORM, 0, 100);
  1662. rng.fill(b, RNG::UNIFORM, 0, 100);
  1663. // [-2,2) range means that the each generated random number
  1664. // will be one of -2, -1, 0, 1. Saturated to [0,255], it will become
  1665. // 0, 0, 0, 1 => the mask will be filled by ~25%.
  1666. rng.fill(mask, RNG::UNIFORM, -2, 2);
  1667. a.convertTo(a1, depth1);
  1668. b.convertTo(b1, depth1);
  1669. // invert the mask
  1670. cv::compare(mask, 0, mask1, CMP_EQ);
  1671. a1.setTo(0, mask1);
  1672. b1.setTo(0, mask1);
  1673. if( op == 0 )
  1674. {
  1675. cv::add(a, b, c, mask);
  1676. cv::add(a1, b1, d);
  1677. }
  1678. else if( op == 1 )
  1679. {
  1680. cv::subtract(a, b, c, mask);
  1681. cv::subtract(a1, b1, d);
  1682. }
  1683. else if( op == 2 )
  1684. {
  1685. cv::bitwise_and(a, b, c, mask);
  1686. cv::bitwise_and(a1, b1, d);
  1687. }
  1688. else if( op == 3 )
  1689. {
  1690. cv::bitwise_or(a, b, c, mask);
  1691. cv::bitwise_or(a1, b1, d);
  1692. }
  1693. else if( op == 4 )
  1694. {
  1695. cv::bitwise_xor(a, b, c, mask);
  1696. cv::bitwise_xor(a1, b1, d);
  1697. }
  1698. Mat d1;
  1699. d.convertTo(d1, depth);
  1700. EXPECT_LE(cvtest::norm(c, d1, CV_C), DBL_EPSILON);
  1701. }
  1702. Mat_<uchar> tmpSrc(100,100);
  1703. tmpSrc = 124;
  1704. Mat_<uchar> tmpMask(100,100);
  1705. tmpMask = 255;
  1706. Mat_<uchar> tmpDst(100,100);
  1707. tmpDst = 2;
  1708. tmpSrc.copyTo(tmpDst,tmpMask);
  1709. }
  1710. TEST(Multiply, FloatingPointRounding)
  1711. {
  1712. cv::Mat src(1, 1, CV_8UC1, cv::Scalar::all(110)), dst;
  1713. cv::Scalar s(147.286359696927, 1, 1 ,1);
  1714. cv::multiply(src, s, dst, 1, CV_16U);
  1715. // with CV_32F this produce result 16202
  1716. ASSERT_EQ(dst.at<ushort>(0,0), 16201);
  1717. }
  1718. TEST(Core_Add, AddToColumnWhen3Rows)
  1719. {
  1720. cv::Mat m1 = (cv::Mat_<double>(3, 2) << 1, 2, 3, 4, 5, 6);
  1721. m1.col(1) += 10;
  1722. cv::Mat m2 = (cv::Mat_<double>(3, 2) << 1, 12, 3, 14, 5, 16);
  1723. ASSERT_EQ(0, countNonZero(m1 - m2));
  1724. }
  1725. TEST(Core_Add, AddToColumnWhen4Rows)
  1726. {
  1727. cv::Mat m1 = (cv::Mat_<double>(4, 2) << 1, 2, 3, 4, 5, 6, 7, 8);
  1728. m1.col(1) += 10;
  1729. cv::Mat m2 = (cv::Mat_<double>(4, 2) << 1, 12, 3, 14, 5, 16, 7, 18);
  1730. ASSERT_EQ(0, countNonZero(m1 - m2));
  1731. }
  1732. TEST(Core_round, CvRound)
  1733. {
  1734. ASSERT_EQ(2, cvRound(2.0));
  1735. ASSERT_EQ(2, cvRound(2.1));
  1736. ASSERT_EQ(-2, cvRound(-2.1));
  1737. ASSERT_EQ(3, cvRound(2.8));
  1738. ASSERT_EQ(-3, cvRound(-2.8));
  1739. ASSERT_EQ(2, cvRound(2.5));
  1740. ASSERT_EQ(4, cvRound(3.5));
  1741. ASSERT_EQ(-2, cvRound(-2.5));
  1742. ASSERT_EQ(-4, cvRound(-3.5));
  1743. }
  1744. typedef testing::TestWithParam<Size> Mul1;
  1745. TEST_P(Mul1, One)
  1746. {
  1747. Size size = GetParam();
  1748. cv::Mat src(size, CV_32FC1, cv::Scalar::all(2)), dst,
  1749. ref_dst(size, CV_32FC1, cv::Scalar::all(6));
  1750. cv::multiply(3, src, dst);
  1751. ASSERT_EQ(0, cvtest::norm(dst, ref_dst, cv::NORM_INF));
  1752. }
  1753. INSTANTIATE_TEST_CASE_P(Arithm, Mul1, testing::Values(Size(2, 2), Size(1, 1)));
  1754. class SubtractOutputMatNotEmpty : public testing::TestWithParam< tuple<cv::Size, perf::MatType, perf::MatDepth, bool> >
  1755. {
  1756. public:
  1757. cv::Size size;
  1758. int src_type;
  1759. int dst_depth;
  1760. bool fixed;
  1761. void SetUp()
  1762. {
  1763. size = get<0>(GetParam());
  1764. src_type = get<1>(GetParam());
  1765. dst_depth = get<2>(GetParam());
  1766. fixed = get<3>(GetParam());
  1767. }
  1768. };
  1769. TEST_P(SubtractOutputMatNotEmpty, Mat_Mat)
  1770. {
  1771. cv::Mat src1(size, src_type, cv::Scalar::all(16));
  1772. cv::Mat src2(size, src_type, cv::Scalar::all(16));
  1773. cv::Mat dst;
  1774. if (!fixed)
  1775. {
  1776. cv::subtract(src1, src2, dst, cv::noArray(), dst_depth);
  1777. }
  1778. else
  1779. {
  1780. const cv::Mat fixed_dst(size, CV_MAKE_TYPE((dst_depth > 0 ? dst_depth : CV_16S), src1.channels()));
  1781. cv::subtract(src1, src2, fixed_dst, cv::noArray(), dst_depth);
  1782. dst = fixed_dst;
  1783. dst_depth = fixed_dst.depth();
  1784. }
  1785. ASSERT_FALSE(dst.empty());
  1786. ASSERT_EQ(src1.size(), dst.size());
  1787. ASSERT_EQ(dst_depth > 0 ? dst_depth : src1.depth(), dst.depth());
  1788. ASSERT_EQ(0, cv::countNonZero(dst.reshape(1)));
  1789. }
  1790. TEST_P(SubtractOutputMatNotEmpty, Mat_Mat_WithMask)
  1791. {
  1792. cv::Mat src1(size, src_type, cv::Scalar::all(16));
  1793. cv::Mat src2(size, src_type, cv::Scalar::all(16));
  1794. cv::Mat mask(size, CV_8UC1, cv::Scalar::all(255));
  1795. cv::Mat dst;
  1796. if (!fixed)
  1797. {
  1798. cv::subtract(src1, src2, dst, mask, dst_depth);
  1799. }
  1800. else
  1801. {
  1802. const cv::Mat fixed_dst(size, CV_MAKE_TYPE((dst_depth > 0 ? dst_depth : CV_16S), src1.channels()));
  1803. cv::subtract(src1, src2, fixed_dst, mask, dst_depth);
  1804. dst = fixed_dst;
  1805. dst_depth = fixed_dst.depth();
  1806. }
  1807. ASSERT_FALSE(dst.empty());
  1808. ASSERT_EQ(src1.size(), dst.size());
  1809. ASSERT_EQ(dst_depth > 0 ? dst_depth : src1.depth(), dst.depth());
  1810. ASSERT_EQ(0, cv::countNonZero(dst.reshape(1)));
  1811. }
  1812. TEST_P(SubtractOutputMatNotEmpty, Mat_Mat_Expr)
  1813. {
  1814. cv::Mat src1(size, src_type, cv::Scalar::all(16));
  1815. cv::Mat src2(size, src_type, cv::Scalar::all(16));
  1816. cv::Mat dst = src1 - src2;
  1817. ASSERT_FALSE(dst.empty());
  1818. ASSERT_EQ(src1.size(), dst.size());
  1819. ASSERT_EQ(src1.depth(), dst.depth());
  1820. ASSERT_EQ(0, cv::countNonZero(dst.reshape(1)));
  1821. }
  1822. TEST_P(SubtractOutputMatNotEmpty, Mat_Scalar)
  1823. {
  1824. cv::Mat src(size, src_type, cv::Scalar::all(16));
  1825. cv::Mat dst;
  1826. if (!fixed)
  1827. {
  1828. cv::subtract(src, cv::Scalar::all(16), dst, cv::noArray(), dst_depth);
  1829. }
  1830. else
  1831. {
  1832. const cv::Mat fixed_dst(size, CV_MAKE_TYPE((dst_depth > 0 ? dst_depth : CV_16S), src.channels()));
  1833. cv::subtract(src, cv::Scalar::all(16), fixed_dst, cv::noArray(), dst_depth);
  1834. dst = fixed_dst;
  1835. dst_depth = fixed_dst.depth();
  1836. }
  1837. ASSERT_FALSE(dst.empty());
  1838. ASSERT_EQ(src.size(), dst.size());
  1839. ASSERT_EQ(dst_depth > 0 ? dst_depth : src.depth(), dst.depth());
  1840. ASSERT_EQ(0, cv::countNonZero(dst.reshape(1)));
  1841. }
  1842. TEST_P(SubtractOutputMatNotEmpty, Mat_Scalar_WithMask)
  1843. {
  1844. cv::Mat src(size, src_type, cv::Scalar::all(16));
  1845. cv::Mat mask(size, CV_8UC1, cv::Scalar::all(255));
  1846. cv::Mat dst;
  1847. if (!fixed)
  1848. {
  1849. cv::subtract(src, cv::Scalar::all(16), dst, mask, dst_depth);
  1850. }
  1851. else
  1852. {
  1853. const cv::Mat fixed_dst(size, CV_MAKE_TYPE((dst_depth > 0 ? dst_depth : CV_16S), src.channels()));
  1854. cv::subtract(src, cv::Scalar::all(16), fixed_dst, mask, dst_depth);
  1855. dst = fixed_dst;
  1856. dst_depth = fixed_dst.depth();
  1857. }
  1858. ASSERT_FALSE(dst.empty());
  1859. ASSERT_EQ(src.size(), dst.size());
  1860. ASSERT_EQ(dst_depth > 0 ? dst_depth : src.depth(), dst.depth());
  1861. ASSERT_EQ(0, cv::countNonZero(dst.reshape(1)));
  1862. }
  1863. TEST_P(SubtractOutputMatNotEmpty, Scalar_Mat)
  1864. {
  1865. cv::Mat src(size, src_type, cv::Scalar::all(16));
  1866. cv::Mat dst;
  1867. if (!fixed)
  1868. {
  1869. cv::subtract(cv::Scalar::all(16), src, dst, cv::noArray(), dst_depth);
  1870. }
  1871. else
  1872. {
  1873. const cv::Mat fixed_dst(size, CV_MAKE_TYPE((dst_depth > 0 ? dst_depth : CV_16S), src.channels()));
  1874. cv::subtract(cv::Scalar::all(16), src, fixed_dst, cv::noArray(), dst_depth);
  1875. dst = fixed_dst;
  1876. dst_depth = fixed_dst.depth();
  1877. }
  1878. ASSERT_FALSE(dst.empty());
  1879. ASSERT_EQ(src.size(), dst.size());
  1880. ASSERT_EQ(dst_depth > 0 ? dst_depth : src.depth(), dst.depth());
  1881. ASSERT_EQ(0, cv::countNonZero(dst.reshape(1)));
  1882. }
  1883. TEST_P(SubtractOutputMatNotEmpty, Scalar_Mat_WithMask)
  1884. {
  1885. cv::Mat src(size, src_type, cv::Scalar::all(16));
  1886. cv::Mat mask(size, CV_8UC1, cv::Scalar::all(255));
  1887. cv::Mat dst;
  1888. if (!fixed)
  1889. {
  1890. cv::subtract(cv::Scalar::all(16), src, dst, mask, dst_depth);
  1891. }
  1892. else
  1893. {
  1894. const cv::Mat fixed_dst(size, CV_MAKE_TYPE((dst_depth > 0 ? dst_depth : CV_16S), src.channels()));
  1895. cv::subtract(cv::Scalar::all(16), src, fixed_dst, mask, dst_depth);
  1896. dst = fixed_dst;
  1897. dst_depth = fixed_dst.depth();
  1898. }
  1899. ASSERT_FALSE(dst.empty());
  1900. ASSERT_EQ(src.size(), dst.size());
  1901. ASSERT_EQ(dst_depth > 0 ? dst_depth : src.depth(), dst.depth());
  1902. ASSERT_EQ(0, cv::countNonZero(dst.reshape(1)));
  1903. }
  1904. TEST_P(SubtractOutputMatNotEmpty, Mat_Mat_3d)
  1905. {
  1906. int dims[] = {5, size.height, size.width};
  1907. cv::Mat src1(3, dims, src_type, cv::Scalar::all(16));
  1908. cv::Mat src2(3, dims, src_type, cv::Scalar::all(16));
  1909. cv::Mat dst;
  1910. if (!fixed)
  1911. {
  1912. cv::subtract(src1, src2, dst, cv::noArray(), dst_depth);
  1913. }
  1914. else
  1915. {
  1916. const cv::Mat fixed_dst(3, dims, CV_MAKE_TYPE((dst_depth > 0 ? dst_depth : CV_16S), src1.channels()));
  1917. cv::subtract(src1, src2, fixed_dst, cv::noArray(), dst_depth);
  1918. dst = fixed_dst;
  1919. dst_depth = fixed_dst.depth();
  1920. }
  1921. ASSERT_FALSE(dst.empty());
  1922. ASSERT_EQ(src1.dims, dst.dims);
  1923. ASSERT_EQ(src1.size, dst.size);
  1924. ASSERT_EQ(dst_depth > 0 ? dst_depth : src1.depth(), dst.depth());
  1925. ASSERT_EQ(0, cv::countNonZero(dst.reshape(1)));
  1926. }
  1927. INSTANTIATE_TEST_CASE_P(Arithm, SubtractOutputMatNotEmpty, testing::Combine(
  1928. testing::Values(cv::Size(16, 16), cv::Size(13, 13), cv::Size(16, 13), cv::Size(13, 16)),
  1929. testing::Values(perf::MatType(CV_8UC1), CV_8UC3, CV_8UC4, CV_16SC1, CV_16SC3),
  1930. testing::Values(-1, CV_16S, CV_32S, CV_32F),
  1931. testing::Bool()));
  1932. TEST(Core_FindNonZero, regression)
  1933. {
  1934. Mat img(10, 10, CV_8U, Scalar::all(0));
  1935. vector<Point> pts, pts2(5);
  1936. findNonZero(img, pts);
  1937. findNonZero(img, pts2);
  1938. ASSERT_TRUE(pts.empty() && pts2.empty());
  1939. RNG rng((uint64)-1);
  1940. size_t nz = 0;
  1941. for( int i = 0; i < 10; i++ )
  1942. {
  1943. int idx = rng.uniform(0, img.rows*img.cols);
  1944. if( !img.data[idx] ) nz++;
  1945. img.data[idx] = (uchar)rng.uniform(1, 256);
  1946. }
  1947. findNonZero(img, pts);
  1948. ASSERT_TRUE(pts.size() == nz);
  1949. img.convertTo( img, CV_8S );
  1950. pts.clear();
  1951. findNonZero(img, pts);
  1952. ASSERT_TRUE(pts.size() == nz);
  1953. img.convertTo( img, CV_16U );
  1954. pts.resize(pts.size()*2);
  1955. findNonZero(img, pts);
  1956. ASSERT_TRUE(pts.size() == nz);
  1957. img.convertTo( img, CV_16S );
  1958. pts.resize(pts.size()*3);
  1959. findNonZero(img, pts);
  1960. ASSERT_TRUE(pts.size() == nz);
  1961. img.convertTo( img, CV_32S );
  1962. pts.resize(pts.size()*4);
  1963. findNonZero(img, pts);
  1964. ASSERT_TRUE(pts.size() == nz);
  1965. img.convertTo( img, CV_32F );
  1966. pts.resize(pts.size()*5);
  1967. findNonZero(img, pts);
  1968. ASSERT_TRUE(pts.size() == nz);
  1969. img.convertTo( img, CV_64F );
  1970. pts.clear();
  1971. findNonZero(img, pts);
  1972. ASSERT_TRUE(pts.size() == nz);
  1973. }
  1974. TEST(Core_BoolVector, support)
  1975. {
  1976. std::vector<bool> test;
  1977. int i, n = 205;
  1978. int nz = 0;
  1979. test.resize(n);
  1980. for( i = 0; i < n; i++ )
  1981. {
  1982. test[i] = theRNG().uniform(0, 2) != 0;
  1983. nz += (int)test[i];
  1984. }
  1985. ASSERT_EQ( nz, countNonZero(test) );
  1986. ASSERT_FLOAT_EQ((float)nz/n, (float)(cv::mean(test)[0]));
  1987. }
  1988. TEST(MinMaxLoc, Mat_UcharMax_Without_Loc)
  1989. {
  1990. Mat_<uchar> mat(50, 50);
  1991. uchar iMaxVal = std::numeric_limits<uchar>::max();
  1992. mat.setTo(iMaxVal);
  1993. double min, max;
  1994. Point minLoc, maxLoc;
  1995. minMaxLoc(mat, &min, &max, &minLoc, &maxLoc, Mat());
  1996. ASSERT_EQ(iMaxVal, min);
  1997. ASSERT_EQ(iMaxVal, max);
  1998. ASSERT_EQ(Point(0, 0), minLoc);
  1999. ASSERT_EQ(Point(0, 0), maxLoc);
  2000. }
  2001. TEST(MinMaxLoc, Mat_IntMax_Without_Mask)
  2002. {
  2003. Mat_<int> mat(50, 50);
  2004. int iMaxVal = std::numeric_limits<int>::max();
  2005. mat.setTo(iMaxVal);
  2006. double min, max;
  2007. Point minLoc, maxLoc;
  2008. minMaxLoc(mat, &min, &max, &minLoc, &maxLoc, Mat());
  2009. ASSERT_EQ(iMaxVal, min);
  2010. ASSERT_EQ(iMaxVal, max);
  2011. ASSERT_EQ(Point(0, 0), minLoc);
  2012. ASSERT_EQ(Point(0, 0), maxLoc);
  2013. }
  2014. TEST(Normalize, regression_5876_inplace_change_type)
  2015. {
  2016. double initial_values[] = {1, 2, 5, 4, 3};
  2017. float result_values[] = {0, 0.25, 1, 0.75, 0.5};
  2018. Mat m(Size(5, 1), CV_64FC1, initial_values);
  2019. Mat result(Size(5, 1), CV_32FC1, result_values);
  2020. normalize(m, m, 1, 0, NORM_MINMAX, CV_32F);
  2021. EXPECT_EQ(0, cvtest::norm(m, result, NORM_INF));
  2022. }
  2023. TEST(Normalize, regression_6125)
  2024. {
  2025. float initial_values[] = {
  2026. 1888, 1692, 369, 263, 199,
  2027. 280, 326, 129, 143, 126,
  2028. 233, 221, 130, 126, 150,
  2029. 249, 575, 574, 63, 12
  2030. };
  2031. Mat src(Size(20, 1), CV_32F, initial_values);
  2032. float min = 0., max = 400.;
  2033. normalize(src, src, 0, 400, NORM_MINMAX, CV_32F);
  2034. for(int i = 0; i < 20; i++)
  2035. {
  2036. EXPECT_GE(src.at<float>(i), min) << "Value should be >= 0";
  2037. EXPECT_LE(src.at<float>(i), max) << "Value should be <= 400";
  2038. }
  2039. }
  2040. TEST(MinMaxLoc, regression_4955_nans)
  2041. {
  2042. cv::Mat one_mat(2, 2, CV_32F, cv::Scalar(1));
  2043. cv::minMaxLoc(one_mat, NULL, NULL, NULL, NULL);
  2044. cv::Mat nan_mat(2, 2, CV_32F, cv::Scalar(std::numeric_limits<float>::quiet_NaN()));
  2045. cv::minMaxLoc(nan_mat, NULL, NULL, NULL, NULL);
  2046. }
  2047. TEST(Subtract, scalarc1_matc3)
  2048. {
  2049. int scalar = 255;
  2050. cv::Mat srcImage(5, 5, CV_8UC3, cv::Scalar::all(5)), destImage;
  2051. cv::subtract(scalar, srcImage, destImage);
  2052. ASSERT_EQ(0, cv::norm(cv::Mat(5, 5, CV_8UC3, cv::Scalar::all(250)), destImage, cv::NORM_INF));
  2053. }
  2054. TEST(Subtract, scalarc4_matc4)
  2055. {
  2056. cv::Scalar sc(255, 255, 255, 255);
  2057. cv::Mat srcImage(5, 5, CV_8UC4, cv::Scalar::all(5)), destImage;
  2058. cv::subtract(sc, srcImage, destImage);
  2059. ASSERT_EQ(0, cv::norm(cv::Mat(5, 5, CV_8UC4, cv::Scalar::all(250)), destImage, cv::NORM_INF));
  2060. }
  2061. TEST(Compare, empty)
  2062. {
  2063. cv::Mat temp, dst1, dst2;
  2064. EXPECT_NO_THROW(cv::compare(temp, temp, dst1, cv::CMP_EQ));
  2065. EXPECT_TRUE(dst1.empty());
  2066. EXPECT_THROW(dst2 = temp > 5, cv::Exception);
  2067. }
  2068. TEST(Compare, regression_8999)
  2069. {
  2070. Mat_<double> A(4,1); A << 1, 3, 2, 4;
  2071. Mat_<double> B(1,1); B << 2;
  2072. Mat C;
  2073. EXPECT_THROW(cv::compare(A, B, C, CMP_LT), cv::Exception);
  2074. }
  2075. TEST(Compare, regression_16F_do_not_crash)
  2076. {
  2077. cv::Mat mat1(2, 2, CV_16F, cv::Scalar(1));
  2078. cv::Mat mat2(2, 2, CV_16F, cv::Scalar(2));
  2079. cv::Mat dst;
  2080. EXPECT_THROW(cv::compare(mat1, mat2, dst, cv::CMP_EQ), cv::Exception);
  2081. }
  2082. TEST(Core_minMaxIdx, regression_9207_1)
  2083. {
  2084. const int rows = 4;
  2085. const int cols = 3;
  2086. uchar mask_[rows*cols] = {
  2087. 255, 255, 255,
  2088. 255, 0, 255,
  2089. 0, 255, 255,
  2090. 0, 0, 255
  2091. };
  2092. uchar src_[rows*cols] = {
  2093. 1, 1, 1,
  2094. 1, 1, 1,
  2095. 2, 1, 1,
  2096. 2, 2, 1
  2097. };
  2098. Mat mask(Size(cols, rows), CV_8UC1, mask_);
  2099. Mat src(Size(cols, rows), CV_8UC1, src_);
  2100. double minVal = -0.0, maxVal = -0.0;
  2101. int minIdx[2] = { -2, -2 }, maxIdx[2] = { -2, -2 };
  2102. cv::minMaxIdx(src, &minVal, &maxVal, minIdx, maxIdx, mask);
  2103. EXPECT_EQ(0, minIdx[0]);
  2104. EXPECT_EQ(0, minIdx[1]);
  2105. EXPECT_EQ(0, maxIdx[0]);
  2106. EXPECT_EQ(0, maxIdx[1]);
  2107. }
  2108. class TransposeND : public testing::TestWithParam< tuple<std::vector<int>, perf::MatType> >
  2109. {
  2110. public:
  2111. std::vector<int> m_shape;
  2112. int m_type;
  2113. void SetUp()
  2114. {
  2115. std::tie(m_shape, m_type) = GetParam();
  2116. }
  2117. };
  2118. TEST_P(TransposeND, basic)
  2119. {
  2120. Mat inp(m_shape, m_type);
  2121. randu(inp, 0, 255);
  2122. std::vector<int> order(m_shape.size());
  2123. std::iota(order.begin(), order.end(), 0);
  2124. auto transposer = [&order] (const std::vector<int>& id)
  2125. {
  2126. std::vector<int> ret(id.size());
  2127. for (size_t i = 0; i < id.size(); ++i)
  2128. {
  2129. ret[i] = id[order[i]];
  2130. }
  2131. return ret;
  2132. };
  2133. auto advancer = [&inp] (std::vector<int>& id)
  2134. {
  2135. for (int j = static_cast<int>(id.size() - 1); j >= 0; --j)
  2136. {
  2137. ++id[j];
  2138. if (id[j] != inp.size[j])
  2139. {
  2140. break;
  2141. }
  2142. id[j] = 0;
  2143. }
  2144. };
  2145. do
  2146. {
  2147. Mat out;
  2148. cv::transposeND(inp, order, out);
  2149. std::vector<int> id(order.size());
  2150. for (size_t i = 0; i < inp.total(); ++i)
  2151. {
  2152. auto new_id = transposer(id);
  2153. switch (inp.type())
  2154. {
  2155. case CV_8UC1:
  2156. ASSERT_EQ(inp.at<uint8_t>(id.data()), out.at<uint8_t>(new_id.data()));
  2157. break;
  2158. case CV_32FC1:
  2159. ASSERT_EQ(inp.at<float>(id.data()), out.at<float>(new_id.data()));
  2160. break;
  2161. default:
  2162. FAIL() << "Unsupported type: " << inp.type();
  2163. }
  2164. advancer(id);
  2165. }
  2166. } while (std::next_permutation(order.begin(), order.end()));
  2167. }
  2168. INSTANTIATE_TEST_CASE_P(Arithm, TransposeND, testing::Combine(
  2169. testing::Values(std::vector<int>{2, 3, 4}, std::vector<int>{5, 10}),
  2170. testing::Values(perf::MatType(CV_8UC1), CV_32FC1)
  2171. ));
  2172. class FlipND : public testing::TestWithParam< tuple<std::vector<int>, perf::MatType> >
  2173. {
  2174. public:
  2175. std::vector<int> m_shape;
  2176. int m_type;
  2177. void SetUp()
  2178. {
  2179. std::tie(m_shape, m_type) = GetParam();
  2180. }
  2181. };
  2182. TEST_P(FlipND, basic)
  2183. {
  2184. Mat inp(m_shape, m_type);
  2185. randu(inp, 0, 255);
  2186. int ndim = static_cast<int>(m_shape.size());
  2187. std::vector<int> axes(ndim*2); // [-shape, shape)
  2188. std::iota(axes.begin(), axes.end(), -ndim);
  2189. auto get_flipped_indices = [&inp, ndim] (size_t total, std::vector<int>& indices, int axis)
  2190. {
  2191. const int* shape = inp.size.p;
  2192. size_t t = total, idx;
  2193. for (int i = ndim - 1; i >= 0; --i)
  2194. {
  2195. idx = t / shape[i];
  2196. indices[i] = int(t - idx * shape[i]);
  2197. t = idx;
  2198. }
  2199. int _axis = (axis + ndim) % ndim;
  2200. std::vector<int> flipped_indices = indices;
  2201. flipped_indices[_axis] = shape[_axis] - 1 - indices[_axis];
  2202. return flipped_indices;
  2203. };
  2204. for (size_t i = 0; i < axes.size(); ++i)
  2205. {
  2206. int axis = axes[i];
  2207. Mat out;
  2208. cv::flipND(inp, out, axis);
  2209. // check values
  2210. std::vector<int> indices(ndim, 0);
  2211. for (size_t j = 0; j < inp.total(); ++j)
  2212. {
  2213. auto flipped_indices = get_flipped_indices(j, indices, axis);
  2214. switch (inp.type())
  2215. {
  2216. case CV_8UC1:
  2217. ASSERT_EQ(inp.at<uint8_t>(indices.data()), out.at<uint8_t>(flipped_indices.data()));
  2218. break;
  2219. case CV_32FC1:
  2220. ASSERT_EQ(inp.at<float>(indices.data()), out.at<float>(flipped_indices.data()));
  2221. break;
  2222. default:
  2223. FAIL() << "Unsupported type: " << inp.type();
  2224. }
  2225. }
  2226. }
  2227. }
  2228. INSTANTIATE_TEST_CASE_P(Arithm, FlipND, testing::Combine(
  2229. testing::Values(std::vector<int>{5, 10}, std::vector<int>{2, 3, 4}),
  2230. testing::Values(perf::MatType(CV_8UC1), CV_32FC1)
  2231. ));
  2232. TEST(BroadcastTo, basic) {
  2233. std::vector<int> shape_src{2, 1};
  2234. std::vector<int> data_src{1, 2};
  2235. Mat src(static_cast<int>(shape_src.size()), shape_src.data(), CV_32SC1, data_src.data());
  2236. auto get_index = [](const std::vector<int>& shape, size_t cnt) {
  2237. std::vector<int> index(shape.size());
  2238. size_t t = cnt;
  2239. for (int i = static_cast<int>(shape.size() - 1); i >= 0; --i) {
  2240. size_t idx = t / shape[i];
  2241. index[i] = static_cast<int>(t - idx * shape[i]);
  2242. t = idx;
  2243. }
  2244. return index;
  2245. };
  2246. auto fn_verify = [&get_index](const Mat& ref, const Mat& res) {
  2247. // check type
  2248. EXPECT_EQ(ref.type(), res.type());
  2249. // check shape
  2250. EXPECT_EQ(ref.dims, res.dims);
  2251. for (int i = 0; i < ref.dims; ++i) {
  2252. EXPECT_EQ(ref.size[i], res.size[i]);
  2253. }
  2254. // check value
  2255. std::vector<int> shape{ref.size.p, ref.size.p + ref.dims};
  2256. for (size_t i = 0; i < ref.total(); ++i) {
  2257. auto index = get_index(shape, i);
  2258. switch (ref.type()) {
  2259. case CV_32SC1: {
  2260. ASSERT_EQ(ref.at<int>(index.data()), res.at<int>(index.data()));
  2261. } break;
  2262. case CV_8UC1: {
  2263. ASSERT_EQ(ref.at<uint8_t>(index.data()), res.at<uint8_t>(index.data()));
  2264. } break;
  2265. case CV_32FC1: {
  2266. ASSERT_EQ(ref.at<float>(index.data()), res.at<float>(index.data()));
  2267. } break;
  2268. default: FAIL() << "Unsupported type: " << ref.type();
  2269. }
  2270. }
  2271. };
  2272. {
  2273. std::vector<int> shape{4, 2, 3};
  2274. std::vector<int> data_ref{
  2275. 1, 1, 1, // [0, 0, :]
  2276. 2, 2, 2, // [0, 1, :]
  2277. 1, 1, 1, // [1, 0, :]
  2278. 2, 2, 2, // [1, 1, :]
  2279. 1, 1, 1, // [2, 0, :]
  2280. 2, 2, 2, // [2, 1, :]
  2281. 1, 1, 1, // [3, 0, :]
  2282. 2, 2, 2 // [3, 1, :]
  2283. };
  2284. Mat ref(static_cast<int>(shape.size()), shape.data(), src.type(), data_ref.data());
  2285. Mat dst;
  2286. broadcast(src, shape, dst);
  2287. fn_verify(ref, dst);
  2288. }
  2289. {
  2290. Mat _src;
  2291. src.convertTo(_src, CV_8U);
  2292. std::vector<int> shape{4, 2, 3};
  2293. std::vector<uint8_t> data_ref{
  2294. 1, 1, 1, // [0, 0, :]
  2295. 2, 2, 2, // [0, 1, :]
  2296. 1, 1, 1, // [1, 0, :]
  2297. 2, 2, 2, // [1, 1, :]
  2298. 1, 1, 1, // [2, 0, :]
  2299. 2, 2, 2, // [2, 1, :]
  2300. 1, 1, 1, // [3, 0, :]
  2301. 2, 2, 2 // [3, 1, :]
  2302. };
  2303. Mat ref(static_cast<int>(shape.size()), shape.data(), _src.type(), data_ref.data());
  2304. Mat dst;
  2305. broadcast(_src, shape, dst);
  2306. fn_verify(ref, dst);
  2307. }
  2308. {
  2309. Mat _src;
  2310. src.convertTo(_src, CV_32F);
  2311. std::vector<int> shape{1, 1, 2, 1}; // {2, 1}
  2312. std::vector<float> data_ref{
  2313. 1.f, // [0, 0, 0, 0]
  2314. 2.f, // [0, 0, 1, 0]
  2315. };
  2316. Mat ref(static_cast<int>(shape.size()), shape.data(), _src.type(), data_ref.data());
  2317. Mat dst;
  2318. broadcast(_src, shape, dst);
  2319. fn_verify(ref, dst);
  2320. }
  2321. {
  2322. std::vector<int> _shape_src{2, 3, 4};
  2323. std::vector<float> _data_src{
  2324. 1.f, 2.f, 3.f, 4.f, // [0, 0, :]
  2325. 2.f, 3.f, 4.f, 5.f, // [0, 1, :]
  2326. 3.f, 4.f, 5.f, 6.f, // [0, 2, :]
  2327. 4.f, 5.f, 6.f, 7.f, // [1, 0, :]
  2328. 5.f, 6.f, 7.f, 8.f, // [1, 1, :]
  2329. 6.f, 7.f, 8.f, 9.f, // [1, 2, :]
  2330. };
  2331. Mat _src(static_cast<int>(_shape_src.size()), _shape_src.data(), CV_32FC1, _data_src.data());
  2332. std::vector<int> shape{2, 1, 2, 3, 4};
  2333. std::vector<float> data_ref{
  2334. 1.f, 2.f, 3.f, 4.f, // [0, 0, 0, 0, :]
  2335. 2.f, 3.f, 4.f, 5.f, // [0, 0, 0, 1, :]
  2336. 3.f, 4.f, 5.f, 6.f, // [0, 0, 0, 2, :]
  2337. 4.f, 5.f, 6.f, 7.f, // [0, 0, 1, 0, :]
  2338. 5.f, 6.f, 7.f, 8.f, // [0, 0, 1, 1, :]
  2339. 6.f, 7.f, 8.f, 9.f, // [0, 0, 1, 2, :]
  2340. 1.f, 2.f, 3.f, 4.f, // [1, 0, 0, 0, :]
  2341. 2.f, 3.f, 4.f, 5.f, // [1, 0, 0, 1, :]
  2342. 3.f, 4.f, 5.f, 6.f, // [1, 0, 0, 2, :]
  2343. 4.f, 5.f, 6.f, 7.f, // [1, 0, 1, 0, :]
  2344. 5.f, 6.f, 7.f, 8.f, // [1, 0, 1, 1, :]
  2345. 6.f, 7.f, 8.f, 9.f, // [1, 0, 1, 2, :]
  2346. };
  2347. Mat ref(static_cast<int>(shape.size()), shape.data(), _src.type(), data_ref.data());
  2348. Mat dst;
  2349. broadcast(_src, shape, dst);
  2350. fn_verify(ref, dst);
  2351. }
  2352. }
  2353. TEST(Core_minMaxIdx, regression_9207_2)
  2354. {
  2355. const int rows = 13;
  2356. const int cols = 15;
  2357. uchar mask_[rows*cols] = {
  2358. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255,
  2359. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255,
  2360. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255,
  2361. 0, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255,
  2362. 255, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255,
  2363. 255, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 255, 255,
  2364. 255, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 255, 255, 255, 0,
  2365. 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 0, 255, 0,
  2366. 255, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 255, 255, 0,
  2367. 255, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 255, 0,
  2368. 255, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  2369. 0, 255, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  2370. 0, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  2371. };
  2372. uchar src_[15*13] = {
  2373. 5, 5, 5, 5, 5, 6, 5, 2, 0, 4, 6, 6, 4, 1, 0,
  2374. 6, 5, 4, 4, 5, 6, 6, 5, 2, 0, 4, 6, 5, 2, 0,
  2375. 3, 2, 1, 1, 2, 4, 6, 6, 4, 2, 3, 4, 4, 2, 0,
  2376. 1, 0, 0, 0, 0, 1, 4, 5, 4, 4, 4, 4, 3, 2, 0,
  2377. 0, 0, 0, 0, 0, 0, 2, 3, 4, 4, 4, 3, 2, 1, 0,
  2378. 0, 0, 0, 0, 0, 0, 0, 2, 3, 4, 3, 2, 1, 0, 0,
  2379. 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1,
  2380. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
  2381. 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1,
  2382. 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 3, 3, 1, 0, 1,
  2383. 0, 0, 0, 0, 0, 0, 1, 4, 5, 6, 5, 4, 3, 2, 0,
  2384. 1, 0, 0, 0, 0, 0, 3, 5, 5, 4, 3, 4, 4, 3, 0,
  2385. 2, 0, 0, 0, 0, 2, 5, 6, 5, 2, 2, 5, 4, 3, 0
  2386. };
  2387. Mat mask(Size(cols, rows), CV_8UC1, mask_);
  2388. Mat src(Size(cols, rows), CV_8UC1, src_);
  2389. double minVal = -0.0, maxVal = -0.0;
  2390. int minIdx[2] = { -2, -2 }, maxIdx[2] = { -2, -2 };
  2391. cv::minMaxIdx(src, &minVal, &maxVal, minIdx, maxIdx, mask);
  2392. EXPECT_EQ(0, minIdx[0]);
  2393. EXPECT_EQ(14, minIdx[1]);
  2394. EXPECT_EQ(0, maxIdx[0]);
  2395. EXPECT_EQ(14, maxIdx[1]);
  2396. }
  2397. TEST(Core_MinMaxIdx, MatND)
  2398. {
  2399. const int shape[3] = {5,5,3};
  2400. cv::Mat src = cv::Mat(3, shape, CV_8UC1);
  2401. src.setTo(1);
  2402. src.data[1] = 0;
  2403. src.data[5*5*3-2] = 2;
  2404. int minIdx[3];
  2405. int maxIdx[3];
  2406. double minVal, maxVal;
  2407. cv::minMaxIdx(src, &minVal, &maxVal, minIdx, maxIdx);
  2408. EXPECT_EQ(0, minVal);
  2409. EXPECT_EQ(2, maxVal);
  2410. EXPECT_EQ(0, minIdx[0]);
  2411. EXPECT_EQ(0, minIdx[1]);
  2412. EXPECT_EQ(1, minIdx[2]);
  2413. EXPECT_EQ(4, maxIdx[0]);
  2414. EXPECT_EQ(4, maxIdx[1]);
  2415. EXPECT_EQ(1, maxIdx[2]);
  2416. }
  2417. TEST(Core_Set, regression_11044)
  2418. {
  2419. Mat testFloat(Size(3, 3), CV_32FC1);
  2420. Mat testDouble(Size(3, 3), CV_64FC1);
  2421. testFloat.setTo(1);
  2422. EXPECT_EQ(1, testFloat.at<float>(0,0));
  2423. testFloat.setTo(std::numeric_limits<float>::infinity());
  2424. EXPECT_EQ(std::numeric_limits<float>::infinity(), testFloat.at<float>(0, 0));
  2425. testFloat.setTo(1);
  2426. EXPECT_EQ(1, testFloat.at<float>(0, 0));
  2427. testFloat.setTo(std::numeric_limits<double>::infinity());
  2428. EXPECT_EQ(std::numeric_limits<float>::infinity(), testFloat.at<float>(0, 0));
  2429. testDouble.setTo(1);
  2430. EXPECT_EQ(1, testDouble.at<double>(0, 0));
  2431. testDouble.setTo(std::numeric_limits<float>::infinity());
  2432. EXPECT_EQ(std::numeric_limits<double>::infinity(), testDouble.at<double>(0, 0));
  2433. testDouble.setTo(1);
  2434. EXPECT_EQ(1, testDouble.at<double>(0, 0));
  2435. testDouble.setTo(std::numeric_limits<double>::infinity());
  2436. EXPECT_EQ(std::numeric_limits<double>::infinity(), testDouble.at<double>(0, 0));
  2437. Mat testMask(Size(3, 3), CV_8UC1, Scalar(1));
  2438. testFloat.setTo(1);
  2439. EXPECT_EQ(1, testFloat.at<float>(0, 0));
  2440. testFloat.setTo(std::numeric_limits<float>::infinity(), testMask);
  2441. EXPECT_EQ(std::numeric_limits<float>::infinity(), testFloat.at<float>(0, 0));
  2442. testFloat.setTo(1);
  2443. EXPECT_EQ(1, testFloat.at<float>(0, 0));
  2444. testFloat.setTo(std::numeric_limits<double>::infinity(), testMask);
  2445. EXPECT_EQ(std::numeric_limits<float>::infinity(), testFloat.at<float>(0, 0));
  2446. testDouble.setTo(1);
  2447. EXPECT_EQ(1, testDouble.at<double>(0, 0));
  2448. testDouble.setTo(std::numeric_limits<float>::infinity(), testMask);
  2449. EXPECT_EQ(std::numeric_limits<double>::infinity(), testDouble.at<double>(0, 0));
  2450. testDouble.setTo(1);
  2451. EXPECT_EQ(1, testDouble.at<double>(0, 0));
  2452. testDouble.setTo(std::numeric_limits<double>::infinity(), testMask);
  2453. EXPECT_EQ(std::numeric_limits<double>::infinity(), testDouble.at<double>(0, 0));
  2454. }
  2455. TEST(Core_Norm, IPP_regression_NORM_L1_16UC3_small)
  2456. {
  2457. int cn = 3;
  2458. Size sz(9, 4); // width < 16
  2459. Mat a(sz, CV_MAKE_TYPE(CV_16U, cn), Scalar::all(1));
  2460. Mat b(sz, CV_MAKE_TYPE(CV_16U, cn), Scalar::all(2));
  2461. uchar mask_[9*4] = {
  2462. 255, 255, 255, 0, 255, 255, 0, 255, 0,
  2463. 0, 255, 0, 0, 255, 255, 255, 255, 0,
  2464. 0, 0, 0, 255, 0, 255, 0, 255, 255,
  2465. 0, 0, 255, 0, 255, 255, 255, 0, 255
  2466. };
  2467. Mat mask(sz, CV_8UC1, mask_);
  2468. EXPECT_EQ((double)9*4*cn, cv::norm(a, b, NORM_L1)); // without mask, IPP works well
  2469. EXPECT_EQ((double)20*cn, cv::norm(a, b, NORM_L1, mask));
  2470. }
  2471. TEST(Core_Norm, NORM_L2_8UC4)
  2472. {
  2473. // Tests there is no integer overflow in norm computation for multiple channels.
  2474. const int kSide = 100;
  2475. cv::Mat4b a(kSide, kSide, cv::Scalar(255, 255, 255, 255));
  2476. cv::Mat4b b = cv::Mat4b::zeros(kSide, kSide);
  2477. const double kNorm = 2.*kSide*255.;
  2478. EXPECT_EQ(kNorm, cv::norm(a, b, NORM_L2));
  2479. }
  2480. TEST(Core_ConvertTo, regression_12121)
  2481. {
  2482. {
  2483. Mat src(4, 64, CV_32SC1, Scalar(-1));
  2484. Mat dst;
  2485. src.convertTo(dst, CV_8U);
  2486. EXPECT_EQ(0, dst.at<uchar>(0, 0)) << "src=" << src.at<int>(0, 0);
  2487. }
  2488. {
  2489. Mat src(4, 64, CV_32SC1, Scalar(INT_MIN));
  2490. Mat dst;
  2491. src.convertTo(dst, CV_8U);
  2492. EXPECT_EQ(0, dst.at<uchar>(0, 0)) << "src=" << src.at<int>(0, 0);
  2493. }
  2494. {
  2495. Mat src(4, 64, CV_32SC1, Scalar(INT_MIN + 32767));
  2496. Mat dst;
  2497. src.convertTo(dst, CV_8U);
  2498. EXPECT_EQ(0, dst.at<uchar>(0, 0)) << "src=" << src.at<int>(0, 0);
  2499. }
  2500. {
  2501. Mat src(4, 64, CV_32SC1, Scalar(INT_MIN + 32768));
  2502. Mat dst;
  2503. src.convertTo(dst, CV_8U);
  2504. EXPECT_EQ(0, dst.at<uchar>(0, 0)) << "src=" << src.at<int>(0, 0);
  2505. }
  2506. {
  2507. Mat src(4, 64, CV_32SC1, Scalar(32768));
  2508. Mat dst;
  2509. src.convertTo(dst, CV_8U);
  2510. EXPECT_EQ(255, dst.at<uchar>(0, 0)) << "src=" << src.at<int>(0, 0);
  2511. }
  2512. {
  2513. Mat src(4, 64, CV_32SC1, Scalar(INT_MIN));
  2514. Mat dst;
  2515. src.convertTo(dst, CV_16U);
  2516. EXPECT_EQ(0, dst.at<ushort>(0, 0)) << "src=" << src.at<int>(0, 0);
  2517. }
  2518. {
  2519. Mat src(4, 64, CV_32SC1, Scalar(INT_MIN + 32767));
  2520. Mat dst;
  2521. src.convertTo(dst, CV_16U);
  2522. EXPECT_EQ(0, dst.at<ushort>(0, 0)) << "src=" << src.at<int>(0, 0);
  2523. }
  2524. {
  2525. Mat src(4, 64, CV_32SC1, Scalar(INT_MIN + 32768));
  2526. Mat dst;
  2527. src.convertTo(dst, CV_16U);
  2528. EXPECT_EQ(0, dst.at<ushort>(0, 0)) << "src=" << src.at<int>(0, 0);
  2529. }
  2530. {
  2531. Mat src(4, 64, CV_32SC1, Scalar(65536));
  2532. Mat dst;
  2533. src.convertTo(dst, CV_16U);
  2534. EXPECT_EQ(65535, dst.at<ushort>(0, 0)) << "src=" << src.at<int>(0, 0);
  2535. }
  2536. }
  2537. TEST(Core_MeanStdDev, regression_multichannel)
  2538. {
  2539. {
  2540. uchar buf[] = { 1, 2, 3, 4, 5, 6, 7, 8,
  2541. 3, 4, 5, 6, 7, 8, 9, 10 };
  2542. double ref_buf[] = { 2., 3., 4., 5., 6., 7., 8., 9.,
  2543. 1., 1., 1., 1., 1., 1., 1., 1. };
  2544. Mat src(1, 2, CV_MAKETYPE(CV_8U, 8), buf);
  2545. Mat ref_m(8, 1, CV_64FC1, ref_buf);
  2546. Mat ref_sd(8, 1, CV_64FC1, ref_buf + 8);
  2547. Mat dst_m, dst_sd;
  2548. meanStdDev(src, dst_m, dst_sd);
  2549. EXPECT_EQ(0, cv::norm(dst_m, ref_m, NORM_L1));
  2550. EXPECT_EQ(0, cv::norm(dst_sd, ref_sd, NORM_L1));
  2551. }
  2552. }
  2553. // Related issue : https://github.com/opencv/opencv/issues/26861
  2554. TEST(Core_MeanStdDevTest, LargeImage)
  2555. {
  2556. applyTestTag(CV_TEST_TAG_VERYLONG);
  2557. applyTestTag(CV_TEST_TAG_MEMORY_14GB);
  2558. // (1<<16) * ((1<<15)+10) = ~2.147 billion
  2559. cv::Mat largeImage = cv::Mat::ones((1 << 16), ((1 << 15) + 10), CV_8U);
  2560. cv::Scalar mean, stddev;
  2561. cv::meanStdDev(largeImage, mean, stddev);
  2562. EXPECT_NEAR(mean[0], 1.0, 1e-5);
  2563. EXPECT_NEAR(stddev[0], 0.0, 1e-5);
  2564. }
  2565. template <typename T> static inline
  2566. void testDivideInitData(Mat& src1, Mat& src2)
  2567. {
  2568. CV_StaticAssert(std::numeric_limits<T>::is_integer, "");
  2569. const static T src1_[] = {
  2570. 0, 0, 0, 0,
  2571. 8, 8, 8, 8,
  2572. -8, -8, -8, -8
  2573. };
  2574. Mat(3, 4, traits::Type<T>::value, (void*)src1_).copyTo(src1);
  2575. const static T src2_[] = {
  2576. 1, 2, 0, std::numeric_limits<T>::max(),
  2577. 1, 2, 0, std::numeric_limits<T>::max(),
  2578. 1, 2, 0, std::numeric_limits<T>::max(),
  2579. };
  2580. Mat(3, 4, traits::Type<T>::value, (void*)src2_).copyTo(src2);
  2581. }
  2582. template <typename T> static inline
  2583. void testDivideInitDataFloat(Mat& src1, Mat& src2)
  2584. {
  2585. CV_StaticAssert(!std::numeric_limits<T>::is_integer, "");
  2586. const static T src1_[] = {
  2587. 0, 0, 0, 0,
  2588. 8, 8, 8, 8,
  2589. -8, -8, -8, -8
  2590. };
  2591. Mat(3, 4, traits::Type<T>::value, (void*)src1_).copyTo(src1);
  2592. const static T src2_[] = {
  2593. 1, 2, 0, std::numeric_limits<T>::infinity(),
  2594. 1, 2, 0, std::numeric_limits<T>::infinity(),
  2595. 1, 2, 0, std::numeric_limits<T>::infinity(),
  2596. };
  2597. Mat(3, 4, traits::Type<T>::value, (void*)src2_).copyTo(src2);
  2598. }
  2599. template <> inline void testDivideInitData<float>(Mat& src1, Mat& src2) { testDivideInitDataFloat<float>(src1, src2); }
  2600. template <> inline void testDivideInitData<double>(Mat& src1, Mat& src2) { testDivideInitDataFloat<double>(src1, src2); }
  2601. template <typename T> static inline
  2602. void testDivideChecks(const Mat& dst)
  2603. {
  2604. ASSERT_FALSE(dst.empty());
  2605. CV_StaticAssert(std::numeric_limits<T>::is_integer, "");
  2606. for (int y = 0; y < dst.rows; y++)
  2607. {
  2608. for (int x = 0; x < dst.cols; x++)
  2609. {
  2610. if ((x % 4) == 2)
  2611. {
  2612. EXPECT_EQ(0, dst.at<T>(y, x)) << "dst(" << y << ", " << x << ") = " << dst.at<T>(y, x);
  2613. }
  2614. else
  2615. {
  2616. EXPECT_TRUE(0 == cvIsNaN((double)dst.at<T>(y, x))) << "dst(" << y << ", " << x << ") = " << dst.at<T>(y, x);
  2617. EXPECT_TRUE(0 == cvIsInf((double)dst.at<T>(y, x))) << "dst(" << y << ", " << x << ") = " << dst.at<T>(y, x);
  2618. }
  2619. }
  2620. }
  2621. }
  2622. template <typename T> static inline
  2623. void testDivideChecksFP(const Mat& dst)
  2624. {
  2625. ASSERT_FALSE(dst.empty());
  2626. CV_StaticAssert(!std::numeric_limits<T>::is_integer, "");
  2627. for (int y = 0; y < dst.rows; y++)
  2628. {
  2629. for (int x = 0; x < dst.cols; x++)
  2630. {
  2631. if ((y % 3) == 0 && (x % 4) == 2)
  2632. {
  2633. EXPECT_TRUE(cvIsNaN(dst.at<T>(y, x))) << "dst(" << y << ", " << x << ") = " << dst.at<T>(y, x);
  2634. }
  2635. else if ((x % 4) == 2)
  2636. {
  2637. EXPECT_TRUE(cvIsInf(dst.at<T>(y, x))) << "dst(" << y << ", " << x << ") = " << dst.at<T>(y, x);
  2638. }
  2639. else
  2640. {
  2641. EXPECT_FALSE(cvIsNaN(dst.at<T>(y, x))) << "dst(" << y << ", " << x << ") = " << dst.at<T>(y, x);
  2642. EXPECT_FALSE(cvIsInf(dst.at<T>(y, x))) << "dst(" << y << ", " << x << ") = " << dst.at<T>(y, x);
  2643. }
  2644. }
  2645. }
  2646. }
  2647. template <> inline void testDivideChecks<float>(const Mat& dst) { testDivideChecksFP<float>(dst); }
  2648. template <> inline void testDivideChecks<double>(const Mat& dst) { testDivideChecksFP<double>(dst); }
  2649. template <typename T> static inline
  2650. void testDivide(bool isUMat, double scale, bool largeSize, bool tailProcessing, bool roi)
  2651. {
  2652. Mat src1, src2;
  2653. testDivideInitData<T>(src1, src2);
  2654. ASSERT_FALSE(src1.empty()); ASSERT_FALSE(src2.empty());
  2655. if (largeSize)
  2656. {
  2657. repeat(src1.clone(), 1, 8, src1);
  2658. repeat(src2.clone(), 1, 8, src2);
  2659. }
  2660. if (tailProcessing)
  2661. {
  2662. src1 = src1(Rect(0, 0, src1.cols - 1, src1.rows));
  2663. src2 = src2(Rect(0, 0, src2.cols - 1, src2.rows));
  2664. }
  2665. if (!roi && tailProcessing)
  2666. {
  2667. src1 = src1.clone();
  2668. src2 = src2.clone();
  2669. }
  2670. Mat dst;
  2671. if (!isUMat)
  2672. {
  2673. cv::divide(src1, src2, dst, scale);
  2674. }
  2675. else
  2676. {
  2677. UMat usrc1, usrc2, udst;
  2678. src1.copyTo(usrc1);
  2679. src2.copyTo(usrc2);
  2680. cv::divide(usrc1, usrc2, udst, scale);
  2681. udst.copyTo(dst);
  2682. }
  2683. testDivideChecks<T>(dst);
  2684. if (::testing::Test::HasFailure())
  2685. {
  2686. std::cout << "src1 = " << std::endl << src1 << std::endl;
  2687. std::cout << "src2 = " << std::endl << src2 << std::endl;
  2688. std::cout << "dst = " << std::endl << dst << std::endl;
  2689. }
  2690. }
  2691. typedef tuple<bool, double, bool, bool, bool> DivideRulesParam;
  2692. typedef testing::TestWithParam<DivideRulesParam> Core_DivideRules;
  2693. TEST_P(Core_DivideRules, type_32s)
  2694. {
  2695. DivideRulesParam param = GetParam();
  2696. testDivide<int>(get<0>(param), get<1>(param), get<2>(param), get<3>(param), get<4>(param));
  2697. }
  2698. TEST_P(Core_DivideRules, type_16s)
  2699. {
  2700. DivideRulesParam param = GetParam();
  2701. testDivide<short>(get<0>(param), get<1>(param), get<2>(param), get<3>(param), get<4>(param));
  2702. }
  2703. TEST_P(Core_DivideRules, type_32f)
  2704. {
  2705. DivideRulesParam param = GetParam();
  2706. testDivide<float>(get<0>(param), get<1>(param), get<2>(param), get<3>(param), get<4>(param));
  2707. }
  2708. TEST_P(Core_DivideRules, type_64f)
  2709. {
  2710. DivideRulesParam param = GetParam();
  2711. testDivide<double>(get<0>(param), get<1>(param), get<2>(param), get<3>(param), get<4>(param));
  2712. }
  2713. INSTANTIATE_TEST_CASE_P(/* */, Core_DivideRules, testing::Combine(
  2714. /* isMat */ testing::Values(false),
  2715. /* scale */ testing::Values(1.0, 5.0),
  2716. /* largeSize */ testing::Bool(),
  2717. /* tail */ testing::Bool(),
  2718. /* roi */ testing::Bool()
  2719. ));
  2720. INSTANTIATE_TEST_CASE_P(UMat, Core_DivideRules, testing::Combine(
  2721. /* isMat */ testing::Values(true),
  2722. /* scale */ testing::Values(1.0, 5.0),
  2723. /* largeSize */ testing::Bool(),
  2724. /* tail */ testing::Bool(),
  2725. /* roi */ testing::Bool()
  2726. ));
  2727. TEST(Core_MinMaxIdx, rows_overflow)
  2728. {
  2729. const int N = 65536 + 1;
  2730. const int M = 1;
  2731. {
  2732. setRNGSeed(123);
  2733. Mat m(N, M, CV_32FC1);
  2734. randu(m, -100, 100);
  2735. double minVal = 0, maxVal = 0;
  2736. int minIdx[CV_MAX_DIM] = { 0 }, maxIdx[CV_MAX_DIM] = { 0 };
  2737. cv::minMaxIdx(m, &minVal, &maxVal, minIdx, maxIdx);
  2738. double minVal0 = 0, maxVal0 = 0;
  2739. int minIdx0[CV_MAX_DIM] = { 0 }, maxIdx0[CV_MAX_DIM] = { 0 };
  2740. cv::ipp::setUseIPP(false);
  2741. cv::minMaxIdx(m, &minVal0, &maxVal0, minIdx0, maxIdx0);
  2742. cv::ipp::setUseIPP(true);
  2743. EXPECT_FALSE(fabs(minVal0 - minVal) > 1e-6 || fabs(maxVal0 - maxVal) > 1e-6) << "NxM=" << N << "x" << M <<
  2744. " min=" << minVal0 << " vs " << minVal <<
  2745. " max=" << maxVal0 << " vs " << maxVal;
  2746. }
  2747. }
  2748. TEST(Core_Magnitude, regression_19506)
  2749. {
  2750. for (int N = 1; N <= 64; ++N)
  2751. {
  2752. Mat a(1, N, CV_32FC1, Scalar::all(1e-20));
  2753. Mat res;
  2754. magnitude(a, a, res);
  2755. EXPECT_LE(cvtest::norm(res, NORM_L1), 1e-15) << N;
  2756. }
  2757. }
  2758. PARAM_TEST_CASE(Core_CartPolar_reverse, int, bool)
  2759. {
  2760. int depth;
  2761. bool angleInDegrees;
  2762. virtual void SetUp()
  2763. {
  2764. depth = GET_PARAM(0);
  2765. angleInDegrees = GET_PARAM(1);
  2766. }
  2767. };
  2768. TEST_P(Core_CartPolar_reverse, reverse)
  2769. {
  2770. const int type = CV_MAKETYPE(depth, 1);
  2771. cv::Mat A[2] = {cv::Mat(10, 10, type), cv::Mat(10, 10, type)};
  2772. cv::Mat B[2], C[2];
  2773. cv::UMat uA[2];
  2774. cv::UMat uB[2];
  2775. cv::UMat uC[2];
  2776. for(int i = 0; i < 2; ++i)
  2777. {
  2778. cvtest::randUni(rng, A[i], Scalar::all(-1000), Scalar::all(1000));
  2779. A[i].copyTo(uA[i]);
  2780. }
  2781. // Reverse
  2782. cv::cartToPolar(A[0], A[1], B[0], B[1], angleInDegrees);
  2783. cv::polarToCart(B[0], B[1], C[0], C[1], angleInDegrees);
  2784. EXPECT_MAT_NEAR(A[0], C[0], 2);
  2785. EXPECT_MAT_NEAR(A[1], C[1], 2);
  2786. }
  2787. INSTANTIATE_TEST_CASE_P(Core_CartPolar, Core_CartPolar_reverse,
  2788. testing::Combine(
  2789. testing::Values(CV_32F, CV_64F),
  2790. testing::Values(false, true)
  2791. )
  2792. );
  2793. PARAM_TEST_CASE(Core_CartToPolar_inplace, int, bool)
  2794. {
  2795. int depth;
  2796. bool angleInDegrees;
  2797. virtual void SetUp()
  2798. {
  2799. depth = GET_PARAM(0);
  2800. angleInDegrees = GET_PARAM(1);
  2801. }
  2802. };
  2803. TEST_P(Core_CartToPolar_inplace, inplace)
  2804. {
  2805. const int type = CV_MAKETYPE(depth, 1);
  2806. cv::Mat A[2] = {cv::Mat(10, 10, type), cv::Mat(10, 10, type)};
  2807. cv::Mat B[2], C[2];
  2808. cv::UMat uA[2];
  2809. cv::UMat uB[2];
  2810. cv::UMat uC[2];
  2811. for(int i = 0; i < 2; ++i)
  2812. {
  2813. cvtest::randUni(rng, A[i], Scalar::all(-1000), Scalar::all(1000));
  2814. A[i].copyTo(uA[i]);
  2815. }
  2816. // Inplace x<->mag y<->angle
  2817. for(int i = 0; i < 2; ++i)
  2818. A[i].copyTo(B[i]);
  2819. cv::cartToPolar(A[0], A[1], C[0], C[1], angleInDegrees);
  2820. cv::cartToPolar(B[0], B[1], B[0], B[1], angleInDegrees);
  2821. EXPECT_MAT_NEAR(C[0], B[0], 2);
  2822. EXPECT_MAT_NEAR(C[1], B[1], 2);
  2823. // Inplace x<->angle y<->mag
  2824. for(int i = 0; i < 2; ++i)
  2825. A[i].copyTo(B[i]);
  2826. cv::cartToPolar(A[0], A[1], C[0], C[1], angleInDegrees);
  2827. cv::cartToPolar(B[0], B[1], B[1], B[0], angleInDegrees);
  2828. EXPECT_MAT_NEAR(C[0], B[1], 2);
  2829. EXPECT_MAT_NEAR(C[1], B[0], 2);
  2830. // Inplace OCL x<->mag y<->angle
  2831. for(int i = 0; i < 2; ++i)
  2832. uA[i].copyTo(uB[i]);
  2833. cv::cartToPolar(uA[0], uA[1], uC[0], uC[1], angleInDegrees);
  2834. cv::cartToPolar(uB[0], uB[1], uB[0], uB[1], angleInDegrees);
  2835. EXPECT_MAT_NEAR(uC[0], uB[0], 2);
  2836. EXPECT_MAT_NEAR(uC[1], uB[1], 2);
  2837. // Inplace OCL x<->angle y<->mag
  2838. for(int i = 0; i < 2; ++i)
  2839. uA[i].copyTo(uB[i]);
  2840. cv::cartToPolar(uA[0], uA[1], uC[0], uC[1], angleInDegrees);
  2841. cv::cartToPolar(uB[0], uB[1], uB[1], uB[0], angleInDegrees);
  2842. EXPECT_MAT_NEAR(uC[0], uB[1], 2);
  2843. EXPECT_MAT_NEAR(uC[1], uB[0], 2);
  2844. }
  2845. INSTANTIATE_TEST_CASE_P(Core_CartPolar, Core_CartToPolar_inplace,
  2846. testing::Combine(
  2847. testing::Values(CV_32F, CV_64F),
  2848. testing::Values(false, true)
  2849. )
  2850. );
  2851. PARAM_TEST_CASE(Core_PolarToCart_inplace, int, bool, bool)
  2852. {
  2853. int depth;
  2854. bool angleInDegrees;
  2855. bool implicitMagnitude;
  2856. virtual void SetUp()
  2857. {
  2858. depth = GET_PARAM(0);
  2859. angleInDegrees = GET_PARAM(1);
  2860. implicitMagnitude = GET_PARAM(2);
  2861. }
  2862. };
  2863. TEST_P(Core_PolarToCart_inplace, inplace)
  2864. {
  2865. const int type = CV_MAKETYPE(depth, 1);
  2866. cv::Mat A[2] = {cv::Mat(10, 10, type), cv::Mat(10, 10, type)};
  2867. cv::Mat B[2], C[2];
  2868. cv::UMat uA[2];
  2869. cv::UMat uB[2];
  2870. cv::UMat uC[2];
  2871. for(int i = 0; i < 2; ++i)
  2872. {
  2873. cvtest::randUni(rng, A[i], Scalar::all(-1000), Scalar::all(1000));
  2874. A[i].copyTo(uA[i]);
  2875. }
  2876. // Inplace OCL x<->mag y<->angle
  2877. for(int i = 0; i < 2; ++i)
  2878. A[i].copyTo(B[i]);
  2879. cv::polarToCart(implicitMagnitude ? cv::noArray() : A[0], A[1], C[0], C[1], angleInDegrees);
  2880. cv::polarToCart(implicitMagnitude ? cv::noArray() : B[0], B[1], B[0], B[1], angleInDegrees);
  2881. EXPECT_MAT_NEAR(C[0], B[0], 2);
  2882. EXPECT_MAT_NEAR(C[1], B[1], 2);
  2883. // Inplace OCL x<->angle y<->mag
  2884. for(int i = 0; i < 2; ++i)
  2885. A[i].copyTo(B[i]);
  2886. cv::polarToCart(implicitMagnitude ? cv::noArray() : A[0], A[1], C[0], C[1], angleInDegrees);
  2887. cv::polarToCart(implicitMagnitude ? cv::noArray() : B[0], B[1], B[1], B[0], angleInDegrees);
  2888. EXPECT_MAT_NEAR(C[0], B[1], 2);
  2889. EXPECT_MAT_NEAR(C[1], B[0], 2);
  2890. // Inplace OCL x<->mag y<->angle
  2891. for(int i = 0; i < 2; ++i)
  2892. uA[i].copyTo(uB[i]);
  2893. cv::polarToCart(implicitMagnitude ? cv::noArray() : uA[0], uA[1], uC[0], uC[1], angleInDegrees);
  2894. cv::polarToCart(implicitMagnitude ? cv::noArray() : uB[0], uB[1], uB[0], uB[1], angleInDegrees);
  2895. EXPECT_MAT_NEAR(uC[0], uB[0], 2);
  2896. EXPECT_MAT_NEAR(uC[1], uB[1], 2);
  2897. // Inplace OCL x<->angle y<->mag
  2898. for(int i = 0; i < 2; ++i)
  2899. uA[i].copyTo(uB[i]);
  2900. cv::polarToCart(implicitMagnitude ? cv::noArray() : uA[0], uA[1], uC[0], uC[1], angleInDegrees);
  2901. cv::polarToCart(implicitMagnitude ? cv::noArray() : uB[0], uB[1], uB[1], uB[0], angleInDegrees);
  2902. EXPECT_MAT_NEAR(uC[0], uB[1], 2);
  2903. EXPECT_MAT_NEAR(uC[1], uB[0], 2);
  2904. }
  2905. INSTANTIATE_TEST_CASE_P(Core_CartPolar, Core_PolarToCart_inplace,
  2906. testing::Combine(
  2907. testing::Values(CV_32F, CV_64F),
  2908. testing::Values(false, true),
  2909. testing::Values(true, false)
  2910. )
  2911. );
  2912. CV_ENUM(LutIdxType, CV_8U, CV_8S, CV_16U, CV_16S)
  2913. CV_ENUM(LutMatType, CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F, CV_16F)
  2914. struct Core_LUT: public testing::TestWithParam< std::tuple<LutIdxType, LutMatType> >
  2915. {
  2916. template<typename Ti, typename T, int ch, bool same_cn>
  2917. cv::Mat referenceWithType(cv::Mat input, cv::Mat table)
  2918. {
  2919. cv::Mat ref(input.size(), CV_MAKE_TYPE(table.depth(), ch));
  2920. for (int i = 0; i < input.rows; i++)
  2921. {
  2922. for (int j = 0; j < input.cols; j++)
  2923. {
  2924. if(ch == 1)
  2925. {
  2926. ref.at<T>(i, j) = table.at<T>(input.at<Ti>(i, j));
  2927. }
  2928. else
  2929. {
  2930. Vec<T, ch> val;
  2931. for (int k = 0; k < ch; k++)
  2932. {
  2933. if (same_cn)
  2934. {
  2935. val[k] = table.at<Vec<T, ch>>(input.at<Vec<Ti, ch>>(i, j)[k])[k];
  2936. }
  2937. else
  2938. {
  2939. val[k] = table.at<T>(input.at<Vec<Ti, ch>>(i, j)[k]);
  2940. }
  2941. }
  2942. ref.at<Vec<T, ch>>(i, j) = val;
  2943. }
  2944. }
  2945. }
  2946. return ref;
  2947. }
  2948. template<int ch = 1, bool same_cn = false>
  2949. cv::Mat reference(cv::Mat input, cv::Mat table)
  2950. {
  2951. cv::Mat ret = cv::Mat();
  2952. if ((input.depth() == CV_8U) || (input.depth() == CV_8S)) // Index type for LUT operation
  2953. {
  2954. switch(table.depth()) // Value type for LUT operation
  2955. {
  2956. case CV_8U: ret = referenceWithType<uint8_t, uint8_t, ch, same_cn>(input, table); break;
  2957. case CV_8S: ret = referenceWithType<uint8_t, int8_t, ch, same_cn>(input, table); break;
  2958. case CV_16U: ret = referenceWithType<uint8_t, uint16_t, ch, same_cn>(input, table); break;
  2959. case CV_16S: ret = referenceWithType<uint8_t, int16_t, ch, same_cn>(input, table); break;
  2960. case CV_32S: ret = referenceWithType<uint8_t, int32_t, ch, same_cn>(input, table); break;
  2961. case CV_32F: ret = referenceWithType<uint8_t, float, ch, same_cn>(input, table); break;
  2962. case CV_64F: ret = referenceWithType<uint8_t, double, ch, same_cn>(input, table); break;
  2963. case CV_16F: ret = referenceWithType<uint8_t, uint16_t, ch, same_cn>(input, table); break;
  2964. default: ret = cv::Mat(); break;
  2965. }
  2966. }
  2967. else if ((input.depth() == CV_16U) || (input.depth() == CV_16S))
  2968. {
  2969. switch(table.depth()) // Value type for LUT operation
  2970. {
  2971. case CV_8U: ret = referenceWithType<uint16_t, uint8_t, ch, same_cn>(input, table); break;
  2972. case CV_8S: ret = referenceWithType<uint16_t, int8_t, ch, same_cn>(input, table); break;
  2973. case CV_16U: ret = referenceWithType<uint16_t, uint16_t, ch, same_cn>(input, table); break;
  2974. case CV_16S: ret = referenceWithType<uint16_t, int16_t, ch, same_cn>(input, table); break;
  2975. case CV_32S: ret = referenceWithType<uint16_t, int32_t, ch, same_cn>(input, table); break;
  2976. case CV_32F: ret = referenceWithType<uint16_t, float, ch, same_cn>(input, table); break;
  2977. case CV_64F: ret = referenceWithType<uint16_t, double, ch, same_cn>(input, table); break;
  2978. case CV_16F: ret = referenceWithType<uint16_t, uint16_t, ch, same_cn>(input, table); break;
  2979. default: ret = cv::Mat(); break;
  2980. }
  2981. }
  2982. return ret;
  2983. }
  2984. };
  2985. TEST_P(Core_LUT, accuracy)
  2986. {
  2987. int idx_type = get<0>(GetParam());
  2988. int value_type = get<1>(GetParam());
  2989. ASSERT_TRUE((idx_type == CV_8U) || (idx_type == CV_8S) || (idx_type == CV_16U ) || (idx_type == CV_16S));
  2990. const int tableSize = ((idx_type == CV_8U) || (idx_type == CV_8S)) ? 256: 65536;
  2991. cv::Mat input(117, 113, CV_MAKE_TYPE(idx_type, 1));
  2992. randu(input, getMinVal(idx_type), getMaxVal(idx_type));
  2993. cv::Mat table(1, tableSize, CV_MAKE_TYPE(value_type, 1));
  2994. randu(table, getMinVal(value_type), getMaxVal(value_type));
  2995. cv::Mat output;
  2996. ASSERT_NO_THROW(cv::LUT(input, table, output));
  2997. ASSERT_FALSE(output.empty());
  2998. cv::Mat gt = reference(input, table);
  2999. ASSERT_FALSE(gt.empty());
  3000. ASSERT_EQ(0, cv::norm(output, gt, cv::NORM_INF));
  3001. }
  3002. TEST_P(Core_LUT, accuracy_multi)
  3003. {
  3004. int idx_type = get<0>(GetParam());
  3005. int value_type = get<1>(GetParam());
  3006. ASSERT_TRUE((idx_type == CV_8U) || (idx_type == CV_8S) || (idx_type == CV_16U) || (idx_type == CV_16S));
  3007. const int tableSize = ((idx_type == CV_8U) || (idx_type == CV_8S) ) ? 256: 65536;
  3008. cv::Mat input(117, 113, CV_MAKE_TYPE(idx_type, 3));
  3009. randu(input, getMinVal(idx_type), getMaxVal(idx_type));
  3010. cv::Mat table(1, tableSize, CV_MAKE_TYPE(value_type, 1));
  3011. randu(table, getMinVal(value_type), getMaxVal(value_type));
  3012. cv::Mat output;
  3013. ASSERT_NO_THROW(cv::LUT(input, table, output));
  3014. ASSERT_FALSE(output.empty());
  3015. cv::Mat gt = reference<3>(input, table);
  3016. ASSERT_FALSE(gt.empty());
  3017. ASSERT_EQ(0, cv::norm(output, gt, cv::NORM_INF));
  3018. }
  3019. TEST_P(Core_LUT, accuracy_multi2)
  3020. {
  3021. int idx_type = get<0>(GetParam());
  3022. int value_type = get<1>(GetParam());
  3023. ASSERT_TRUE((idx_type == CV_8U) || (idx_type == CV_8S) || (idx_type == CV_16U) || (idx_type == CV_16S));
  3024. const int tableSize = ((idx_type == CV_8U) || (idx_type == CV_8S)) ? 256: 65536;
  3025. cv::Mat input(117, 113, CV_MAKE_TYPE(idx_type, 3));
  3026. randu(input, getMinVal(idx_type), getMaxVal(idx_type));
  3027. cv::Mat table(1, tableSize, CV_MAKE_TYPE(value_type, 3));
  3028. randu(table, getMinVal(value_type), getMaxVal(value_type));
  3029. cv::Mat output;
  3030. ASSERT_NO_THROW(cv::LUT(input, table, output));
  3031. ASSERT_FALSE(output.empty());
  3032. cv::Mat gt = reference<3, true>(input, table);
  3033. ASSERT_FALSE(gt.empty());
  3034. ASSERT_EQ(0, cv::norm(output, gt, cv::NORM_INF));
  3035. }
  3036. INSTANTIATE_TEST_CASE_P(/**/, Core_LUT, testing::Combine( LutIdxType::all(), LutMatType::all()));
  3037. }} // namespace