ImgprocTest.java 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075
  1. package org.opencv.test.imgproc;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import org.opencv.core.Core;
  6. import org.opencv.core.CvType;
  7. import org.opencv.core.Mat;
  8. import org.opencv.core.MatOfFloat;
  9. import org.opencv.core.MatOfInt;
  10. import org.opencv.core.MatOfInt4;
  11. import org.opencv.core.MatOfPoint;
  12. import org.opencv.core.MatOfPoint2f;
  13. import org.opencv.core.Point;
  14. import org.opencv.core.Rect;
  15. import org.opencv.core.RotatedRect;
  16. import org.opencv.core.Scalar;
  17. import org.opencv.core.Size;
  18. import org.opencv.core.TermCriteria;
  19. import org.opencv.imgproc.Imgproc;
  20. import org.opencv.test.OpenCVTestCase;
  21. public class ImgprocTest extends OpenCVTestCase {
  22. Point anchorPoint;
  23. private int imgprocSz;
  24. Size size;
  25. @Override
  26. protected void setUp() throws Exception {
  27. super.setUp();
  28. imgprocSz = 2;
  29. anchorPoint = new Point(2, 2);
  30. size = new Size(3, 3);
  31. }
  32. public void testAccumulateMatMat() {
  33. Mat src = getMat(CvType.CV_64F, 2);
  34. Mat dst = getMat(CvType.CV_64F, 0);
  35. Mat dst2 = src.clone();
  36. Imgproc.accumulate(src, dst);
  37. Imgproc.accumulate(src, dst2);
  38. assertMatEqual(src, dst, EPS);
  39. assertMatEqual(getMat(CvType.CV_64F, 4), dst2, EPS);
  40. }
  41. public void testAccumulateMatMatMat() {
  42. Mat src = getMat(CvType.CV_64F, 2);
  43. Mat mask = makeMask(getMat(CvType.CV_8U, 1));
  44. Mat dst = getMat(CvType.CV_64F, 0);
  45. Mat dst2 = src.clone();
  46. Imgproc.accumulate(src, dst, mask);
  47. Imgproc.accumulate(src, dst2, mask);
  48. assertMatEqual(makeMask(getMat(CvType.CV_64F, 2)), dst, EPS);
  49. assertMatEqual(makeMask(getMat(CvType.CV_64F, 4), 2), dst2, EPS);
  50. }
  51. public void testAccumulateProductMatMatMat() {
  52. Mat src = getMat(CvType.CV_64F, 2);
  53. Mat dst = getMat(CvType.CV_64F, 0);
  54. Mat dst2 = src.clone();
  55. Imgproc.accumulateProduct(src, src, dst);
  56. Imgproc.accumulateProduct(src, dst, dst2);
  57. assertMatEqual(getMat(CvType.CV_64F, 4), dst, EPS);
  58. assertMatEqual(getMat(CvType.CV_64F, 10), dst2, EPS);
  59. }
  60. public void testAccumulateProductMatMatMatMat() {
  61. Mat src = getMat(CvType.CV_64F, 2);
  62. Mat mask = makeMask(getMat(CvType.CV_8U, 1));
  63. Mat dst = getMat(CvType.CV_64F, 0);
  64. Mat dst2 = src.clone();
  65. Imgproc.accumulateProduct(src, src, dst, mask);
  66. Imgproc.accumulateProduct(src, dst, dst2, mask);
  67. assertMatEqual(makeMask(getMat(CvType.CV_64F, 4)), dst, EPS);
  68. assertMatEqual(makeMask(getMat(CvType.CV_64F, 10), 2), dst2, EPS);
  69. }
  70. public void testAccumulateSquareMatMat() {
  71. Mat src = getMat(CvType.CV_64F, 2);
  72. Mat dst = getMat(CvType.CV_64F, 0);
  73. Mat dst2 = src.clone();
  74. Imgproc.accumulateSquare(src, dst);
  75. Imgproc.accumulateSquare(src, dst2);
  76. assertMatEqual(getMat(CvType.CV_64F, 4), dst, EPS);
  77. assertMatEqual(getMat(CvType.CV_64F, 6), dst2, EPS);
  78. }
  79. public void testAccumulateSquareMatMatMat() {
  80. Mat src = getMat(CvType.CV_64F, 2);
  81. Mat mask = makeMask(getMat(CvType.CV_8U, 1));
  82. Mat dst = getMat(CvType.CV_64F, 0);
  83. Mat dst2 = src.clone();
  84. Imgproc.accumulateSquare(src, dst, mask);
  85. Imgproc.accumulateSquare(src, dst2, mask);
  86. assertMatEqual(makeMask(getMat(CvType.CV_64F, 4)), dst, EPS);
  87. assertMatEqual(makeMask(getMat(CvType.CV_64F, 6), 2), dst2, EPS);
  88. }
  89. public void testAccumulateWeightedMatMatDouble() {
  90. Mat src = getMat(CvType.CV_64F, 2);
  91. Mat dst = getMat(CvType.CV_64F, 4);
  92. Mat dst2 = src.clone();
  93. Imgproc.accumulateWeighted(src, dst, 0.5);
  94. Imgproc.accumulateWeighted(src, dst2, 2);
  95. assertMatEqual(getMat(CvType.CV_64F, 3), dst, EPS);
  96. assertMatEqual(getMat(CvType.CV_64F, 2), dst2, EPS);
  97. }
  98. public void testAccumulateWeightedMatMatDoubleMat() {
  99. Mat src = getMat(CvType.CV_64F, 2);
  100. Mat mask = makeMask(getMat(CvType.CV_8U, 1));
  101. Mat dst = getMat(CvType.CV_64F, 4);
  102. Mat dst2 = src.clone();
  103. Imgproc.accumulateWeighted(src, dst, 0.5, mask);
  104. Imgproc.accumulateWeighted(src, dst2, 2, mask);
  105. assertMatEqual(makeMask(getMat(CvType.CV_64F, 3), 4), dst, EPS);
  106. assertMatEqual(getMat(CvType.CV_64F, 2), dst2, EPS);
  107. }
  108. public void testAdaptiveThreshold() {
  109. Mat src = makeMask(getMat(CvType.CV_8U, 50), 20);
  110. Mat dst = new Mat();
  111. Imgproc.adaptiveThreshold(src, dst, 1, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 3, 0);
  112. assertEquals(src.rows(), Core.countNonZero(dst));
  113. }
  114. public void testApproxPolyDP() {
  115. MatOfPoint2f curve = new MatOfPoint2f(new Point(1, 3), new Point(2, 4), new Point(3, 5), new Point(4, 4), new Point(5, 3));
  116. MatOfPoint2f approxCurve = new MatOfPoint2f();
  117. Imgproc.approxPolyDP(curve, approxCurve, EPS, true);
  118. List<Point> approxCurveGold = new ArrayList<Point>(3);
  119. approxCurveGold.add(new Point(1, 3));
  120. approxCurveGold.add(new Point(3, 5));
  121. approxCurveGold.add(new Point(5, 3));
  122. assertListPointEquals(approxCurve.toList(), approxCurveGold, EPS);
  123. }
  124. public void testArcLength() {
  125. MatOfPoint2f curve = new MatOfPoint2f(new Point(1, 3), new Point(2, 4), new Point(3, 5), new Point(4, 4), new Point(5, 3));
  126. double arcLength = Imgproc.arcLength(curve, false);
  127. assertEquals(5.656854249, arcLength, 0.000001);
  128. }
  129. public void testBilateralFilterMatMatIntDoubleDouble() {
  130. Imgproc.bilateralFilter(gray255, dst, 5, 10, 5);
  131. assertMatEqual(gray255, dst);
  132. // TODO_: write better test
  133. }
  134. public void testBilateralFilterMatMatIntDoubleDoubleInt() {
  135. Imgproc.bilateralFilter(gray255, dst, 5, 10, 5, Core.BORDER_REFLECT);
  136. assertMatEqual(gray255, dst);
  137. // TODO_: write better test
  138. }
  139. public void testBlurMatMatSize() {
  140. Imgproc.blur(gray0, dst, size);
  141. assertMatEqual(gray0, dst);
  142. Imgproc.blur(gray255, dst, size);
  143. assertMatEqual(gray255, dst);
  144. // TODO_: write better test
  145. }
  146. public void testBlurMatMatSizePoint() {
  147. Imgproc.blur(gray0, dst, size, anchorPoint);
  148. assertMatEqual(gray0, dst);
  149. // TODO_: write better test
  150. }
  151. public void testBlurMatMatSizePointInt() {
  152. Imgproc.blur(gray0, dst, size, anchorPoint, Core.BORDER_REFLECT);
  153. assertMatEqual(gray0, dst);
  154. // TODO_: write better test
  155. }
  156. public void testBoundingRect() {
  157. MatOfPoint points = new MatOfPoint(new Point(0, 0), new Point(0, 4), new Point(4, 0), new Point(4, 4));
  158. Point p1 = new Point(1, 1);
  159. Point p2 = new Point(-5, -2);
  160. Rect bbox = Imgproc.boundingRect(points);
  161. assertTrue(bbox.contains(p1));
  162. assertFalse(bbox.contains(p2));
  163. }
  164. public void testBoxFilterMatMatIntSize() {
  165. Size size = new Size(3, 3);
  166. Imgproc.boxFilter(gray0, dst, 8, size);
  167. assertMatEqual(gray0, dst);
  168. // TODO_: write better test
  169. }
  170. public void testBoxFilterMatMatIntSizePointBoolean() {
  171. Imgproc.boxFilter(gray255, dst, 8, size, anchorPoint, false);
  172. assertMatEqual(gray255, dst);
  173. // TODO_: write better test
  174. }
  175. public void testBoxFilterMatMatIntSizePointBooleanInt() {
  176. Imgproc.boxFilter(gray255, dst, 8, size, anchorPoint, false, Core.BORDER_REFLECT);
  177. assertMatEqual(gray255, dst);
  178. // TODO_: write better test
  179. }
  180. public void testCalcBackProject() {
  181. List<Mat> images = Arrays.asList(grayChess);
  182. MatOfInt channels = new MatOfInt(0);
  183. MatOfInt histSize = new MatOfInt(10);
  184. MatOfFloat ranges = new MatOfFloat(0f, 256f);
  185. Mat hist = new Mat();
  186. Imgproc.calcHist(images, channels, new Mat(), hist, histSize, ranges);
  187. Core.normalize(hist, hist);
  188. Imgproc.calcBackProject(images, channels, hist, dst, ranges, 255);
  189. assertEquals(grayChess.size(), dst.size());
  190. assertEquals(grayChess.depth(), dst.depth());
  191. assertFalse(0 == Core.countNonZero(dst));
  192. }
  193. public void testCalcHistListOfMatListOfIntegerMatMatListOfIntegerListOfFloat() {
  194. List<Mat> images = Arrays.asList(gray128);
  195. MatOfInt channels = new MatOfInt(0);
  196. MatOfInt histSize = new MatOfInt(10);
  197. MatOfFloat ranges = new MatOfFloat(0f, 256f);
  198. Mat hist = new Mat();
  199. Imgproc.calcHist(images, channels, new Mat(), hist, histSize, ranges);
  200. truth = new Mat(10, 1, CvType.CV_32F, Scalar.all(0)) {
  201. {
  202. put(5, 0, 100);
  203. }
  204. };
  205. assertMatEqual(truth, hist, EPS);
  206. }
  207. public void testCalcHistListOfMatListOfIntegerMatMatListOfIntegerListOfFloat2D() {
  208. List<Mat> images = Arrays.asList(gray255, gray128);
  209. MatOfInt channels = new MatOfInt(0, 1);
  210. MatOfInt histSize = new MatOfInt(10, 10);
  211. MatOfFloat ranges = new MatOfFloat(0f, 256f, 0f, 256f);
  212. Mat hist = new Mat();
  213. Imgproc.calcHist(images, channels, new Mat(), hist, histSize, ranges);
  214. truth = new Mat(10, 10, CvType.CV_32F, Scalar.all(0)) {
  215. {
  216. put(9, 5, 100);
  217. }
  218. };
  219. assertMatEqual(truth, hist, EPS);
  220. }
  221. public void testCalcHistListOfMatListOfIntegerMatMatListOfIntegerListOfFloat3D() {
  222. List<Mat> images = Arrays.asList(rgbLena);
  223. Mat hist3D = new Mat();
  224. List<Mat> histList = Arrays.asList( new Mat[] {new Mat(), new Mat(), new Mat()} );
  225. MatOfInt histSize = new MatOfInt(10);
  226. MatOfFloat ranges = new MatOfFloat(0f, 256f);
  227. for(int i=0; i<rgbLena.channels(); i++)
  228. {
  229. Imgproc.calcHist(images, new MatOfInt(i), new Mat(), histList.get(i), histSize, ranges);
  230. assertEquals(10, histList.get(i).checkVector(1));
  231. }
  232. Core.merge(histList, hist3D);
  233. assertEquals(CvType.CV_32FC3, hist3D.type());
  234. assertEquals(10, hist3D.checkVector(3));
  235. Mat truth = new Mat(10, 1, CvType.CV_32FC3);
  236. truth.put(0, 0,
  237. 0, 24870, 0,
  238. 1863, 31926, 1,
  239. 56682, 37677, 2260,
  240. 77278, 44751, 32436,
  241. 69397, 41343, 18526,
  242. 27180, 40407, 18658,
  243. 21101, 15993, 32042,
  244. 8343, 18585, 47786,
  245. 300, 6567, 80988,
  246. 0, 25, 29447
  247. );
  248. assertMatEqual(truth, hist3D, EPS);
  249. }
  250. public void testCalcHistListOfMatListOfIntegerMatMatListOfIntegerListOfFloatBoolean() {
  251. List<Mat> images = Arrays.asList(gray255, gray128);
  252. MatOfInt channels = new MatOfInt(0, 1);
  253. MatOfInt histSize = new MatOfInt(10, 10);
  254. MatOfFloat ranges = new MatOfFloat(0f, 256f, 0f, 256f);
  255. Mat hist = new Mat();
  256. Imgproc.calcHist(images, channels, new Mat(), hist, histSize, ranges, true);
  257. truth = new Mat(10, 10, CvType.CV_32F, Scalar.all(0)) {
  258. {
  259. put(9, 5, 100);
  260. }
  261. };
  262. assertMatEqual(truth, hist, EPS);
  263. }
  264. public void testCannyMatMatDoubleDouble() {
  265. Imgproc.Canny(gray255, dst, 5, 10);
  266. assertMatEqual(gray0, dst);
  267. // TODO_: write better test
  268. }
  269. public void testCannyMatMatDoubleDoubleIntBoolean() {
  270. Imgproc.Canny(gray0, dst, 5, 10, 5, true);
  271. assertMatEqual(gray0, dst);
  272. // TODO_: write better test
  273. }
  274. public void testCompareHist() {
  275. Mat H1 = new Mat(3, 1, CvType.CV_32F);
  276. Mat H2 = new Mat(3, 1, CvType.CV_32F);
  277. H1.put(0, 0, 1, 2, 3);
  278. H2.put(0, 0, 4, 5, 6);
  279. double distance = Imgproc.compareHist(H1, H2, Imgproc.CV_COMP_CORREL);
  280. assertEquals(1., distance, EPS);
  281. }
  282. public void testContourAreaMat() {
  283. Mat contour = new Mat(1, 4, CvType.CV_32FC2);
  284. contour.put(0, 0, 0, 0, 10, 0, 10, 10, 5, 4);
  285. double area = Imgproc.contourArea(contour);
  286. assertEquals(45., area, EPS);
  287. }
  288. public void testContourAreaMatBoolean() {
  289. Mat contour = new Mat(1, 4, CvType.CV_32FC2);
  290. contour.put(0, 0, 0, 0, 10, 0, 10, 10, 5, 4);
  291. double area = Imgproc.contourArea(contour, true);
  292. assertEquals(45., area, EPS);
  293. // TODO_: write better test
  294. }
  295. public void testConvertMapsMatMatMatMatInt() {
  296. Mat map1 = new Mat(1, 4, CvType.CV_32FC1, new Scalar(1));
  297. Mat map2 = new Mat(1, 4, CvType.CV_32FC1, new Scalar(2));
  298. Mat dstmap1 = new Mat(1, 4, CvType.CV_16SC2);
  299. Mat dstmap2 = new Mat(1, 4, CvType.CV_16UC1);
  300. Imgproc.convertMaps(map1, map2, dstmap1, dstmap2, CvType.CV_16SC2);
  301. Mat truthMap1 = new Mat(1, 4, CvType.CV_16SC2);
  302. truthMap1.put(0, 0, 1, 2, 1, 2, 1, 2, 1, 2);
  303. assertMatEqual(truthMap1, dstmap1);
  304. Mat truthMap2 = new Mat(1, 4, CvType.CV_16UC1, new Scalar(0));
  305. assertMatEqual(truthMap2, dstmap2);
  306. }
  307. public void testConvertMapsMatMatMatMatIntBoolean() {
  308. Mat map1 = new Mat(1, 3, CvType.CV_32FC1, new Scalar(2));
  309. Mat map2 = new Mat(1, 3, CvType.CV_32FC1, new Scalar(4));
  310. Mat dstmap1 = new Mat(1, 3, CvType.CV_16SC2);
  311. Mat dstmap2 = new Mat(1, 3, CvType.CV_16UC1);
  312. Imgproc.convertMaps(map1, map2, dstmap1, dstmap2, CvType.CV_16SC2, false);
  313. // TODO_: write better test (last param == true)
  314. Mat truthMap1 = new Mat(1, 3, CvType.CV_16SC2);
  315. truthMap1.put(0, 0, 2, 4, 2, 4, 2, 4);
  316. assertMatEqual(truthMap1, dstmap1);
  317. Mat truthMap2 = new Mat(1, 3, CvType.CV_16UC1, new Scalar(0));
  318. assertMatEqual(truthMap2, dstmap2);
  319. }
  320. public void testConvexHullMatMat() {
  321. MatOfPoint points = new MatOfPoint(
  322. new Point(20, 0),
  323. new Point(40, 0),
  324. new Point(30, 20),
  325. new Point(0, 20),
  326. new Point(20, 10),
  327. new Point(30, 10)
  328. );
  329. MatOfInt hull = new MatOfInt();
  330. Imgproc.convexHull(points, hull);
  331. MatOfInt expHull = new MatOfInt(
  332. 0, 1, 2, 3
  333. );
  334. assertMatEqual(expHull, hull, EPS);
  335. }
  336. public void testConvexHullMatMatBooleanBoolean() {
  337. MatOfPoint points = new MatOfPoint(
  338. new Point(2, 0),
  339. new Point(4, 0),
  340. new Point(3, 2),
  341. new Point(0, 2),
  342. new Point(2, 1),
  343. new Point(3, 1)
  344. );
  345. MatOfInt hull = new MatOfInt();
  346. Imgproc.convexHull(points, hull, true);
  347. MatOfInt expHull = new MatOfInt(
  348. 3, 2, 1, 0
  349. );
  350. assertMatEqual(expHull, hull, EPS);
  351. }
  352. public void testConvexityDefects() {
  353. MatOfPoint points = new MatOfPoint(
  354. new Point(20, 0),
  355. new Point(40, 0),
  356. new Point(30, 20),
  357. new Point(0, 20),
  358. new Point(20, 10),
  359. new Point(30, 10)
  360. );
  361. MatOfInt hull = new MatOfInt();
  362. Imgproc.convexHull(points, hull);
  363. MatOfInt4 convexityDefects = new MatOfInt4();
  364. Imgproc.convexityDefects(points, hull, convexityDefects);
  365. assertMatEqual(new MatOfInt4(3, 0, 5, 3620), convexityDefects);
  366. }
  367. public void testCornerEigenValsAndVecsMatMatIntInt() {
  368. fail("Not yet implemented");
  369. // TODO: write better test
  370. Mat src = new Mat(imgprocSz, imgprocSz, CvType.CV_32FC1);
  371. src.put(0, 0, 1, 2);
  372. src.put(1, 0, 4, 2);
  373. int blockSize = 3;
  374. int ksize = 5;
  375. // TODO: eigen vals and vectors returned = 0 for most src matrices
  376. Imgproc.cornerEigenValsAndVecs(src, dst, blockSize, ksize);
  377. truth = new Mat(imgprocSz, imgprocSz, CvType.CV_32FC(6), new Scalar(0));
  378. assertMatEqual(truth, dst, EPS);
  379. }
  380. public void testCornerEigenValsAndVecsMatMatIntIntInt() {
  381. fail("Not yet implemented");
  382. // TODO: write better test
  383. Mat src = new Mat(4, 4, CvType.CV_32FC1, new Scalar(128));
  384. int blockSize = 3;
  385. int ksize = 5;
  386. truth = new Mat(4, 4, CvType.CV_32FC(6), new Scalar(0));
  387. Imgproc.cornerEigenValsAndVecs(src, dst, blockSize, ksize, Core.BORDER_REFLECT);
  388. assertMatEqual(truth, dst, EPS);
  389. }
  390. public void testCornerHarrisMatMatIntIntDouble() {
  391. fail("Not yet implemented");
  392. // TODO: write better test
  393. truth = new Mat(matSize, matSize, CvType.CV_32FC1, new Scalar(0));
  394. int blockSize = 5;
  395. int ksize = 7;
  396. double k = 0.1;
  397. Imgproc.cornerHarris(gray128, dst, blockSize, ksize, k);
  398. assertMatEqual(truth, dst, EPS);
  399. }
  400. public void testCornerHarrisMatMatIntIntDoubleInt() {
  401. fail("Not yet implemented");
  402. // TODO: write better test
  403. truth = new Mat(matSize, matSize, CvType.CV_32FC1, new Scalar(0));
  404. int blockSize = 5;
  405. int ksize = 7;
  406. double k = 0.1;
  407. Imgproc.cornerHarris(gray255, dst, blockSize, ksize, k, Core.BORDER_REFLECT);
  408. assertMatEqual(truth, dst, EPS);
  409. }
  410. public void testCornerMinEigenValMatMatInt() {
  411. fail("Not yet implemented");
  412. // TODO: write better test
  413. Mat src = new Mat(imgprocSz, imgprocSz, CvType.CV_32FC1);
  414. src.put(0, 0, 1, 2);
  415. src.put(1, 0, 2, 1);
  416. int blockSize = 5;
  417. Imgproc.cornerMinEigenVal(src, dst, blockSize);
  418. truth = new Mat(imgprocSz, imgprocSz, CvType.CV_32FC1, new Scalar(0));
  419. assertMatEqual(truth, dst, EPS);
  420. Imgproc.cornerMinEigenVal(gray255, dst, blockSize);
  421. truth = new Mat(matSize, matSize, CvType.CV_32FC1, new Scalar(0));
  422. assertMatEqual(truth, dst, EPS);
  423. }
  424. public void testCornerMinEigenValMatMatIntInt() {
  425. Mat src = Mat.eye(3, 3, CvType.CV_32FC1);
  426. int blockSize = 3;
  427. int ksize = 5;
  428. Imgproc.cornerMinEigenVal(src, dst, blockSize, ksize);
  429. truth = new Mat(3, 3, CvType.CV_32FC1) {
  430. {
  431. put(0, 0, 1. / 18, 1. / 36, 1. / 18);
  432. put(1, 0, 1. / 36, 1. / 18, 1. / 36);
  433. put(2, 0, 1. / 18, 1. / 36, 1. / 18);
  434. }
  435. };
  436. assertMatEqual(truth, dst, EPS);
  437. }
  438. public void testCornerMinEigenValMatMatIntIntInt() {
  439. Mat src = Mat.eye(3, 3, CvType.CV_32FC1);
  440. int blockSize = 3;
  441. int ksize = 5;
  442. Imgproc.cornerMinEigenVal(src, dst, blockSize, ksize, Core.BORDER_REFLECT);
  443. truth = new Mat(3, 3, CvType.CV_32FC1) {
  444. {
  445. put(0, 0, 0.68055558, 0.92708349, 0.5868057);
  446. put(1, 0, 0.92708343, 0.92708343, 0.92708343);
  447. put(2, 0, 0.58680564, 0.92708343, 0.68055564);
  448. }
  449. };
  450. assertMatEqual(truth, dst, EPS);
  451. }
  452. public void testCornerSubPix() {
  453. Mat img = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(128));
  454. Point truthPosition = new Point(img.cols() / 2, img.rows() / 2);
  455. Rect r = new Rect(new Point(0, 0), truthPosition);
  456. Imgproc.rectangle(img, r.tl(), r.br(), new Scalar(0), Imgproc.FILLED);
  457. MatOfPoint2f corners = new MatOfPoint2f(new Point(truthPosition.x + 1, truthPosition.y + 1));
  458. Size winSize = new Size(2, 2);
  459. Size zeroZone = new Size(-1, -1);
  460. TermCriteria criteria = new TermCriteria(TermCriteria.EPS, 0, 0.01);
  461. Imgproc.cornerSubPix(img, corners, winSize, zeroZone, criteria);
  462. assertPointEquals(truthPosition, corners.toList().get(0), weakEPS);
  463. }
  464. public void testCvtColorMatMatInt() {
  465. fail("Not yet implemented");
  466. }
  467. public void testCvtColorMatMatIntInt() {
  468. fail("Not yet implemented");
  469. }
  470. public void testDilateMatMatMat() {
  471. Mat kernel = new Mat();
  472. Imgproc.dilate(gray255, dst, kernel);
  473. assertMatEqual(gray255, dst);
  474. Imgproc.dilate(gray1, dst, kernel);
  475. assertMatEqual(gray1, dst);
  476. // TODO_: write better test
  477. }
  478. public void testDilateMatMatMatPoint() {
  479. fail("Not yet implemented");
  480. }
  481. public void testDilateMatMatMatPointInt() {
  482. fail("Not yet implemented");
  483. }
  484. public void testDilateMatMatMatPointIntInt() {
  485. fail("Not yet implemented");
  486. }
  487. public void testDilateMatMatMatPointIntIntScalar() {
  488. fail("Not yet implemented");
  489. }
  490. public void testDistanceTransformWithLabels() {
  491. Mat dstLables = getMat(CvType.CV_32SC1, 0);
  492. Mat labels = new Mat();
  493. Imgproc.distanceTransformWithLabels(gray128, dst, labels, Imgproc.CV_DIST_L2, 3);
  494. assertMatEqual(dstLables, labels);
  495. assertMatEqual(getMat(CvType.CV_32FC1, 8192), dst, EPS);
  496. }
  497. public void testDrawContoursMatListOfMatIntScalar() {
  498. Imgproc.rectangle(gray0, new Point(1, 2), new Point(7, 8), new Scalar(100));
  499. List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
  500. Imgproc.findContours(gray0, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
  501. Imgproc.drawContours(gray0, contours, -1, new Scalar(0));
  502. assertEquals(0, Core.countNonZero(gray0));
  503. }
  504. public void testDrawContoursMatListOfMatIntScalarInt() {
  505. Imgproc.rectangle(gray0, new Point(1, 2), new Point(7, 8), new Scalar(100));
  506. List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
  507. Imgproc.findContours(gray0, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
  508. Imgproc.drawContours(gray0, contours, -1, new Scalar(0), Imgproc.FILLED);
  509. assertEquals(0, Core.countNonZero(gray0));
  510. }
  511. public void testDrawContoursMatListOfMatIntScalarIntIntMatIntPoint() {
  512. fail("Not yet implemented");
  513. }
  514. public void testEqualizeHist() {
  515. Imgproc.equalizeHist(gray0, dst);
  516. assertMatEqual(gray0, dst);
  517. Imgproc.equalizeHist(gray255, dst);
  518. assertMatEqual(gray255, dst);
  519. // TODO_: write better test
  520. }
  521. public void testErodeMatMatMat() {
  522. Mat kernel = new Mat();
  523. Imgproc.erode(gray128, dst, kernel);
  524. assertMatEqual(gray128, dst);
  525. }
  526. public void testErodeMatMatMatPointInt() {
  527. Mat src = new Mat(3, 3, CvType.CV_8U) {
  528. {
  529. put(0, 0, 15, 9, 10);
  530. put(1, 0, 10, 8, 12);
  531. put(2, 0, 12, 20, 25);
  532. }
  533. };
  534. Mat kernel = new Mat();
  535. Imgproc.erode(src, dst, kernel, anchorPoint, 10);
  536. truth = new Mat(3, 3, CvType.CV_8U, new Scalar(8));
  537. assertMatEqual(truth, dst);
  538. }
  539. public void testErodeMatMatMatPointIntIntScalar() {
  540. Mat src = new Mat(3, 3, CvType.CV_8U) {
  541. {
  542. put(0, 0, 15, 9, 10);
  543. put(1, 0, 10, 8, 12);
  544. put(2, 0, 12, 20, 25);
  545. }
  546. };
  547. Mat kernel = new Mat();
  548. Scalar sc = new Scalar(3, 3);
  549. Imgproc.erode(src, dst, kernel, anchorPoint, 10, Core.BORDER_REFLECT, sc);
  550. truth = new Mat(3, 3, CvType.CV_8U, new Scalar(8));
  551. assertMatEqual(truth, dst);
  552. }
  553. public void testFilter2DMatMatIntMat() {
  554. Mat src = Mat.eye(4, 4, CvType.CV_32F);
  555. Mat kernel = new Mat(imgprocSz, imgprocSz, CvType.CV_32F, new Scalar(1));
  556. Imgproc.filter2D(src, dst, -1, kernel);
  557. truth = new Mat(4, 4, CvType.CV_32F) {
  558. {
  559. put(0, 0, 2, 2, 1, 0);
  560. put(1, 0, 2, 2, 1, 0);
  561. put(2, 0, 1, 1, 2, 1);
  562. put(3, 0, 0, 0, 1, 2);
  563. }
  564. };
  565. assertMatEqual(truth, dst, EPS);
  566. }
  567. public void testFilter2DMatMatIntMatPointDouble() {
  568. fail("Not yet implemented");
  569. }
  570. public void testFilter2DMatMatIntMatPointDoubleInt() {
  571. Mat kernel = new Mat(imgprocSz, imgprocSz, CvType.CV_32F, new Scalar(0));
  572. Point point = new Point(0, 0);
  573. Imgproc.filter2D(gray128, dst, -1, kernel, point, 2, Core.BORDER_CONSTANT);
  574. assertMatEqual(gray2, dst);
  575. }
  576. public void testFindContoursMatListOfMatMatIntInt() {
  577. Mat img = new Mat(50, 50, CvType.CV_8UC1, new Scalar(0));
  578. List<MatOfPoint> contours = new ArrayList<MatOfPoint>(5);
  579. Mat hierarchy = new Mat();
  580. Imgproc.findContours(img, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
  581. // no contours on empty image
  582. assertEquals(contours.size(), 0);
  583. assertEquals(contours.size(), hierarchy.total());
  584. Imgproc.rectangle(img, new Point(10, 20), new Point(20, 30), new Scalar(100), 3, Imgproc.LINE_AA, 0);
  585. Imgproc.rectangle(img, new Point(30, 35), new Point(40, 45), new Scalar(200));
  586. Imgproc.findContours(img, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
  587. // two contours of two rectangles
  588. assertEquals(contours.size(), 2);
  589. assertEquals(contours.size(), hierarchy.total());
  590. }
  591. public void testFindContoursMatListOfMatMatIntIntPoint() {
  592. Mat img = new Mat(50, 50, CvType.CV_8UC1, new Scalar(0));
  593. Mat img2 = img.submat(5, 50, 3, 50);
  594. List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
  595. List<MatOfPoint> contours2 = new ArrayList<MatOfPoint>();
  596. Mat hierarchy = new Mat();
  597. Imgproc.rectangle(img, new Point(10, 20), new Point(20, 30), new Scalar(100), 3, Imgproc.LINE_AA, 0);
  598. Imgproc.rectangle(img, new Point(30, 35), new Point(40, 45), new Scalar(200));
  599. Imgproc.findContours(img, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
  600. Imgproc.findContours(img2, contours2, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, new Point(3, 5));
  601. assertEquals(contours.size(), contours2.size());
  602. assertMatEqual(contours.get(0), contours2.get(0));
  603. /*
  604. Log.d("findContours", "hierarchy=" + hierarchy);
  605. int iBuff[] = new int[ (int) (hierarchy.total() * hierarchy.channels()) ]; // [ Contour0 (next sibling num, previous sibling num, 1st child num, parent num), Contour1(...), ...
  606. hierarchy.get(0, 0, iBuff);
  607. Log.d("findContours", Arrays.toString(iBuff));
  608. */
  609. }
  610. public void testFitEllipse() {
  611. MatOfPoint2f points = new MatOfPoint2f(new Point(0, 0), new Point(-1, 1), new Point(1, 1), new Point(1, -1), new Point(-1, -1));
  612. RotatedRect rrect = new RotatedRect();
  613. rrect = Imgproc.fitEllipse(points);
  614. double FIT_ELLIPSE_CENTER_EPS = 0.01;
  615. double FIT_ELLIPSE_SIZE_EPS = 0.4;
  616. assertEquals(0.0, rrect.center.x, FIT_ELLIPSE_CENTER_EPS);
  617. assertEquals(0.0, rrect.center.y, FIT_ELLIPSE_CENTER_EPS);
  618. assertEquals(2.828, rrect.size.width, FIT_ELLIPSE_SIZE_EPS);
  619. assertEquals(2.828, rrect.size.height, FIT_ELLIPSE_SIZE_EPS);
  620. }
  621. public void testFitLine() {
  622. Mat points = new Mat(1, 4, CvType.CV_32FC2);
  623. points.put(0, 0, 0, 0, 2, 3, 3, 4, 5, 8);
  624. Mat linePoints = new Mat(4, 1, CvType.CV_32FC1);
  625. linePoints.put(0, 0, 0.53198653, 0.84675282, 2.5, 3.75);
  626. Imgproc.fitLine(points, dst, Imgproc.CV_DIST_L12, 0, 0.01, 0.01);
  627. assertMatEqual(linePoints, dst, EPS);
  628. }
  629. public void testFloodFillMatMatPointScalar() {
  630. Mat mask = new Mat(matSize + 2, matSize + 2, CvType.CV_8U, new Scalar(0));
  631. Mat img = gray0;
  632. Imgproc.circle(mask, new Point(matSize / 2 + 1, matSize / 2 + 1), 3, new Scalar(2));
  633. int retval = Imgproc.floodFill(img, mask, new Point(matSize / 2, matSize / 2), new Scalar(1));
  634. assertEquals(Core.countNonZero(img), retval);
  635. Imgproc.circle(mask, new Point(matSize / 2 + 1, matSize / 2 + 1), 3, new Scalar(0));
  636. assertEquals(retval + 4 * (matSize + 1), Core.countNonZero(mask));
  637. assertMatEqual(mask.submat(1, matSize + 1, 1, matSize + 1), img);
  638. }
  639. public void testFloodFillMatMatPointScalar_WithoutMask() {
  640. Mat img = gray0;
  641. Imgproc.circle(img, new Point(matSize / 2, matSize / 2), 3, new Scalar(2));
  642. // TODO: ideally we should pass null instead of "new Mat()"
  643. int retval = Imgproc.floodFill(img, new Mat(), new Point(matSize / 2, matSize / 2), new Scalar(1));
  644. Imgproc.circle(img, new Point(matSize / 2, matSize / 2), 3, new Scalar(0));
  645. assertEquals(Core.countNonZero(img), retval);
  646. }
  647. public void testFloodFillMatMatPointScalarRect() {
  648. fail("Not yet implemented");
  649. }
  650. public void testFloodFillMatMatPointScalarRectScalar() {
  651. fail("Not yet implemented");
  652. }
  653. public void testFloodFillMatMatPointScalarRectScalarScalar() {
  654. fail("Not yet implemented");
  655. }
  656. public void testFloodFillMatMatPointScalarRectScalarScalarInt() {
  657. fail("Not yet implemented");
  658. }
  659. public void testGaussianBlurMatMatSizeDouble() {
  660. Imgproc.GaussianBlur(gray0, dst, size, 1);
  661. assertMatEqual(gray0, dst);
  662. Imgproc.GaussianBlur(gray2, dst, size, 1);
  663. assertMatEqual(gray2, dst);
  664. }
  665. public void testGaussianBlurMatMatSizeDoubleDouble() {
  666. Imgproc.GaussianBlur(gray2, dst, size, 0, 0);
  667. assertMatEqual(gray2, dst);
  668. // TODO_: write better test
  669. }
  670. public void testGaussianBlurMatMatSizeDoubleDoubleInt() {
  671. Imgproc.GaussianBlur(gray2, dst, size, 1, 3, Core.BORDER_REFLECT);
  672. assertMatEqual(gray2, dst);
  673. // TODO_: write better test
  674. }
  675. public void testGetAffineTransform() {
  676. MatOfPoint2f src = new MatOfPoint2f(new Point(2, 3), new Point(3, 1), new Point(1, 4));
  677. MatOfPoint2f dst = new MatOfPoint2f(new Point(3, 3), new Point(7, 4), new Point(5, 6));
  678. Mat transform = Imgproc.getAffineTransform(src, dst);
  679. Mat truth = new Mat(2, 3, CvType.CV_64FC1) {
  680. {
  681. put(0, 0, -8, -6, 37);
  682. put(1, 0, -7, -4, 29);
  683. }
  684. };
  685. assertMatEqual(truth, transform, EPS);
  686. }
  687. public void testGetDerivKernelsMatMatIntIntInt() {
  688. Mat kx = new Mat(imgprocSz, imgprocSz, CvType.CV_32F);
  689. Mat ky = new Mat(imgprocSz, imgprocSz, CvType.CV_32F);
  690. Mat expKx = new Mat(3, 1, CvType.CV_32F);
  691. Mat expKy = new Mat(3, 1, CvType.CV_32F);
  692. kx.put(0, 0, 1, 1);
  693. kx.put(1, 0, 1, 1);
  694. ky.put(0, 0, 2, 2);
  695. ky.put(1, 0, 2, 2);
  696. expKx.put(0, 0, 1, -2, 1);
  697. expKy.put(0, 0, 1, -2, 1);
  698. Imgproc.getDerivKernels(kx, ky, 2, 2, 3);
  699. assertMatEqual(expKx, kx, EPS);
  700. assertMatEqual(expKy, ky, EPS);
  701. }
  702. public void testGetDerivKernelsMatMatIntIntIntBooleanInt() {
  703. Mat kx = new Mat(imgprocSz, imgprocSz, CvType.CV_32F);
  704. Mat ky = new Mat(imgprocSz, imgprocSz, CvType.CV_32F);
  705. Mat expKx = new Mat(3, 1, CvType.CV_32F);
  706. Mat expKy = new Mat(3, 1, CvType.CV_32F);
  707. kx.put(0, 0, 1, 1);
  708. kx.put(1, 0, 1, 1);
  709. ky.put(0, 0, 2, 2);
  710. ky.put(1, 0, 2, 2);
  711. expKx.put(0, 0, 1, -2, 1);
  712. expKy.put(0, 0, 1, -2, 1);
  713. Imgproc.getDerivKernels(kx, ky, 2, 2, 3, true, CvType.CV_32F);
  714. assertMatEqual(expKx, kx, EPS);
  715. assertMatEqual(expKy, ky, EPS);
  716. // TODO_: write better test
  717. }
  718. public void testGetGaussianKernelIntDouble() {
  719. dst = Imgproc.getGaussianKernel(1, 0.5);
  720. truth = new Mat(1, 1, CvType.CV_64FC1, new Scalar(1));
  721. assertMatEqual(truth, dst, EPS);
  722. }
  723. public void testGetGaussianKernelIntDoubleInt() {
  724. dst = Imgproc.getGaussianKernel(3, 0.8, CvType.CV_32F);
  725. truth = new Mat(3, 1, CvType.CV_32F);
  726. truth.put(0, 0, 0.23899426, 0.52201146, 0.23899426);
  727. assertMatEqual(truth, dst, EPS);
  728. }
  729. public void testGetPerspectiveTransform() {
  730. fail("Not yet implemented");
  731. }
  732. public void testGetRectSubPixMatSizePointMat() {
  733. Size size = new Size(3, 3);
  734. Point center = new Point(gray255.cols() / 2, gray255.rows() / 2);
  735. Imgproc.getRectSubPix(gray255, size, center, dst);
  736. truth = new Mat(3, 3, CvType.CV_8U, new Scalar(255));
  737. assertMatEqual(truth, dst);
  738. }
  739. public void testGetRectSubPixMatSizePointMatInt() {
  740. Mat src = new Mat(10, 10, CvType.CV_32F, new Scalar(2));
  741. Size patchSize = new Size(5, 5);
  742. Point center = new Point(src.cols() / 2, src.rows() / 2);
  743. Imgproc.getRectSubPix(src, patchSize, center, dst);
  744. truth = new Mat(5, 5, CvType.CV_32F, new Scalar(2));
  745. assertMatEqual(truth, dst, EPS);
  746. }
  747. public void testGetRotationMatrix2D() {
  748. Point center = new Point(0, 0);
  749. dst = Imgproc.getRotationMatrix2D(center, 0, 1);
  750. truth = new Mat(2, 3, CvType.CV_64F) {
  751. {
  752. put(0, 0, 1, 0, 0);
  753. put(1, 0, 0, 1, 0);
  754. }
  755. };
  756. assertMatEqual(truth, dst, EPS);
  757. }
  758. public void testGetStructuringElementIntSize() {
  759. dst = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, size);
  760. truth = new Mat(3, 3, CvType.CV_8UC1, new Scalar(1));
  761. assertMatEqual(truth, dst);
  762. }
  763. public void testGetStructuringElementIntSizePoint() {
  764. dst = Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, size, anchorPoint);
  765. truth = new Mat(3, 3, CvType.CV_8UC1) {
  766. {
  767. put(0, 0, 0, 0, 1);
  768. put(1, 0, 0, 0, 1);
  769. put(2, 0, 1, 1, 1);
  770. }
  771. };
  772. assertMatEqual(truth, dst);
  773. }
  774. public void testGoodFeaturesToTrackMatListOfPointIntDoubleDouble() {
  775. Mat src = gray0;
  776. Imgproc.rectangle(src, new Point(2, 2), new Point(8, 8), new Scalar(100), -1);
  777. MatOfPoint lp = new MatOfPoint();
  778. Imgproc.goodFeaturesToTrack(src, lp, 100, 0.01, 3);
  779. assertEquals(4, lp.total());
  780. }
  781. public void testGoodFeaturesToTrackMatListOfPointIntDoubleDoubleMatIntBooleanDouble() {
  782. Mat src = gray0;
  783. Imgproc.rectangle(src, new Point(2, 2), new Point(8, 8), new Scalar(100), -1);
  784. MatOfPoint lp = new MatOfPoint();
  785. Imgproc.goodFeaturesToTrack(src, lp, 100, 0.01, 3, gray1, 4, 3, true, 0);
  786. assertEquals(4, lp.total());
  787. }
  788. public void testGrabCutMatMatRectMatMatInt() {
  789. fail("Not yet implemented");
  790. }
  791. public void testGrabCutMatMatRectMatMatIntInt() {
  792. fail("Not yet implemented");
  793. }
  794. public void testHoughCirclesMatMatIntDoubleDouble() {
  795. int sz = 512;
  796. Mat img = new Mat(sz, sz, CvType.CV_8U, new Scalar(128));
  797. Mat circles = new Mat();
  798. Imgproc.HoughCircles(img, circles, Imgproc.CV_HOUGH_GRADIENT, 2, img.rows() / 4);
  799. assertEquals(0, circles.cols());
  800. }
  801. public void testHoughCirclesMatMatIntDoubleDouble1() {
  802. int sz = 512;
  803. Mat img = new Mat(sz, sz, CvType.CV_8U, new Scalar(128));
  804. Mat circles = new Mat();
  805. Point center = new Point(img.cols() / 2, img.rows() / 2);
  806. int radius = Math.min(img.cols() / 4, img.rows() / 4);
  807. Imgproc.circle(img, center, radius, colorBlack, 3);
  808. Imgproc.HoughCircles(img, circles, Imgproc.CV_HOUGH_GRADIENT, 2, img.rows() / 4);
  809. assertEquals(1, circles.cols());
  810. }
  811. public void testHoughCirclesMatMatIntDoubleDoubleDoubleDoubleIntInt() {
  812. fail("Not yet implemented");
  813. }
  814. public void testHoughLinesMatMatDoubleDoubleInt() {
  815. int sz = 512;
  816. Mat img = new Mat(sz, sz, CvType.CV_8U, new Scalar(0));
  817. Point point1 = new Point(50, 50);
  818. Point point2 = new Point(img.cols() / 2, img.rows() / 2);
  819. Imgproc.line(img, point1, point2, colorWhite, 1);
  820. Mat lines = new Mat();
  821. Imgproc.HoughLines(img, lines, 1, 3.1415926/180, 100);
  822. assertEquals(1, lines.cols());
  823. /*
  824. Log.d("HoughLines", "lines=" + lines);
  825. int num = (int)lines.total();
  826. int buff[] = new int[num*4]; //[ (x1, y1, x2, y2), (...), ...]
  827. lines.get(0, 0, buff);
  828. Log.d("HoughLines", "lines=" + Arrays.toString(buff));
  829. */
  830. }
  831. public void testHoughLinesMatMatDoubleDoubleIntDouble() {
  832. fail("Not yet implemented");
  833. }
  834. public void testHoughLinesMatMatDoubleDoubleIntDoubleDouble() {
  835. fail("Not yet implemented");
  836. }
  837. public void testHoughLinesPMatMatDoubleDoubleInt() {
  838. int sz = 512;
  839. Mat img = new Mat(sz, sz, CvType.CV_8U, new Scalar(0));
  840. Point point1 = new Point(0, 0);
  841. Point point2 = new Point(sz, sz);
  842. Point point3 = new Point(sz, 0);
  843. Point point4 = new Point(2*sz/3, sz/3);
  844. Imgproc.line(img, point1, point2, Scalar.all(255), 1);
  845. Imgproc.line(img, point3, point4, Scalar.all(255), 1);
  846. Mat lines = new Mat();
  847. Imgproc.HoughLinesP(img, lines, 1, 3.1415926/180, 100);
  848. assertEquals(2, lines.rows());
  849. /*
  850. Log.d("HoughLinesP", "lines=" + lines);
  851. int num = (int)lines.cols();
  852. int buff[] = new int[num*4]; // CV_32SC4 as [ (x1, y1, x2, y2), (...), ...]
  853. lines.get(0, 0, buff);
  854. Log.d("HoughLinesP", "lines=" + Arrays.toString(buff));
  855. */
  856. }
  857. public void testHoughLinesPMatMatDoubleDoubleIntDouble() {
  858. fail("Not yet implemented");
  859. }
  860. public void testHoughLinesPMatMatDoubleDoubleIntDoubleDouble() {
  861. fail("Not yet implemented");
  862. }
  863. public void testHuMoments() {
  864. fail("Not yet implemented");
  865. }
  866. public void testIntegral2MatMatMat() {
  867. Mat src = new Mat(3, 3, CvType.CV_32F, new Scalar(3));
  868. Mat expSum = new Mat(4, 4, CvType.CV_64F);
  869. Mat expSqsum = new Mat(4, 4, CvType.CV_64F);
  870. Mat sum = new Mat();
  871. Mat sqsum = new Mat();
  872. expSum.put(0, 0, 0, 0, 0, 0);
  873. expSum.put(1, 0, 0, 3, 6, 9);
  874. expSum.put(2, 0, 0, 6, 12, 18);
  875. expSum.put(3, 0, 0, 9, 18, 27);
  876. expSqsum.put(0, 0, 0, 0, 0, 0);
  877. expSqsum.put(1, 0, 0, 9, 18, 27);
  878. expSqsum.put(2, 0, 0, 18, 36, 54);
  879. expSqsum.put(3, 0, 0, 27, 54, 81);
  880. Imgproc.integral2(src, sum, sqsum);
  881. assertMatEqual(expSum, sum, EPS);
  882. assertMatEqual(expSqsum, sqsum, EPS);
  883. }
  884. public void testIntegral2MatMatMatInt() {
  885. Mat src = new Mat(3, 3, CvType.CV_32F, new Scalar(3));
  886. Mat expSum = new Mat(4, 4, CvType.CV_64F);
  887. Mat expSqsum = new Mat(4, 4, CvType.CV_64F);
  888. Mat sum = new Mat();
  889. Mat sqsum = new Mat();
  890. expSum.put(0, 0, 0, 0, 0, 0);
  891. expSum.put(1, 0, 0, 3, 6, 9);
  892. expSum.put(2, 0, 0, 6, 12, 18);
  893. expSum.put(3, 0, 0, 9, 18, 27);
  894. expSqsum.put(0, 0, 0, 0, 0, 0);
  895. expSqsum.put(1, 0, 0, 9, 18, 27);
  896. expSqsum.put(2, 0, 0, 18, 36, 54);
  897. expSqsum.put(3, 0, 0, 27, 54, 81);
  898. Imgproc.integral2(src, sum, sqsum, CvType.CV_64F, CvType.CV_64F);
  899. assertMatEqual(expSum, sum, EPS);
  900. assertMatEqual(expSqsum, sqsum, EPS);
  901. }
  902. public void testIntegral3MatMatMatMat() {
  903. Mat src = new Mat(1, 1, CvType.CV_32F, new Scalar(1));
  904. Mat expSum = new Mat(imgprocSz, imgprocSz, CvType.CV_64F);
  905. Mat expSqsum = new Mat(imgprocSz, imgprocSz, CvType.CV_64F);
  906. Mat expTilted = new Mat(imgprocSz, imgprocSz, CvType.CV_64F);
  907. Mat sum = new Mat();
  908. Mat sqsum = new Mat();
  909. Mat tilted = new Mat();
  910. expSum.put(0, 0, 0, 0);
  911. expSum.put(1, 0, 0, 1);
  912. expSqsum.put(0, 0, 0, 0);
  913. expSqsum.put(1, 0, 0, 1);
  914. expTilted.put(0, 0, 0, 0);
  915. expTilted.put(1, 0, 0, 1);
  916. Imgproc.integral3(src, sum, sqsum, tilted);
  917. assertMatEqual(expSum, sum, EPS);
  918. assertMatEqual(expSqsum, sqsum, EPS);
  919. assertMatEqual(expTilted, tilted, EPS);
  920. }
  921. public void testIntegral3MatMatMatMatInt() {
  922. Mat src = new Mat(1, 1, CvType.CV_32F, new Scalar(1));
  923. Mat expSum = new Mat(imgprocSz, imgprocSz, CvType.CV_64F);
  924. Mat expSqsum = new Mat(imgprocSz, imgprocSz, CvType.CV_64F);
  925. Mat expTilted = new Mat(imgprocSz, imgprocSz, CvType.CV_64F);
  926. Mat sum = new Mat();
  927. Mat sqsum = new Mat();
  928. Mat tilted = new Mat();
  929. expSum.put(0, 0, 0, 0);
  930. expSum.put(1, 0, 0, 1);
  931. expSqsum.put(0, 0, 0, 0);
  932. expSqsum.put(1, 0, 0, 1);
  933. expTilted.put(0, 0, 0, 0);
  934. expTilted.put(1, 0, 0, 1);
  935. Imgproc.integral3(src, sum, sqsum, tilted, CvType.CV_64F, CvType.CV_64F);
  936. assertMatEqual(expSum, sum, EPS);
  937. assertMatEqual(expSqsum, sqsum, EPS);
  938. assertMatEqual(expTilted, tilted, EPS);
  939. }
  940. public void testIntegralMatMat() {
  941. Mat src = new Mat(imgprocSz, imgprocSz, CvType.CV_32F, new Scalar(2));
  942. Imgproc.integral(src, dst);
  943. truth = new Mat(3, 3, CvType.CV_64F) {
  944. {
  945. put(0, 0, 0, 0, 0);
  946. put(1, 0, 0, 2, 4);
  947. put(2, 0, 0, 4, 8);
  948. }
  949. };
  950. assertMatEqual(truth, dst, EPS);
  951. }
  952. public void testIntegralMatMatInt() {
  953. Mat src = new Mat(imgprocSz, imgprocSz, CvType.CV_32F, new Scalar(2));
  954. Imgproc.integral(src, dst, CvType.CV_64F);
  955. truth = new Mat(3, 3, CvType.CV_64F) {
  956. {
  957. put(0, 0, 0, 0, 0);
  958. put(1, 0, 0, 2, 4);
  959. put(2, 0, 0, 4, 8);
  960. }
  961. };
  962. assertMatEqual(truth, dst, EPS);
  963. }
  964. public void testInvertAffineTransform() {
  965. Mat src = new Mat(2, 3, CvType.CV_64F, new Scalar(1));
  966. Imgproc.invertAffineTransform(src, dst);
  967. truth = new Mat(2, 3, CvType.CV_64F, new Scalar(0));
  968. assertMatEqual(truth, dst, EPS);
  969. }
  970. public void testIsContourConvex() {
  971. MatOfPoint contour1 = new MatOfPoint(new Point(0, 0), new Point(10, 0), new Point(10, 10), new Point(5, 4));
  972. assertFalse(Imgproc.isContourConvex(contour1));
  973. MatOfPoint contour2 = new MatOfPoint(new Point(0, 0), new Point(10, 0), new Point(10, 10), new Point(5, 6));
  974. assertTrue(Imgproc.isContourConvex(contour2));
  975. }
  976. public void testLaplacianMatMatInt() {
  977. Imgproc.Laplacian(gray0, dst, CvType.CV_8U);
  978. assertMatEqual(gray0, dst);
  979. }
  980. public void testLaplacianMatMatIntIntDoubleDouble() {
  981. Mat src = Mat.eye(imgprocSz, imgprocSz, CvType.CV_32F);
  982. Imgproc.Laplacian(src, dst, CvType.CV_32F, 1, 2, EPS);
  983. truth = new Mat(imgprocSz, imgprocSz, CvType.CV_32F) {
  984. {
  985. put(0, 0, -7.9990001, 8.0009995);
  986. put(1, 0, 8.0009995, -7.9990001);
  987. }
  988. };
  989. assertMatEqual(truth, dst, EPS);
  990. }
  991. public void testLaplacianMatMatIntIntDoubleDoubleInt() {
  992. Mat src = new Mat(3, 3, CvType.CV_32F, new Scalar(2));
  993. Imgproc.Laplacian(src, dst, CvType.CV_32F, 1, 2, EPS, Core.BORDER_REFLECT);
  994. truth = new Mat(3, 3, CvType.CV_32F, new Scalar(0.00099945068));
  995. assertMatEqual(truth, dst, EPS);
  996. }
  997. public void testMatchShapes() {
  998. Mat contour1 = new Mat(1, 4, CvType.CV_32FC2);
  999. Mat contour2 = new Mat(1, 4, CvType.CV_32FC2);
  1000. contour1.put(0, 0, 1, 1, 5, 1, 4, 3, 6, 2);
  1001. contour2.put(0, 0, 1, 1, 6, 1, 4, 1, 2, 5);
  1002. double distance = Imgproc.matchShapes(contour1, contour2, Imgproc.CV_CONTOURS_MATCH_I1, 1);
  1003. assertEquals(2.81109697365334, distance, EPS);
  1004. }
  1005. public void testMatchTemplate() {
  1006. Mat image = new Mat(imgprocSz, imgprocSz, CvType.CV_8U);
  1007. Mat templ = new Mat(imgprocSz, imgprocSz, CvType.CV_8U);
  1008. image.put(0, 0, 1, 2, 3, 4);
  1009. templ.put(0, 0, 5, 6, 7, 8);
  1010. Imgproc.matchTemplate(image, templ, dst, Imgproc.TM_CCORR);
  1011. truth = new Mat(1, 1, CvType.CV_32F, new Scalar(70));
  1012. assertMatEqual(truth, dst, EPS);
  1013. Imgproc.matchTemplate(gray255, gray0, dst, Imgproc.TM_CCORR);
  1014. truth = new Mat(1, 1, CvType.CV_32F, new Scalar(0));
  1015. assertMatEqual(truth, dst, EPS);
  1016. }
  1017. public void testMedianBlur() {
  1018. Imgproc.medianBlur(gray255, dst, 5);
  1019. assertMatEqual(gray255, dst);
  1020. Imgproc.medianBlur(gray2, dst, 3);
  1021. assertMatEqual(gray2, dst);
  1022. // TODO_: write better test
  1023. }
  1024. public void testMinAreaRect() {
  1025. MatOfPoint2f points = new MatOfPoint2f(new Point(1, 1), new Point(5, 1), new Point(4, 3), new Point(6, 2));
  1026. RotatedRect rrect = Imgproc.minAreaRect(points);
  1027. assertEquals(new Size(5, 2), rrect.size);
  1028. assertEquals(0., rrect.angle);
  1029. assertEquals(new Point(3.5, 2), rrect.center);
  1030. }
  1031. public void testMinEnclosingCircle() {
  1032. MatOfPoint2f points = new MatOfPoint2f(new Point(0, 0), new Point(-100, 0), new Point(0, -100), new Point(100, 0), new Point(0, 100));
  1033. Point actualCenter = new Point();
  1034. float[] radius = new float[1];
  1035. Imgproc.minEnclosingCircle(points, actualCenter, radius);
  1036. assertEquals(new Point(0, 0), actualCenter);
  1037. assertEquals(100.0f, radius[0], 1.0);
  1038. }
  1039. public void testMomentsMat() {
  1040. fail("Not yet implemented");
  1041. }
  1042. public void testMomentsMatBoolean() {
  1043. fail("Not yet implemented");
  1044. }
  1045. public void testMorphologyExMatMatIntMat() {
  1046. Imgproc.morphologyEx(gray255, dst, Imgproc.MORPH_GRADIENT, gray0);
  1047. assertMatEqual(gray0, dst);
  1048. // TODO_: write better test
  1049. }
  1050. public void testMorphologyExMatMatIntMatPointInt() {
  1051. Mat src = Mat.eye(imgprocSz, imgprocSz, CvType.CV_8U);
  1052. Mat kernel = new Mat(imgprocSz, imgprocSz, CvType.CV_8U, new Scalar(0));
  1053. Point point = new Point(0, 0);
  1054. Imgproc.morphologyEx(src, dst, Imgproc.MORPH_CLOSE, kernel, point, 10);
  1055. truth = Mat.eye(imgprocSz, imgprocSz, CvType.CV_8U);
  1056. assertMatEqual(truth, dst);
  1057. // TODO_: write better test
  1058. }
  1059. public void testMorphologyExMatMatIntMatPointIntIntScalar() {
  1060. Mat src = new Mat(imgprocSz, imgprocSz, CvType.CV_8U);
  1061. src.put(0, 0, 2, 1);
  1062. src.put(1, 0, 2, 1);
  1063. Mat kernel = new Mat(imgprocSz, imgprocSz, CvType.CV_8U, new Scalar(1));
  1064. Point point = new Point(1, 1);
  1065. Scalar sc = new Scalar(3, 3);
  1066. Imgproc.morphologyEx(src, dst, Imgproc.MORPH_TOPHAT, kernel, point, 10, Core.BORDER_REFLECT, sc);
  1067. truth = new Mat(imgprocSz, imgprocSz, CvType.CV_8U) {
  1068. {
  1069. put(0, 0, 1, 0);
  1070. put(1, 0, 1, 0);
  1071. }
  1072. };
  1073. assertMatEqual(truth, dst);
  1074. // TODO_: write better test
  1075. }
  1076. public void testPointPolygonTest() {
  1077. MatOfPoint2f contour = new MatOfPoint2f(new Point(0, 0), new Point(1, 3), new Point(3, 4), new Point(4, 3), new Point(2, 1));
  1078. double sign1 = Imgproc.pointPolygonTest(contour, new Point(2, 2), false);
  1079. assertEquals(1.0, sign1);
  1080. double sign2 = Imgproc.pointPolygonTest(contour, new Point(4, 4), true);
  1081. assertEquals(-Math.sqrt(0.5), sign2);
  1082. }
  1083. public void testPreCornerDetectMatMatInt() {
  1084. Mat src = new Mat(4, 4, CvType.CV_32F, new Scalar(1));
  1085. int ksize = 3;
  1086. Imgproc.preCornerDetect(src, dst, ksize);
  1087. truth = new Mat(4, 4, CvType.CV_32F, new Scalar(0));
  1088. assertMatEqual(truth, dst, EPS);
  1089. }
  1090. public void testPreCornerDetectMatMatIntInt() {
  1091. Mat src = new Mat(4, 4, CvType.CV_32F, new Scalar(1));
  1092. int ksize = 3;
  1093. Imgproc.preCornerDetect(src, dst, ksize, Core.BORDER_REFLECT);
  1094. truth = new Mat(4, 4, CvType.CV_32F, new Scalar(0));
  1095. assertMatEqual(truth, dst, EPS);
  1096. // TODO_: write better test
  1097. }
  1098. public void testPyrDownMatMat() {
  1099. Mat src = new Mat(4, 4, CvType.CV_32F) {
  1100. {
  1101. put(0, 0, 2, 1, 4, 2);
  1102. put(1, 0, 3, 2, 6, 8);
  1103. put(2, 0, 4, 6, 8, 10);
  1104. put(3, 0, 12, 32, 6, 18);
  1105. }
  1106. };
  1107. Imgproc.pyrDown(src, dst);
  1108. truth = new Mat(imgprocSz, imgprocSz, CvType.CV_32F) {
  1109. {
  1110. put(0, 0, 2.78125, 4.609375);
  1111. put(1, 0, 8.546875, 8.8515625);
  1112. }
  1113. };
  1114. assertMatEqual(truth, dst, EPS);
  1115. }
  1116. public void testPyrDownMatMatSize() {
  1117. Mat src = new Mat(4, 4, CvType.CV_32F) {
  1118. {
  1119. put(0, 0, 2, 1, 4, 2);
  1120. put(1, 0, 3, 2, 6, 8);
  1121. put(2, 0, 4, 6, 8, 10);
  1122. put(3, 0, 12, 32, 6, 18);
  1123. }
  1124. };
  1125. Size dstSize = new Size(2, 2);
  1126. Imgproc.pyrDown(src, dst, dstSize);
  1127. truth = new Mat(imgprocSz, imgprocSz, CvType.CV_32F) {
  1128. {
  1129. put(0, 0, 2.78125, 4.609375);
  1130. put(1, 0, 8.546875, 8.8515625);
  1131. }
  1132. };
  1133. assertMatEqual(truth, dst, EPS);
  1134. // TODO_: write better test
  1135. }
  1136. public void testPyrMeanShiftFilteringMatMatDoubleDouble() {
  1137. Mat src = new Mat(matSize, matSize, CvType.CV_8UC3, new Scalar(0));
  1138. Imgproc.pyrMeanShiftFiltering(src, dst, 10, 50);
  1139. assertMatEqual(src, dst);
  1140. // TODO_: write better test
  1141. }
  1142. public void testPyrMeanShiftFilteringMatMatDoubleDoubleInt() {
  1143. fail("Not yet implemented");
  1144. }
  1145. public void testPyrMeanShiftFilteringMatMatDoubleDoubleIntTermCriteria() {
  1146. fail("Not yet implemented");
  1147. }
  1148. public void testPyrUpMatMat() {
  1149. Mat src = new Mat(imgprocSz, imgprocSz, CvType.CV_32F);
  1150. src.put(0, 0, 2, 1);
  1151. src.put(1, 0, 3, 2);
  1152. Imgproc.pyrUp(src, dst);
  1153. truth = new Mat(4, 4, CvType.CV_32F) {
  1154. {
  1155. put(0, 0, 2, 1.75, 1.375, 1.25);
  1156. put(1, 0, 2.25, 2, 1.625, 1.5);
  1157. put(2, 0, 2.625, 2.375, 2, 1.875);
  1158. put(3, 0, 2.75, 2.5, 2.125, 2);
  1159. }
  1160. };
  1161. assertMatEqual(truth, dst, EPS);
  1162. }
  1163. public void testPyrUpMatMatSize() {
  1164. fail("Not yet implemented");
  1165. }
  1166. public void testRemapMatMatMatMatInt() {
  1167. fail("Not yet implemented");
  1168. // this test does something weird
  1169. Mat src = new Mat(imgprocSz, imgprocSz, CvType.CV_32F, new Scalar(2));
  1170. Mat map1 = new Mat(1, 3, CvType.CV_32FC1);
  1171. Mat map2 = new Mat(1, 3, CvType.CV_32FC1);
  1172. map1.put(0, 0, 3, 6, 5);
  1173. map2.put(0, 0, 4, 8, 12);
  1174. Imgproc.remap(src, dst, map1, map2, Imgproc.INTER_LINEAR);
  1175. truth = new Mat(1, 3, CvType.CV_32F, new Scalar(0));
  1176. assertMatEqual(truth, dst, EPS);
  1177. }
  1178. public void testRemapMatMatMatMatIntIntScalar() {
  1179. fail("Not yet implemented");
  1180. // this test does something weird
  1181. Mat src = new Mat(imgprocSz, imgprocSz, CvType.CV_32F, new Scalar(2));
  1182. Mat map1 = new Mat(1, 3, CvType.CV_32FC1);
  1183. Mat map2 = new Mat(1, 3, CvType.CV_32FC1);
  1184. Scalar sc = new Scalar(0);
  1185. map1.put(0, 0, 3, 6, 5, 0);
  1186. map2.put(0, 0, 4, 8, 12);
  1187. truth = new Mat(1, 3, CvType.CV_32F, new Scalar(2));
  1188. Imgproc.remap(src, dst, map1, map2, Imgproc.INTER_LINEAR, Core.BORDER_REFLECT, sc);
  1189. assertMatEqual(truth, dst, EPS);
  1190. }
  1191. public void testResizeMatMatSize() {
  1192. Mat src = new Mat(imgprocSz, imgprocSz, CvType.CV_8UC1, new Scalar(1));
  1193. Size dsize = new Size(1, 1);
  1194. Imgproc.resize(src, dst, dsize, 0, 0, Imgproc.INTER_LINEAR_EXACT);
  1195. truth = new Mat(1, 1, CvType.CV_8UC1, new Scalar(1));
  1196. assertMatEqual(truth, dst);
  1197. }
  1198. public void testResizeMatMatSizeDoubleDoubleInt() {
  1199. Imgproc.resize(gray255, dst, new Size(2, 2), 0, 0, Imgproc.INTER_AREA);
  1200. truth = new Mat(2, 2, CvType.CV_8UC1, new Scalar(255));
  1201. assertMatEqual(truth, dst);
  1202. // TODO_: write better test
  1203. }
  1204. public void testScharrMatMatIntIntInt() {
  1205. Mat src = Mat.eye(imgprocSz, imgprocSz, CvType.CV_32F);
  1206. Imgproc.Scharr(src, dst, CvType.CV_32F, 1, 0);
  1207. truth = new Mat(imgprocSz, imgprocSz, CvType.CV_32F, new Scalar(0));
  1208. assertMatEqual(truth, dst, EPS);
  1209. }
  1210. public void testScharrMatMatIntIntIntDoubleDouble() {
  1211. Mat src = Mat.eye(imgprocSz, imgprocSz, CvType.CV_32F);
  1212. Imgproc.Scharr(src, dst, CvType.CV_32F, 1, 0, 1.5, 0.001);
  1213. truth = new Mat(imgprocSz, imgprocSz, CvType.CV_32F, new Scalar(0.001));
  1214. assertMatEqual(truth, dst, EPS);
  1215. }
  1216. public void testScharrMatMatIntIntIntDoubleDoubleInt() {
  1217. Mat src = Mat.eye(3, 3, CvType.CV_32F);
  1218. Imgproc.Scharr(src, dst, CvType.CV_32F, 1, 0, 1.5, 0, Core.BORDER_REFLECT);
  1219. truth = new Mat(3, 3, CvType.CV_32F) {
  1220. {
  1221. put(0, 0, -15, -19.5, -4.5);
  1222. put(1, 0, 10.5, 0, -10.5);
  1223. put(2, 0, 4.5, 19.5, 15);
  1224. }
  1225. };
  1226. assertMatEqual(truth, dst, EPS);
  1227. }
  1228. public void testSepFilter2DMatMatIntMatMat() {
  1229. Mat src = new Mat(imgprocSz, imgprocSz, CvType.CV_32F, new Scalar(2));
  1230. Mat kernelX = new Mat(1, 3, CvType.CV_32FC1);
  1231. Mat kernelY = new Mat(1, 3, CvType.CV_32FC1);
  1232. kernelX.put(0, 0, 4, 3, 7);
  1233. kernelY.put(0, 0, 9, 4, 2);
  1234. Imgproc.sepFilter2D(src, dst, CvType.CV_32F, kernelX, kernelY);
  1235. truth = new Mat(imgprocSz, imgprocSz, CvType.CV_32F, new Scalar(420));
  1236. assertMatEqual(truth, dst, EPS);
  1237. }
  1238. public void testSepFilter2DMatMatIntMatMatPointDouble() {
  1239. Mat src = new Mat(imgprocSz, imgprocSz, CvType.CV_32FC1, new Scalar(2));
  1240. Mat kernelX = new Mat(1, 3, CvType.CV_32FC1);
  1241. kernelX.put(0, 0, 2, 2, 2);
  1242. Mat kernelY = new Mat(1, 3, CvType.CV_32FC1);
  1243. kernelY.put(0, 0, 1, 1, 1);
  1244. Imgproc.sepFilter2D(src, dst, CvType.CV_32F, kernelX, kernelY, anchorPoint, weakEPS);
  1245. truth = new Mat(imgprocSz, imgprocSz, CvType.CV_32F, new Scalar(36 + weakEPS));
  1246. assertMatEqual(truth, dst, EPS);
  1247. }
  1248. public void testSepFilter2DMatMatIntMatMatPointDoubleInt() {
  1249. Mat kernelX = new Mat(1, 3, CvType.CV_32FC1);
  1250. kernelX.put(0, 0, 2, 2, 2);
  1251. Mat kernelY = new Mat(1, 3, CvType.CV_32FC1);
  1252. kernelY.put(0, 0, 1, 1, 1);
  1253. Imgproc.sepFilter2D(gray0, dst, CvType.CV_32F, kernelX, kernelY, anchorPoint, weakEPS, Core.BORDER_REFLECT);
  1254. truth = new Mat(10, 10, CvType.CV_32F, new Scalar(weakEPS));
  1255. assertMatEqual(truth, dst, EPS);
  1256. // TODO_: write better test
  1257. }
  1258. public void testSobelMatMatIntIntInt() {
  1259. Imgproc.Sobel(gray255, dst, CvType.CV_8U, 1, 0);
  1260. assertMatEqual(gray0, dst);
  1261. }
  1262. public void testSobelMatMatIntIntIntIntDoubleDouble() {
  1263. Imgproc.Sobel(gray255, dst, CvType.CV_8U, 1, 0, 3, 2, 0.001);
  1264. assertMatEqual(gray0, dst);
  1265. // TODO_: write better test
  1266. }
  1267. public void testSobelMatMatIntIntIntIntDoubleDoubleInt() {
  1268. Mat src = new Mat(3, 3, CvType.CV_32F) {
  1269. {
  1270. put(0, 0, 2, 0, 1);
  1271. put(1, 0, 6, 4, 3);
  1272. put(2, 0, 1, 0, 2);
  1273. }
  1274. };
  1275. Imgproc.Sobel(src, dst, CvType.CV_32F, 1, 0, 3, 2, 0, Core.BORDER_REPLICATE);
  1276. truth = new Mat(3, 3, CvType.CV_32F) {
  1277. {
  1278. put(0, 0, -16, -12, 4);
  1279. put(1, 0, -14, -12, 2);
  1280. put(2, 0, -10, 0, 10);
  1281. }
  1282. };
  1283. assertMatEqual(truth, dst, EPS);
  1284. }
  1285. public void testThreshold() {
  1286. Imgproc.threshold(makeMask(gray0.clone(), 10), dst, 5, 255, Imgproc.THRESH_TRUNC);
  1287. assertMatEqual(makeMask(gray0.clone(), 5), dst);
  1288. Imgproc.threshold(makeMask(gray2.clone(), 10), dst, 1, 255, Imgproc.THRESH_BINARY);
  1289. assertMatEqual(gray255, dst);
  1290. Imgproc.threshold(makeMask(gray2.clone(), 10), dst, 3, 255, Imgproc.THRESH_BINARY_INV);
  1291. assertMatEqual(makeMask(gray255.clone(), 0), dst);
  1292. }
  1293. public void testWarpAffineMatMatMatSize() {
  1294. Mat src = new Mat(3, 3, CvType.CV_32F) {
  1295. {
  1296. put(0, 0, 2, 0, 1);
  1297. put(1, 0, 6, 4, 3);
  1298. put(2, 0, 1, 0, 2);
  1299. }
  1300. };
  1301. Mat M = new Mat(2, 3, CvType.CV_32F) {
  1302. {
  1303. put(0, 0, 1, 0, 1);
  1304. put(1, 0, 0, 1, 1);
  1305. }
  1306. };
  1307. Imgproc.warpAffine(src, dst, M, new Size(3, 3));
  1308. truth = new Mat(3, 3, CvType.CV_32F) {
  1309. {
  1310. put(0, 0, 0, 0, 0);
  1311. put(1, 0, 0, 2, 0);
  1312. put(2, 0, 0, 6, 4);
  1313. }
  1314. };
  1315. assertMatEqual(truth, dst, EPS);
  1316. }
  1317. public void testWarpAffineMatMatMatSizeInt() {
  1318. Mat src = new Mat(3, 3, CvType.CV_32F) {
  1319. {
  1320. put(0, 0, 2, 4, 1);
  1321. put(1, 0, 6, 4, 3);
  1322. put(2, 0, 0, 2, 2);
  1323. }
  1324. };
  1325. Mat M = new Mat(2, 3, CvType.CV_32F) {
  1326. {
  1327. put(0, 0, 1, 0, 0);
  1328. put(1, 0, 0, 0, 1);
  1329. }
  1330. };
  1331. Imgproc.warpAffine(src, dst, M, new Size(2, 2), Imgproc.WARP_INVERSE_MAP);
  1332. truth = new Mat(2, 2, CvType.CV_32F) {
  1333. {
  1334. put(0, 0, 6, 4);
  1335. put(1, 0, 6, 4);
  1336. }
  1337. };
  1338. assertMatEqual(truth, dst, EPS);
  1339. }
  1340. public void testWarpAffineMatMatMatSizeIntInt() {
  1341. fail("Not yet implemented");
  1342. }
  1343. public void testWarpAffineMatMatMatSizeIntIntScalar() {
  1344. fail("Not yet implemented");
  1345. }
  1346. public void testWarpPerspectiveMatMatMatSize() {
  1347. Mat src = new Mat(3, 3, CvType.CV_32F) {
  1348. {
  1349. put(0, 0, 2, 4, 1);
  1350. put(1, 0, 0, 4, 5);
  1351. put(2, 0, 1, 2, 2);
  1352. }
  1353. };
  1354. Mat M = new Mat(3, 3, CvType.CV_32F) {
  1355. {
  1356. put(0, 0, 1, 0, 1);
  1357. put(1, 0, 0, 1, 1);
  1358. put(2, 0, 0, 0, 1);
  1359. }
  1360. };
  1361. Imgproc.warpPerspective(src, dst, M, new Size(3, 3));
  1362. truth = new Mat(3, 3, CvType.CV_32F) {
  1363. {
  1364. put(0, 0, 0, 0, 0);
  1365. put(1, 0, 0, 2, 4);
  1366. put(2, 0, 0, 0, 4);
  1367. }
  1368. };
  1369. assertMatEqual(truth, dst, EPS);
  1370. }
  1371. public void testWarpPerspectiveMatMatMatSizeInt() {
  1372. fail("Not yet implemented");
  1373. }
  1374. public void testWarpPerspectiveMatMatMatSizeIntInt() {
  1375. fail("Not yet implemented");
  1376. }
  1377. public void testWarpPerspectiveMatMatMatSizeIntIntScalar() {
  1378. fail("Not yet implemented");
  1379. }
  1380. public void testWatershed() {
  1381. Mat image = Mat.eye(4, 4, CvType.CV_8UC(3));
  1382. Mat markers = new Mat(4, 4, CvType.CV_32SC1, new Scalar(0));
  1383. Imgproc.watershed(image, markers);
  1384. truth = new Mat(4, 4, CvType.CV_32SC1) {
  1385. {
  1386. put(0, 0, -1, -1, -1, -1);
  1387. put(1, 0, -1, 0, 0, -1);
  1388. put(2, 0, -1, 0, 0, -1);
  1389. put(3, 0, -1, -1, -1, -1);
  1390. }
  1391. };
  1392. assertMatEqual(truth, markers);
  1393. }
  1394. public void testGetTextSize() {
  1395. String text = "Android all the way";
  1396. double fontScale = 2;
  1397. int thickness = 3;
  1398. int baseLine[] = new int[1];
  1399. Imgproc.getTextSize(text, Imgproc.FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, thickness, null);
  1400. Size res = Imgproc.getTextSize(text, Imgproc.FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, thickness, baseLine);
  1401. assertEquals(543.0, res.width);
  1402. assertEquals(44.0, res.height);
  1403. assertEquals(20, baseLine[0]);
  1404. }
  1405. public void testCircleMatPointIntScalar() {
  1406. Point center = new Point(gray0.cols() / 2, gray0.rows() / 2);
  1407. int radius = Math.min(gray0.cols() / 4, gray0.rows() / 4);
  1408. Scalar color = new Scalar(128);
  1409. Imgproc.circle(gray0, center, radius, color);
  1410. assertTrue(0 != Core.countNonZero(gray0));
  1411. }
  1412. public void testCircleMatPointIntScalarInt() {
  1413. Point center = new Point(gray0.cols() / 2, gray0.rows() / 2);
  1414. int radius = Math.min(gray0.cols() / 4, gray0.rows() / 4);
  1415. Scalar color = new Scalar(128);
  1416. Imgproc.circle(gray0, center, radius, color, Imgproc.FILLED);
  1417. assertTrue(0 != Core.countNonZero(gray0));
  1418. }
  1419. public void testCircleMatPointIntScalarIntIntInt() {
  1420. Point center = new Point(gray0.cols() / 2, gray0.rows() / 2);
  1421. Point center2 = new Point(gray0.cols(), gray0.rows());
  1422. int radius = Math.min(gray0.cols() / 4, gray0.rows() / 4);
  1423. Scalar color128 = new Scalar(128);
  1424. Scalar color0 = new Scalar(0);
  1425. Imgproc.circle(gray0, center2, radius * 2, color128, 2, Imgproc.LINE_4, 1/*
  1426. * Number
  1427. * of
  1428. * fractional
  1429. * bits
  1430. */);
  1431. assertFalse(0 == Core.countNonZero(gray0));
  1432. Imgproc.circle(gray0, center, radius, color0, 2, Imgproc.LINE_4, 0);
  1433. assertTrue(0 == Core.countNonZero(gray0));
  1434. }
  1435. public void testClipLine() {
  1436. Rect r = new Rect(10, 10, 10, 10);
  1437. Point pt1 = new Point(5.0, 15.0);
  1438. Point pt2 = new Point(25.0, 15.0);
  1439. assertTrue(Imgproc.clipLine(r, pt1, pt2));
  1440. Point pt1Clipped = new Point(10.0, 15.0);
  1441. Point pt2Clipped = new Point(19.0, 15.0);
  1442. assertEquals(pt1Clipped, pt1);
  1443. assertEquals(pt2Clipped, pt2);
  1444. pt1 = new Point(5.0, 5.0);
  1445. pt2 = new Point(25.0, 5.0);
  1446. pt1Clipped = new Point(5.0, 5.0);
  1447. pt2Clipped = new Point(25.0, 5.0);
  1448. assertFalse(Imgproc.clipLine(r, pt1, pt2));
  1449. assertEquals(pt1Clipped, pt1);
  1450. assertEquals(pt2Clipped, pt2);
  1451. }
  1452. public void testEllipse2Poly() {
  1453. Point center = new Point(4, 4);
  1454. Size axes = new Size(2, 2);
  1455. int angle = 30;
  1456. int arcStart = 30;
  1457. int arcEnd = 60;
  1458. int delta = 2;
  1459. MatOfPoint pts = new MatOfPoint();
  1460. Imgproc.ellipse2Poly(center, axes, angle, arcStart, arcEnd, delta, pts);
  1461. Point truth[] = {
  1462. new Point(5, 6),
  1463. new Point(4, 6)
  1464. };
  1465. assertArrayPointsEquals(truth, pts.toArray(), EPS);
  1466. }
  1467. public void testEllipseMatPointSizeDoubleDoubleDoubleScalar() {
  1468. Point center = new Point(gray0.cols() / 2, gray0.rows() / 2);
  1469. Size axes = new Size(2, 2);
  1470. double angle = 30, startAngle = 60, endAngle = 90;
  1471. Imgproc.ellipse(gray0, center, axes, angle, startAngle, endAngle, colorWhite);
  1472. assertTrue(0 != Core.countNonZero(gray0));
  1473. }
  1474. public void testEllipseMatPointSizeDoubleDoubleDoubleScalarInt() {
  1475. Point center = new Point(gray0.cols() / 2, gray0.rows() / 2);
  1476. Size axes = new Size(2, 2);
  1477. double angle = 30, startAngle = 60, endAngle = 90;
  1478. Imgproc.ellipse(gray0, center, axes, angle, startAngle, endAngle, colorWhite, Imgproc.FILLED);
  1479. assertTrue(0 != Core.countNonZero(gray0));
  1480. }
  1481. public void testEllipseMatPointSizeDoubleDoubleDoubleScalarIntIntInt() {
  1482. Point center = new Point(gray0.cols() / 2, gray0.rows() / 2);
  1483. Size axes = new Size(2, 2);
  1484. Point center2 = new Point(gray0.cols(), gray0.rows());
  1485. Size axes2 = new Size(4, 4);
  1486. double angle = 30, startAngle = 0, endAngle = 30;
  1487. Imgproc.ellipse(gray0, center, axes, angle, startAngle, endAngle, colorWhite, Imgproc.FILLED, Imgproc.LINE_4, 0);
  1488. assertTrue(0 != Core.countNonZero(gray0));
  1489. Imgproc.ellipse(gray0, center2, axes2, angle, startAngle, endAngle, colorBlack, Imgproc.FILLED, Imgproc.LINE_4, 1);
  1490. assertEquals(0, Core.countNonZero(gray0));
  1491. }
  1492. public void testEllipseMatRotatedRectScalar() {
  1493. int matSize = 10;
  1494. Mat gray0 = Mat.zeros(matSize, matSize, CvType.CV_8U);
  1495. Point center = new Point(matSize / 2, matSize / 2);
  1496. Size size = new Size(matSize / 4, matSize / 2);
  1497. RotatedRect box = new RotatedRect(center, size, 45);
  1498. Imgproc.ellipse(gray0, box, new Scalar(1));
  1499. final byte[] truth = new byte[] {
  1500. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1501. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1502. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1503. 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
  1504. 0, 0, 0, 0, 1, 1, 0, 1, 0, 0,
  1505. 0, 0, 0, 0, 1, 0, 1, 0, 0, 0,
  1506. 0, 0, 0, 1, 0, 1, 1, 0, 0, 0,
  1507. 0, 0, 0, 1, 1, 0, 0, 0, 0, 0,
  1508. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1509. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  1510. assertMatEqual(new Mat(matSize, matSize, CvType.CV_8U) {
  1511. {
  1512. put(0, 0, truth);
  1513. }
  1514. }, gray0);
  1515. }
  1516. public void testEllipseMatRotatedRectScalarInt() {
  1517. Point center = new Point(matSize / 2, matSize / 2);
  1518. Size size = new Size(matSize / 4, matSize / 2);
  1519. RotatedRect box = new RotatedRect(center, size, 45);
  1520. Imgproc.ellipse(gray0, box, new Scalar(1), Imgproc.FILLED);
  1521. Imgproc.ellipse(gray0, box, new Scalar(0));
  1522. assertTrue(0 < Core.countNonZero(gray0));
  1523. }
  1524. public void testEllipseMatRotatedRectScalarIntInt() {
  1525. Point center = new Point(matSize / 2, matSize / 2);
  1526. Size size = new Size(2, matSize * 2 / 3);
  1527. RotatedRect box = new RotatedRect(center, size, 20);
  1528. Imgproc.ellipse(gray0, box, new Scalar(9), 1, Imgproc.LINE_AA);
  1529. Imgproc.ellipse(gray0, box, new Scalar(0), 1, Imgproc.LINE_4);
  1530. assertTrue(0 < Core.countNonZero(gray0));
  1531. }
  1532. public void testPolylinesMatListOfListOfPointBooleanScalar() {
  1533. Mat img = gray0;
  1534. List<MatOfPoint> polyline = new ArrayList<MatOfPoint>();
  1535. polyline.add(new MatOfPoint(new Point(1, 1), new Point(7, 1), new Point(7, 6), new Point(1, 6)));
  1536. Imgproc.polylines(img, polyline, true, new Scalar(100));
  1537. assertEquals(22, Core.countNonZero(img));
  1538. Imgproc.polylines(img, polyline, false, new Scalar(0));
  1539. assertEquals(4, Core.countNonZero(img));
  1540. }
  1541. public void testPolylinesMatListOfListOfPointBooleanScalarInt() {
  1542. Mat img = gray0;
  1543. List<MatOfPoint> polyline = new ArrayList<MatOfPoint>();
  1544. polyline.add(new MatOfPoint(new Point(1, 1), new Point(7, 1), new Point(7, 6), new Point(1, 6)));
  1545. Imgproc.polylines(img, polyline, true, new Scalar(100), 2);
  1546. assertEquals(62, Core.countNonZero(img));
  1547. }
  1548. public void testPolylinesMatListOfListOfPointBooleanScalarIntIntInt() {
  1549. Mat img = gray0;
  1550. List<MatOfPoint> polyline1 = new ArrayList<MatOfPoint>();
  1551. polyline1.add(new MatOfPoint(new Point(1, 1), new Point(7, 1), new Point(7, 6), new Point(1, 6)));
  1552. List<MatOfPoint> polyline2 = new ArrayList<MatOfPoint>();
  1553. polyline2.add(new MatOfPoint(new Point(2, 2), new Point(14, 2), new Point(14, 12), new Point(2, 12)));
  1554. Imgproc.polylines(img, polyline1, true, new Scalar(100), 2, Imgproc.LINE_8, 0);
  1555. assertTrue(Core.countNonZero(img) > 0);
  1556. Imgproc.polylines(img, polyline2, true, new Scalar(0), 2, Imgproc.LINE_8, 1);
  1557. assertEquals(0, Core.countNonZero(img));
  1558. }
  1559. public void testPutTextMatStringPointIntDoubleScalar() {
  1560. String text = "Hello World";
  1561. Size labelSize = new Size(175, 22);
  1562. Mat img = new Mat(20 + (int) labelSize.height, 20 + (int) labelSize.width, CvType.CV_8U, colorBlack);
  1563. Point origin = new Point(10, labelSize.height + 10);
  1564. Imgproc.putText(img, text, origin, Imgproc.FONT_HERSHEY_SIMPLEX, 1.0, colorWhite);
  1565. assertTrue(Core.countNonZero(img) > 0);
  1566. // check that border is not corrupted
  1567. Imgproc.rectangle(img, new Point(11, 11), new Point(labelSize.width + 10, labelSize.height + 10), colorBlack, Imgproc.FILLED);
  1568. assertEquals(0, Core.countNonZero(img));
  1569. }
  1570. public void testPutTextMatStringPointIntDoubleScalarInt() {
  1571. String text = "Hello World";
  1572. Size labelSize = new Size(176, 22);
  1573. Mat img = new Mat(20 + (int) labelSize.height, 20 + (int) labelSize.width, CvType.CV_8U, colorBlack);
  1574. Point origin = new Point(10, labelSize.height + 10);
  1575. Imgproc.putText(img, text, origin, Imgproc.FONT_HERSHEY_SIMPLEX, 1.0, colorWhite, 2);
  1576. assertTrue(Core.countNonZero(img) > 0);
  1577. // check that border is not corrupted
  1578. Imgproc.rectangle(img, new Point(10, 10), new Point(labelSize.width + 10 + 1, labelSize.height + 10 + 1), colorBlack, Imgproc.FILLED);
  1579. assertEquals(0, Core.countNonZero(img));
  1580. }
  1581. public void testPutTextMatStringPointIntDoubleScalarIntIntBoolean() {
  1582. String text = "Hello World";
  1583. Size labelSize = new Size(175, 22);
  1584. Mat img = new Mat(20 + (int) labelSize.height, 20 + (int) labelSize.width, CvType.CV_8U, colorBlack);
  1585. Point origin = new Point(10, 10);
  1586. Imgproc.putText(img, text, origin, Imgproc.FONT_HERSHEY_SIMPLEX, 1.0, colorWhite, 1, Imgproc.LINE_8, true);
  1587. assertTrue(Core.countNonZero(img) > 0);
  1588. // check that border is not corrupted
  1589. Imgproc.rectangle(img, new Point(10, 10), new Point(labelSize.width + 9, labelSize.height + 9), colorBlack, Imgproc.FILLED);
  1590. assertEquals(0, Core.countNonZero(img));
  1591. }
  1592. }