test_element_operations.cpp 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #include "test_precomp.hpp"
  43. #ifdef HAVE_CUDA
  44. namespace opencv_test { namespace {
  45. ////////////////////////////////////////////////////////////////////////////////
  46. // Add_Array
  47. PARAM_TEST_CASE(Add_Array, cv::cuda::DeviceInfo, cv::Size, std::pair<MatDepth, MatDepth>, Channels, UseRoi)
  48. {
  49. cv::cuda::DeviceInfo devInfo;
  50. cv::Size size;
  51. std::pair<MatDepth, MatDepth> depth;
  52. int channels;
  53. bool useRoi;
  54. int stype;
  55. int dtype;
  56. virtual void SetUp()
  57. {
  58. devInfo = GET_PARAM(0);
  59. size = GET_PARAM(1);
  60. depth = GET_PARAM(2);
  61. channels = GET_PARAM(3);
  62. useRoi = GET_PARAM(4);
  63. cv::cuda::setDevice(devInfo.deviceID());
  64. stype = CV_MAKE_TYPE(depth.first, channels);
  65. dtype = CV_MAKE_TYPE(depth.second, channels);
  66. }
  67. };
  68. CUDA_TEST_P(Add_Array, Accuracy)
  69. {
  70. cv::Mat mat1 = randomMat(size, stype);
  71. cv::Mat mat2 = randomMat(size, stype);
  72. if ((depth.first == CV_64F || depth.second == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  73. {
  74. try
  75. {
  76. cv::cuda::GpuMat dst;
  77. cv::cuda::add(loadMat(mat1), loadMat(mat2), dst, cv::cuda::GpuMat(), depth.second);
  78. }
  79. catch (const cv::Exception& e)
  80. {
  81. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  82. }
  83. }
  84. else
  85. {
  86. cv::cuda::GpuMat dst = createMat(size, dtype, useRoi);
  87. dst.setTo(cv::Scalar::all(0));
  88. cv::cuda::add(loadMat(mat1, useRoi), loadMat(mat2, useRoi), dst, cv::cuda::GpuMat(), depth.second);
  89. cv::Mat dst_gold(size, dtype, cv::Scalar::all(0));
  90. cv::add(mat1, mat2, dst_gold, cv::noArray(), depth.second);
  91. EXPECT_MAT_NEAR(dst_gold, dst, depth.first >= CV_32F || depth.second >= CV_32F ? 1e-4 : 0.0);
  92. }
  93. }
  94. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Add_Array, testing::Combine(
  95. ALL_DEVICES,
  96. DIFFERENT_SIZES,
  97. DEPTH_PAIRS,
  98. ALL_CHANNELS,
  99. WHOLE_SUBMAT));
  100. PARAM_TEST_CASE(Add_Array_Mask, cv::cuda::DeviceInfo, cv::Size, std::pair<MatDepth, MatDepth>, UseRoi)
  101. {
  102. cv::cuda::DeviceInfo devInfo;
  103. cv::Size size;
  104. std::pair<MatDepth, MatDepth> depth;
  105. bool useRoi;
  106. int stype;
  107. int dtype;
  108. virtual void SetUp()
  109. {
  110. devInfo = GET_PARAM(0);
  111. size = GET_PARAM(1);
  112. depth = GET_PARAM(2);
  113. useRoi = GET_PARAM(3);
  114. cv::cuda::setDevice(devInfo.deviceID());
  115. stype = CV_MAKE_TYPE(depth.first, 1);
  116. dtype = CV_MAKE_TYPE(depth.second, 1);
  117. }
  118. };
  119. CUDA_TEST_P(Add_Array_Mask, Accuracy)
  120. {
  121. cv::Mat mat1 = randomMat(size, stype);
  122. cv::Mat mat2 = randomMat(size, stype);
  123. cv::Mat mask = randomMat(size, CV_8UC1, 0, 2);
  124. if ((depth.first == CV_64F || depth.second == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  125. {
  126. try
  127. {
  128. cv::cuda::GpuMat dst;
  129. cv::cuda::add(loadMat(mat1), loadMat(mat2), dst, cv::cuda::GpuMat(), depth.second);
  130. }
  131. catch (const cv::Exception& e)
  132. {
  133. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  134. }
  135. }
  136. else
  137. {
  138. cv::cuda::GpuMat dst = createMat(size, dtype, useRoi);
  139. dst.setTo(cv::Scalar::all(0));
  140. cv::cuda::add(loadMat(mat1, useRoi), loadMat(mat2, useRoi), dst, loadMat(mask, useRoi), depth.second);
  141. cv::Mat dst_gold(size, dtype, cv::Scalar::all(0));
  142. cv::add(mat1, mat2, dst_gold, mask, depth.second);
  143. EXPECT_MAT_NEAR(dst_gold, dst, depth.first >= CV_32F || depth.second >= CV_32F ? 1e-4 : 0.0);
  144. }
  145. }
  146. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Add_Array_Mask, testing::Combine(
  147. ALL_DEVICES,
  148. DIFFERENT_SIZES,
  149. DEPTH_PAIRS,
  150. WHOLE_SUBMAT));
  151. ////////////////////////////////////////////////////////////////////////////////
  152. // Add_Scalar
  153. PARAM_TEST_CASE(Add_Scalar, cv::cuda::DeviceInfo, cv::Size, std::pair<MatDepth, MatDepth>, UseRoi)
  154. {
  155. cv::cuda::DeviceInfo devInfo;
  156. cv::Size size;
  157. std::pair<MatDepth, MatDepth> depth;
  158. bool useRoi;
  159. virtual void SetUp()
  160. {
  161. devInfo = GET_PARAM(0);
  162. size = GET_PARAM(1);
  163. depth = GET_PARAM(2);
  164. useRoi = GET_PARAM(3);
  165. cv::cuda::setDevice(devInfo.deviceID());
  166. }
  167. };
  168. CUDA_TEST_P(Add_Scalar, WithOutMask)
  169. {
  170. cv::Mat mat = randomMat(size, depth.first);
  171. cv::Scalar val = randomScalar(0, 255);
  172. if ((depth.first == CV_64F || depth.second == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  173. {
  174. try
  175. {
  176. cv::cuda::GpuMat dst;
  177. cv::cuda::add(loadMat(mat), val, dst, cv::cuda::GpuMat(), depth.second);
  178. }
  179. catch (const cv::Exception& e)
  180. {
  181. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  182. }
  183. }
  184. else
  185. {
  186. cv::cuda::GpuMat dst = createMat(size, depth.second, useRoi);
  187. dst.setTo(cv::Scalar::all(0));
  188. cv::cuda::add(loadMat(mat, useRoi), val, dst, cv::cuda::GpuMat(), depth.second);
  189. cv::Mat dst_gold(size, depth.second, cv::Scalar::all(0));
  190. cv::add(mat, val, dst_gold, cv::noArray(), depth.second);
  191. EXPECT_MAT_NEAR(dst_gold, dst, depth.first >= CV_32F || depth.second >= CV_32F ? 1e-4 : 1.0);
  192. }
  193. }
  194. CUDA_TEST_P(Add_Scalar, WithMask)
  195. {
  196. cv::Mat mat = randomMat(size, depth.first);
  197. cv::Scalar val = randomScalar(0, 255);
  198. cv::Mat mask = randomMat(size, CV_8UC1, 0.0, 2.0);
  199. if ((depth.first == CV_64F || depth.second == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  200. {
  201. try
  202. {
  203. cv::cuda::GpuMat dst;
  204. cv::cuda::add(loadMat(mat), val, dst, cv::cuda::GpuMat(), depth.second);
  205. }
  206. catch (const cv::Exception& e)
  207. {
  208. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  209. }
  210. }
  211. else
  212. {
  213. cv::cuda::GpuMat dst = createMat(size, depth.second, useRoi);
  214. dst.setTo(cv::Scalar::all(0));
  215. cv::cuda::add(loadMat(mat, useRoi), val, dst, loadMat(mask, useRoi), depth.second);
  216. cv::Mat dst_gold(size, depth.second, cv::Scalar::all(0));
  217. cv::add(mat, val, dst_gold, mask, depth.second);
  218. EXPECT_MAT_NEAR(dst_gold, dst, depth.first >= CV_32F || depth.second >= CV_32F ? 1e-4 : 1.0);
  219. }
  220. }
  221. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Add_Scalar, testing::Combine(
  222. ALL_DEVICES,
  223. DIFFERENT_SIZES,
  224. DEPTH_PAIRS,
  225. WHOLE_SUBMAT));
  226. ////////////////////////////////////////////////////////////////////////////////
  227. // Add_Scalar_First
  228. PARAM_TEST_CASE(Add_Scalar_First, cv::cuda::DeviceInfo, cv::Size, std::pair<MatDepth, MatDepth>, UseRoi)
  229. {
  230. cv::cuda::DeviceInfo devInfo;
  231. cv::Size size;
  232. std::pair<MatDepth, MatDepth> depth;
  233. bool useRoi;
  234. virtual void SetUp()
  235. {
  236. devInfo = GET_PARAM(0);
  237. size = GET_PARAM(1);
  238. depth = GET_PARAM(2);
  239. useRoi = GET_PARAM(3);
  240. cv::cuda::setDevice(devInfo.deviceID());
  241. }
  242. };
  243. CUDA_TEST_P(Add_Scalar_First, WithOutMask)
  244. {
  245. cv::Mat mat = randomMat(size, depth.first);
  246. cv::Scalar val = randomScalar(0, 255);
  247. if ((depth.first == CV_64F || depth.second == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  248. {
  249. try
  250. {
  251. cv::cuda::GpuMat dst;
  252. cv::cuda::add(val, loadMat(mat), dst, cv::cuda::GpuMat(), depth.second);
  253. }
  254. catch (const cv::Exception& e)
  255. {
  256. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  257. }
  258. }
  259. else
  260. {
  261. cv::cuda::GpuMat dst = createMat(size, depth.second, useRoi);
  262. dst.setTo(cv::Scalar::all(0));
  263. cv::cuda::add(val, loadMat(mat, useRoi), dst, cv::cuda::GpuMat(), depth.second);
  264. cv::Mat dst_gold(size, depth.second, cv::Scalar::all(0));
  265. cv::add(val, mat, dst_gold, cv::noArray(), depth.second);
  266. EXPECT_MAT_NEAR(dst_gold, dst, depth.first >= CV_32F || depth.second >= CV_32F ? 1e-4 : 0.0);
  267. }
  268. }
  269. CUDA_TEST_P(Add_Scalar_First, WithMask)
  270. {
  271. cv::Mat mat = randomMat(size, depth.first);
  272. cv::Scalar val = randomScalar(0, 255);
  273. cv::Mat mask = randomMat(size, CV_8UC1, 0.0, 2.0);
  274. if ((depth.first == CV_64F || depth.second == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  275. {
  276. try
  277. {
  278. cv::cuda::GpuMat dst;
  279. cv::cuda::add(val, loadMat(mat), dst, cv::cuda::GpuMat(), depth.second);
  280. }
  281. catch (const cv::Exception& e)
  282. {
  283. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  284. }
  285. }
  286. else
  287. {
  288. cv::cuda::GpuMat dst = createMat(size, depth.second, useRoi);
  289. dst.setTo(cv::Scalar::all(0));
  290. cv::cuda::add(val, loadMat(mat, useRoi), dst, loadMat(mask, useRoi), depth.second);
  291. cv::Mat dst_gold(size, depth.second, cv::Scalar::all(0));
  292. cv::add(val, mat, dst_gold, mask, depth.second);
  293. EXPECT_MAT_NEAR(dst_gold, dst, depth.first >= CV_32F || depth.second >= CV_32F ? 1e-4 : 0.0);
  294. }
  295. }
  296. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Add_Scalar_First, testing::Combine(
  297. ALL_DEVICES,
  298. DIFFERENT_SIZES,
  299. DEPTH_PAIRS,
  300. WHOLE_SUBMAT));
  301. ////////////////////////////////////////////////////////////////////////////////
  302. // Subtract_Array
  303. PARAM_TEST_CASE(Subtract_Array, cv::cuda::DeviceInfo, cv::Size, std::pair<MatDepth, MatDepth>, Channels, UseRoi)
  304. {
  305. cv::cuda::DeviceInfo devInfo;
  306. cv::Size size;
  307. std::pair<MatDepth, MatDepth> depth;
  308. int channels;
  309. bool useRoi;
  310. int stype;
  311. int dtype;
  312. virtual void SetUp()
  313. {
  314. devInfo = GET_PARAM(0);
  315. size = GET_PARAM(1);
  316. depth = GET_PARAM(2);
  317. channels = GET_PARAM(3);
  318. useRoi = GET_PARAM(4);
  319. cv::cuda::setDevice(devInfo.deviceID());
  320. stype = CV_MAKE_TYPE(depth.first, channels);
  321. dtype = CV_MAKE_TYPE(depth.second, channels);
  322. }
  323. };
  324. CUDA_TEST_P(Subtract_Array, Accuracy)
  325. {
  326. cv::Mat mat1 = randomMat(size, stype);
  327. cv::Mat mat2 = randomMat(size, stype);
  328. if ((depth.first == CV_64F || depth.second == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  329. {
  330. try
  331. {
  332. cv::cuda::GpuMat dst;
  333. cv::cuda::subtract(loadMat(mat1), loadMat(mat2), dst, cv::cuda::GpuMat(), depth.second);
  334. }
  335. catch (const cv::Exception& e)
  336. {
  337. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  338. }
  339. }
  340. else
  341. {
  342. cv::cuda::GpuMat dst = createMat(size, dtype, useRoi);
  343. dst.setTo(cv::Scalar::all(0));
  344. cv::cuda::subtract(loadMat(mat1, useRoi), loadMat(mat2, useRoi), dst, cv::cuda::GpuMat(), depth.second);
  345. cv::Mat dst_gold(size, dtype, cv::Scalar::all(0));
  346. cv::subtract(mat1, mat2, dst_gold, cv::noArray(), depth.second);
  347. EXPECT_MAT_NEAR(dst_gold, dst, depth.first >= CV_32F || depth.second >= CV_32F ? 1e-4 : 0.0);
  348. }
  349. }
  350. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Subtract_Array, testing::Combine(
  351. ALL_DEVICES,
  352. DIFFERENT_SIZES,
  353. DEPTH_PAIRS,
  354. ALL_CHANNELS,
  355. WHOLE_SUBMAT));
  356. PARAM_TEST_CASE(Subtract_Array_Mask, cv::cuda::DeviceInfo, cv::Size, std::pair<MatDepth, MatDepth>, UseRoi)
  357. {
  358. cv::cuda::DeviceInfo devInfo;
  359. cv::Size size;
  360. std::pair<MatDepth, MatDepth> depth;
  361. bool useRoi;
  362. int stype;
  363. int dtype;
  364. virtual void SetUp()
  365. {
  366. devInfo = GET_PARAM(0);
  367. size = GET_PARAM(1);
  368. depth = GET_PARAM(2);
  369. useRoi = GET_PARAM(3);
  370. cv::cuda::setDevice(devInfo.deviceID());
  371. stype = CV_MAKE_TYPE(depth.first, 1);
  372. dtype = CV_MAKE_TYPE(depth.second, 1);
  373. }
  374. };
  375. CUDA_TEST_P(Subtract_Array_Mask, Accuracy)
  376. {
  377. cv::Mat mat1 = randomMat(size, stype);
  378. cv::Mat mat2 = randomMat(size, stype);
  379. cv::Mat mask = randomMat(size, CV_8UC1, 0.0, 2.0);
  380. if ((depth.first == CV_64F || depth.second == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  381. {
  382. try
  383. {
  384. cv::cuda::GpuMat dst;
  385. cv::cuda::subtract(loadMat(mat1), loadMat(mat2), dst, cv::cuda::GpuMat(), depth.second);
  386. }
  387. catch (const cv::Exception& e)
  388. {
  389. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  390. }
  391. }
  392. else
  393. {
  394. cv::cuda::GpuMat dst = createMat(size, dtype, useRoi);
  395. dst.setTo(cv::Scalar::all(0));
  396. cv::cuda::subtract(loadMat(mat1, useRoi), loadMat(mat2, useRoi), dst, loadMat(mask, useRoi), depth.second);
  397. cv::Mat dst_gold(size, dtype, cv::Scalar::all(0));
  398. cv::subtract(mat1, mat2, dst_gold, mask, depth.second);
  399. EXPECT_MAT_NEAR(dst_gold, dst, depth.first >= CV_32F || depth.second >= CV_32F ? 1e-4 : 0.0);
  400. }
  401. }
  402. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Subtract_Array_Mask, testing::Combine(
  403. ALL_DEVICES,
  404. DIFFERENT_SIZES,
  405. DEPTH_PAIRS,
  406. WHOLE_SUBMAT));
  407. ////////////////////////////////////////////////////////////////////////////////
  408. // Subtract_Scalar
  409. PARAM_TEST_CASE(Subtract_Scalar, cv::cuda::DeviceInfo, cv::Size, std::pair<MatDepth, MatDepth>, UseRoi)
  410. {
  411. cv::cuda::DeviceInfo devInfo;
  412. cv::Size size;
  413. std::pair<MatDepth, MatDepth> depth;
  414. bool useRoi;
  415. virtual void SetUp()
  416. {
  417. devInfo = GET_PARAM(0);
  418. size = GET_PARAM(1);
  419. depth = GET_PARAM(2);
  420. useRoi = GET_PARAM(3);
  421. cv::cuda::setDevice(devInfo.deviceID());
  422. }
  423. };
  424. CUDA_TEST_P(Subtract_Scalar, WithOutMask)
  425. {
  426. cv::Mat mat = randomMat(size, depth.first);
  427. cv::Scalar val = randomScalar(0, 255);
  428. if ((depth.first == CV_64F || depth.second == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  429. {
  430. try
  431. {
  432. cv::cuda::GpuMat dst;
  433. cv::cuda::subtract(loadMat(mat), val, dst, cv::cuda::GpuMat(), depth.second);
  434. }
  435. catch (const cv::Exception& e)
  436. {
  437. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  438. }
  439. }
  440. else
  441. {
  442. cv::cuda::GpuMat dst = createMat(size, depth.second, useRoi);
  443. dst.setTo(cv::Scalar::all(0));
  444. cv::cuda::subtract(loadMat(mat, useRoi), val, dst, cv::cuda::GpuMat(), depth.second);
  445. cv::Mat dst_gold(size, depth.second, cv::Scalar::all(0));
  446. cv::subtract(mat, val, dst_gold, cv::noArray(), depth.second);
  447. EXPECT_MAT_NEAR(dst_gold, dst, depth.first >= CV_32F || depth.second >= CV_32F ? 1e-4 : 1.0);
  448. }
  449. }
  450. CUDA_TEST_P(Subtract_Scalar, WithMask)
  451. {
  452. cv::Mat mat = randomMat(size, depth.first);
  453. cv::Scalar val = randomScalar(0, 255);
  454. cv::Mat mask = randomMat(size, CV_8UC1, 0.0, 2.0);
  455. if ((depth.first == CV_64F || depth.second == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  456. {
  457. try
  458. {
  459. cv::cuda::GpuMat dst;
  460. cv::cuda::subtract(loadMat(mat), val, dst, cv::cuda::GpuMat(), depth.second);
  461. }
  462. catch (const cv::Exception& e)
  463. {
  464. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  465. }
  466. }
  467. else
  468. {
  469. cv::cuda::GpuMat dst = createMat(size, depth.second, useRoi);
  470. dst.setTo(cv::Scalar::all(0));
  471. cv::cuda::subtract(loadMat(mat, useRoi), val, dst, loadMat(mask, useRoi), depth.second);
  472. cv::Mat dst_gold(size, depth.second, cv::Scalar::all(0));
  473. cv::subtract(mat, val, dst_gold, mask, depth.second);
  474. EXPECT_MAT_NEAR(dst_gold, dst, depth.first >= CV_32F || depth.second >= CV_32F ? 1e-4 : 1.0);
  475. }
  476. }
  477. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Subtract_Scalar, testing::Combine(
  478. ALL_DEVICES,
  479. DIFFERENT_SIZES,
  480. DEPTH_PAIRS,
  481. WHOLE_SUBMAT));
  482. ////////////////////////////////////////////////////////////////////////////////
  483. // Subtract_Scalar_First
  484. PARAM_TEST_CASE(Subtract_Scalar_First, cv::cuda::DeviceInfo, cv::Size, std::pair<MatDepth, MatDepth>, UseRoi)
  485. {
  486. cv::cuda::DeviceInfo devInfo;
  487. cv::Size size;
  488. std::pair<MatDepth, MatDepth> depth;
  489. bool useRoi;
  490. virtual void SetUp()
  491. {
  492. devInfo = GET_PARAM(0);
  493. size = GET_PARAM(1);
  494. depth = GET_PARAM(2);
  495. useRoi = GET_PARAM(3);
  496. cv::cuda::setDevice(devInfo.deviceID());
  497. }
  498. };
  499. CUDA_TEST_P(Subtract_Scalar_First, WithOutMask)
  500. {
  501. cv::Mat mat = randomMat(size, depth.first);
  502. cv::Scalar val = randomScalar(0, 255);
  503. if ((depth.first == CV_64F || depth.second == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  504. {
  505. try
  506. {
  507. cv::cuda::GpuMat dst;
  508. cv::cuda::subtract(val, loadMat(mat), dst, cv::cuda::GpuMat(), depth.second);
  509. }
  510. catch (const cv::Exception& e)
  511. {
  512. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  513. }
  514. }
  515. else
  516. {
  517. cv::cuda::GpuMat dst = createMat(size, depth.second, useRoi);
  518. dst.setTo(cv::Scalar::all(0));
  519. cv::cuda::subtract(val, loadMat(mat, useRoi), dst, cv::cuda::GpuMat(), depth.second);
  520. cv::Mat dst_gold(size, depth.second, cv::Scalar::all(0));
  521. cv::subtract(val, mat, dst_gold, cv::noArray(), depth.second);
  522. EXPECT_MAT_NEAR(dst_gold, dst, depth.first >= CV_32F || depth.second >= CV_32F ? 1e-4 : 0.0);
  523. }
  524. }
  525. CUDA_TEST_P(Subtract_Scalar_First, WithMask)
  526. {
  527. cv::Mat mat = randomMat(size, depth.first);
  528. cv::Scalar val = randomScalar(0, 255);
  529. cv::Mat mask = randomMat(size, CV_8UC1, 0.0, 2.0);
  530. if ((depth.first == CV_64F || depth.second == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  531. {
  532. try
  533. {
  534. cv::cuda::GpuMat dst;
  535. cv::cuda::subtract(val, loadMat(mat), dst, cv::cuda::GpuMat(), depth.second);
  536. }
  537. catch (const cv::Exception& e)
  538. {
  539. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  540. }
  541. }
  542. else
  543. {
  544. cv::cuda::GpuMat dst = createMat(size, depth.second, useRoi);
  545. dst.setTo(cv::Scalar::all(0));
  546. cv::cuda::subtract(val, loadMat(mat, useRoi), dst, loadMat(mask, useRoi), depth.second);
  547. cv::Mat dst_gold(size, depth.second, cv::Scalar::all(0));
  548. cv::subtract(val, mat, dst_gold, mask, depth.second);
  549. EXPECT_MAT_NEAR(dst_gold, dst, depth.first >= CV_32F || depth.second >= CV_32F ? 1e-4 : 0.0);
  550. }
  551. }
  552. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Subtract_Scalar_First, testing::Combine(
  553. ALL_DEVICES,
  554. DIFFERENT_SIZES,
  555. DEPTH_PAIRS,
  556. WHOLE_SUBMAT));
  557. ////////////////////////////////////////////////////////////////////////////////
  558. // Multiply_Array
  559. PARAM_TEST_CASE(Multiply_Array, cv::cuda::DeviceInfo, cv::Size, std::pair<MatDepth, MatDepth>, Channels, UseRoi)
  560. {
  561. cv::cuda::DeviceInfo devInfo;
  562. cv::Size size;
  563. std::pair<MatDepth, MatDepth> depth;
  564. int channels;
  565. bool useRoi;
  566. int stype;
  567. int dtype;
  568. virtual void SetUp()
  569. {
  570. devInfo = GET_PARAM(0);
  571. size = GET_PARAM(1);
  572. depth = GET_PARAM(2);
  573. channels = GET_PARAM(3);
  574. useRoi = GET_PARAM(4);
  575. cv::cuda::setDevice(devInfo.deviceID());
  576. stype = CV_MAKE_TYPE(depth.first, channels);
  577. dtype = CV_MAKE_TYPE(depth.second, channels);
  578. }
  579. };
  580. CUDA_TEST_P(Multiply_Array, WithOutScale)
  581. {
  582. cv::Mat mat1 = randomMat(size, stype);
  583. cv::Mat mat2 = randomMat(size, stype);
  584. if ((depth.first == CV_64F || depth.second == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  585. {
  586. try
  587. {
  588. cv::cuda::GpuMat dst;
  589. cv::cuda::multiply(loadMat(mat1), loadMat(mat2), dst, 1, depth.second);
  590. }
  591. catch (const cv::Exception& e)
  592. {
  593. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  594. }
  595. }
  596. else
  597. {
  598. cv::cuda::GpuMat dst = createMat(size, dtype, useRoi);
  599. cv::cuda::multiply(loadMat(mat1, useRoi), loadMat(mat2, useRoi), dst, 1, depth.second);
  600. cv::Mat dst_gold;
  601. cv::multiply(mat1, mat2, dst_gold, 1, depth.second);
  602. EXPECT_MAT_NEAR(dst_gold, dst, depth.first >= CV_32F || depth.second >= CV_32F ? 1e-2 : 0.0);
  603. }
  604. }
  605. CUDA_TEST_P(Multiply_Array, WithScale)
  606. {
  607. cv::Mat mat1 = randomMat(size, stype);
  608. cv::Mat mat2 = randomMat(size, stype);
  609. double scale = randomDouble(0.0, 255.0);
  610. if ((depth.first == CV_64F || depth.second == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  611. {
  612. try
  613. {
  614. cv::cuda::GpuMat dst;
  615. cv::cuda::multiply(loadMat(mat1), loadMat(mat2), dst, scale, depth.second);
  616. }
  617. catch (const cv::Exception& e)
  618. {
  619. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  620. }
  621. }
  622. else
  623. {
  624. cv::cuda::GpuMat dst = createMat(size, dtype, useRoi);
  625. cv::cuda::multiply(loadMat(mat1, useRoi), loadMat(mat2, useRoi), dst, scale, depth.second);
  626. cv::Mat dst_gold;
  627. cv::multiply(mat1, mat2, dst_gold, scale, depth.second);
  628. EXPECT_MAT_NEAR(dst_gold, dst, 2.0);
  629. }
  630. }
  631. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Multiply_Array, testing::Combine(
  632. ALL_DEVICES,
  633. DIFFERENT_SIZES,
  634. DEPTH_PAIRS,
  635. ALL_CHANNELS,
  636. WHOLE_SUBMAT));
  637. ////////////////////////////////////////////////////////////////////////////////
  638. // Multiply_Array_Special
  639. PARAM_TEST_CASE(Multiply_Array_Special, cv::cuda::DeviceInfo, cv::Size, UseRoi)
  640. {
  641. cv::cuda::DeviceInfo devInfo;
  642. cv::Size size;
  643. bool useRoi;
  644. virtual void SetUp()
  645. {
  646. devInfo = GET_PARAM(0);
  647. size = GET_PARAM(1);
  648. useRoi = GET_PARAM(2);
  649. cv::cuda::setDevice(devInfo.deviceID());
  650. }
  651. };
  652. CUDA_TEST_P(Multiply_Array_Special, Case_8UC4x_32FC1)
  653. {
  654. cv::Mat mat1 = randomMat(size, CV_8UC4);
  655. cv::Mat mat2 = randomMat(size, CV_32FC1);
  656. cv::cuda::GpuMat dst = createMat(size, CV_8UC4, useRoi);
  657. cv::cuda::multiply(loadMat(mat1, useRoi), loadMat(mat2, useRoi), dst);
  658. cv::Mat h_dst(dst);
  659. for (int y = 0; y < h_dst.rows; ++y)
  660. {
  661. const cv::Vec4b* mat1_row = mat1.ptr<cv::Vec4b>(y);
  662. const float* mat2_row = mat2.ptr<float>(y);
  663. const cv::Vec4b* dst_row = h_dst.ptr<cv::Vec4b>(y);
  664. for (int x = 0; x < h_dst.cols; ++x)
  665. {
  666. cv::Vec4b val1 = mat1_row[x];
  667. float val2 = mat2_row[x];
  668. cv::Vec4b actual = dst_row[x];
  669. cv::Vec4b gold;
  670. gold[0] = cv::saturate_cast<uchar>(val1[0] * val2);
  671. gold[1] = cv::saturate_cast<uchar>(val1[1] * val2);
  672. gold[2] = cv::saturate_cast<uchar>(val1[2] * val2);
  673. gold[3] = cv::saturate_cast<uchar>(val1[3] * val2);
  674. ASSERT_LE(std::abs(gold[0] - actual[0]), 1.0);
  675. ASSERT_LE(std::abs(gold[1] - actual[1]), 1.0);
  676. ASSERT_LE(std::abs(gold[1] - actual[1]), 1.0);
  677. ASSERT_LE(std::abs(gold[1] - actual[1]), 1.0);
  678. }
  679. }
  680. }
  681. CUDA_TEST_P(Multiply_Array_Special, Case_16SC4x_32FC1)
  682. {
  683. cv::Mat mat1 = randomMat(size, CV_16SC4);
  684. cv::Mat mat2 = randomMat(size, CV_32FC1);
  685. cv::cuda::GpuMat dst = createMat(size, CV_16SC4, useRoi);
  686. cv::cuda::multiply(loadMat(mat1, useRoi), loadMat(mat2, useRoi), dst);
  687. cv::Mat h_dst(dst);
  688. for (int y = 0; y < h_dst.rows; ++y)
  689. {
  690. const cv::Vec4s* mat1_row = mat1.ptr<cv::Vec4s>(y);
  691. const float* mat2_row = mat2.ptr<float>(y);
  692. const cv::Vec4s* dst_row = h_dst.ptr<cv::Vec4s>(y);
  693. for (int x = 0; x < h_dst.cols; ++x)
  694. {
  695. cv::Vec4s val1 = mat1_row[x];
  696. float val2 = mat2_row[x];
  697. cv::Vec4s actual = dst_row[x];
  698. cv::Vec4s gold;
  699. gold[0] = cv::saturate_cast<short>(val1[0] * val2);
  700. gold[1] = cv::saturate_cast<short>(val1[1] * val2);
  701. gold[2] = cv::saturate_cast<short>(val1[2] * val2);
  702. gold[3] = cv::saturate_cast<short>(val1[3] * val2);
  703. ASSERT_LE(std::abs(gold[0] - actual[0]), 1.0);
  704. ASSERT_LE(std::abs(gold[1] - actual[1]), 1.0);
  705. ASSERT_LE(std::abs(gold[1] - actual[1]), 1.0);
  706. ASSERT_LE(std::abs(gold[1] - actual[1]), 1.0);
  707. }
  708. }
  709. }
  710. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Multiply_Array_Special, testing::Combine(
  711. ALL_DEVICES,
  712. DIFFERENT_SIZES,
  713. WHOLE_SUBMAT));
  714. ////////////////////////////////////////////////////////////////////////////////
  715. // Multiply_Scalar
  716. PARAM_TEST_CASE(Multiply_Scalar, cv::cuda::DeviceInfo, cv::Size, std::pair<MatDepth, MatDepth>, UseRoi)
  717. {
  718. cv::cuda::DeviceInfo devInfo;
  719. cv::Size size;
  720. std::pair<MatDepth, MatDepth> depth;
  721. bool useRoi;
  722. virtual void SetUp()
  723. {
  724. devInfo = GET_PARAM(0);
  725. size = GET_PARAM(1);
  726. depth = GET_PARAM(2);
  727. useRoi = GET_PARAM(3);
  728. cv::cuda::setDevice(devInfo.deviceID());
  729. }
  730. };
  731. CUDA_TEST_P(Multiply_Scalar, WithOutScale)
  732. {
  733. cv::Mat mat = randomMat(size, depth.first);
  734. cv::Scalar val = randomScalar(0, 255);
  735. if ((depth.first == CV_64F || depth.second == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  736. {
  737. try
  738. {
  739. cv::cuda::GpuMat dst;
  740. cv::cuda::multiply(loadMat(mat), val, dst, 1, depth.second);
  741. }
  742. catch (const cv::Exception& e)
  743. {
  744. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  745. }
  746. }
  747. else
  748. {
  749. cv::cuda::GpuMat dst = createMat(size, depth.second, useRoi);
  750. cv::cuda::multiply(loadMat(mat, useRoi), val, dst, 1, depth.second);
  751. cv::Mat dst_gold;
  752. cv::multiply(mat, val, dst_gold, 1, depth.second);
  753. EXPECT_MAT_NEAR(dst_gold, dst, 1.0);
  754. }
  755. }
  756. CUDA_TEST_P(Multiply_Scalar, WithScale)
  757. {
  758. cv::Mat mat = randomMat(size, depth.first);
  759. cv::Scalar val = randomScalar(0, 255);
  760. double scale = randomDouble(0.0, 255.0);
  761. if ((depth.first == CV_64F || depth.second == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  762. {
  763. try
  764. {
  765. cv::cuda::GpuMat dst;
  766. cv::cuda::multiply(loadMat(mat), val, dst, scale, depth.second);
  767. }
  768. catch (const cv::Exception& e)
  769. {
  770. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  771. }
  772. }
  773. else
  774. {
  775. cv::cuda::GpuMat dst = createMat(size, depth.second, useRoi);
  776. cv::cuda::multiply(loadMat(mat, useRoi), val, dst, scale, depth.second);
  777. cv::Mat dst_gold;
  778. cv::multiply(mat, val, dst_gold, scale, depth.second);
  779. EXPECT_MAT_NEAR(dst_gold, dst, 1.0);
  780. }
  781. }
  782. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Multiply_Scalar, testing::Combine(
  783. ALL_DEVICES,
  784. DIFFERENT_SIZES,
  785. DEPTH_PAIRS,
  786. WHOLE_SUBMAT));
  787. ////////////////////////////////////////////////////////////////////////////////
  788. // Multiply_Scalar_First
  789. PARAM_TEST_CASE(Multiply_Scalar_First, cv::cuda::DeviceInfo, cv::Size, std::pair<MatDepth, MatDepth>, UseRoi)
  790. {
  791. cv::cuda::DeviceInfo devInfo;
  792. cv::Size size;
  793. std::pair<MatDepth, MatDepth> depth;
  794. bool useRoi;
  795. virtual void SetUp()
  796. {
  797. devInfo = GET_PARAM(0);
  798. size = GET_PARAM(1);
  799. depth = GET_PARAM(2);
  800. useRoi = GET_PARAM(3);
  801. cv::cuda::setDevice(devInfo.deviceID());
  802. }
  803. };
  804. CUDA_TEST_P(Multiply_Scalar_First, WithOutScale)
  805. {
  806. cv::Mat mat = randomMat(size, depth.first);
  807. cv::Scalar val = randomScalar(0, 255);
  808. if ((depth.first == CV_64F || depth.second == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  809. {
  810. try
  811. {
  812. cv::cuda::GpuMat dst;
  813. cv::cuda::multiply(val, loadMat(mat), dst, 1, depth.second);
  814. }
  815. catch (const cv::Exception& e)
  816. {
  817. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  818. }
  819. }
  820. else
  821. {
  822. cv::cuda::GpuMat dst = createMat(size, depth.second, useRoi);
  823. cv::cuda::multiply(val, loadMat(mat, useRoi), dst, 1, depth.second);
  824. cv::Mat dst_gold;
  825. cv::multiply(val, mat, dst_gold, 1, depth.second);
  826. EXPECT_MAT_NEAR(dst_gold, dst, 1.0);
  827. }
  828. }
  829. CUDA_TEST_P(Multiply_Scalar_First, WithScale)
  830. {
  831. cv::Mat mat = randomMat(size, depth.first);
  832. cv::Scalar val = randomScalar(0, 255);
  833. double scale = randomDouble(0.0, 255.0);
  834. if ((depth.first == CV_64F || depth.second == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  835. {
  836. try
  837. {
  838. cv::cuda::GpuMat dst;
  839. cv::cuda::multiply(val, loadMat(mat), dst, scale, depth.second);
  840. }
  841. catch (const cv::Exception& e)
  842. {
  843. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  844. }
  845. }
  846. else
  847. {
  848. cv::cuda::GpuMat dst = createMat(size, depth.second, useRoi);
  849. cv::cuda::multiply(val, loadMat(mat, useRoi), dst, scale, depth.second);
  850. cv::Mat dst_gold;
  851. cv::multiply(val, mat, dst_gold, scale, depth.second);
  852. EXPECT_MAT_NEAR(dst_gold, dst, 1.0);
  853. }
  854. }
  855. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Multiply_Scalar_First, testing::Combine(
  856. ALL_DEVICES,
  857. DIFFERENT_SIZES,
  858. DEPTH_PAIRS,
  859. WHOLE_SUBMAT));
  860. ////////////////////////////////////////////////////////////////////////////////
  861. // Divide_Array
  862. PARAM_TEST_CASE(Divide_Array, cv::cuda::DeviceInfo, cv::Size, std::pair<MatDepth, MatDepth>, Channels, UseRoi)
  863. {
  864. cv::cuda::DeviceInfo devInfo;
  865. cv::Size size;
  866. std::pair<MatDepth, MatDepth> depth;
  867. int channels;
  868. bool useRoi;
  869. int stype;
  870. int dtype;
  871. virtual void SetUp()
  872. {
  873. devInfo = GET_PARAM(0);
  874. size = GET_PARAM(1);
  875. depth = GET_PARAM(2);
  876. channels = GET_PARAM(3);
  877. useRoi = GET_PARAM(4);
  878. cv::cuda::setDevice(devInfo.deviceID());
  879. stype = CV_MAKE_TYPE(depth.first, channels);
  880. dtype = CV_MAKE_TYPE(depth.second, channels);
  881. }
  882. };
  883. CUDA_TEST_P(Divide_Array, WithOutScale)
  884. {
  885. cv::Mat mat1 = randomMat(size, stype);
  886. cv::Mat mat2 = randomMat(size, stype, 1.0, 255.0);
  887. if ((depth.first == CV_64F || depth.second == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  888. {
  889. try
  890. {
  891. cv::cuda::GpuMat dst;
  892. cv::cuda::divide(loadMat(mat1), loadMat(mat2), dst, 1, depth.second);
  893. }
  894. catch (const cv::Exception& e)
  895. {
  896. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  897. }
  898. }
  899. else
  900. {
  901. cv::cuda::GpuMat dst = createMat(size, dtype, useRoi);
  902. cv::cuda::divide(loadMat(mat1, useRoi), loadMat(mat2, useRoi), dst, 1, depth.second);
  903. cv::Mat dst_gold;
  904. cv::divide(mat1, mat2, dst_gold, 1, depth.second);
  905. EXPECT_MAT_NEAR(dst_gold, dst, depth.first >= CV_32F || depth.second >= CV_32F ? 1e-4 : 1.0);
  906. }
  907. }
  908. CUDA_TEST_P(Divide_Array, WithScale)
  909. {
  910. cv::Mat mat1 = randomMat(size, stype);
  911. cv::Mat mat2 = randomMat(size, stype, 1.0, 255.0);
  912. double scale = randomDouble(0.0, 255.0);
  913. if ((depth.first == CV_64F || depth.second == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  914. {
  915. try
  916. {
  917. cv::cuda::GpuMat dst;
  918. cv::cuda::divide(loadMat(mat1), loadMat(mat2), dst, scale, depth.second);
  919. }
  920. catch (const cv::Exception& e)
  921. {
  922. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  923. }
  924. }
  925. else
  926. {
  927. cv::cuda::GpuMat dst = createMat(size, dtype, useRoi);
  928. cv::cuda::divide(loadMat(mat1, useRoi), loadMat(mat2, useRoi), dst, scale, depth.second);
  929. cv::Mat dst_gold;
  930. cv::divide(mat1, mat2, dst_gold, scale, depth.second);
  931. EXPECT_MAT_NEAR(dst_gold, dst, depth.first >= CV_32F || depth.second >= CV_32F ? 1e-2 : 1.0);
  932. }
  933. }
  934. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Divide_Array, testing::Combine(
  935. ALL_DEVICES,
  936. DIFFERENT_SIZES,
  937. DEPTH_PAIRS,
  938. ALL_CHANNELS,
  939. WHOLE_SUBMAT));
  940. ////////////////////////////////////////////////////////////////////////////////
  941. // Divide_Array_Special
  942. PARAM_TEST_CASE(Divide_Array_Special, cv::cuda::DeviceInfo, cv::Size, UseRoi)
  943. {
  944. cv::cuda::DeviceInfo devInfo;
  945. cv::Size size;
  946. bool useRoi;
  947. virtual void SetUp()
  948. {
  949. devInfo = GET_PARAM(0);
  950. size = GET_PARAM(1);
  951. useRoi = GET_PARAM(2);
  952. cv::cuda::setDevice(devInfo.deviceID());
  953. }
  954. };
  955. CUDA_TEST_P(Divide_Array_Special, Case_8UC4x_32FC1)
  956. {
  957. cv::Mat mat1 = randomMat(size, CV_8UC4);
  958. cv::Mat mat2 = randomMat(size, CV_32FC1, 1.0, 255.0);
  959. cv::cuda::GpuMat dst = createMat(size, CV_8UC4, useRoi);
  960. cv::cuda::divide(loadMat(mat1, useRoi), loadMat(mat2, useRoi), dst);
  961. cv::Mat h_dst(dst);
  962. for (int y = 0; y < h_dst.rows; ++y)
  963. {
  964. const cv::Vec4b* mat1_row = mat1.ptr<cv::Vec4b>(y);
  965. const float* mat2_row = mat2.ptr<float>(y);
  966. const cv::Vec4b* dst_row = h_dst.ptr<cv::Vec4b>(y);
  967. for (int x = 0; x < h_dst.cols; ++x)
  968. {
  969. cv::Vec4b val1 = mat1_row[x];
  970. float val2 = mat2_row[x];
  971. cv::Vec4b actual = dst_row[x];
  972. cv::Vec4b gold;
  973. gold[0] = cv::saturate_cast<uchar>(val1[0] / val2);
  974. gold[1] = cv::saturate_cast<uchar>(val1[1] / val2);
  975. gold[2] = cv::saturate_cast<uchar>(val1[2] / val2);
  976. gold[3] = cv::saturate_cast<uchar>(val1[3] / val2);
  977. ASSERT_LE(std::abs(gold[0] - actual[0]), 1.0);
  978. ASSERT_LE(std::abs(gold[1] - actual[1]), 1.0);
  979. ASSERT_LE(std::abs(gold[1] - actual[1]), 1.0);
  980. ASSERT_LE(std::abs(gold[1] - actual[1]), 1.0);
  981. }
  982. }
  983. }
  984. CUDA_TEST_P(Divide_Array_Special, Case_16SC4x_32FC1)
  985. {
  986. cv::Mat mat1 = randomMat(size, CV_16SC4);
  987. cv::Mat mat2 = randomMat(size, CV_32FC1, 1.0, 255.0);
  988. cv::cuda::GpuMat dst = createMat(size, CV_16SC4, useRoi);
  989. cv::cuda::divide(loadMat(mat1, useRoi), loadMat(mat2, useRoi), dst);
  990. cv::Mat h_dst(dst);
  991. for (int y = 0; y < h_dst.rows; ++y)
  992. {
  993. const cv::Vec4s* mat1_row = mat1.ptr<cv::Vec4s>(y);
  994. const float* mat2_row = mat2.ptr<float>(y);
  995. const cv::Vec4s* dst_row = h_dst.ptr<cv::Vec4s>(y);
  996. for (int x = 0; x < h_dst.cols; ++x)
  997. {
  998. cv::Vec4s val1 = mat1_row[x];
  999. float val2 = mat2_row[x];
  1000. cv::Vec4s actual = dst_row[x];
  1001. cv::Vec4s gold;
  1002. gold[0] = cv::saturate_cast<short>(val1[0] / val2);
  1003. gold[1] = cv::saturate_cast<short>(val1[1] / val2);
  1004. gold[2] = cv::saturate_cast<short>(val1[2] / val2);
  1005. gold[3] = cv::saturate_cast<short>(val1[3] / val2);
  1006. ASSERT_LE(std::abs(gold[0] - actual[0]), 1.0);
  1007. ASSERT_LE(std::abs(gold[1] - actual[1]), 1.0);
  1008. ASSERT_LE(std::abs(gold[1] - actual[1]), 1.0);
  1009. ASSERT_LE(std::abs(gold[1] - actual[1]), 1.0);
  1010. }
  1011. }
  1012. }
  1013. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Divide_Array_Special, testing::Combine(
  1014. ALL_DEVICES,
  1015. DIFFERENT_SIZES,
  1016. WHOLE_SUBMAT));
  1017. ////////////////////////////////////////////////////////////////////////////////
  1018. // Divide_Scalar
  1019. PARAM_TEST_CASE(Divide_Scalar, cv::cuda::DeviceInfo, cv::Size, std::pair<MatDepth, MatDepth>, UseRoi)
  1020. {
  1021. cv::cuda::DeviceInfo devInfo;
  1022. cv::Size size;
  1023. std::pair<MatDepth, MatDepth> depth;
  1024. bool useRoi;
  1025. virtual void SetUp()
  1026. {
  1027. devInfo = GET_PARAM(0);
  1028. size = GET_PARAM(1);
  1029. depth = GET_PARAM(2);
  1030. useRoi = GET_PARAM(3);
  1031. cv::cuda::setDevice(devInfo.deviceID());
  1032. }
  1033. };
  1034. CUDA_TEST_P(Divide_Scalar, WithOutScale)
  1035. {
  1036. cv::Mat mat = randomMat(size, depth.first);
  1037. cv::Scalar val = randomScalar(1.0, 255.0);
  1038. if ((depth.first == CV_64F || depth.second == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  1039. {
  1040. try
  1041. {
  1042. cv::cuda::GpuMat dst;
  1043. cv::cuda::divide(loadMat(mat), val, dst, 1, depth.second);
  1044. }
  1045. catch (const cv::Exception& e)
  1046. {
  1047. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  1048. }
  1049. }
  1050. else
  1051. {
  1052. cv::cuda::GpuMat dst = createMat(size, depth.second, useRoi);
  1053. cv::cuda::divide(loadMat(mat, useRoi), val, dst, 1, depth.second);
  1054. cv::Mat dst_gold;
  1055. cv::divide(mat, val, dst_gold, 1, depth.second);
  1056. EXPECT_MAT_NEAR(dst_gold, dst, depth.first >= CV_32F || depth.second >= CV_32F ? 1e-4 : 1.0);
  1057. }
  1058. }
  1059. CUDA_TEST_P(Divide_Scalar, WithScale)
  1060. {
  1061. cv::Mat mat = randomMat(size, depth.first);
  1062. cv::Scalar val = randomScalar(1.0, 255.0);
  1063. double scale = randomDouble(0.0, 255.0);
  1064. if ((depth.first == CV_64F || depth.second == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  1065. {
  1066. try
  1067. {
  1068. cv::cuda::GpuMat dst;
  1069. cv::cuda::divide(loadMat(mat), val, dst, scale, depth.second);
  1070. }
  1071. catch (const cv::Exception& e)
  1072. {
  1073. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  1074. }
  1075. }
  1076. else
  1077. {
  1078. cv::cuda::GpuMat dst = createMat(size, depth.second, useRoi);
  1079. cv::cuda::divide(loadMat(mat, useRoi), val, dst, scale, depth.second);
  1080. cv::Mat dst_gold;
  1081. cv::divide(mat, val, dst_gold, scale, depth.second);
  1082. EXPECT_MAT_NEAR(dst_gold, dst, depth.first >= CV_32F || depth.second >= CV_32F ? 1e-2 : 1.0);
  1083. }
  1084. }
  1085. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Divide_Scalar, testing::Combine(
  1086. ALL_DEVICES,
  1087. DIFFERENT_SIZES,
  1088. DEPTH_PAIRS,
  1089. WHOLE_SUBMAT));
  1090. ////////////////////////////////////////////////////////////////////////////////
  1091. // Divide_Scalar_First
  1092. PARAM_TEST_CASE(Divide_Scalar_First, cv::cuda::DeviceInfo, cv::Size, std::pair<MatDepth, MatDepth>, UseRoi)
  1093. {
  1094. cv::cuda::DeviceInfo devInfo;
  1095. cv::Size size;
  1096. std::pair<MatDepth, MatDepth> depth;
  1097. bool useRoi;
  1098. virtual void SetUp()
  1099. {
  1100. devInfo = GET_PARAM(0);
  1101. size = GET_PARAM(1);
  1102. depth = GET_PARAM(2);
  1103. useRoi = GET_PARAM(3);
  1104. cv::cuda::setDevice(devInfo.deviceID());
  1105. }
  1106. };
  1107. CUDA_TEST_P(Divide_Scalar_First, Accuracy)
  1108. {
  1109. double scale = randomDouble(0.0, 255.0);
  1110. cv::Mat mat = randomMat(size, depth.first, 1.0, 255.0);
  1111. if ((depth.first == CV_64F || depth.second == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  1112. {
  1113. try
  1114. {
  1115. cv::cuda::GpuMat dst;
  1116. cv::cuda::divide(scale, loadMat(mat), dst, 1.0, depth.second);
  1117. }
  1118. catch (const cv::Exception& e)
  1119. {
  1120. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  1121. }
  1122. }
  1123. else
  1124. {
  1125. cv::cuda::GpuMat dst = createMat(size, depth.second, useRoi);
  1126. cv::cuda::divide(scale, loadMat(mat, useRoi), dst, 1.0, depth.second);
  1127. cv::Mat dst_gold;
  1128. cv::divide(scale, mat, dst_gold, depth.second);
  1129. EXPECT_MAT_NEAR(dst_gold, dst, depth.first >= CV_32F || depth.second >= CV_32F ? 1e-4 : 1.0);
  1130. }
  1131. }
  1132. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Divide_Scalar_First, testing::Combine(
  1133. ALL_DEVICES,
  1134. DIFFERENT_SIZES,
  1135. DEPTH_PAIRS,
  1136. WHOLE_SUBMAT));
  1137. ////////////////////////////////////////////////////////////////////////////////
  1138. // AbsDiff
  1139. PARAM_TEST_CASE(AbsDiff, cv::cuda::DeviceInfo, cv::Size, MatDepth, UseRoi)
  1140. {
  1141. cv::cuda::DeviceInfo devInfo;
  1142. cv::Size size;
  1143. int depth;
  1144. bool useRoi;
  1145. virtual void SetUp()
  1146. {
  1147. devInfo = GET_PARAM(0);
  1148. size = GET_PARAM(1);
  1149. depth = GET_PARAM(2);
  1150. useRoi = GET_PARAM(3);
  1151. cv::cuda::setDevice(devInfo.deviceID());
  1152. }
  1153. };
  1154. CUDA_TEST_P(AbsDiff, Array)
  1155. {
  1156. cv::Mat src1 = randomMat(size, depth);
  1157. cv::Mat src2 = randomMat(size, depth);
  1158. if (depth == CV_64F && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  1159. {
  1160. try
  1161. {
  1162. cv::cuda::GpuMat dst;
  1163. cv::cuda::absdiff(loadMat(src1), loadMat(src2), dst);
  1164. }
  1165. catch (const cv::Exception& e)
  1166. {
  1167. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  1168. }
  1169. }
  1170. else
  1171. {
  1172. cv::cuda::GpuMat dst = createMat(size, depth, useRoi);
  1173. cv::cuda::absdiff(loadMat(src1, useRoi), loadMat(src2, useRoi), dst);
  1174. cv::Mat dst_gold;
  1175. cv::absdiff(src1, src2, dst_gold);
  1176. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  1177. }
  1178. }
  1179. CUDA_TEST_P(AbsDiff, Scalar)
  1180. {
  1181. cv::Mat src = randomMat(size, depth);
  1182. cv::Scalar val = randomScalar(0.0, 255.0);
  1183. if (depth == CV_64F && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  1184. {
  1185. try
  1186. {
  1187. cv::cuda::GpuMat dst;
  1188. cv::cuda::absdiff(loadMat(src), val, dst);
  1189. }
  1190. catch (const cv::Exception& e)
  1191. {
  1192. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  1193. }
  1194. }
  1195. else
  1196. {
  1197. cv::cuda::GpuMat dst = createMat(size, depth, useRoi);
  1198. cv::cuda::absdiff(loadMat(src, useRoi), val, dst);
  1199. cv::Mat dst_gold;
  1200. cv::absdiff(src, val, dst_gold);
  1201. EXPECT_MAT_NEAR(dst_gold, dst, depth <= CV_32F ? 1.0 : 1e-5);
  1202. }
  1203. }
  1204. CUDA_TEST_P(AbsDiff, Scalar_First)
  1205. {
  1206. cv::Mat src = randomMat(size, depth);
  1207. cv::Scalar val = randomScalar(0.0, 255.0);
  1208. if (depth == CV_64F && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  1209. {
  1210. try
  1211. {
  1212. cv::cuda::GpuMat dst;
  1213. cv::cuda::absdiff(val, loadMat(src), dst);
  1214. }
  1215. catch (const cv::Exception& e)
  1216. {
  1217. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  1218. }
  1219. }
  1220. else
  1221. {
  1222. cv::cuda::GpuMat dst = createMat(size, depth, useRoi);
  1223. cv::cuda::absdiff(val, loadMat(src, useRoi), dst);
  1224. cv::Mat dst_gold;
  1225. cv::absdiff(val, src, dst_gold);
  1226. EXPECT_MAT_NEAR(dst_gold, dst, depth <= CV_32F ? 1.0 : 1e-5);
  1227. }
  1228. }
  1229. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, AbsDiff, testing::Combine(
  1230. ALL_DEVICES,
  1231. DIFFERENT_SIZES,
  1232. ALL_DEPTH,
  1233. WHOLE_SUBMAT));
  1234. ////////////////////////////////////////////////////////////////////////////////
  1235. // Abs
  1236. PARAM_TEST_CASE(Abs, cv::cuda::DeviceInfo, cv::Size, MatDepth, UseRoi)
  1237. {
  1238. cv::cuda::DeviceInfo devInfo;
  1239. cv::Size size;
  1240. int depth;
  1241. bool useRoi;
  1242. virtual void SetUp()
  1243. {
  1244. devInfo = GET_PARAM(0);
  1245. size = GET_PARAM(1);
  1246. depth = GET_PARAM(2);
  1247. useRoi = GET_PARAM(3);
  1248. cv::cuda::setDevice(devInfo.deviceID());
  1249. }
  1250. };
  1251. CUDA_TEST_P(Abs, Accuracy)
  1252. {
  1253. cv::Mat src = randomMat(size, depth);
  1254. cv::cuda::GpuMat dst = createMat(size, depth, useRoi);
  1255. cv::cuda::abs(loadMat(src, useRoi), dst);
  1256. cv::Mat dst_gold = cv::abs(src);
  1257. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  1258. }
  1259. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Abs, testing::Combine(
  1260. ALL_DEVICES,
  1261. DIFFERENT_SIZES,
  1262. testing::Values(MatDepth(CV_16S), MatDepth(CV_32F)),
  1263. WHOLE_SUBMAT));
  1264. ////////////////////////////////////////////////////////////////////////////////
  1265. // Sqr
  1266. PARAM_TEST_CASE(Sqr, cv::cuda::DeviceInfo, cv::Size, MatDepth, UseRoi)
  1267. {
  1268. cv::cuda::DeviceInfo devInfo;
  1269. cv::Size size;
  1270. int depth;
  1271. bool useRoi;
  1272. virtual void SetUp()
  1273. {
  1274. devInfo = GET_PARAM(0);
  1275. size = GET_PARAM(1);
  1276. depth = GET_PARAM(2);
  1277. useRoi = GET_PARAM(3);
  1278. cv::cuda::setDevice(devInfo.deviceID());
  1279. }
  1280. };
  1281. CUDA_TEST_P(Sqr, Accuracy)
  1282. {
  1283. cv::Mat src = randomMat(size, depth, 0, depth == CV_8U ? 16 : 255);
  1284. cv::cuda::GpuMat dst = createMat(size, depth, useRoi);
  1285. cv::cuda::sqr(loadMat(src, useRoi), dst);
  1286. cv::Mat dst_gold;
  1287. cv::multiply(src, src, dst_gold);
  1288. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  1289. }
  1290. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Sqr, testing::Combine(
  1291. ALL_DEVICES,
  1292. DIFFERENT_SIZES,
  1293. testing::Values(MatDepth(CV_8U),
  1294. MatDepth(CV_16U),
  1295. MatDepth(CV_16S),
  1296. MatDepth(CV_32F)),
  1297. WHOLE_SUBMAT));
  1298. ////////////////////////////////////////////////////////////////////////////////
  1299. // Sqrt
  1300. namespace
  1301. {
  1302. template <typename T> void sqrtImpl(const cv::Mat& src, cv::Mat& dst)
  1303. {
  1304. dst.create(src.size(), src.type());
  1305. for (int y = 0; y < src.rows; ++y)
  1306. {
  1307. for (int x = 0; x < src.cols; ++x)
  1308. dst.at<T>(y, x) = static_cast<T>(std::sqrt(static_cast<float>(src.at<T>(y, x))));
  1309. }
  1310. }
  1311. void sqrtGold(const cv::Mat& src, cv::Mat& dst)
  1312. {
  1313. typedef void (*func_t)(const cv::Mat& src, cv::Mat& dst);
  1314. const func_t funcs[] =
  1315. {
  1316. sqrtImpl<uchar>, sqrtImpl<schar>, sqrtImpl<ushort>, sqrtImpl<short>,
  1317. sqrtImpl<int>, sqrtImpl<float>
  1318. };
  1319. funcs[src.depth()](src, dst);
  1320. }
  1321. }
  1322. PARAM_TEST_CASE(Sqrt, cv::cuda::DeviceInfo, cv::Size, MatDepth, UseRoi)
  1323. {
  1324. cv::cuda::DeviceInfo devInfo;
  1325. cv::Size size;
  1326. int depth;
  1327. bool useRoi;
  1328. virtual void SetUp()
  1329. {
  1330. devInfo = GET_PARAM(0);
  1331. size = GET_PARAM(1);
  1332. depth = GET_PARAM(2);
  1333. useRoi = GET_PARAM(3);
  1334. cv::cuda::setDevice(devInfo.deviceID());
  1335. }
  1336. };
  1337. CUDA_TEST_P(Sqrt, Accuracy)
  1338. {
  1339. cv::Mat src = randomMat(size, depth);
  1340. cv::cuda::GpuMat dst = createMat(size, depth, useRoi);
  1341. cv::cuda::sqrt(loadMat(src, useRoi), dst);
  1342. cv::Mat dst_gold;
  1343. sqrtGold(src, dst_gold);
  1344. EXPECT_MAT_NEAR(dst_gold, dst, depth < CV_32F ? 1.0 : 1e-5);
  1345. }
  1346. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Sqrt, testing::Combine(
  1347. ALL_DEVICES,
  1348. DIFFERENT_SIZES,
  1349. testing::Values(MatDepth(CV_8U),
  1350. MatDepth(CV_16U),
  1351. MatDepth(CV_16S),
  1352. MatDepth(CV_32F)),
  1353. WHOLE_SUBMAT));
  1354. ////////////////////////////////////////////////////////////////////////////////
  1355. // Log
  1356. namespace
  1357. {
  1358. template <typename T> void logImpl(const cv::Mat& src, cv::Mat& dst)
  1359. {
  1360. dst.create(src.size(), src.type());
  1361. for (int y = 0; y < src.rows; ++y)
  1362. {
  1363. for (int x = 0; x < src.cols; ++x)
  1364. dst.at<T>(y, x) = static_cast<T>(std::log(static_cast<float>(src.at<T>(y, x))));
  1365. }
  1366. }
  1367. void logGold(const cv::Mat& src, cv::Mat& dst)
  1368. {
  1369. typedef void (*func_t)(const cv::Mat& src, cv::Mat& dst);
  1370. const func_t funcs[] =
  1371. {
  1372. logImpl<uchar>, logImpl<schar>, logImpl<ushort>, logImpl<short>,
  1373. logImpl<int>, logImpl<float>
  1374. };
  1375. funcs[src.depth()](src, dst);
  1376. }
  1377. }
  1378. PARAM_TEST_CASE(Log, cv::cuda::DeviceInfo, cv::Size, MatDepth, UseRoi)
  1379. {
  1380. cv::cuda::DeviceInfo devInfo;
  1381. cv::Size size;
  1382. int depth;
  1383. bool useRoi;
  1384. virtual void SetUp()
  1385. {
  1386. devInfo = GET_PARAM(0);
  1387. size = GET_PARAM(1);
  1388. depth = GET_PARAM(2);
  1389. useRoi = GET_PARAM(3);
  1390. cv::cuda::setDevice(devInfo.deviceID());
  1391. }
  1392. };
  1393. CUDA_TEST_P(Log, Accuracy)
  1394. {
  1395. cv::Mat src = randomMat(size, depth, 1.0, 255.0);
  1396. cv::cuda::GpuMat dst = createMat(size, depth, useRoi);
  1397. cv::cuda::log(loadMat(src, useRoi), dst);
  1398. cv::Mat dst_gold;
  1399. logGold(src, dst_gold);
  1400. EXPECT_MAT_NEAR(dst_gold, dst, depth < CV_32F ? 1.0 : 1e-6);
  1401. }
  1402. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Log, testing::Combine(
  1403. ALL_DEVICES,
  1404. DIFFERENT_SIZES,
  1405. testing::Values(MatDepth(CV_8U),
  1406. MatDepth(CV_16U),
  1407. MatDepth(CV_16S),
  1408. MatDepth(CV_32F)),
  1409. WHOLE_SUBMAT));
  1410. ////////////////////////////////////////////////////////////////////////////////
  1411. // Exp
  1412. namespace
  1413. {
  1414. template <typename T> void expImpl(const cv::Mat& src, cv::Mat& dst)
  1415. {
  1416. dst.create(src.size(), src.type());
  1417. for (int y = 0; y < src.rows; ++y)
  1418. {
  1419. for (int x = 0; x < src.cols; ++x)
  1420. dst.at<T>(y, x) = cv::saturate_cast<T>(static_cast<int>(std::exp(static_cast<float>(src.at<T>(y, x)))));
  1421. }
  1422. }
  1423. void expImpl_float(const cv::Mat& src, cv::Mat& dst)
  1424. {
  1425. dst.create(src.size(), src.type());
  1426. for (int y = 0; y < src.rows; ++y)
  1427. {
  1428. for (int x = 0; x < src.cols; ++x)
  1429. dst.at<float>(y, x) = std::exp(static_cast<float>(src.at<float>(y, x)));
  1430. }
  1431. }
  1432. void expGold(const cv::Mat& src, cv::Mat& dst)
  1433. {
  1434. typedef void (*func_t)(const cv::Mat& src, cv::Mat& dst);
  1435. const func_t funcs[] =
  1436. {
  1437. expImpl<uchar>, expImpl<schar>, expImpl<ushort>, expImpl<short>,
  1438. expImpl<int>, expImpl_float
  1439. };
  1440. funcs[src.depth()](src, dst);
  1441. }
  1442. }
  1443. PARAM_TEST_CASE(Exp, cv::cuda::DeviceInfo, cv::Size, MatDepth, UseRoi)
  1444. {
  1445. cv::cuda::DeviceInfo devInfo;
  1446. cv::Size size;
  1447. int depth;
  1448. bool useRoi;
  1449. virtual void SetUp()
  1450. {
  1451. devInfo = GET_PARAM(0);
  1452. size = GET_PARAM(1);
  1453. depth = GET_PARAM(2);
  1454. useRoi = GET_PARAM(3);
  1455. cv::cuda::setDevice(devInfo.deviceID());
  1456. }
  1457. };
  1458. CUDA_TEST_P(Exp, Accuracy)
  1459. {
  1460. cv::Mat src = randomMat(size, depth, 0.0, 10.0);
  1461. cv::cuda::GpuMat dst = createMat(size, depth, useRoi);
  1462. cv::cuda::exp(loadMat(src, useRoi), dst);
  1463. cv::Mat dst_gold;
  1464. expGold(src, dst_gold);
  1465. EXPECT_MAT_NEAR(dst_gold, dst, depth < CV_32F ? 1.0 : 1e-2);
  1466. }
  1467. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Exp, testing::Combine(
  1468. ALL_DEVICES,
  1469. DIFFERENT_SIZES,
  1470. testing::Values(MatDepth(CV_8U),
  1471. MatDepth(CV_16U),
  1472. MatDepth(CV_16S),
  1473. MatDepth(CV_32F)),
  1474. WHOLE_SUBMAT));
  1475. ////////////////////////////////////////////////////////////////////////////////
  1476. // Pow
  1477. PARAM_TEST_CASE(Pow, cv::cuda::DeviceInfo, cv::Size, MatDepth, UseRoi)
  1478. {
  1479. cv::cuda::DeviceInfo devInfo;
  1480. cv::Size size;
  1481. int depth;
  1482. bool useRoi;
  1483. virtual void SetUp()
  1484. {
  1485. devInfo = GET_PARAM(0);
  1486. size = GET_PARAM(1);
  1487. depth = GET_PARAM(2);
  1488. useRoi = GET_PARAM(3);
  1489. cv::cuda::setDevice(devInfo.deviceID());
  1490. }
  1491. };
  1492. CUDA_TEST_P(Pow, Accuracy)
  1493. {
  1494. cv::Mat src = randomMat(size, depth, 0.0, 10.0);
  1495. double power = randomDouble(2.0, 4.0);
  1496. if (src.depth() < CV_32F)
  1497. power = static_cast<int>(power);
  1498. if (depth == CV_64F && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  1499. {
  1500. try
  1501. {
  1502. cv::cuda::GpuMat dst;
  1503. cv::cuda::pow(loadMat(src), power, dst);
  1504. }
  1505. catch (const cv::Exception& e)
  1506. {
  1507. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  1508. }
  1509. }
  1510. else
  1511. {
  1512. cv::cuda::GpuMat dst = createMat(size, depth, useRoi);
  1513. cv::cuda::pow(loadMat(src, useRoi), power, dst);
  1514. cv::Mat dst_gold;
  1515. cv::pow(src, power, dst_gold);
  1516. EXPECT_MAT_NEAR(dst_gold, dst, depth < CV_32F ? 0.0 : 1e-1);
  1517. }
  1518. }
  1519. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Pow, testing::Combine(
  1520. ALL_DEVICES,
  1521. DIFFERENT_SIZES,
  1522. ALL_DEPTH,
  1523. WHOLE_SUBMAT));
  1524. ////////////////////////////////////////////////////////////////////////////////
  1525. // Compare_Array
  1526. CV_ENUM(CmpCode, cv::CMP_EQ, cv::CMP_GT, cv::CMP_GE, cv::CMP_LT, cv::CMP_LE, cv::CMP_NE)
  1527. #define ALL_CMP_CODES testing::Values(CmpCode(cv::CMP_EQ), CmpCode(cv::CMP_NE), CmpCode(cv::CMP_GT), CmpCode(cv::CMP_GE), CmpCode(cv::CMP_LT), CmpCode(cv::CMP_LE))
  1528. PARAM_TEST_CASE(Compare_Array, cv::cuda::DeviceInfo, cv::Size, MatDepth, CmpCode, UseRoi)
  1529. {
  1530. cv::cuda::DeviceInfo devInfo;
  1531. cv::Size size;
  1532. int depth;
  1533. int cmp_code;
  1534. bool useRoi;
  1535. virtual void SetUp()
  1536. {
  1537. devInfo = GET_PARAM(0);
  1538. size = GET_PARAM(1);
  1539. depth = GET_PARAM(2);
  1540. cmp_code = GET_PARAM(3);
  1541. useRoi = GET_PARAM(4);
  1542. cv::cuda::setDevice(devInfo.deviceID());
  1543. }
  1544. };
  1545. CUDA_TEST_P(Compare_Array, Accuracy)
  1546. {
  1547. cv::Mat src1 = randomMat(size, depth);
  1548. cv::Mat src2 = randomMat(size, depth);
  1549. if (depth == CV_64F && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  1550. {
  1551. try
  1552. {
  1553. cv::cuda::GpuMat dst;
  1554. cv::cuda::compare(loadMat(src1), loadMat(src2), dst, cmp_code);
  1555. }
  1556. catch (const cv::Exception& e)
  1557. {
  1558. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  1559. }
  1560. }
  1561. else
  1562. {
  1563. cv::cuda::GpuMat dst = createMat(size, CV_8UC1, useRoi);
  1564. cv::cuda::compare(loadMat(src1, useRoi), loadMat(src2, useRoi), dst, cmp_code);
  1565. cv::Mat dst_gold;
  1566. cv::compare(src1, src2, dst_gold, cmp_code);
  1567. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  1568. }
  1569. }
  1570. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Compare_Array, testing::Combine(
  1571. ALL_DEVICES,
  1572. DIFFERENT_SIZES,
  1573. ALL_DEPTH,
  1574. ALL_CMP_CODES,
  1575. WHOLE_SUBMAT));
  1576. ////////////////////////////////////////////////////////////////////////////////
  1577. // Compare_Scalar
  1578. namespace
  1579. {
  1580. template <template <typename> class Op, typename T>
  1581. void compareScalarImpl(const cv::Mat& src, cv::Scalar sc, cv::Mat& dst)
  1582. {
  1583. Op<T> op;
  1584. const int cn = src.channels();
  1585. dst.create(src.size(), CV_MAKE_TYPE(CV_8U, cn));
  1586. for (int y = 0; y < src.rows; ++y)
  1587. {
  1588. for (int x = 0; x < src.cols; ++x)
  1589. {
  1590. for (int c = 0; c < cn; ++c)
  1591. {
  1592. T src_val = src.at<T>(y, x * cn + c);
  1593. T sc_val = cv::saturate_cast<T>(sc.val[c]);
  1594. dst.at<uchar>(y, x * cn + c) = static_cast<uchar>(static_cast<int>(op(src_val, sc_val)) * 255);
  1595. }
  1596. }
  1597. }
  1598. }
  1599. void compareScalarGold(const cv::Mat& src, cv::Scalar sc, cv::Mat& dst, int cmpop)
  1600. {
  1601. typedef void (*func_t)(const cv::Mat& src, cv::Scalar sc, cv::Mat& dst);
  1602. static const func_t funcs[7][6] =
  1603. {
  1604. {compareScalarImpl<std::equal_to, unsigned char> , compareScalarImpl<std::greater, unsigned char> , compareScalarImpl<std::greater_equal, unsigned char> , compareScalarImpl<std::less, unsigned char> , compareScalarImpl<std::less_equal, unsigned char> , compareScalarImpl<std::not_equal_to, unsigned char> },
  1605. {compareScalarImpl<std::equal_to, signed char> , compareScalarImpl<std::greater, signed char> , compareScalarImpl<std::greater_equal, signed char> , compareScalarImpl<std::less, signed char> , compareScalarImpl<std::less_equal, signed char> , compareScalarImpl<std::not_equal_to, signed char> },
  1606. {compareScalarImpl<std::equal_to, unsigned short>, compareScalarImpl<std::greater, unsigned short>, compareScalarImpl<std::greater_equal, unsigned short>, compareScalarImpl<std::less, unsigned short>, compareScalarImpl<std::less_equal, unsigned short>, compareScalarImpl<std::not_equal_to, unsigned short>},
  1607. {compareScalarImpl<std::equal_to, short> , compareScalarImpl<std::greater, short> , compareScalarImpl<std::greater_equal, short> , compareScalarImpl<std::less, short> , compareScalarImpl<std::less_equal, short> , compareScalarImpl<std::not_equal_to, short> },
  1608. {compareScalarImpl<std::equal_to, int> , compareScalarImpl<std::greater, int> , compareScalarImpl<std::greater_equal, int> , compareScalarImpl<std::less, int> , compareScalarImpl<std::less_equal, int> , compareScalarImpl<std::not_equal_to, int> },
  1609. {compareScalarImpl<std::equal_to, float> , compareScalarImpl<std::greater, float> , compareScalarImpl<std::greater_equal, float> , compareScalarImpl<std::less, float> , compareScalarImpl<std::less_equal, float> , compareScalarImpl<std::not_equal_to, float> },
  1610. {compareScalarImpl<std::equal_to, double> , compareScalarImpl<std::greater, double> , compareScalarImpl<std::greater_equal, double> , compareScalarImpl<std::less, double> , compareScalarImpl<std::less_equal, double> , compareScalarImpl<std::not_equal_to, double> }
  1611. };
  1612. funcs[src.depth()][cmpop](src, sc, dst);
  1613. }
  1614. }
  1615. PARAM_TEST_CASE(Compare_Scalar, cv::cuda::DeviceInfo, cv::Size, MatType, CmpCode, UseRoi)
  1616. {
  1617. cv::cuda::DeviceInfo devInfo;
  1618. cv::Size size;
  1619. int type;
  1620. int cmp_code;
  1621. bool useRoi;
  1622. virtual void SetUp()
  1623. {
  1624. devInfo = GET_PARAM(0);
  1625. size = GET_PARAM(1);
  1626. type = GET_PARAM(2);
  1627. cmp_code = GET_PARAM(3);
  1628. useRoi = GET_PARAM(4);
  1629. cv::cuda::setDevice(devInfo.deviceID());
  1630. }
  1631. };
  1632. CUDA_TEST_P(Compare_Scalar, Accuracy)
  1633. {
  1634. cv::Mat src = randomMat(size, type);
  1635. cv::Scalar sc = randomScalar(0.0, 255.0);
  1636. if (src.depth() < CV_32F)
  1637. {
  1638. sc.val[0] = cvRound(sc.val[0]);
  1639. sc.val[1] = cvRound(sc.val[1]);
  1640. sc.val[2] = cvRound(sc.val[2]);
  1641. sc.val[3] = cvRound(sc.val[3]);
  1642. }
  1643. if (src.depth() == CV_64F && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  1644. {
  1645. try
  1646. {
  1647. cv::cuda::GpuMat dst;
  1648. cv::cuda::compare(loadMat(src), sc, dst, cmp_code);
  1649. }
  1650. catch (const cv::Exception& e)
  1651. {
  1652. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  1653. }
  1654. }
  1655. else
  1656. {
  1657. cv::cuda::GpuMat dst = createMat(size, CV_MAKE_TYPE(CV_8U, src.channels()), useRoi);
  1658. cv::cuda::compare(loadMat(src, useRoi), sc, dst, cmp_code);
  1659. cv::Mat dst_gold;
  1660. compareScalarGold(src, sc, dst_gold, cmp_code);
  1661. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  1662. }
  1663. }
  1664. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Compare_Scalar, testing::Combine(
  1665. ALL_DEVICES,
  1666. DIFFERENT_SIZES,
  1667. TYPES(CV_8U, CV_64F, 1, 4),
  1668. ALL_CMP_CODES,
  1669. WHOLE_SUBMAT));
  1670. //////////////////////////////////////////////////////////////////////////////
  1671. // Bitwise_Array
  1672. PARAM_TEST_CASE(Bitwise_Array, cv::cuda::DeviceInfo, cv::Size, MatType)
  1673. {
  1674. cv::cuda::DeviceInfo devInfo;
  1675. cv::Size size;
  1676. int type;
  1677. cv::Mat src1;
  1678. cv::Mat src2;
  1679. virtual void SetUp()
  1680. {
  1681. devInfo = GET_PARAM(0);
  1682. size = GET_PARAM(1);
  1683. type = GET_PARAM(2);
  1684. cv::cuda::setDevice(devInfo.deviceID());
  1685. src1 = randomMat(size, type, 0.0, std::numeric_limits<int>::max());
  1686. src2 = randomMat(size, type, 0.0, std::numeric_limits<int>::max());
  1687. }
  1688. };
  1689. CUDA_TEST_P(Bitwise_Array, Not)
  1690. {
  1691. cv::cuda::GpuMat dst;
  1692. cv::cuda::bitwise_not(loadMat(src1), dst);
  1693. cv::Mat dst_gold = ~src1;
  1694. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  1695. }
  1696. CUDA_TEST_P(Bitwise_Array, Or)
  1697. {
  1698. cv::cuda::GpuMat dst;
  1699. cv::cuda::bitwise_or(loadMat(src1), loadMat(src2), dst);
  1700. cv::Mat dst_gold = src1 | src2;
  1701. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  1702. }
  1703. CUDA_TEST_P(Bitwise_Array, And)
  1704. {
  1705. cv::cuda::GpuMat dst;
  1706. cv::cuda::bitwise_and(loadMat(src1), loadMat(src2), dst);
  1707. cv::Mat dst_gold = src1 & src2;
  1708. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  1709. }
  1710. CUDA_TEST_P(Bitwise_Array, Xor)
  1711. {
  1712. cv::cuda::GpuMat dst;
  1713. cv::cuda::bitwise_xor(loadMat(src1), loadMat(src2), dst);
  1714. cv::Mat dst_gold = src1 ^ src2;
  1715. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  1716. }
  1717. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Bitwise_Array, testing::Combine(
  1718. ALL_DEVICES,
  1719. DIFFERENT_SIZES,
  1720. TYPES(CV_8U, CV_32S, 1, 4)));
  1721. //////////////////////////////////////////////////////////////////////////////
  1722. // Bitwise_Scalar
  1723. PARAM_TEST_CASE(Bitwise_Scalar, cv::cuda::DeviceInfo, cv::Size, MatDepth, Channels)
  1724. {
  1725. cv::cuda::DeviceInfo devInfo;
  1726. cv::Size size;
  1727. int depth;
  1728. int channels;
  1729. cv::Mat src;
  1730. cv::Scalar val;
  1731. virtual void SetUp()
  1732. {
  1733. devInfo = GET_PARAM(0);
  1734. size = GET_PARAM(1);
  1735. depth = GET_PARAM(2);
  1736. channels = GET_PARAM(3);
  1737. cv::cuda::setDevice(devInfo.deviceID());
  1738. src = randomMat(size, CV_MAKE_TYPE(depth, channels));
  1739. cv::Scalar_<int> ival = randomScalar(0.0, std::numeric_limits<int>::max());
  1740. val = ival;
  1741. }
  1742. };
  1743. CUDA_TEST_P(Bitwise_Scalar, Or)
  1744. {
  1745. cv::cuda::GpuMat dst;
  1746. cv::cuda::bitwise_or(loadMat(src), val, dst);
  1747. cv::Mat dst_gold;
  1748. cv::bitwise_or(src, val, dst_gold);
  1749. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  1750. }
  1751. CUDA_TEST_P(Bitwise_Scalar, And)
  1752. {
  1753. cv::cuda::GpuMat dst;
  1754. cv::cuda::bitwise_and(loadMat(src), val, dst);
  1755. cv::Mat dst_gold;
  1756. cv::bitwise_and(src, val, dst_gold);
  1757. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  1758. }
  1759. CUDA_TEST_P(Bitwise_Scalar, Xor)
  1760. {
  1761. cv::cuda::GpuMat dst;
  1762. cv::cuda::bitwise_xor(loadMat(src), val, dst);
  1763. cv::Mat dst_gold;
  1764. cv::bitwise_xor(src, val, dst_gold);
  1765. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  1766. }
  1767. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Bitwise_Scalar, testing::Combine(
  1768. ALL_DEVICES,
  1769. DIFFERENT_SIZES,
  1770. testing::Values(MatDepth(CV_8U), MatDepth(CV_16U), MatDepth(CV_32S)),
  1771. IMAGE_CHANNELS));
  1772. //////////////////////////////////////////////////////////////////////////////
  1773. // RShift
  1774. namespace
  1775. {
  1776. template <typename T> void rhiftImpl(const cv::Mat& src, cv::Scalar_<int> val, cv::Mat& dst)
  1777. {
  1778. const int cn = src.channels();
  1779. dst.create(src.size(), src.type());
  1780. for (int y = 0; y < src.rows; ++y)
  1781. {
  1782. for (int x = 0; x < src.cols; ++x)
  1783. {
  1784. for (int c = 0; c < cn; ++c)
  1785. dst.at<T>(y, x * cn + c) = src.at<T>(y, x * cn + c) >> val.val[c];
  1786. }
  1787. }
  1788. }
  1789. void rhiftGold(const cv::Mat& src, cv::Scalar_<int> val, cv::Mat& dst)
  1790. {
  1791. typedef void (*func_t)(const cv::Mat& src, cv::Scalar_<int> val, cv::Mat& dst);
  1792. const func_t funcs[] =
  1793. {
  1794. rhiftImpl<uchar>, rhiftImpl<schar>, rhiftImpl<ushort>, rhiftImpl<short>, rhiftImpl<int>
  1795. };
  1796. funcs[src.depth()](src, val, dst);
  1797. }
  1798. }
  1799. PARAM_TEST_CASE(RShift, cv::cuda::DeviceInfo, cv::Size, MatDepth, Channels, UseRoi)
  1800. {
  1801. cv::cuda::DeviceInfo devInfo;
  1802. cv::Size size;
  1803. int depth;
  1804. int channels;
  1805. bool useRoi;
  1806. virtual void SetUp()
  1807. {
  1808. devInfo = GET_PARAM(0);
  1809. size = GET_PARAM(1);
  1810. depth = GET_PARAM(2);
  1811. channels = GET_PARAM(3);
  1812. useRoi = GET_PARAM(4);
  1813. cv::cuda::setDevice(devInfo.deviceID());
  1814. }
  1815. };
  1816. CUDA_TEST_P(RShift, Accuracy)
  1817. {
  1818. int type = CV_MAKE_TYPE(depth, channels);
  1819. cv::Mat src = randomMat(size, type);
  1820. cv::Scalar_<int> val = randomScalar(0.0, 8.0);
  1821. cv::cuda::GpuMat dst = createMat(size, type, useRoi);
  1822. cv::cuda::rshift(loadMat(src, useRoi), val, dst);
  1823. cv::Mat dst_gold;
  1824. rhiftGold(src, val, dst_gold);
  1825. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  1826. }
  1827. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, RShift, testing::Combine(
  1828. ALL_DEVICES,
  1829. DIFFERENT_SIZES,
  1830. testing::Values(MatDepth(CV_8U),
  1831. MatDepth(CV_8S),
  1832. MatDepth(CV_16U),
  1833. MatDepth(CV_16S),
  1834. MatDepth(CV_32S)),
  1835. IMAGE_CHANNELS,
  1836. WHOLE_SUBMAT));
  1837. //////////////////////////////////////////////////////////////////////////////
  1838. // LShift
  1839. namespace
  1840. {
  1841. template <typename T> void lhiftImpl(const cv::Mat& src, cv::Scalar_<int> val, cv::Mat& dst)
  1842. {
  1843. const int cn = src.channels();
  1844. dst.create(src.size(), src.type());
  1845. for (int y = 0; y < src.rows; ++y)
  1846. {
  1847. for (int x = 0; x < src.cols; ++x)
  1848. {
  1849. for (int c = 0; c < cn; ++c)
  1850. dst.at<T>(y, x * cn + c) = src.at<T>(y, x * cn + c) << val.val[c];
  1851. }
  1852. }
  1853. }
  1854. void lhiftGold(const cv::Mat& src, cv::Scalar_<int> val, cv::Mat& dst)
  1855. {
  1856. typedef void (*func_t)(const cv::Mat& src, cv::Scalar_<int> val, cv::Mat& dst);
  1857. const func_t funcs[] =
  1858. {
  1859. lhiftImpl<uchar>, lhiftImpl<schar>, lhiftImpl<ushort>, lhiftImpl<short>, lhiftImpl<int>
  1860. };
  1861. funcs[src.depth()](src, val, dst);
  1862. }
  1863. }
  1864. PARAM_TEST_CASE(LShift, cv::cuda::DeviceInfo, cv::Size, MatDepth, Channels, UseRoi)
  1865. {
  1866. cv::cuda::DeviceInfo devInfo;
  1867. cv::Size size;
  1868. int depth;
  1869. int channels;
  1870. bool useRoi;
  1871. virtual void SetUp()
  1872. {
  1873. devInfo = GET_PARAM(0);
  1874. size = GET_PARAM(1);
  1875. depth = GET_PARAM(2);
  1876. channels = GET_PARAM(3);
  1877. useRoi = GET_PARAM(4);
  1878. cv::cuda::setDevice(devInfo.deviceID());
  1879. }
  1880. };
  1881. CUDA_TEST_P(LShift, Accuracy)
  1882. {
  1883. int type = CV_MAKE_TYPE(depth, channels);
  1884. cv::Mat src = randomMat(size, type);
  1885. cv::Scalar_<int> val = randomScalar(0.0, 8.0);
  1886. cv::cuda::GpuMat dst = createMat(size, type, useRoi);
  1887. cv::cuda::lshift(loadMat(src, useRoi), val, dst);
  1888. cv::Mat dst_gold;
  1889. lhiftGold(src, val, dst_gold);
  1890. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  1891. }
  1892. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, LShift, testing::Combine(
  1893. ALL_DEVICES,
  1894. DIFFERENT_SIZES,
  1895. testing::Values(MatDepth(CV_8U), MatDepth(CV_16U), MatDepth(CV_32S)),
  1896. IMAGE_CHANNELS,
  1897. WHOLE_SUBMAT));
  1898. //////////////////////////////////////////////////////////////////////////////
  1899. // Min
  1900. PARAM_TEST_CASE(Min, cv::cuda::DeviceInfo, cv::Size, MatDepth, UseRoi)
  1901. {
  1902. cv::cuda::DeviceInfo devInfo;
  1903. cv::Size size;
  1904. int depth;
  1905. bool useRoi;
  1906. virtual void SetUp()
  1907. {
  1908. devInfo = GET_PARAM(0);
  1909. size = GET_PARAM(1);
  1910. depth = GET_PARAM(2);
  1911. useRoi = GET_PARAM(3);
  1912. cv::cuda::setDevice(devInfo.deviceID());
  1913. }
  1914. };
  1915. CUDA_TEST_P(Min, Array)
  1916. {
  1917. cv::Mat src1 = randomMat(size, depth);
  1918. cv::Mat src2 = randomMat(size, depth);
  1919. if (depth == CV_64F && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  1920. {
  1921. try
  1922. {
  1923. cv::cuda::GpuMat dst;
  1924. cv::cuda::min(loadMat(src1), loadMat(src2), dst);
  1925. }
  1926. catch (const cv::Exception& e)
  1927. {
  1928. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  1929. }
  1930. }
  1931. else
  1932. {
  1933. cv::cuda::GpuMat dst = createMat(size, depth, useRoi);
  1934. cv::cuda::min(loadMat(src1, useRoi), loadMat(src2, useRoi), dst);
  1935. cv::Mat dst_gold = cv::min(src1, src2);
  1936. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  1937. }
  1938. }
  1939. CUDA_TEST_P(Min, Scalar)
  1940. {
  1941. cv::Mat src = randomMat(size, depth);
  1942. double val = randomDouble(0.0, 255.0);
  1943. if (depth == CV_64F && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  1944. {
  1945. try
  1946. {
  1947. cv::cuda::GpuMat dst;
  1948. cv::cuda::min(loadMat(src), val, dst);
  1949. }
  1950. catch (const cv::Exception& e)
  1951. {
  1952. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  1953. }
  1954. }
  1955. else
  1956. {
  1957. cv::cuda::GpuMat dst = createMat(size, depth, useRoi);
  1958. cv::cuda::min(loadMat(src, useRoi), val, dst);
  1959. cv::Mat dst_gold = cv::min(src, val);
  1960. EXPECT_MAT_NEAR(dst_gold, dst, depth < CV_32F ? 1.0 : 1e-5);
  1961. }
  1962. }
  1963. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Min, testing::Combine(
  1964. ALL_DEVICES,
  1965. DIFFERENT_SIZES,
  1966. ALL_DEPTH,
  1967. WHOLE_SUBMAT));
  1968. //////////////////////////////////////////////////////////////////////////////
  1969. // Max
  1970. PARAM_TEST_CASE(Max, cv::cuda::DeviceInfo, cv::Size, MatDepth, UseRoi)
  1971. {
  1972. cv::cuda::DeviceInfo devInfo;
  1973. cv::Size size;
  1974. int depth;
  1975. bool useRoi;
  1976. virtual void SetUp()
  1977. {
  1978. devInfo = GET_PARAM(0);
  1979. size = GET_PARAM(1);
  1980. depth = GET_PARAM(2);
  1981. useRoi = GET_PARAM(3);
  1982. cv::cuda::setDevice(devInfo.deviceID());
  1983. }
  1984. };
  1985. CUDA_TEST_P(Max, Array)
  1986. {
  1987. cv::Mat src1 = randomMat(size, depth);
  1988. cv::Mat src2 = randomMat(size, depth);
  1989. if (depth == CV_64F && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  1990. {
  1991. try
  1992. {
  1993. cv::cuda::GpuMat dst;
  1994. cv::cuda::max(loadMat(src1), loadMat(src2), dst);
  1995. }
  1996. catch (const cv::Exception& e)
  1997. {
  1998. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  1999. }
  2000. }
  2001. else
  2002. {
  2003. cv::cuda::GpuMat dst = createMat(size, depth, useRoi);
  2004. cv::cuda::max(loadMat(src1, useRoi), loadMat(src2, useRoi), dst);
  2005. cv::Mat dst_gold = cv::max(src1, src2);
  2006. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  2007. }
  2008. }
  2009. CUDA_TEST_P(Max, Scalar)
  2010. {
  2011. cv::Mat src = randomMat(size, depth);
  2012. double val = randomDouble(0.0, 255.0);
  2013. if (depth == CV_64F && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  2014. {
  2015. try
  2016. {
  2017. cv::cuda::GpuMat dst;
  2018. cv::cuda::max(loadMat(src), val, dst);
  2019. }
  2020. catch (const cv::Exception& e)
  2021. {
  2022. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  2023. }
  2024. }
  2025. else
  2026. {
  2027. cv::cuda::GpuMat dst = createMat(size, depth, useRoi);
  2028. cv::cuda::max(loadMat(src, useRoi), val, dst);
  2029. cv::Mat dst_gold = cv::max(src, val);
  2030. EXPECT_MAT_NEAR(dst_gold, dst, depth < CV_32F ? 1.0 : 1e-5);
  2031. }
  2032. }
  2033. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Max, testing::Combine(
  2034. ALL_DEVICES,
  2035. DIFFERENT_SIZES,
  2036. ALL_DEPTH,
  2037. WHOLE_SUBMAT));
  2038. //////////////////////////////////////////////////////////////////////////////
  2039. // AddWeighted
  2040. PARAM_TEST_CASE(AddWeighted, cv::cuda::DeviceInfo, cv::Size, MatDepth, MatDepth, MatDepth, UseRoi)
  2041. {
  2042. cv::cuda::DeviceInfo devInfo;
  2043. cv::Size size;
  2044. int depth1;
  2045. int depth2;
  2046. int dst_depth;
  2047. bool useRoi;
  2048. virtual void SetUp()
  2049. {
  2050. devInfo = GET_PARAM(0);
  2051. size = GET_PARAM(1);
  2052. depth1 = GET_PARAM(2);
  2053. depth2 = GET_PARAM(3);
  2054. dst_depth = GET_PARAM(4);
  2055. useRoi = GET_PARAM(5);
  2056. cv::cuda::setDevice(devInfo.deviceID());
  2057. }
  2058. };
  2059. CUDA_TEST_P(AddWeighted, Accuracy)
  2060. {
  2061. cv::Mat src1 = randomMat(size, depth1);
  2062. cv::Mat src2 = randomMat(size, depth2);
  2063. double alpha = randomDouble(-10.0, 10.0);
  2064. double beta = randomDouble(-10.0, 10.0);
  2065. double gamma = randomDouble(-10.0, 10.0);
  2066. if ((depth1 == CV_64F || depth2 == CV_64F || dst_depth == CV_64F) && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  2067. {
  2068. try
  2069. {
  2070. cv::cuda::GpuMat dst;
  2071. cv::cuda::addWeighted(loadMat(src1), alpha, loadMat(src2), beta, gamma, dst, dst_depth);
  2072. }
  2073. catch (const cv::Exception& e)
  2074. {
  2075. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  2076. }
  2077. }
  2078. else
  2079. {
  2080. cv::cuda::GpuMat dst = createMat(size, dst_depth, useRoi);
  2081. cv::cuda::addWeighted(loadMat(src1, useRoi), alpha, loadMat(src2, useRoi), beta, gamma, dst, dst_depth);
  2082. cv::Mat dst_gold;
  2083. cv::addWeighted(src1, alpha, src2, beta, gamma, dst_gold, dst_depth);
  2084. EXPECT_MAT_NEAR(dst_gold, dst, dst_depth < CV_32F ? 2.0 : 1e-3);
  2085. }
  2086. }
  2087. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, AddWeighted, testing::Combine(
  2088. ALL_DEVICES,
  2089. DIFFERENT_SIZES,
  2090. ALL_DEPTH,
  2091. ALL_DEPTH,
  2092. ALL_DEPTH,
  2093. WHOLE_SUBMAT));
  2094. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  2095. // Threshold
  2096. CV_ENUM(ThreshOp, cv::THRESH_BINARY, cv::THRESH_BINARY_INV, cv::THRESH_TRUNC, cv::THRESH_TOZERO, cv::THRESH_TOZERO_INV)
  2097. #define ALL_THRESH_OPS testing::Values(ThreshOp(cv::THRESH_BINARY), ThreshOp(cv::THRESH_BINARY_INV), ThreshOp(cv::THRESH_TRUNC), ThreshOp(cv::THRESH_TOZERO), ThreshOp(cv::THRESH_TOZERO_INV))
  2098. PARAM_TEST_CASE(Threshold, cv::cuda::DeviceInfo, cv::Size, MatType, Channels, ThreshOp, UseRoi)
  2099. {
  2100. cv::cuda::DeviceInfo devInfo;
  2101. cv::Size size;
  2102. int type;
  2103. int channel;
  2104. int threshOp;
  2105. bool useRoi;
  2106. virtual void SetUp()
  2107. {
  2108. devInfo = GET_PARAM(0);
  2109. size = GET_PARAM(1);
  2110. type = GET_PARAM(2);
  2111. channel = GET_PARAM(3);
  2112. threshOp = GET_PARAM(4);
  2113. useRoi = GET_PARAM(5);
  2114. cv::cuda::setDevice(devInfo.deviceID());
  2115. }
  2116. };
  2117. CUDA_TEST_P(Threshold, Accuracy)
  2118. {
  2119. cv::Mat src = randomMat(size, CV_MAKE_TYPE(type, channel));
  2120. double maxVal = randomDouble(20.0, 127.0);
  2121. double thresh = randomDouble(0.0, maxVal);
  2122. cv::cuda::GpuMat dst = createMat(src.size(), src.type(), useRoi);
  2123. cv::cuda::threshold(loadMat(src, useRoi), dst, thresh, maxVal, threshOp);
  2124. cv::Mat dst_gold;
  2125. cv::threshold(src, dst_gold, thresh, maxVal, threshOp);
  2126. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  2127. }
  2128. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Threshold, testing::Combine(
  2129. ALL_DEVICES,
  2130. DIFFERENT_SIZES,
  2131. testing::Values(MatDepth(CV_8U), MatDepth(CV_16S), MatDepth(CV_32F)),
  2132. ALL_CHANNELS,
  2133. ALL_THRESH_OPS,
  2134. WHOLE_SUBMAT));
  2135. ////////////////////////////////////////////////////////////////////////////////
  2136. // InRange
  2137. PARAM_TEST_CASE(InRange, cv::cuda::DeviceInfo, cv::Size, MatDepth, Channels, UseRoi)
  2138. {
  2139. cv::cuda::DeviceInfo devInfo;
  2140. cv::Size size;
  2141. int depth;
  2142. int channels;
  2143. bool useRoi;
  2144. virtual void SetUp()
  2145. {
  2146. devInfo = GET_PARAM(0);
  2147. size = GET_PARAM(1);
  2148. depth = GET_PARAM(2);
  2149. channels = GET_PARAM(3);
  2150. useRoi = GET_PARAM(4);
  2151. cv::cuda::setDevice(devInfo.deviceID());
  2152. }
  2153. };
  2154. CUDA_TEST_P(InRange, Accuracy)
  2155. {
  2156. // Set max value to 127 for signed char
  2157. const int max_bound = (depth == CV_8S) ? 127 : 255;
  2158. // Create lower and upper bound scalars, and make sure lowerb[i] <=
  2159. // upperb[i]
  2160. const cv::Scalar bound1 = randomScalar(0, max_bound);
  2161. const cv::Scalar bound2 = randomScalar(0, max_bound);
  2162. cv::Scalar lowerb, upperb;
  2163. for (int i = 0; i < 4; i++) {
  2164. lowerb[i] = std::min(bound1[i], bound2[i]);
  2165. upperb[i] = std::max(bound1[i], bound2[i]);
  2166. }
  2167. // Create mats and run CPU and GPU versions
  2168. const cv::Mat src = randomMat(size, CV_MAKE_TYPE(depth, channels));
  2169. cv::cuda::GpuMat dst;
  2170. cv::cuda::inRange(loadMat(src, useRoi), lowerb, upperb, dst);
  2171. cv::Mat dst_gold;
  2172. cv::inRange(src, lowerb, upperb, dst_gold);
  2173. EXPECT_MAT_NEAR(dst_gold, dst, 0);
  2174. }
  2175. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, InRange, testing::Combine(
  2176. ALL_DEVICES,
  2177. DIFFERENT_SIZES,
  2178. ALL_DEPTH,
  2179. ALL_CHANNELS,
  2180. WHOLE_SUBMAT));
  2181. ////////////////////////////////////////////////////////////////////////////////
  2182. // Magnitude
  2183. PARAM_TEST_CASE(Magnitude, cv::cuda::DeviceInfo, cv::Size, UseRoi)
  2184. {
  2185. cv::cuda::DeviceInfo devInfo;
  2186. cv::Size size;
  2187. bool useRoi;
  2188. virtual void SetUp()
  2189. {
  2190. devInfo = GET_PARAM(0);
  2191. size = GET_PARAM(1);
  2192. useRoi = GET_PARAM(2);
  2193. cv::cuda::setDevice(devInfo.deviceID());
  2194. }
  2195. };
  2196. CUDA_TEST_P(Magnitude, NPP)
  2197. {
  2198. cv::Mat src = randomMat(size, CV_32FC2);
  2199. cv::cuda::GpuMat dst = createMat(size, CV_32FC1, useRoi);
  2200. cv::cuda::magnitude(loadMat(src, useRoi), dst);
  2201. cv::Mat arr[2];
  2202. cv::split(src, arr);
  2203. cv::Mat dst_gold;
  2204. cv::magnitude(arr[0], arr[1], dst_gold);
  2205. EXPECT_MAT_NEAR(dst_gold, dst, 1e-4);
  2206. }
  2207. CUDA_TEST_P(Magnitude, Sqr_NPP)
  2208. {
  2209. cv::Mat src = randomMat(size, CV_32FC2);
  2210. cv::cuda::GpuMat dst = createMat(size, CV_32FC1, useRoi);
  2211. cv::cuda::magnitudeSqr(loadMat(src, useRoi), dst);
  2212. cv::Mat arr[2];
  2213. cv::split(src, arr);
  2214. cv::Mat dst_gold;
  2215. cv::magnitude(arr[0], arr[1], dst_gold);
  2216. cv::multiply(dst_gold, dst_gold, dst_gold);
  2217. EXPECT_MAT_NEAR(dst_gold, dst, 1e-1);
  2218. }
  2219. CUDA_TEST_P(Magnitude, Accuracy)
  2220. {
  2221. cv::Mat x = randomMat(size, CV_32FC1);
  2222. cv::Mat y = randomMat(size, CV_32FC1);
  2223. cv::cuda::GpuMat dst = createMat(size, CV_32FC1, useRoi);
  2224. cv::cuda::magnitude(loadMat(x, useRoi), loadMat(y, useRoi), dst);
  2225. cv::Mat dst_gold;
  2226. cv::magnitude(x, y, dst_gold);
  2227. EXPECT_MAT_NEAR(dst_gold, dst, 1e-4);
  2228. }
  2229. CUDA_TEST_P(Magnitude, Sqr_Accuracy)
  2230. {
  2231. cv::Mat x = randomMat(size, CV_32FC1);
  2232. cv::Mat y = randomMat(size, CV_32FC1);
  2233. cv::cuda::GpuMat dst = createMat(size, CV_32FC1, useRoi);
  2234. cv::cuda::magnitudeSqr(loadMat(x, useRoi), loadMat(y, useRoi), dst);
  2235. cv::Mat dst_gold;
  2236. cv::magnitude(x, y, dst_gold);
  2237. cv::multiply(dst_gold, dst_gold, dst_gold);
  2238. EXPECT_MAT_NEAR(dst_gold, dst, 1e-1);
  2239. }
  2240. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Magnitude, testing::Combine(
  2241. ALL_DEVICES,
  2242. DIFFERENT_SIZES,
  2243. WHOLE_SUBMAT));
  2244. ////////////////////////////////////////////////////////////////////////////////
  2245. // Phase
  2246. namespace
  2247. {
  2248. IMPLEMENT_PARAM_CLASS(AngleInDegrees, bool)
  2249. }
  2250. PARAM_TEST_CASE(Phase, cv::cuda::DeviceInfo, cv::Size, AngleInDegrees, UseRoi)
  2251. {
  2252. cv::cuda::DeviceInfo devInfo;
  2253. cv::Size size;
  2254. bool angleInDegrees;
  2255. bool useRoi;
  2256. virtual void SetUp()
  2257. {
  2258. devInfo = GET_PARAM(0);
  2259. size = GET_PARAM(1);
  2260. angleInDegrees = GET_PARAM(2);
  2261. useRoi = GET_PARAM(3);
  2262. cv::cuda::setDevice(devInfo.deviceID());
  2263. }
  2264. };
  2265. CUDA_TEST_P(Phase, Accuracy)
  2266. {
  2267. cv::Mat x = randomMat(size, CV_32FC1);
  2268. cv::Mat y = randomMat(size, CV_32FC1);
  2269. cv::cuda::GpuMat dst = createMat(size, CV_32FC1, useRoi);
  2270. cv::cuda::phase(loadMat(x, useRoi), loadMat(y, useRoi), dst, angleInDegrees);
  2271. cv::Mat dst_gold;
  2272. cv::phase(x, y, dst_gold, angleInDegrees);
  2273. EXPECT_MAT_NEAR(dst_gold, dst, angleInDegrees ? 1e-2 : 1e-3);
  2274. }
  2275. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Phase, testing::Combine(
  2276. ALL_DEVICES,
  2277. DIFFERENT_SIZES,
  2278. testing::Values(AngleInDegrees(false), AngleInDegrees(true)),
  2279. WHOLE_SUBMAT));
  2280. ////////////////////////////////////////////////////////////////////////////////
  2281. // CartToPolar
  2282. PARAM_TEST_CASE(CartToPolar, cv::cuda::DeviceInfo, cv::Size, AngleInDegrees, UseRoi)
  2283. {
  2284. cv::cuda::DeviceInfo devInfo;
  2285. cv::Size size;
  2286. bool angleInDegrees;
  2287. bool useRoi;
  2288. virtual void SetUp()
  2289. {
  2290. devInfo = GET_PARAM(0);
  2291. size = GET_PARAM(1);
  2292. angleInDegrees = GET_PARAM(2);
  2293. useRoi = GET_PARAM(3);
  2294. cv::cuda::setDevice(devInfo.deviceID());
  2295. }
  2296. };
  2297. CUDA_TEST_P(CartToPolar, Accuracy)
  2298. {
  2299. cv::Mat x = randomMat(size, CV_32FC1);
  2300. cv::Mat y = randomMat(size, CV_32FC1);
  2301. cv::cuda::GpuMat mag = createMat(size, CV_32FC1, useRoi);
  2302. cv::cuda::GpuMat angle = createMat(size, CV_32FC1, useRoi);
  2303. cv::cuda::cartToPolar(loadMat(x, useRoi), loadMat(y, useRoi), mag, angle, angleInDegrees);
  2304. cv::Mat mag_gold;
  2305. cv::Mat angle_gold;
  2306. cv::cartToPolar(x, y, mag_gold, angle_gold, angleInDegrees);
  2307. EXPECT_MAT_NEAR(mag_gold, mag, 1e-4);
  2308. EXPECT_MAT_NEAR(angle_gold, angle, angleInDegrees ? 1e-2 : 1e-3);
  2309. }
  2310. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, CartToPolar, testing::Combine(
  2311. ALL_DEVICES,
  2312. DIFFERENT_SIZES,
  2313. testing::Values(AngleInDegrees(false), AngleInDegrees(true)),
  2314. WHOLE_SUBMAT));
  2315. ////////////////////////////////////////////////////////////////////////////////
  2316. // polarToCart
  2317. PARAM_TEST_CASE(PolarToCart, cv::cuda::DeviceInfo, cv::Size, MatType, AngleInDegrees, UseRoi)
  2318. {
  2319. cv::cuda::DeviceInfo devInfo;
  2320. cv::Size size;
  2321. int type;
  2322. bool angleInDegrees;
  2323. bool useRoi;
  2324. virtual void SetUp()
  2325. {
  2326. devInfo = GET_PARAM(0);
  2327. size = GET_PARAM(1);
  2328. type = GET_PARAM(2);
  2329. angleInDegrees = GET_PARAM(3);
  2330. useRoi = GET_PARAM(4);
  2331. cv::cuda::setDevice(devInfo.deviceID());
  2332. }
  2333. };
  2334. CUDA_TEST_P(PolarToCart, Accuracy)
  2335. {
  2336. cv::Mat magnitude = randomMat(size, type);
  2337. cv::Mat angle = randomMat(size, type);
  2338. const double tol = (type == CV_32FC1 ? 1.6e-4 : 1e-4) * (angleInDegrees ? 1.0 : 19.47);
  2339. cv::cuda::GpuMat x = createMat(size, type, useRoi);
  2340. cv::cuda::GpuMat y = createMat(size, type, useRoi);
  2341. cv::cuda::polarToCart(loadMat(magnitude, useRoi), loadMat(angle, useRoi), x, y, angleInDegrees);
  2342. cv::Mat x_gold;
  2343. cv::Mat y_gold;
  2344. cv::polarToCart(magnitude, angle, x_gold, y_gold, angleInDegrees);
  2345. EXPECT_MAT_NEAR(x_gold, x, tol);
  2346. EXPECT_MAT_NEAR(y_gold, y, tol);
  2347. }
  2348. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, PolarToCart, testing::Combine(
  2349. ALL_DEVICES,
  2350. DIFFERENT_SIZES,
  2351. testing::Values(CV_32FC1, CV_64FC1),
  2352. testing::Values(AngleInDegrees(false), AngleInDegrees(true)),
  2353. WHOLE_SUBMAT));
  2354. }} // namespace
  2355. #endif // HAVE_CUDA