ImgprocTest.swift 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  1. //
  2. // ImgprocTest.swift
  3. //
  4. // Created by Giles Payne on 2020/02/08.
  5. //
  6. import XCTest
  7. import OpenCV
  8. class ImgprocTest: OpenCVTestCase {
  9. let anchorPoint = Point(x: 2, y: 2)
  10. let imgprocSz: Int32 = 2
  11. let size = Size(width: 3, height: 3)
  12. func testAccumulateMatMat() throws {
  13. let src = getMat(CvType.CV_64F, vals: [2])
  14. let dst = getMat(CvType.CV_64F, vals: [0])
  15. let dst2 = src.clone()
  16. Imgproc.accumulate(src: src, dst: dst)
  17. Imgproc.accumulate(src: src, dst: dst2)
  18. try assertMatEqual(src, dst, OpenCVTestCase.EPS)
  19. try assertMatEqual(getMat(CvType.CV_64F, vals: [4]), dst2, OpenCVTestCase.EPS)
  20. }
  21. func testAccumulateMatMatMat() throws {
  22. let src = getMat(CvType.CV_64F, vals: [2])
  23. let mask = makeMask(getMat(CvType.CV_8U, vals: [1]))
  24. let dst = getMat(CvType.CV_64F, vals: [0])
  25. let dst2 = src.clone()
  26. Imgproc.accumulate(src: src, dst: dst, mask: mask)
  27. Imgproc.accumulate(src: src, dst: dst2, mask: mask)
  28. try assertMatEqual(makeMask(getMat(CvType.CV_64F, vals: [2])), dst, OpenCVTestCase.EPS)
  29. try assertMatEqual(makeMask(getMat(CvType.CV_64F, vals: [4]), vals: [2]), dst2, OpenCVTestCase.EPS)
  30. }
  31. func testAccumulateProductMatMatMat() throws {
  32. let src = getMat(CvType.CV_64F, vals: [2])
  33. let dst = getMat(CvType.CV_64F, vals: [0])
  34. let dst2 = src.clone()
  35. Imgproc.accumulateProduct(src1: src, src2: src, dst: dst)
  36. Imgproc.accumulateProduct(src1: src, src2: dst, dst: dst2)
  37. try assertMatEqual(getMat(CvType.CV_64F, vals:[4]), dst, OpenCVTestCase.EPS)
  38. try assertMatEqual(getMat(CvType.CV_64F, vals:[10]), dst2, OpenCVTestCase.EPS)
  39. }
  40. func testAccumulateProductMatMatMatMat() throws {
  41. let src = getMat(CvType.CV_64F, vals: [2])
  42. let mask = makeMask(getMat(CvType.CV_8U, vals: [1]))
  43. let dst = getMat(CvType.CV_64F, vals: [0])
  44. let dst2 = src.clone()
  45. Imgproc.accumulateProduct(src1: src, src2: src, dst: dst, mask: mask)
  46. Imgproc.accumulateProduct(src1: src, src2: dst, dst: dst2, mask: mask)
  47. try assertMatEqual(makeMask(getMat(CvType.CV_64F, vals: [4])), dst, OpenCVTestCase.EPS)
  48. try assertMatEqual(makeMask(getMat(CvType.CV_64F, vals: [10]), vals:[2]), dst2, OpenCVTestCase.EPS)
  49. }
  50. func testAccumulateSquareMatMat() throws {
  51. let src = getMat(CvType.CV_64F, vals: [2])
  52. let dst = getMat(CvType.CV_64F, vals: [0])
  53. let dst2 = src.clone()
  54. Imgproc.accumulateSquare(src: src, dst: dst)
  55. Imgproc.accumulateSquare(src: src, dst: dst2)
  56. try assertMatEqual(getMat(CvType.CV_64F, vals: [4]), dst, OpenCVTestCase.EPS)
  57. try assertMatEqual(getMat(CvType.CV_64F, vals: [6]), dst2, OpenCVTestCase.EPS)
  58. }
  59. func testAccumulateSquareMatMatMat() throws {
  60. let src = getMat(CvType.CV_64F, vals: [2])
  61. let mask = makeMask(getMat(CvType.CV_8U, vals: [1]))
  62. let dst = getMat(CvType.CV_64F, vals: [0])
  63. let dst2 = src.clone()
  64. Imgproc.accumulateSquare(src: src, dst: dst, mask: mask)
  65. Imgproc.accumulateSquare(src: src, dst: dst2, mask: mask)
  66. try assertMatEqual(makeMask(getMat(CvType.CV_64F, vals: [4])), dst, OpenCVTestCase.EPS)
  67. try assertMatEqual(makeMask(getMat(CvType.CV_64F, vals: [6]), vals: [2]), dst2, OpenCVTestCase.EPS)
  68. }
  69. func testAccumulateWeightedMatMatDouble() throws {
  70. let src = getMat(CvType.CV_64F, vals: [2])
  71. let dst = getMat(CvType.CV_64F, vals: [4])
  72. let dst2 = src.clone()
  73. Imgproc.accumulateWeighted(src: src, dst: dst, alpha: 0.5)
  74. Imgproc.accumulateWeighted(src: src, dst: dst2, alpha: 2)
  75. try assertMatEqual(getMat(CvType.CV_64F, vals: [3]), dst, OpenCVTestCase.EPS)
  76. try assertMatEqual(getMat(CvType.CV_64F, vals: [2]), dst2, OpenCVTestCase.EPS)
  77. }
  78. func testAccumulateWeightedMatMatDoubleMat() throws {
  79. let src = getMat(CvType.CV_64F, vals: [2])
  80. let mask = makeMask(getMat(CvType.CV_8U, vals: [1]))
  81. let dst = getMat(CvType.CV_64F, vals: [4])
  82. let dst2 = src.clone()
  83. Imgproc.accumulateWeighted(src: src, dst: dst, alpha: 0.5, mask: mask)
  84. Imgproc.accumulateWeighted(src: src, dst: dst2, alpha: 2, mask: mask)
  85. try assertMatEqual(makeMask(getMat(CvType.CV_64F, vals: [3]), vals: [4]), dst, OpenCVTestCase.EPS)
  86. try assertMatEqual(getMat(CvType.CV_64F, vals: [2]), dst2, OpenCVTestCase.EPS)
  87. }
  88. func testAdaptiveThreshold() {
  89. let src = makeMask(getMat(CvType.CV_8U, vals: [50]), vals:[20])
  90. let dst = Mat()
  91. Imgproc.adaptiveThreshold(src: src, dst: dst, maxValue: 1, adaptiveMethod: .ADAPTIVE_THRESH_MEAN_C, thresholdType: .THRESH_BINARY, blockSize: 3, C: 0)
  92. XCTAssertEqual(src.rows(), Core.countNonZero(src: dst))
  93. }
  94. func testApproxPolyDP() {
  95. let curve = [Point2f(x: 1, y: 3), Point2f(x: 2, y: 4), Point2f(x: 3, y: 5), Point2f(x: 4, y: 4), Point2f(x: 5, y: 3)]
  96. var approxCurve = [Point2f]()
  97. Imgproc.approxPolyDP(curve: curve, approxCurve: &approxCurve, epsilon: OpenCVTestCase.EPS, closed: true)
  98. let approxCurveGold = [Point2f(x: 1, y: 3), Point2f(x: 3, y: 5), Point2f(x: 5, y: 3)]
  99. XCTAssert(approxCurve == approxCurveGold)
  100. }
  101. func testArcLength() {
  102. let curve = [Point2f(x: 1, y: 3), Point2f(x: 2, y: 4), Point2f(x: 3, y: 5), Point2f(x: 4, y: 4), Point2f(x: 5, y: 3)]
  103. let arcLength = Imgproc.arcLength(curve: curve, closed: false)
  104. XCTAssertEqual(5.656854249, arcLength, accuracy:0.000001)
  105. }
  106. func testBilateralFilterMatMatIntDoubleDouble() throws {
  107. Imgproc.bilateralFilter(src: gray255, dst: dst, d: 5, sigmaColor: 10, sigmaSpace: 5)
  108. try assertMatEqual(gray255, dst)
  109. }
  110. func testBilateralFilterMatMatIntDoubleDoubleInt() throws {
  111. Imgproc.bilateralFilter(src: gray255, dst: dst, d: 5, sigmaColor: 10, sigmaSpace: 5, borderType: .BORDER_REFLECT)
  112. try assertMatEqual(gray255, dst)
  113. }
  114. func testBlurMatMatSize() throws {
  115. Imgproc.blur(src: gray0, dst: dst, ksize: size)
  116. try assertMatEqual(gray0, dst)
  117. Imgproc.blur(src: gray255, dst: dst, ksize: size)
  118. try assertMatEqual(gray255, dst)
  119. }
  120. func testBlurMatMatSizePoint() throws {
  121. Imgproc.blur(src: gray0, dst: dst, ksize: size, anchor: anchorPoint)
  122. try assertMatEqual(gray0, dst)
  123. }
  124. func testBlurMatMatSizePointInt() throws {
  125. Imgproc.blur(src: gray0, dst: dst, ksize: size, anchor: anchorPoint, borderType: .BORDER_REFLECT)
  126. try assertMatEqual(gray0, dst)
  127. }
  128. func testBoundingRect() {
  129. let points = [Point(x: 0, y: 0), Point(x: 0, y: 4), Point(x: 4, y: 0), Point(x: 4, y: 4)]
  130. let p1 = Point(x: 1, y: 1)
  131. let p2 = Point(x: -5, y: -2)
  132. let bbox = Imgproc.boundingRect(array: MatOfPoint(array: points))
  133. XCTAssert(bbox.contains(p1))
  134. XCTAssertFalse(bbox.contains(p2))
  135. }
  136. func testBoxFilterMatMatIntSize() throws {
  137. let size = Size(width: 3, height: 3)
  138. Imgproc.boxFilter(src: gray0, dst: dst, ddepth: 8, ksize: size)
  139. try assertMatEqual(gray0, dst)
  140. }
  141. func testBoxFilterMatMatIntSizePointBoolean() throws {
  142. Imgproc.boxFilter(src: gray255, dst: dst, ddepth: 8, ksize: size, anchor: anchorPoint, normalize: false)
  143. try assertMatEqual(gray255, dst)
  144. }
  145. func testBoxFilterMatMatIntSizePointBooleanInt() throws {
  146. Imgproc.boxFilter(src: gray255, dst: dst, ddepth: 8, ksize: size, anchor: anchorPoint, normalize: false, borderType: .BORDER_REFLECT)
  147. try assertMatEqual(gray255, dst)
  148. }
  149. func testCalcBackProject() {
  150. let images = [grayChess]
  151. let channels:[Int32] = [0]
  152. let histSize:[Int32] = [10]
  153. let ranges:[Float] = [0, 256]
  154. let hist = Mat()
  155. Imgproc.calcHist(images: images, channels: channels, mask: Mat(), hist: hist, histSize: histSize, ranges: ranges)
  156. Core.normalize(src: hist, dst: hist)
  157. Imgproc.calcBackProject(images: images, channels: channels, hist: hist, dst: dst, ranges: ranges, scale: 255)
  158. XCTAssertEqual(grayChess.size(), dst.size())
  159. XCTAssertEqual(grayChess.depth(), dst.depth())
  160. XCTAssertFalse(0 == Core.countNonZero(src: dst))
  161. }
  162. func testCalcHistListOfMatListOfIntegerMatMatListOfIntegerListOfFloat() throws {
  163. let images = [gray128]
  164. let channels:[Int32] = [0]
  165. let histSize:[Int32] = [10]
  166. let ranges:[Float] = [0, 256]
  167. let hist = Mat()
  168. Imgproc.calcHist(images: images, channels: channels, mask: Mat(), hist: hist, histSize: histSize, ranges: ranges)
  169. truth = Mat(rows: 10, cols: 1, type: CvType.CV_32F, scalar: Scalar.all(0))
  170. try truth!.put(row: 5, col: 0, data: [100] as [Float])
  171. try assertMatEqual(truth!, hist, OpenCVTestCase.EPS)
  172. }
  173. func testCalcHistListOfMatListOfIntegerMatMatListOfIntegerListOfFloat2D() throws {
  174. let images = [gray255, gray128]
  175. let channels:[Int32] = [0, 1]
  176. let histSize:[Int32] = [10, 10]
  177. let ranges:[Float] = [0, 256, 0, 256]
  178. let hist = Mat()
  179. Imgproc.calcHist(images: images, channels: channels, mask: Mat(), hist: hist, histSize: histSize, ranges: ranges)
  180. truth = Mat(rows: 10, cols: 10, type: CvType.CV_32F, scalar: Scalar.all(0))
  181. try truth!.put(row: 9, col: 5, data: [100] as [Float])
  182. try assertMatEqual(truth!, hist, OpenCVTestCase.EPS)
  183. }
  184. func testCalcHistListOfMatListOfIntegerMatMatListOfIntegerListOfFloat3D() throws {
  185. let images = [rgbLena]
  186. let hist3D = Mat()
  187. let histList = [Mat(), Mat(), Mat()]
  188. let histSize: [Int32] = [10]
  189. let ranges: [Float] = [0, 256]
  190. for i:Int in 0..<Int(rgbLena.channels()) {
  191. Imgproc.calcHist(images: images, channels: [Int32(i)], mask: Mat(), hist: histList[i], histSize: histSize, ranges: ranges)
  192. XCTAssertEqual(10, histList[i].checkVector(elemChannels: 1))
  193. }
  194. Core.merge(mv: histList, dst: hist3D)
  195. XCTAssertEqual(CvType.CV_32FC3, hist3D.type())
  196. XCTAssertEqual(10, hist3D.checkVector(elemChannels: 3))
  197. let truth = Mat(rows: 10, cols: 1, type: CvType.CV_32FC3)
  198. try truth.put(row: 0, col: 0,
  199. data: [0, 24870, 0,
  200. 1863, 31926, 1,
  201. 56682, 37677, 2260,
  202. 77278, 44751, 32436,
  203. 69397, 41343, 18526,
  204. 27180, 40407, 18658,
  205. 21101, 15993, 32042,
  206. 8343, 18585, 47786,
  207. 300, 6567, 80988,
  208. 0, 25, 29447] as [Float])
  209. try assertMatEqual(truth, hist3D, OpenCVTestCase.EPS)
  210. }
  211. func testCalcHistListOfMatListOfIntegerMatMatListOfIntegerListOfFloatBoolean() throws {
  212. let images = [gray255, gray128]
  213. let channels:[Int32] = [0, 1]
  214. let histSize:[Int32] = [10, 10]
  215. let ranges:[Float] = [0, 256, 0, 256]
  216. let hist = Mat()
  217. Imgproc.calcHist(images: images, channels: channels, mask: Mat(), hist: hist, histSize: histSize, ranges: ranges, accumulate: true)
  218. truth = Mat(rows: 10, cols: 10, type: CvType.CV_32F, scalar: Scalar.all(0))
  219. try truth!.put(row: 9, col: 5, data: [100] as [Float])
  220. try assertMatEqual(truth!, hist, OpenCVTestCase.EPS)
  221. }
  222. func testCannyMatMatDoubleDouble() throws {
  223. Imgproc.Canny(image: gray255, edges: dst, threshold1: 5, threshold2: 10)
  224. try assertMatEqual(gray0, dst)
  225. }
  226. func testCannyMatMatDoubleDoubleIntBoolean() throws {
  227. Imgproc.Canny(image: gray0, edges: dst, threshold1: 5, threshold2: 10, apertureSize: 5, L2gradient: true)
  228. try assertMatEqual(gray0, dst)
  229. }
  230. func testCompareHist() throws {
  231. let H1 = Mat(rows: 3, cols: 1, type: CvType.CV_32F)
  232. let H2 = Mat(rows: 3, cols: 1, type: CvType.CV_32F)
  233. try H1.put(row: 0, col: 0, data: [1, 2, 3] as [Float])
  234. try H2.put(row: 0, col: 0, data: [4, 5, 6] as [Float])
  235. let distance = Imgproc.compareHist(H1: H1, H2: H2, method: .HISTCMP_CORREL)
  236. XCTAssertEqual(1.0, distance, accuracy: OpenCVTestCase.EPS)
  237. }
  238. func testContourAreaMat() throws {
  239. let contour = Mat(rows: 1, cols: 4, type: CvType.CV_32FC2)
  240. try contour.put(row: 0, col: 0, data: [0, 0, 10, 0, 10, 10, 5, 4] as [Float])
  241. let area = Imgproc.contourArea(contour: contour)
  242. XCTAssertEqual(45.0, area, accuracy: OpenCVTestCase.EPS)
  243. }
  244. func testContourAreaMatBoolean() throws {
  245. let contour = Mat(rows: 1, cols: 4, type: CvType.CV_32FC2)
  246. try contour.put(row: 0, col: 0, data: [0, 0, 10, 0, 10, 10, 5, 4] as [Float])
  247. let area = Imgproc.contourArea(contour: contour, oriented: true)
  248. XCTAssertEqual(45.0, area, accuracy: OpenCVTestCase.EPS)
  249. }
  250. func testConvertMapsMatMatMatMatInt() throws {
  251. let map1 = Mat(rows: 1, cols: 4, type: CvType.CV_32FC1, scalar: Scalar(1))
  252. let map2 = Mat(rows: 1, cols: 4, type: CvType.CV_32FC1, scalar: Scalar(2))
  253. let dstmap1 = Mat(rows: 1, cols: 4, type: CvType.CV_16SC2)
  254. let dstmap2 = Mat(rows: 1, cols: 4, type: CvType.CV_16UC1)
  255. Imgproc.convertMaps(map1: map1, map2: map2, dstmap1: dstmap1, dstmap2: dstmap2, dstmap1type: CvType.CV_16SC2)
  256. let truthMap1 = Mat(rows: 1, cols: 4, type: CvType.CV_16SC2)
  257. try truthMap1.put(row: 0, col: 0, data: [1, 2, 1, 2, 1, 2, 1, 2] as [Int16])
  258. try assertMatEqual(truthMap1, dstmap1)
  259. let truthMap2 = Mat(rows: 1, cols: 4, type: CvType.CV_16UC1, scalar: Scalar(0))
  260. try assertMatEqual(truthMap2, dstmap2)
  261. }
  262. func testConvertMapsMatMatMatMatIntBoolean() throws {
  263. let map1 = Mat(rows: 1, cols: 3, type: CvType.CV_32FC1, scalar: Scalar(2))
  264. let map2 = Mat(rows: 1, cols: 3, type: CvType.CV_32FC1, scalar: Scalar(4))
  265. let dstmap1 = Mat(rows: 1, cols: 3, type: CvType.CV_16SC2)
  266. let dstmap2 = Mat(rows: 1, cols: 3, type: CvType.CV_16UC1)
  267. Imgproc.convertMaps(map1: map1, map2: map2, dstmap1: dstmap1, dstmap2: dstmap2, dstmap1type: CvType.CV_16SC2, nninterpolation: false)
  268. // TODO_: write better test (last param == true)
  269. let truthMap1 = Mat(rows: 1, cols: 3, type: CvType.CV_16SC2)
  270. try truthMap1.put(row: 0, col: 0, data: [2, 4, 2, 4, 2, 4] as [Int16])
  271. try assertMatEqual(truthMap1, dstmap1)
  272. let truthMap2 = Mat(rows: 1, cols: 3, type: CvType.CV_16UC1, scalar: Scalar(0))
  273. try assertMatEqual(truthMap2, dstmap2)
  274. }
  275. func testConvexHullMatMat() {
  276. let points = [Point(x: 20, y: 0),
  277. Point(x: 40, y: 0),
  278. Point(x: 30, y: 20),
  279. Point(x: 0, y: 20),
  280. Point(x: 20, y: 10),
  281. Point(x: 30, y: 10)]
  282. var hull = [Int32]()
  283. Imgproc.convexHull(points: points, hull: &hull)
  284. XCTAssert([0, 1, 2, 3] == hull)
  285. }
  286. func testConvexHullMatMatBooleanBoolean() {
  287. let points = [Point(x: 2, y: 0),
  288. Point(x: 4, y: 0),
  289. Point(x: 3, y: 2),
  290. Point(x: 0, y: 2),
  291. Point(x: 2, y: 1),
  292. Point(x: 3, y: 1)]
  293. var hull = [Int32]()
  294. Imgproc.convexHull(points: points, hull: &hull, clockwise: true)
  295. XCTAssert([3, 2, 1, 0] == hull)
  296. }
  297. func testConvexityDefects() throws {
  298. let points = [Point(x: 20, y: 0),
  299. Point(x: 40, y: 0),
  300. Point(x: 30, y: 20),
  301. Point(x: 0, y: 20),
  302. Point(x: 20, y: 10),
  303. Point(x: 30, y: 10)]
  304. var hull = [Int32]()
  305. Imgproc.convexHull(points: points, hull: &hull)
  306. var convexityDefects = [Int4]()
  307. Imgproc.convexityDefects(contour: points, convexhull: hull, convexityDefects: &convexityDefects)
  308. XCTAssertTrue(Int4(v0: 3, v1: 0, v2: 5, v3: 3620) == convexityDefects[0])
  309. }
  310. func testCornerEigenValsAndVecsMatMatIntInt() throws {
  311. let src = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32FC1)
  312. try src.put(row: 0, col: 0, data: [1, 2] as [Float])
  313. try src.put(row: 1, col: 0, data: [4, 2] as [Float])
  314. let blockSize:Int32 = 3
  315. let ksize:Int32 = 5
  316. // TODO: eigen vals and vectors returned = 0 for most src matrices
  317. Imgproc.cornerEigenValsAndVecs(src: src, dst: dst, blockSize: blockSize, ksize: ksize)
  318. truth = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32FC(6), scalar: Scalar(0))
  319. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  320. }
  321. func testCornerEigenValsAndVecsMatMatIntIntInt() throws {
  322. let src = Mat(rows: 4, cols: 4, type: CvType.CV_32FC1, scalar: Scalar(128))
  323. let blockSize:Int32 = 3
  324. let ksize:Int32 = 5
  325. truth = Mat(rows: 4, cols: 4, type: CvType.CV_32FC(6), scalar: Scalar(0))
  326. Imgproc.cornerEigenValsAndVecs(src: src, dst: dst, blockSize: blockSize, ksize: ksize, borderType: .BORDER_REFLECT)
  327. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  328. }
  329. func testCornerHarrisMatMatIntIntDouble() throws {
  330. truth = Mat(rows: OpenCVTestCase.matSize, cols: OpenCVTestCase.matSize, type: CvType.CV_32FC1, scalar: Scalar(0))
  331. let blockSize:Int32 = 5
  332. let ksize:Int32 = 7
  333. let k = 0.1
  334. Imgproc.cornerHarris(src: gray128, dst: dst, blockSize: blockSize, ksize: ksize, k: k)
  335. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  336. }
  337. func testCornerHarrisMatMatIntIntDoubleInt() throws {
  338. truth = Mat(rows: OpenCVTestCase.matSize, cols: OpenCVTestCase.matSize, type: CvType.CV_32FC1, scalar: Scalar(0))
  339. let blockSize:Int32 = 5
  340. let ksize:Int32 = 7
  341. let k = 0.1
  342. Imgproc.cornerHarris(src: gray255, dst: dst, blockSize: blockSize, ksize: ksize, k: k, borderType: .BORDER_REFLECT)
  343. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  344. }
  345. func testCornerMinEigenValMatMatInt() throws {
  346. let src = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32FC1)
  347. try src.put(row: 0, col: 0, data: [1, 2] as [Float])
  348. try src.put(row: 1, col: 0, data: [2, 1] as [Float])
  349. let blockSize:Int32 = 5
  350. Imgproc.cornerMinEigenVal(src: src, dst: dst, blockSize: blockSize)
  351. truth = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32FC1, scalar: Scalar(0))
  352. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  353. Imgproc.cornerMinEigenVal(src: gray255, dst: dst, blockSize: blockSize)
  354. truth = Mat(rows: OpenCVTestCase.matSize, cols: OpenCVTestCase.matSize, type: CvType.CV_32FC1, scalar: Scalar(0))
  355. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  356. }
  357. func testCornerMinEigenValMatMatIntInt() throws {
  358. let src = Mat.eye(rows: 3, cols: 3, type: CvType.CV_32FC1)
  359. let blockSize:Int32 = 3
  360. let ksize:Int32 = 5
  361. Imgproc.cornerMinEigenVal(src: src, dst: dst, blockSize: blockSize, ksize: ksize)
  362. truth = Mat(rows: 3, cols: 3, type: CvType.CV_32FC1)
  363. try truth!.put(row: 0, col: 0, data: [1.0 / 18, 1.0 / 36, 1.0 / 18] as [Float])
  364. try truth!.put(row: 1, col: 0, data: [1.0 / 36, 1.0 / 18, 1.0 / 36] as [Float])
  365. try truth!.put(row: 2, col: 0, data: [1.0 / 18, 1.0 / 36, 1.0 / 18] as [Float])
  366. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  367. }
  368. func testCornerMinEigenValMatMatIntIntInt() throws {
  369. let src = Mat.eye(rows: 3, cols: 3, type: CvType.CV_32FC1)
  370. let blockSize:Int32 = 3
  371. let ksize:Int32 = 5
  372. Imgproc.cornerMinEigenVal(src: src, dst: dst, blockSize: blockSize, ksize: ksize, borderType: .BORDER_REFLECT)
  373. truth = Mat(rows: 3, cols: 3, type: CvType.CV_32FC1)
  374. try truth!.put(row: 0, col: 0, data: [0.68055558, 0.92708349, 0.5868057])
  375. try truth!.put(row: 1, col: 0, data: [0.92708343, 0.92708343, 0.92708343])
  376. try truth!.put(row: 2, col: 0, data: [0.58680564, 0.92708343, 0.68055564])
  377. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  378. }
  379. func testCornerSubPix() {
  380. let img = Mat(rows: OpenCVTestCase.matSize, cols: OpenCVTestCase.matSize, type: CvType.CV_8U, scalar: Scalar(128))
  381. let truthPosition = Point(x: img.cols() / 2, y: img.rows() / 2)
  382. let r = Rect(point: Point(x: 0, y: 0), point: truthPosition)
  383. Imgproc.rectangle(img: img, pt1: r.tl(), pt2: r.br(), color: Scalar(0), thickness: Core.FILLED)
  384. let corners = MatOfPoint2f(array: [Point2f(x: Float(truthPosition.x + 1), y: Float(truthPosition.y + 1))])
  385. let winSize = Size(width: 2, height: 2)
  386. let zeroZone = Size(width: -1, height: -1)
  387. let criteria = TermCriteria(type: TermCriteria.eps, maxCount: 0, epsilon: 0.01)
  388. Imgproc.cornerSubPix(image: img, corners: corners, winSize: winSize, zeroZone: zeroZone, criteria: criteria)
  389. assertPoint2fEquals(Point2f(x: Float(truthPosition.x), y: Float(truthPosition.y)), corners.toArray()[0], OpenCVTestCase.weakFEPS)
  390. }
  391. func testDilateMatMatMat() throws {
  392. let kernel = Mat()
  393. Imgproc.dilate(src: gray255, dst: dst, kernel: kernel)
  394. try assertMatEqual(gray255, dst)
  395. Imgproc.dilate(src: gray1, dst: dst, kernel: kernel)
  396. try assertMatEqual(gray1, dst)
  397. }
  398. func testDistanceTransformWithLabels() throws {
  399. let dstLables = getMat(CvType.CV_32SC1, vals: [0])
  400. let labels = Mat()
  401. Imgproc.distanceTransform(src: gray128, dst: dst, labels: labels, distanceType: .DIST_L2, maskSize: .DIST_MASK_3)
  402. try assertMatEqual(dstLables, labels)
  403. try assertMatEqual(getMat(CvType.CV_32FC1, vals: [8192]), dst, OpenCVTestCase.EPS)
  404. }
  405. func testDrawContoursMatListOfMatIntScalar() {
  406. let gray0clone = gray0.clone()
  407. Imgproc.rectangle(img: gray0clone, pt1: Point(x: 1, y: 2), pt2: Point(x: 7, y: 8), color: Scalar(100))
  408. var contours = [[Point]]()
  409. Imgproc.findContours(image: gray0clone, contours: &contours, hierarchy: Mat(), mode: .RETR_EXTERNAL, method: .CHAIN_APPROX_SIMPLE)
  410. Imgproc.drawContours(image: gray0clone, contours: contours, contourIdx: -1, color: Scalar(0))
  411. XCTAssertEqual(0, Core.countNonZero(src: gray0clone))
  412. }
  413. func testDrawContoursMatListOfMatIntScalarInt() {
  414. let gray0clone = gray0.clone()
  415. Imgproc.rectangle(img: gray0clone, pt1: Point(x: 1, y: 2), pt2: Point(x: 7, y: 8), color: Scalar(100))
  416. var contours = [[Point]]()
  417. Imgproc.findContours(image: gray0clone, contours: &contours, hierarchy: Mat(), mode: .RETR_EXTERNAL, method: .CHAIN_APPROX_SIMPLE)
  418. Imgproc.drawContours(image: gray0clone, contours: contours, contourIdx: -1, color: Scalar(0), thickness: Core.FILLED)
  419. XCTAssertEqual(0, Core.countNonZero(src: gray0clone))
  420. }
  421. func testEqualizeHist() throws {
  422. Imgproc.equalizeHist(src: gray0, dst: dst)
  423. try assertMatEqual(gray0, dst)
  424. Imgproc.equalizeHist(src: gray255, dst: dst)
  425. try assertMatEqual(gray255, dst)
  426. }
  427. func testErodeMatMatMat() throws {
  428. let kernel = Mat()
  429. Imgproc.erode(src: gray128, dst: dst, kernel: kernel)
  430. try assertMatEqual(gray128, dst)
  431. }
  432. func testErodeMatMatMatPointInt() throws {
  433. let src = Mat(rows: 3, cols: 3, type: CvType.CV_8U)
  434. try src.put(row: 0, col: 0, data: [15, 9, 10] as [Int8])
  435. try src.put(row: 1, col: 0, data: [10, 8, 12] as [Int8])
  436. try src.put(row: 2, col: 0, data: [12, 20, 25] as [Int8])
  437. let kernel = Mat()
  438. Imgproc.erode(src: src, dst: dst, kernel: kernel, anchor: anchorPoint, iterations: 10)
  439. truth = Mat(rows: 3, cols: 3, type: CvType.CV_8U, scalar: Scalar(8))
  440. try assertMatEqual(truth!, dst)
  441. }
  442. func testErodeMatMatMatPointIntIntScalar() throws {
  443. let src = Mat(rows: 3, cols: 3, type: CvType.CV_8U)
  444. try src.put(row: 0, col: 0, data: [15, 9, 10] as [Int8])
  445. try src.put(row: 1, col: 0, data: [10, 8, 12] as [Int8])
  446. try src.put(row: 2, col: 0, data: [12, 20, 25] as [Int8])
  447. let kernel = Mat()
  448. let sc = Scalar(3, 3)
  449. Imgproc.erode(src: src, dst: dst, kernel: kernel, anchor: anchorPoint, iterations: 10, borderType: .BORDER_REFLECT, borderValue: sc)
  450. truth = Mat(rows: 3, cols: 3, type: CvType.CV_8U, scalar: Scalar(8))
  451. try assertMatEqual(truth!, dst)
  452. }
  453. func testFilter2DMatMatIntMat() throws {
  454. let src = Mat.eye(rows: 4, cols: 4, type: CvType.CV_32F)
  455. let kernel = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32F, scalar: Scalar(1))
  456. Imgproc.filter2D(src: src, dst: dst, ddepth: -1, kernel: kernel)
  457. truth = Mat(rows: 4, cols: 4, type: CvType.CV_32F)
  458. try truth!.put(row: 0, col: 0, data: [2, 2, 1, 0] as [Float])
  459. try truth!.put(row: 1, col: 0, data: [2, 2, 1, 0] as [Float])
  460. try truth!.put(row: 2, col: 0, data: [1, 1, 2, 1] as [Float])
  461. try truth!.put(row: 3, col: 0, data: [0, 0, 1, 2] as [Float])
  462. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  463. }
  464. func testFilter2DMatMatIntMatPointDoubleInt() throws {
  465. let kernel = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32F, scalar: Scalar(0))
  466. let point = Point(x: 0, y: 0)
  467. Imgproc.filter2D(src: gray128, dst: dst, ddepth: -1, kernel: kernel, anchor: point, delta: 2, borderType: .BORDER_CONSTANT)
  468. try assertMatEqual(gray2, dst)
  469. }
  470. func testFindContoursMatListOfMatMatIntInt() {
  471. let img = Mat(rows: 50, cols: 50, type: CvType.CV_8UC1, scalar: Scalar(0))
  472. var contours = [[Point]]()
  473. let hierarchy = Mat()
  474. Imgproc.findContours(image: img, contours: &contours, hierarchy: hierarchy, mode: .RETR_EXTERNAL, method: .CHAIN_APPROX_SIMPLE)
  475. // no contours on empty image
  476. XCTAssertEqual(contours.count, 0)
  477. XCTAssertEqual(contours.count, hierarchy.total())
  478. Imgproc.rectangle(img: img, pt1: Point(x: 10, y: 20), pt2: Point(x: 20, y: 30), color: Scalar(100), thickness: 3, lineType: .LINE_AA, shift: 0)
  479. Imgproc.rectangle(img: img, pt1: Point(x: 30, y: 35), pt2: Point(x: 40, y: 45), color: Scalar(200))
  480. Imgproc.findContours(image: img, contours: &contours, hierarchy: hierarchy, mode: .RETR_EXTERNAL, method: .CHAIN_APPROX_SIMPLE)
  481. // two contours of two rectangles
  482. XCTAssertEqual(contours.count, 2)
  483. XCTAssertEqual(contours.count, hierarchy.total())
  484. }
  485. func testFindContoursMatListOfMatMatIntIntPoint() throws {
  486. let img = Mat(rows: 50, cols: 50, type: CvType.CV_8UC1, scalar: Scalar(0))
  487. let img2 = img.submat(rowStart: 5, rowEnd: 50, colStart: 3, colEnd: 50)
  488. var contours = [[Point]]()
  489. var contours2 = [[Point]]()
  490. let hierarchy = Mat()
  491. Imgproc.rectangle(img: img, pt1: Point(x: 10, y: 20), pt2: Point(x: 20, y: 30), color: Scalar(100), thickness: 3, lineType: .LINE_AA, shift: 0)
  492. Imgproc.rectangle(img: img, pt1: Point(x: 30, y: 35), pt2: Point(x: 40, y: 45), color: Scalar(200))
  493. Imgproc.findContours(image: img, contours: &contours, hierarchy: hierarchy, mode: .RETR_EXTERNAL, method: .CHAIN_APPROX_SIMPLE)
  494. Imgproc.findContours(image: img2, contours: &contours2, hierarchy: hierarchy, mode: .RETR_EXTERNAL, method: .CHAIN_APPROX_SIMPLE, offset: Point(x: 3, y: 5))
  495. XCTAssertEqual(contours.count, contours2.count)
  496. XCTAssert(contours[0] == contours2[0])
  497. }
  498. func testFitEllipse() {
  499. let points = [Point2f(x: 0, y: 0), Point2f(x: -1, y: 1), Point2f(x: 1, y: 1), Point2f(x: 1, y: -1), Point2f(x: -1, y: -1)]
  500. let rrect = Imgproc.fitEllipse(points: points)
  501. let FIT_ELLIPSE_CENTER_EPS:Float = 0.01
  502. let FIT_ELLIPSE_SIZE_EPS:Float = 0.4
  503. assertPoint2fEquals(Point2f(x: 0, y: 0), rrect.center, FIT_ELLIPSE_CENTER_EPS)
  504. XCTAssertEqual(Float(2.828), rrect.size.width, accuracy: FIT_ELLIPSE_SIZE_EPS)
  505. XCTAssertEqual(Float(2.828), rrect.size.height, accuracy: FIT_ELLIPSE_SIZE_EPS)
  506. }
  507. func testFitLine() throws {
  508. let points = Mat(rows: 1, cols: 4, type: CvType.CV_32FC2)
  509. try points.put(row: 0, col: 0, data: [0, 0, 2, 3, 3, 4, 5, 8] as [Float])
  510. let linePoints = Mat(rows: 4, cols: 1, type: CvType.CV_32FC1)
  511. try linePoints.put(row: 0, col: 0, data: [0.53198653, 0.84675282, 2.5, 3.75] as [Float])
  512. Imgproc.fitLine(points: points, line: dst, distType: .DIST_L12, param: 0, reps: 0.01, aeps: 0.01)
  513. try assertMatEqual(linePoints, dst, OpenCVTestCase.EPS)
  514. }
  515. func testFloodFillMatMatPointScalar() throws {
  516. let mask = Mat(rows: OpenCVTestCase.matSize + 2, cols: OpenCVTestCase.matSize + 2, type: CvType.CV_8U, scalar: Scalar(0))
  517. let img = gray0
  518. Imgproc.circle(img: mask, center: Point(x: OpenCVTestCase.matSize / 2 + 1, y: OpenCVTestCase.matSize / 2 + 1), radius: 3, color: Scalar(2))
  519. let retval = Imgproc.floodFill(image: img, mask: mask, seedPoint: Point(x: OpenCVTestCase.matSize / 2, y: OpenCVTestCase.matSize / 2), newVal: Scalar(1))
  520. XCTAssertEqual(Core.countNonZero(src: img), retval)
  521. Imgproc.circle(img: mask, center: Point(x: OpenCVTestCase.matSize / 2 + 1, y: OpenCVTestCase.matSize / 2 + 1), radius: 3, color: Scalar(0))
  522. XCTAssertEqual(retval + 4 * (OpenCVTestCase.matSize + 1), Core.countNonZero(src: mask))
  523. try assertMatEqual(mask.submat(rowStart: 1, rowEnd: OpenCVTestCase.matSize + 1, colStart: 1, colEnd: OpenCVTestCase.matSize + 1), img)
  524. }
  525. func testFloodFillMatMatPointScalar_WithoutMask() {
  526. let img = gray0
  527. Imgproc.circle(img: img, center: Point(x: OpenCVTestCase.matSize / 2, y: OpenCVTestCase.matSize / 2), radius: 3, color: Scalar(2))
  528. // TODO: ideally we should pass null instead of "new Mat()"
  529. let retval = Imgproc.floodFill(image: img, mask: Mat(), seedPoint: Point(x: OpenCVTestCase.matSize / 2, y: OpenCVTestCase.matSize / 2), newVal: Scalar(1))
  530. Imgproc.circle(img: img, center: Point(x: OpenCVTestCase.matSize / 2, y: OpenCVTestCase.matSize / 2), radius: 3, color: Scalar(0))
  531. XCTAssertEqual(Core.countNonZero(src: img), retval)
  532. }
  533. func testGaussianBlurMatMatSizeDouble() throws {
  534. Imgproc.GaussianBlur(src: gray0, dst: dst, ksize: size, sigmaX: 1)
  535. try assertMatEqual(gray0, dst)
  536. Imgproc.GaussianBlur(src: gray2, dst: dst, ksize: size, sigmaX: 1)
  537. try assertMatEqual(gray2, dst)
  538. }
  539. func testGaussianBlurMatMatSizeDoubleDouble() throws {
  540. Imgproc.GaussianBlur(src: gray2, dst: dst, ksize: size, sigmaX: 0, sigmaY: 0)
  541. try assertMatEqual(gray2, dst)
  542. }
  543. func testGaussianBlurMatMatSizeDoubleDoubleInt() throws {
  544. Imgproc.GaussianBlur(src: gray2, dst: dst, ksize: size, sigmaX: 1, sigmaY: 3, borderType: .BORDER_REFLECT)
  545. try assertMatEqual(gray2, dst)
  546. }
  547. func testGetAffineTransform() throws {
  548. let src = [Point2f(x: 2, y: 3), Point2f(x: 3, y: 1), Point2f(x: 1, y: 4)]
  549. let dst = [Point2f(x: 3, y: 3), Point2f(x: 7, y: 4), Point2f(x: 5, y: 6)]
  550. let transform = Imgproc.getAffineTransform(src: src, dst: dst)
  551. let truth = Mat(rows: 2, cols: 3, type: CvType.CV_64FC1)
  552. try truth.put(row: 0, col: 0, data: [-8.0, -6.0, 37.0])
  553. try truth.put(row: 1, col: 0, data: [-7.0, -4.0, 29.0])
  554. try assertMatEqual(truth, transform, OpenCVTestCase.EPS)
  555. }
  556. func testGetDerivKernelsMatMatIntIntInt() throws {
  557. let kx = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32F)
  558. let ky = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32F)
  559. let expKx = Mat(rows: 3, cols: 1, type: CvType.CV_32F)
  560. let expKy = Mat(rows: 3, cols: 1, type: CvType.CV_32F)
  561. try kx.put(row: 0, col: 0, data: [1, 1] as [Float])
  562. try kx.put(row: 1, col: 0, data: [1, 1] as [Float])
  563. try ky.put(row: 0, col: 0, data: [2, 2] as [Float])
  564. try ky.put(row: 1, col: 0, data: [2, 2] as [Float])
  565. try expKx.put(row: 0, col: 0, data: [1, -2, 1] as [Float])
  566. try expKy.put(row: 0, col: 0, data: [1, -2, 1] as [Float])
  567. Imgproc.getDerivKernels(kx: kx, ky: ky, dx: 2, dy: 2, ksize: 3)
  568. try assertMatEqual(expKx, kx, OpenCVTestCase.EPS)
  569. try assertMatEqual(expKy, ky, OpenCVTestCase.EPS)
  570. }
  571. func testGetDerivKernelsMatMatIntIntIntBooleanInt() throws {
  572. let kx = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32F)
  573. let ky = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32F)
  574. let expKx = Mat(rows: 3, cols: 1, type: CvType.CV_32F)
  575. let expKy = Mat(rows: 3, cols: 1, type: CvType.CV_32F)
  576. try kx.put(row: 0, col: 0, data: [1, 1] as [Float])
  577. try kx.put(row: 1, col: 0, data: [1, 1] as [Float])
  578. try ky.put(row: 0, col: 0, data: [2, 2] as [Float])
  579. try ky.put(row: 1, col: 0, data: [2, 2] as [Float])
  580. try expKx.put(row: 0, col: 0, data: [1, -2, 1] as [Float])
  581. try expKy.put(row: 0, col: 0, data: [1, -2, 1] as [Float])
  582. Imgproc.getDerivKernels(kx: kx, ky: ky, dx: 2, dy: 2, ksize: 3, normalize: true, ktype: CvType.CV_32F)
  583. try assertMatEqual(expKx, kx, OpenCVTestCase.EPS)
  584. try assertMatEqual(expKy, ky, OpenCVTestCase.EPS)
  585. // TODO_: write better test
  586. }
  587. func testGetGaussianKernelIntDouble() throws {
  588. dst = Imgproc.getGaussianKernel(ksize: 1, sigma: 0.5)
  589. truth = Mat(rows: 1, cols: 1, type: CvType.CV_64FC1, scalar: Scalar(1))
  590. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  591. }
  592. func testGetGaussianKernelIntDoubleInt() throws {
  593. dst = Imgproc.getGaussianKernel(ksize: 3, sigma: 0.8, ktype: CvType.CV_32F)
  594. truth = Mat(rows: 3, cols: 1, type: CvType.CV_32F)
  595. try truth!.put(row: 0, col: 0, data: [0.23899426, 0.52201146, 0.23899426] as [Float])
  596. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  597. }
  598. func testGetRectSubPixMatSizePointMat() throws {
  599. let size = Size(width: 3, height: 3)
  600. let center = Point2f(x: Float(gray255.cols() / 2), y: Float(gray255.rows() / 2))
  601. Imgproc.getRectSubPix(image: gray255, patchSize: size, center: center, patch: dst)
  602. truth = Mat(rows: 3, cols: 3, type: CvType.CV_8U, scalar: Scalar(255))
  603. try assertMatEqual(truth!, dst)
  604. }
  605. func testGetRectSubPixMatSizePointMatInt() throws {
  606. let src = Mat(rows: 10, cols: 10, type: CvType.CV_32F, scalar: Scalar(2))
  607. let patchSize = Size(width: 5, height: 5)
  608. let center = Point2f(x: Float(src.cols() / 2), y: Float(src.rows() / 2))
  609. Imgproc.getRectSubPix(image: src, patchSize: patchSize, center: center, patch: dst)
  610. truth = Mat(rows: 5, cols: 5, type: CvType.CV_32F, scalar: Scalar(2))
  611. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  612. }
  613. func testGetRotationMatrix2D() throws {
  614. let center = Point2f(x: 0, y: 0)
  615. dst = Imgproc.getRotationMatrix2D(center: center, angle: 0, scale: 1)
  616. truth = Mat(rows: 2, cols: 3, type: CvType.CV_64F)
  617. try truth!.put(row: 0, col: 0, data: [1.0, 0.0, 0.0])
  618. try truth!.put(row: 1, col: 0, data: [0.0, 1.0, 0.0])
  619. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  620. }
  621. func testGetStructuringElementIntSize() throws {
  622. dst = Imgproc.getStructuringElement(shape: .MORPH_RECT, ksize: size)
  623. truth = Mat(rows: 3, cols: 3, type: CvType.CV_8UC1, scalar: Scalar(1))
  624. try assertMatEqual(truth!, dst)
  625. }
  626. func testGetStructuringElementIntSizePoint() throws {
  627. dst = Imgproc.getStructuringElement(shape: .MORPH_CROSS, ksize: size, anchor: anchorPoint)
  628. truth = Mat(rows: 3, cols: 3, type: CvType.CV_8UC1)
  629. try truth!.put(row: 0, col: 0, data: [0, 0, 1] as [Int8])
  630. try truth!.put(row: 1, col: 0, data: [0, 0, 1] as [Int8])
  631. try truth!.put(row: 2, col: 0, data: [1, 1, 1] as [Int8])
  632. try assertMatEqual(truth!, dst)
  633. }
  634. func testGoodFeaturesToTrackMatListOfPointIntDoubleDouble() {
  635. let src = gray0
  636. Imgproc.rectangle(img: src, pt1: Point(x: 2, y: 2), pt2: Point(x: 8, y: 8), color: Scalar(100), thickness: -1)
  637. var lp = [Point]()
  638. Imgproc.goodFeaturesToTrack(image: src, corners: &lp, maxCorners: 100, qualityLevel: 0.01, minDistance: 3)
  639. XCTAssertEqual(4, lp.count)
  640. }
  641. func testGoodFeaturesToTrackMatListOfPointIntDoubleDoubleMatIntBooleanDouble() {
  642. let src = gray0
  643. Imgproc.rectangle(img: src, pt1: Point(x: 2, y: 2), pt2: Point(x: 8, y: 8), color: Scalar(100), thickness: -1)
  644. var lp = [Point]()
  645. Imgproc.goodFeaturesToTrack(image: src, corners: &lp, maxCorners: 100, qualityLevel: 0.01, minDistance: 3, mask: gray1, blockSize: 4, gradientSize: 3, useHarrisDetector: true, k: 0)
  646. XCTAssertEqual(4, lp.count)
  647. }
  648. func testHoughCirclesMatMatIntDoubleDouble() {
  649. let sz:Int32 = 512
  650. let img = Mat(rows: sz, cols: sz, type: CvType.CV_8U, scalar: Scalar(128))
  651. let circles = Mat()
  652. Imgproc.HoughCircles(image: img, circles: circles, method: .HOUGH_GRADIENT, dp: 2.0, minDist: Double(img.rows() / 4))
  653. XCTAssertEqual(0, circles.cols())
  654. }
  655. func testHoughCirclesMatMatIntDoubleDouble1() {
  656. let sz: Int32 = 512
  657. let img = Mat(rows: sz, cols: sz, type: CvType.CV_8U, scalar: Scalar(128))
  658. let circles = Mat()
  659. let center = Point(x: img.cols() / 2, y: img.rows() / 2)
  660. let radius = min(img.cols() / 4, img.rows() / 4)
  661. Imgproc.circle(img: img, center: center, radius: radius, color: colorBlack, thickness: 3)
  662. Imgproc.HoughCircles(image: img, circles: circles, method: .HOUGH_GRADIENT, dp: 2.0, minDist: Double(img.rows() / 4))
  663. XCTAssertEqual(1, circles.cols())
  664. }
  665. func testHoughLinesMatMatDoubleDoubleInt() {
  666. let sz:Int32 = 512
  667. let img = Mat(rows: sz, cols: sz, type: CvType.CV_8U, scalar: Scalar(0))
  668. let point1 = Point(x: 50, y: 50)
  669. let point2 = Point(x: img.cols() / 2, y: img.rows() / 2)
  670. Imgproc.line(img: img, pt1: point1, pt2: point2, color: colorWhite, thickness: 1)
  671. let lines = Mat()
  672. Imgproc.HoughLines(image: img, lines: lines, rho: 1, theta: 3.1415926/180, threshold: 100)
  673. XCTAssertEqual(1, lines.cols())
  674. }
  675. func testHoughLinesPMatMatDoubleDoubleInt() {
  676. let sz:Int32 = 512
  677. let img = Mat(rows: sz, cols: sz, type: CvType.CV_8U, scalar: Scalar(0))
  678. let point1 = Point(x: 0, y: 0)
  679. let point2 = Point(x: sz, y: sz)
  680. let point3 = Point(x: sz, y: 0)
  681. let point4 = Point(x: 2*sz/3, y: sz/3)
  682. Imgproc.line(img: img, pt1: point1, pt2: point2, color: Scalar.all(255), thickness: 1)
  683. Imgproc.line(img: img, pt1: point3, pt2: point4, color: Scalar.all(255), thickness: 1)
  684. let lines = Mat()
  685. Imgproc.HoughLinesP(image: img, lines: lines, rho: 1, theta: 3.1415926/180, threshold: 100)
  686. XCTAssertEqual(2, lines.rows())
  687. }
  688. func testIntegral2MatMatMat() throws {
  689. let src = Mat(rows: 3, cols: 3, type: CvType.CV_32F, scalar: Scalar(3))
  690. let expSum = Mat(rows: 4, cols: 4, type: CvType.CV_64F)
  691. let expSqsum = Mat(rows: 4, cols: 4, type: CvType.CV_64F)
  692. let sum = Mat()
  693. let sqsum = Mat()
  694. try expSum.put(row: 0, col: 0, data: [0.0, 0.0, 0.0, 0.0])
  695. try expSum.put(row: 1, col: 0, data: [0.0, 3.0, 6.0, 9.0])
  696. try expSum.put(row: 2, col: 0, data: [0.0, 6.0, 12.0, 18.0])
  697. try expSum.put(row: 3, col: 0, data: [0.0, 9.0, 18.0, 27.0])
  698. try expSqsum.put(row: 0, col: 0, data: [0.0, 0.0, 0.0, 0.0])
  699. try expSqsum.put(row: 1, col: 0, data: [0.0, 9.0, 18.0, 27.0])
  700. try expSqsum.put(row: 2, col: 0, data: [0.0, 18.0, 36.0, 54.0])
  701. try expSqsum.put(row: 3, col: 0, data: [0.0, 27.0, 54.0, 81.0])
  702. Imgproc.integral(src: src, sum: sum, sqsum: sqsum)
  703. try assertMatEqual(expSum, sum, OpenCVTestCase.EPS)
  704. try assertMatEqual(expSqsum, sqsum, OpenCVTestCase.EPS)
  705. }
  706. func testIntegral2MatMatMatInt() throws {
  707. let src = Mat(rows: 3, cols: 3, type: CvType.CV_32F, scalar: Scalar(3))
  708. let expSum = Mat(rows: 4, cols: 4, type: CvType.CV_64F)
  709. let expSqsum = Mat(rows: 4, cols: 4, type: CvType.CV_64F)
  710. let sum = Mat()
  711. let sqsum = Mat()
  712. try expSum.put(row: 0, col: 0, data: [0.0, 0.0, 0.0, 0.0])
  713. try expSum.put(row: 1, col: 0, data: [0.0, 3.0, 6.0, 9.0])
  714. try expSum.put(row: 2, col: 0, data: [0.0, 6.0, 12.0, 18.0])
  715. try expSum.put(row: 3, col: 0, data: [0.0, 9.0, 18.0, 27.0])
  716. try expSqsum.put(row: 0, col: 0, data: [0.0, 0.0, 0.0, 0.0])
  717. try expSqsum.put(row: 1, col: 0, data: [0.0, 9.0, 18.0, 27.0])
  718. try expSqsum.put(row: 2, col: 0, data: [0.0, 18.0, 36.0, 54.0])
  719. try expSqsum.put(row: 3, col: 0, data: [0.0, 27.0, 54.0, 81.0])
  720. Imgproc.integral(src: src, sum: sum, sqsum: sqsum, sdepth: CvType.CV_64F, sqdepth: CvType.CV_64F)
  721. try assertMatEqual(expSum, sum, OpenCVTestCase.EPS)
  722. try assertMatEqual(expSqsum, sqsum, OpenCVTestCase.EPS)
  723. }
  724. func testIntegral3MatMatMatMat() throws {
  725. let src = Mat(rows: 1, cols: 1, type: CvType.CV_32F, scalar: Scalar(1))
  726. let expSum = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_64F)
  727. let expSqsum = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_64F)
  728. let expTilted = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_64F)
  729. let sum = Mat()
  730. let sqsum = Mat()
  731. let tilted = Mat()
  732. try expSum.put(row: 0, col: 0, data: [0.0, 0.0])
  733. try expSum.put(row: 1, col: 0, data: [0.0, 1.0])
  734. try expSqsum.put(row: 0, col: 0, data: [0.0, 0.0])
  735. try expSqsum.put(row: 1, col: 0, data: [0.0, 1.0])
  736. try expTilted.put(row: 0, col: 0, data: [0.0, 0.0])
  737. try expTilted.put(row: 1, col: 0, data: [0.0, 1.0])
  738. Imgproc.integral(src: src, sum: sum, sqsum: sqsum, tilted: tilted)
  739. try assertMatEqual(expSum, sum, OpenCVTestCase.EPS)
  740. try assertMatEqual(expSqsum, sqsum, OpenCVTestCase.EPS)
  741. try assertMatEqual(expTilted, tilted, OpenCVTestCase.EPS)
  742. }
  743. func testIntegral3MatMatMatMatInt() throws {
  744. let src = Mat(rows: 1, cols: 1, type: CvType.CV_32F, scalar: Scalar(1))
  745. let expSum = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_64F)
  746. let expSqsum = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_64F)
  747. let expTilted = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_64F)
  748. let sum = Mat()
  749. let sqsum = Mat()
  750. let tilted = Mat()
  751. try expSum.put(row: 0, col: 0, data: [0.0, 0.0])
  752. try expSum.put(row: 1, col: 0, data: [0.0, 1.0])
  753. try expSqsum.put(row: 0, col: 0, data: [0.0, 0.0])
  754. try expSqsum.put(row: 1, col: 0, data: [0.0, 1.0])
  755. try expTilted.put(row: 0, col: 0, data: [0.0, 0.0])
  756. try expTilted.put(row: 1, col: 0, data: [0.0, 1.0])
  757. Imgproc.integral(src: src, sum: sum, sqsum: sqsum, tilted: tilted, sdepth: CvType.CV_64F, sqdepth: CvType.CV_64F)
  758. try assertMatEqual(expSum, sum, OpenCVTestCase.EPS)
  759. try assertMatEqual(expSqsum, sqsum, OpenCVTestCase.EPS)
  760. try assertMatEqual(expTilted, tilted, OpenCVTestCase.EPS)
  761. }
  762. func testIntegralMatMat() throws {
  763. let src = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32F, scalar: Scalar(2))
  764. Imgproc.integral(src: src, sum: dst)
  765. truth = Mat(rows: 3, cols: 3, type: CvType.CV_64F)
  766. try truth!.put(row: 0, col: 0, data: [0.0, 0.0, 0.0])
  767. try truth!.put(row: 1, col: 0, data: [0.0, 2.0, 4.0])
  768. try truth!.put(row: 2, col: 0, data: [0.0, 4.0, 8.0])
  769. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  770. }
  771. func testIntegralMatMatInt() throws {
  772. let src = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32F, scalar: Scalar(2))
  773. Imgproc.integral(src: src, sum: dst, sdepth: CvType.CV_64F)
  774. truth = Mat(rows: 3, cols: 3, type: CvType.CV_64F)
  775. try truth!.put(row: 0, col: 0, data: [0.0, 0.0, 0.0])
  776. try truth!.put(row: 1, col: 0, data: [0.0, 2.0, 4.0])
  777. try truth!.put(row: 2, col: 0, data: [0.0, 4.0, 8.0])
  778. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  779. }
  780. func testInvertAffineTransform() throws {
  781. let src = Mat(rows: 2, cols: 3, type: CvType.CV_64F, scalar: Scalar(1))
  782. Imgproc.invertAffineTransform(M: src, iM: dst)
  783. truth = Mat(rows: 2, cols: 3, type: CvType.CV_64F, scalar: Scalar(0))
  784. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  785. }
  786. func testIsContourConvex() {
  787. let contour1 = [Point(x: 0, y: 0), Point(x: 10, y: 0), Point(x: 10, y: 10), Point(x: 5, y: 4)]
  788. XCTAssertFalse(Imgproc.isContourConvex(contour: contour1))
  789. let contour2 = [Point(x: 0, y: 0), Point(x: 10, y: 0), Point(x: 10, y: 10), Point(x: 5, y: 6)]
  790. XCTAssert(Imgproc.isContourConvex(contour: contour2))
  791. }
  792. func testLaplacianMatMatInt() throws {
  793. Imgproc.Laplacian(src: gray0, dst: dst, ddepth: CvType.CV_8U)
  794. try assertMatEqual(gray0, dst)
  795. }
  796. func testLaplacianMatMatIntIntDoubleDouble() throws {
  797. let src = Mat.eye(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32F)
  798. Imgproc.Laplacian(src: src, dst: dst, ddepth: CvType.CV_32F, ksize: 1, scale: 2, delta: OpenCVTestCase.EPS)
  799. truth = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32F)
  800. try truth!.put(row: 0, col: 0, data: [-7.9990001, 8.0009995])
  801. try truth!.put(row: 1, col: 0, data: [8.0009995, -7.9990001])
  802. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  803. }
  804. func testLaplacianMatMatIntIntDoubleDoubleInt() throws {
  805. let src = Mat(rows: 3, cols: 3, type: CvType.CV_32F, scalar: Scalar(2))
  806. Imgproc.Laplacian(src: src, dst: dst, ddepth: CvType.CV_32F, ksize: 1, scale: 2, delta: OpenCVTestCase.EPS, borderType: .BORDER_REFLECT)
  807. truth = Mat(rows: 3, cols: 3, type: CvType.CV_32F, scalar: Scalar(0.00099945068))
  808. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  809. }
  810. func testMatchShapes() throws {
  811. let contour1 = Mat(rows: 1, cols: 4, type: CvType.CV_32FC2)
  812. let contour2 = Mat(rows: 1, cols: 4, type: CvType.CV_32FC2)
  813. try contour1.put(row: 0, col: 0, data: [1, 1, 5, 1, 4, 3, 6, 2] as [Float])
  814. try contour2.put(row: 0, col: 0, data: [1, 1, 6, 1, 4, 1, 2, 5] as [Float])
  815. let distance = Imgproc.matchShapes(contour1: contour1, contour2: contour2, method: .CONTOURS_MATCH_I1, parameter: 1)
  816. XCTAssertEqual(2.81109697365334, distance, accuracy:OpenCVTestCase.EPS)
  817. }
  818. func testMatchTemplate() throws {
  819. let image = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_8U)
  820. let templ = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_8U)
  821. try image.put(row: 0, col: 0, data: [1, 2, 3, 4] as [Int8])
  822. try templ.put(row: 0, col: 0, data: [5, 6, 7, 8] as [Int8])
  823. Imgproc.matchTemplate(image: image, templ: templ, result: dst, method: .TM_CCORR)
  824. truth = Mat(rows: 1, cols: 1, type: CvType.CV_32F, scalar: Scalar(70))
  825. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  826. Imgproc.matchTemplate(image: gray255, templ: gray0, result: dst, method: .TM_CCORR)
  827. truth = Mat(rows: 1, cols: 1, type: CvType.CV_32F, scalar: Scalar(0))
  828. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  829. }
  830. func testMedianBlur() throws {
  831. Imgproc.medianBlur(src: gray255, dst: dst, ksize: 5)
  832. try assertMatEqual(gray255, dst)
  833. Imgproc.medianBlur(src: gray2, dst: dst, ksize: 3)
  834. try assertMatEqual(gray2, dst)
  835. // TODO_: write better test
  836. }
  837. func testMinAreaRect() {
  838. let points = [Point2f(x: 1, y: 1), Point2f(x: 5, y: 1), Point2f(x: 4, y: 3), Point2f(x: 6, y: 2)]
  839. let rrect = Imgproc.minAreaRect(points: points)
  840. XCTAssertEqual(Size2f(width: 5, height: 2), rrect.size)
  841. XCTAssertEqual(0.0, rrect.angle)
  842. XCTAssertEqual(Point2f(x: 3.5, y: 2), rrect.center)
  843. }
  844. func testMinEnclosingCircle() {
  845. let points = [Point2f(x: 0, y: 0), Point2f(x: -100, y: 0), Point2f(x: 0, y: -100), Point2f(x: 100, y: 0), Point2f(x: 0, y: 100)]
  846. let actualCenter = Point2f()
  847. var radius:Float = 0
  848. Imgproc.minEnclosingCircle(points: points, center: actualCenter, radius: &radius)
  849. XCTAssertEqual(Point2f(x: 0, y: 0), actualCenter)
  850. XCTAssertEqual(100.0, radius, accuracy: 1.0)
  851. }
  852. func testMorphologyExMatMatIntMat() throws {
  853. Imgproc.morphologyEx(src: gray255, dst: dst, op: MorphTypes.MORPH_GRADIENT, kernel: gray0)
  854. try assertMatEqual(gray0, dst)
  855. }
  856. func testMorphologyExMatMatIntMatPointInt() throws {
  857. let src = Mat.eye(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_8U)
  858. let kernel = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_8U, scalar: Scalar(0))
  859. let point = Point(x: 0, y: 0)
  860. Imgproc.morphologyEx(src: src, dst: dst, op: MorphTypes.MORPH_CLOSE, kernel: kernel, anchor: point, iterations: 10)
  861. truth = Mat.eye(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_8U)
  862. try assertMatEqual(truth!, dst)
  863. }
  864. func testMorphologyExMatMatIntMatPointIntIntScalar() throws {
  865. let src = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_8U)
  866. try src.put(row: 0, col: 0, data: [2, 1] as [Int8])
  867. try src.put(row: 1, col: 0, data: [2, 1] as [Int8])
  868. let kernel = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_8U, scalar: Scalar(1))
  869. let point = Point(x: 1, y: 1)
  870. let sc = Scalar(3, 3)
  871. Imgproc.morphologyEx(src: src, dst: dst, op: MorphTypes.MORPH_TOPHAT, kernel: kernel, anchor: point, iterations: 10, borderType: .BORDER_REFLECT, borderValue: sc)
  872. truth = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_8U)
  873. try truth!.put(row: 0, col: 0, data: [1, 0] as [Int8])
  874. try truth!.put(row: 1, col: 0, data: [1, 0] as [Int8])
  875. try assertMatEqual(truth!, dst)
  876. }
  877. func testPointPolygonTest() {
  878. let contour = [Point2f(x: 0, y: 0), Point2f(x: 1, y: 3), Point2f(x: 3, y: 4), Point2f(x: 4, y: 3), Point2f(x: 2, y: 1)]
  879. let sign1 = Imgproc.pointPolygonTest(contour: contour, pt: Point2f(x: 2, y: 2), measureDist: false)
  880. XCTAssertEqual(1.0, sign1)
  881. let sign2 = Imgproc.pointPolygonTest(contour: contour, pt: Point2f(x: 4, y: 4), measureDist: true)
  882. XCTAssertEqual(-sqrt(0.5), sign2)
  883. }
  884. func testPreCornerDetectMatMatInt() throws {
  885. let src = Mat(rows: 4, cols: 4, type: CvType.CV_32F, scalar: Scalar(1))
  886. let ksize:Int32 = 3
  887. Imgproc.preCornerDetect(src: src, dst: dst, ksize: ksize)
  888. truth = Mat(rows: 4, cols: 4, type: CvType.CV_32F, scalar: Scalar(0))
  889. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  890. }
  891. func testPreCornerDetectMatMatIntInt() throws {
  892. let src = Mat(rows: 4, cols: 4, type: CvType.CV_32F, scalar: Scalar(1))
  893. let ksize:Int32 = 3
  894. Imgproc.preCornerDetect(src: src, dst: dst, ksize: ksize, borderType: .BORDER_REFLECT)
  895. truth = Mat(rows: 4, cols: 4, type: CvType.CV_32F, scalar: Scalar(0))
  896. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  897. }
  898. func testPyrDownMatMat() throws {
  899. let src = Mat(rows: 4, cols: 4, type: CvType.CV_32F)
  900. try src.put(row: 0, col: 0, data: [2, 1, 4, 2] as [Float])
  901. try src.put(row: 1, col: 0, data: [3, 2, 6, 8] as [Float])
  902. try src.put(row: 2, col: 0, data: [4, 6, 8, 10] as [Float])
  903. try src.put(row: 3, col: 0, data: [12, 32, 6, 18] as [Float])
  904. Imgproc.pyrDown(src: src, dst: dst)
  905. truth = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32F)
  906. try truth!.put(row: 0, col: 0, data: [2.78125, 4.609375])
  907. try truth!.put(row: 1, col: 0, data: [8.546875, 8.8515625])
  908. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  909. }
  910. func testPyrDownMatMatSize() throws {
  911. let src = Mat(rows: 4, cols: 4, type: CvType.CV_32F)
  912. try src.put(row: 0, col: 0, data: [2, 1, 4, 2] as [Float])
  913. try src.put(row: 1, col: 0, data: [3, 2, 6, 8] as [Float])
  914. try src.put(row: 2, col: 0, data: [4, 6, 8, 10] as [Float])
  915. try src.put(row: 3, col: 0, data: [12, 32, 6, 18] as [Float])
  916. let dstSize = Size(width: 2, height: 2)
  917. Imgproc.pyrDown(src: src, dst: dst, dstsize: dstSize)
  918. truth = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32F)
  919. try truth!.put(row: 0, col: 0, data: [2.78125, 4.609375])
  920. try truth!.put(row: 1, col: 0, data: [8.546875, 8.8515625])
  921. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  922. }
  923. func testPyrMeanShiftFilteringMatMatDoubleDouble() throws {
  924. let src = Mat(rows: OpenCVTestCase.matSize, cols: OpenCVTestCase.matSize, type: CvType.CV_8UC3, scalar: Scalar(0))
  925. Imgproc.pyrMeanShiftFiltering(src: src, dst: dst, sp: 10, sr: 50)
  926. try assertMatEqual(src, dst)
  927. }
  928. func testPyrUpMatMat() throws {
  929. let src = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32F)
  930. try src.put(row: 0, col: 0, data: [2, 1] as [Float])
  931. try src.put(row: 1, col: 0, data: [3, 2] as [Float])
  932. Imgproc.pyrUp(src: src, dst: dst)
  933. truth = Mat(rows: 4, cols: 4, type: CvType.CV_32F)
  934. try truth!.put(row: 0, col: 0, data: [2, 1.75, 1.375, 1.25])
  935. try truth!.put(row: 1, col: 0, data: [2.25, 2, 1.625, 1.5])
  936. try truth!.put(row: 2, col: 0, data: [2.625, 2.375, 2, 1.875])
  937. try truth!.put(row: 3, col: 0, data: [2.75, 2.5, 2.125, 2])
  938. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  939. }
  940. func testRemapMatMatMatMatInt() throws {
  941. // this test does something weird
  942. let src = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32F, scalar: Scalar(2))
  943. let map1 = Mat(rows: 1, cols: 3, type: CvType.CV_32FC1)
  944. let map2 = Mat(rows: 1, cols: 3, type: CvType.CV_32FC1)
  945. try map1.put(row: 0, col: 0, data: [3, 6, 5] as [Float])
  946. try map2.put(row: 0, col: 0, data: [4, 8, 12] as [Float])
  947. Imgproc.remap(src: src, dst: dst, map1: map1, map2: map2, interpolation: InterpolationFlags.INTER_LINEAR.rawValue)
  948. truth = Mat(rows: 1, cols: 3, type: CvType.CV_32F, scalar: Scalar(0))
  949. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  950. }
  951. func testRemapMatMatMatMatIntIntScalar() throws {
  952. // this test does something weird
  953. let src = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32F, scalar: Scalar(2))
  954. let map1 = Mat(rows: 1, cols: 3, type: CvType.CV_32FC1)
  955. let map2 = Mat(rows: 1, cols: 3, type: CvType.CV_32FC1)
  956. let sc = Scalar(0)
  957. try map1.put(row: 0, col: 0, data: [3, 6, 5, 0] as [Float])
  958. try map2.put(row: 0, col: 0, data: [4, 8, 12] as [Float])
  959. truth = Mat(rows: 1, cols: 3, type: CvType.CV_32F, scalar: Scalar(2))
  960. Imgproc.remap(src: src, dst: dst, map1: map1, map2: map2, interpolation: InterpolationFlags.INTER_LINEAR.rawValue, borderMode: .BORDER_REFLECT, borderValue: sc)
  961. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  962. }
  963. func testResizeMatMatSize() throws {
  964. let src = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_8UC1, scalar: Scalar(1))
  965. let dsize = Size(width: 1, height: 1)
  966. Imgproc.resize(src: src, dst: dst, dsize: dsize, fx: 0, fy: 0, interpolation: InterpolationFlags.INTER_LINEAR_EXACT.rawValue)
  967. truth = Mat(rows: 1, cols: 1, type: CvType.CV_8UC1, scalar: Scalar(1))
  968. try assertMatEqual(truth!, dst)
  969. }
  970. func testResizeMatMatSizeDoubleDoubleInt() throws {
  971. Imgproc.resize(src: gray255, dst: dst, dsize: Size(width: 2, height: 2), fx: 0, fy: 0, interpolation: InterpolationFlags.INTER_AREA.rawValue)
  972. truth = Mat(rows: 2, cols: 2, type: CvType.CV_8UC1, scalar: Scalar(255))
  973. try assertMatEqual(truth!, dst)
  974. }
  975. func testScharrMatMatIntIntInt() throws {
  976. let src = Mat.eye(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32F)
  977. Imgproc.Scharr(src: src, dst: dst, ddepth: CvType.CV_32F, dx: 1, dy: 0)
  978. truth = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32F, scalar: Scalar(0))
  979. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  980. }
  981. func testScharrMatMatIntIntIntDoubleDouble() throws {
  982. let src = Mat.eye(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32F)
  983. Imgproc.Scharr(src: src, dst: dst, ddepth: CvType.CV_32F, dx: 1, dy: 0, scale: 1.5, delta: 0.001)
  984. truth = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32F, scalar: Scalar(0.001))
  985. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  986. }
  987. func testScharrMatMatIntIntIntDoubleDoubleInt() throws {
  988. let src = Mat.eye(rows: 3, cols: 3, type: CvType.CV_32F)
  989. Imgproc.Scharr(src: src, dst: dst, ddepth: CvType.CV_32F, dx: 1, dy: 0, scale: 1.5, delta: 0, borderType: .BORDER_REFLECT)
  990. truth = Mat(rows: 3, cols: 3, type: CvType.CV_32F)
  991. try truth!.put(row: 0, col: 0, data: [-15, -19.5, -4.5])
  992. try truth!.put(row: 1, col: 0, data: [10.5, 0, -10.5])
  993. try truth!.put(row: 2, col: 0, data: [4.5, 19.5, 15])
  994. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  995. }
  996. func testSepFilter2DMatMatIntMatMat() throws {
  997. let src = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32F, scalar: Scalar(2))
  998. let kernelX = Mat(rows: 1, cols: 3, type: CvType.CV_32FC1)
  999. let kernelY = Mat(rows: 1, cols: 3, type: CvType.CV_32FC1)
  1000. try kernelX.put(row: 0, col: 0, data: [4, 3, 7] as [Float])
  1001. try kernelY.put(row: 0, col: 0, data: [9, 4, 2] as [Float])
  1002. Imgproc.sepFilter2D(src: src, dst: dst, ddepth: CvType.CV_32F, kernelX: kernelX, kernelY: kernelY)
  1003. truth = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32F, scalar: Scalar(420))
  1004. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  1005. }
  1006. func testSepFilter2DMatMatIntMatMatPointDouble() throws {
  1007. let src = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32FC1, scalar: Scalar(2))
  1008. let kernelX = Mat(rows: 1, cols: 3, type: CvType.CV_32FC1)
  1009. try kernelX.put(row: 0, col: 0, data: [2, 2, 2] as [Float])
  1010. let kernelY = Mat(rows: 1, cols: 3, type: CvType.CV_32FC1)
  1011. try kernelY.put(row: 0, col: 0, data: [1, 1, 1] as [Float])
  1012. Imgproc.sepFilter2D(src: src, dst: dst, ddepth: CvType.CV_32F, kernelX: kernelX, kernelY: kernelY, anchor: anchorPoint, delta: OpenCVTestCase.weakEPS)
  1013. truth = Mat(rows: imgprocSz, cols: imgprocSz, type: CvType.CV_32F, scalar: Scalar(36 + OpenCVTestCase.weakEPS))
  1014. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  1015. }
  1016. func testSepFilter2DMatMatIntMatMatPointDoubleInt() throws {
  1017. let kernelX = Mat(rows: 1, cols: 3, type: CvType.CV_32FC1)
  1018. try kernelX.put(row: 0, col: 0, data: [2, 2, 2] as [Float])
  1019. let kernelY = Mat(rows: 1, cols: 3, type: CvType.CV_32FC1)
  1020. try kernelY.put(row: 0, col: 0, data: [1, 1, 1] as [Float])
  1021. Imgproc.sepFilter2D(src: gray0, dst: dst, ddepth: CvType.CV_32F, kernelX: kernelX, kernelY: kernelY, anchor: anchorPoint, delta: OpenCVTestCase.weakEPS, borderType: .BORDER_REFLECT)
  1022. truth = Mat(rows: 10, cols: 10, type: CvType.CV_32F, scalar: Scalar(OpenCVTestCase.weakEPS))
  1023. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  1024. }
  1025. func testSobelMatMatIntIntInt() throws {
  1026. Imgproc.Sobel(src: gray255, dst: dst, ddepth: CvType.CV_8U, dx: 1, dy: 0)
  1027. try assertMatEqual(gray0, dst)
  1028. }
  1029. func testSobelMatMatIntIntIntIntDoubleDouble() throws {
  1030. Imgproc.Sobel(src: gray255, dst: dst, ddepth: CvType.CV_8U, dx: 1, dy: 0, ksize: 3, scale: 2, delta: 0.001)
  1031. try assertMatEqual(gray0, dst)
  1032. }
  1033. func testSobelMatMatIntIntIntIntDoubleDoubleInt() throws {
  1034. let src = Mat(rows: 3, cols: 3, type: CvType.CV_32F)
  1035. try src.put(row: 0, col: 0, data: [2, 0, 1] as [Float])
  1036. try src.put(row: 1, col: 0, data: [6, 4, 3] as [Float])
  1037. try src.put(row: 2, col: 0, data: [1, 0, 2] as [Float])
  1038. Imgproc.Sobel(src: src, dst: dst, ddepth: CvType.CV_32F, dx: 1, dy: 0, ksize: 3, scale: 2, delta: 0, borderType: .BORDER_REPLICATE)
  1039. truth = Mat(rows: 3, cols: 3, type: CvType.CV_32F)
  1040. try truth!.put(row: 0, col: 0, data: [-16, -12, 4] as [Float])
  1041. try truth!.put(row: 1, col: 0, data: [-14, -12, 2] as [Float])
  1042. try truth!.put(row: 2, col: 0, data: [-10, 0, 10] as [Float])
  1043. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  1044. }
  1045. func testThreshold() throws {
  1046. Imgproc.threshold(src: makeMask(gray0.clone(), vals: [10]), dst: dst, thresh: 5, maxval: 255, type: .THRESH_TRUNC)
  1047. try assertMatEqual(makeMask(gray0.clone(), vals: [5]), dst)
  1048. Imgproc.threshold(src: makeMask(gray2.clone(), vals: [10]), dst: dst, thresh: 1, maxval: 255, type: .THRESH_BINARY)
  1049. try assertMatEqual(gray255, dst)
  1050. Imgproc.threshold(src: makeMask(gray2.clone(), vals: [10]), dst: dst, thresh: 3, maxval: 255, type: .THRESH_BINARY_INV)
  1051. try assertMatEqual(makeMask(gray255.clone(), vals: [0]), dst)
  1052. }
  1053. func testWarpAffineMatMatMatSize() throws {
  1054. let src = Mat(rows: 3, cols: 3, type: CvType.CV_32F)
  1055. try src.put(row: 0, col: 0, data: [2, 0, 1] as [Float])
  1056. try src.put(row: 1, col: 0, data: [6, 4, 3] as [Float])
  1057. try src.put(row: 2, col: 0, data: [1, 0, 2] as [Float])
  1058. let M = Mat(rows: 2, cols: 3, type: CvType.CV_32F)
  1059. try M.put(row: 0, col: 0, data: [1, 0, 1] as [Float])
  1060. try M.put(row: 1, col: 0, data: [0, 1, 1] as [Float])
  1061. Imgproc.warpAffine(src: src, dst: dst, M: M, dsize: Size(width: 3, height: 3))
  1062. truth = Mat(rows: 3, cols: 3, type: CvType.CV_32F)
  1063. try truth!.put(row: 0, col: 0, data: [0, 0, 0] as [Float])
  1064. try truth!.put(row: 1, col: 0, data: [0, 2, 0] as [Float])
  1065. try truth!.put(row: 2, col: 0, data: [0, 6, 4] as [Float])
  1066. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  1067. }
  1068. func testWarpAffineMatMatMatSizeInt() throws {
  1069. let src = Mat(rows: 3, cols: 3, type: CvType.CV_32F)
  1070. try src.put(row: 0, col: 0, data: [2, 4, 1] as [Float])
  1071. try src.put(row: 1, col: 0, data: [6, 4, 3] as [Float])
  1072. try src.put(row: 2, col: 0, data: [0, 2, 2] as [Float])
  1073. let M = Mat(rows: 2, cols: 3, type: CvType.CV_32F)
  1074. try M.put(row: 0, col: 0, data: [1, 0, 0] as [Float])
  1075. try M.put(row: 1, col: 0, data: [0, 0, 1] as [Float])
  1076. Imgproc.warpAffine(src: src, dst: dst, M: M, dsize: Size(width: 2, height: 2), flags: InterpolationFlags.WARP_INVERSE_MAP.rawValue)
  1077. truth = Mat(rows: 2, cols: 2, type: CvType.CV_32F)
  1078. try truth!.put(row: 0, col: 0, data: [6, 4] as [Float])
  1079. try truth!.put(row: 1, col: 0, data: [6, 4] as [Float])
  1080. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  1081. }
  1082. func testWarpPerspectiveMatMatMatSize() throws {
  1083. let src = Mat(rows: 3, cols: 3, type: CvType.CV_32F)
  1084. try src.put(row: 0, col: 0, data: [2, 4, 1] as [Float])
  1085. try src.put(row: 1, col: 0, data: [0, 4, 5] as [Float])
  1086. try src.put(row: 2, col: 0, data: [1, 2, 2] as [Float])
  1087. let M = Mat(rows: 3, cols: 3, type: CvType.CV_32F)
  1088. try M.put(row: 0, col: 0, data: [1, 0, 1] as [Float])
  1089. try M.put(row: 1, col: 0, data: [0, 1, 1] as [Float])
  1090. try M.put(row: 2, col: 0, data: [0, 0, 1] as [Float])
  1091. Imgproc.warpPerspective(src: src, dst: dst, M: M, dsize: Size(width: 3, height: 3))
  1092. truth = Mat(rows: 3, cols: 3, type: CvType.CV_32F)
  1093. try truth!.put(row: 0, col: 0, data: [0, 0, 0] as [Float])
  1094. try truth!.put(row: 1, col: 0, data: [0, 2, 4] as [Float])
  1095. try truth!.put(row: 2, col: 0, data: [0, 0, 4] as [Float])
  1096. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  1097. }
  1098. func testWatershed() throws {
  1099. let image = Mat.eye(rows: 4, cols: 4, type: CvType.CV_8UC(3))
  1100. let markers = Mat(rows: 4, cols: 4, type: CvType.CV_32SC1, scalar: Scalar(0))
  1101. Imgproc.watershed(image: image, markers: markers)
  1102. truth = Mat(rows: 4, cols: 4, type: CvType.CV_32SC1)
  1103. try truth!.put(row: 0, col: 0, data: [-1, -1, -1, -1] as [Int32])
  1104. try truth!.put(row: 1, col: 0, data: [-1, 0, 0, -1] as [Int32])
  1105. try truth!.put(row: 2, col: 0, data: [-1, 0, 0, -1] as [Int32])
  1106. try truth!.put(row: 3, col: 0, data: [-1, -1, -1, -1] as [Int32])
  1107. try assertMatEqual(truth!, markers)
  1108. }
  1109. func testGetTextSize() {
  1110. let text = "iOS all the way"
  1111. let fontScale:Int32 = 2
  1112. let thickness:Int32 = 3
  1113. var baseLine:Int32 = 0
  1114. Imgproc.getTextSize(text: text, fontFace: .FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale: Double(fontScale), thickness: thickness, baseLine: &baseLine)
  1115. let res = Imgproc.getTextSize(text: text, fontFace: .FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale: Double(fontScale), thickness: thickness, baseLine: &baseLine)
  1116. XCTAssertEqual(431, res.width)
  1117. XCTAssertEqual(44, res.height)
  1118. XCTAssertEqual(20, baseLine)
  1119. }
  1120. func testCircleMatPointIntScalar() {
  1121. let gray0clone = gray0.clone()
  1122. let center = Point(x: gray0clone.cols() / 2, y: gray0clone.rows() / 2)
  1123. let radius = min(gray0clone.cols() / 4, gray0clone.rows() / 4)
  1124. let color = Scalar(128)
  1125. Imgproc.circle(img: gray0clone, center: center, radius: radius, color: color)
  1126. XCTAssert(0 != Core.countNonZero(src: gray0clone))
  1127. }
  1128. func testCircleMatPointIntScalarInt() {
  1129. let gray0clone = gray0.clone()
  1130. let center = Point(x: gray0clone.cols() / 2, y: gray0clone.rows() / 2)
  1131. let radius = min(gray0clone.cols() / 4, gray0clone.rows() / 4)
  1132. let color = Scalar(128)
  1133. Imgproc.circle(img: gray0clone, center: center, radius: radius, color: color, thickness: Core.FILLED)
  1134. XCTAssert(0 != Core.countNonZero(src: gray0clone))
  1135. }
  1136. func testCircleMatPointIntScalarIntIntInt() {
  1137. let gray0clone = gray0.clone()
  1138. let center = Point(x: gray0clone.cols() / 2, y: gray0clone.rows() / 2)
  1139. let center2 = Point(x: gray0clone.cols(), y: gray0clone.rows())
  1140. let radius = min(gray0clone.cols() / 4, gray0clone.rows() / 4)
  1141. let color128 = Scalar(128)
  1142. let color0 = Scalar(0)
  1143. Imgproc.circle(img: gray0clone, center: center2, radius: radius * 2, color: color128, thickness: 2, lineType: .LINE_4, shift: 1)
  1144. XCTAssertFalse(0 == Core.countNonZero(src: gray0clone))
  1145. Imgproc.circle(img: gray0clone, center: center, radius: radius, color: color0, thickness: 2, lineType: .LINE_4, shift: 0)
  1146. XCTAssert(0 == Core.countNonZero(src: gray0clone))
  1147. }
  1148. func testClipLine() {
  1149. let r = Rect(x: 10, y: 10, width: 10, height: 10)
  1150. var pt1 = Point(x: 5, y: 15)
  1151. var pt2 = Point(x: 25, y: 15)
  1152. XCTAssert(Imgproc.clipLine(imgRect: r, pt1: pt1, pt2: pt2))
  1153. var pt1Clipped = Point(x: 10, y: 15)
  1154. var pt2Clipped = Point(x: 19, y: 15)
  1155. XCTAssertEqual(pt1Clipped, pt1)
  1156. XCTAssertEqual(pt2Clipped, pt2)
  1157. pt1 = Point(x: 5, y: 5)
  1158. pt2 = Point(x: 25, y: 5)
  1159. pt1Clipped = Point(x: 5, y: 5)
  1160. pt2Clipped = Point(x: 25, y: 5)
  1161. XCTAssertFalse(Imgproc.clipLine(imgRect: r, pt1: pt1, pt2: pt2))
  1162. XCTAssertEqual(pt1Clipped, pt1)
  1163. XCTAssertEqual(pt2Clipped, pt2)
  1164. }
  1165. func testEllipse2Poly() {
  1166. let center = Point(x: 4, y: 4)
  1167. let axes = Size(width: 2, height: 2)
  1168. let angle:Int32 = 30
  1169. let arcStart:Int32 = 30
  1170. let arcEnd:Int32 = 60
  1171. let delta:Int32 = 2
  1172. var pts = [Point]()
  1173. Imgproc.ellipse2Poly(center: center, axes: axes, angle: angle, arcStart: arcStart, arcEnd: arcEnd, delta: delta, pts: &pts)
  1174. let truth = [Point(x: 5, y: 6), Point(x: 4, y: 6)]
  1175. XCTAssert(truth == pts)
  1176. }
  1177. func testEllipseMatPointSizeDoubleDoubleDoubleScalar() {
  1178. let gray0clone = gray0.clone()
  1179. let center = Point(x: gray0clone.cols() / 2, y: gray0clone.rows() / 2)
  1180. let axes = Size(width: 2, height: 2)
  1181. let angle = 30.0, startAngle = 60.0, endAngle = 90.0
  1182. Imgproc.ellipse(img: gray0clone, center: center, axes: axes, angle: angle, startAngle: startAngle, endAngle: endAngle, color: colorWhite)
  1183. XCTAssert(0 != Core.countNonZero(src: gray0clone))
  1184. }
  1185. func testEllipseMatPointSizeDoubleDoubleDoubleScalarInt() {
  1186. let gray0clone = gray0.clone()
  1187. let center = Point(x: gray0clone.cols() / 2, y: gray0clone.rows() / 2)
  1188. let axes = Size(width: 2, height: 2)
  1189. let angle = 30.0, startAngle = 60.0, endAngle = 90.0
  1190. Imgproc.ellipse(img: gray0clone, center: center, axes: axes, angle: angle, startAngle: startAngle, endAngle: endAngle, color: colorWhite, thickness: Core.FILLED)
  1191. XCTAssert(0 != Core.countNonZero(src: gray0clone))
  1192. }
  1193. func testEllipseMatPointSizeDoubleDoubleDoubleScalarIntIntInt() {
  1194. let gray0clone = gray0.clone()
  1195. let center = Point(x: gray0clone.cols() / 2, y: gray0clone.rows() / 2)
  1196. let axes = Size(width: 2, height: 2)
  1197. let center2 = Point(x: gray0clone.cols(), y: gray0clone.rows())
  1198. let axes2 = Size(width: 4, height: 4)
  1199. let angle = 30.0, startAngle = 0.0, endAngle = 30.0
  1200. Imgproc.ellipse(img: gray0clone, center: center, axes: axes, angle: angle, startAngle: startAngle, endAngle: endAngle, color: colorWhite, thickness: Core.FILLED, lineType: .LINE_4, shift: 0)
  1201. XCTAssert(0 != Core.countNonZero(src: gray0clone))
  1202. Imgproc.ellipse(img: gray0clone, center: center2, axes: axes2, angle: angle, startAngle: startAngle, endAngle: endAngle, color: colorBlack, thickness: Core.FILLED, lineType: .LINE_4, shift: 1)
  1203. XCTAssertEqual(0, Core.countNonZero(src: gray0clone))
  1204. }
  1205. func testEllipseMatRotatedRectScalar() throws {
  1206. let matSize:Int32 = 10
  1207. let gray0 = Mat.zeros(matSize, cols: matSize, type: CvType.CV_8U)
  1208. let center = Point2f(x: Float(matSize / 2), y: Float(matSize / 2))
  1209. let size = Size2f(width: Float(matSize / 4), height: Float(matSize / 2))
  1210. let box = RotatedRect(center: center, size: size, angle: 45)
  1211. Imgproc.ellipse(img: gray0, box: box, color: Scalar(1))
  1212. let truth = Mat(rows: matSize, cols: matSize, type: CvType.CV_8U)
  1213. try truth.put(row: 0, col: 0, data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1214. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1215. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1216. 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
  1217. 0, 0, 0, 0, 1, 1, 0, 1, 0, 0,
  1218. 0, 0, 0, 0, 1, 0, 1, 0, 0, 0,
  1219. 0, 0, 0, 1, 0, 1, 1, 0, 0, 0,
  1220. 0, 0, 0, 1, 1, 0, 0, 0, 0, 0,
  1221. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1222. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] as [Int8])
  1223. try assertMatEqual(truth, gray0)
  1224. }
  1225. func testEllipseMatRotatedRectScalarInt() {
  1226. let gray0clone = gray0.clone()
  1227. let center = Point2f(x: Float(OpenCVTestCase.matSize / 2), y: Float(OpenCVTestCase.matSize / 2))
  1228. let size = Size2f(width: Float(OpenCVTestCase.matSize / 4), height: Float(OpenCVTestCase.matSize / 2))
  1229. let box = RotatedRect(center: center, size: size, angle: 45)
  1230. Imgproc.ellipse(img: gray0clone, box: box, color: Scalar(1), thickness: Core.FILLED)
  1231. Imgproc.ellipse(img: gray0clone, box: box, color: Scalar(0))
  1232. XCTAssert(0 < Core.countNonZero(src: gray0clone))
  1233. }
  1234. func testEllipseMatRotatedRectScalarIntInt() {
  1235. let gray0clone = gray0.clone()
  1236. let center = Point2f(x: Float(OpenCVTestCase.matSize / 2), y: Float(OpenCVTestCase.matSize / 2))
  1237. let size = Size2f(width: 2, height: Float(OpenCVTestCase.matSize * 2 / 3))
  1238. let box = RotatedRect(center: center, size: size, angle: 20)
  1239. Imgproc.ellipse(img: gray0clone, box: box, color: Scalar(9), thickness: 1, lineType: .LINE_AA)
  1240. Imgproc.ellipse(img: gray0clone, box: box, color: Scalar(0), thickness: 1, lineType: .LINE_4)
  1241. XCTAssert(0 < Core.countNonZero(src: gray0clone))
  1242. }
  1243. func testPolylinesMatListOfListOfPointBooleanScalar() {
  1244. let img = gray0.clone()
  1245. let polyline = [[Point(x: 1, y: 1), Point(x: 7, y: 1), Point(x: 7, y: 6), Point(x: 1, y: 6)]]
  1246. Imgproc.polylines(img: img, pts: polyline, isClosed: true, color: Scalar(100))
  1247. XCTAssertEqual(22, Core.countNonZero(src: img))
  1248. Imgproc.polylines(img: img, pts: polyline, isClosed: false, color: Scalar(0))
  1249. XCTAssertEqual(4, Core.countNonZero(src: img))
  1250. }
  1251. func testPolylinesMatListOfListOfPointBooleanScalarInt() {
  1252. let img = gray0.clone()
  1253. let polyline = [[Point(x: 1, y: 1), Point(x: 7, y: 1), Point(x: 7, y: 6), Point(x: 1, y: 6)]]
  1254. Imgproc.polylines(img: img, pts: polyline, isClosed: true, color: Scalar(100), thickness: 2)
  1255. XCTAssertEqual(62, Core.countNonZero(src: img))
  1256. }
  1257. func testPolylinesMatListOfListOfPointBooleanScalarIntIntInt() {
  1258. let img = gray0.clone()
  1259. let polyline1 = [[Point(x: 1, y: 1), Point(x: 7, y: 1), Point(x: 7, y: 6), Point(x: 1, y: 6)]]
  1260. let polyline2 = [[Point(x: 2, y: 2), Point(x: 14, y: 2), Point(x: 14, y: 12), Point(x: 2, y: 12)]]
  1261. Imgproc.polylines(img: img, pts: polyline1, isClosed: true, color: Scalar(100), thickness: 2, lineType: .LINE_8, shift: 0)
  1262. XCTAssert(Core.countNonZero(src: img) > 0)
  1263. Imgproc.polylines(img: img, pts: polyline2, isClosed: true, color: Scalar(0), thickness: 2, lineType: .LINE_8, shift: 1)
  1264. XCTAssertEqual(0, Core.countNonZero(src: img))
  1265. }
  1266. func testPutTextMatStringPointIntDoubleScalar() {
  1267. let text = "Hello World"
  1268. let labelSize = Size(width: 175, height: 22)
  1269. let img = Mat(rows: 20 + labelSize.height, cols: 20 + labelSize.width, type: CvType.CV_8U, scalar: colorBlack)
  1270. let origin = Point(x: 10, y: labelSize.height + 10)
  1271. Imgproc.putText(img: img, text: text, org: origin, fontFace: .FONT_HERSHEY_SIMPLEX, fontScale: 1.0, color: colorWhite)
  1272. XCTAssert(Core.countNonZero(src: img) > 0)
  1273. // check that border is not corrupted
  1274. Imgproc.rectangle(img: img, pt1: Point(x: 11, y: 11), pt2: Point(x: labelSize.width + 10, y: labelSize.height + 10), color: colorBlack, thickness: Core.FILLED)
  1275. XCTAssertEqual(0, Core.countNonZero(src: img))
  1276. }
  1277. func testPutTextMatStringPointIntDoubleScalarInt() {
  1278. let text = "Hello World"
  1279. let labelSize = Size(width: 176, height: 22)
  1280. let img = Mat(rows: 20 + labelSize.height, cols: 20 + labelSize.width, type: CvType.CV_8U, scalar: colorBlack)
  1281. let origin = Point(x: 10, y: labelSize.height + 10)
  1282. Imgproc.putText(img: img, text: text, org: origin, fontFace: .FONT_HERSHEY_SIMPLEX, fontScale: 1.0, color: colorWhite, thickness: 2)
  1283. XCTAssert(Core.countNonZero(src: img) > 0)
  1284. // check that border is not corrupted
  1285. Imgproc.rectangle(img: img, pt1: Point(x: 10, y: 10), pt2: Point(x: labelSize.width + 10 + 1, y: labelSize.height + 10 + 1), color: colorBlack, thickness: Core.FILLED)
  1286. XCTAssertEqual(0, Core.countNonZero(src: img))
  1287. }
  1288. func testPutTextMatStringPointIntDoubleScalarIntIntBoolean() {
  1289. let text = "Hello World"
  1290. let labelSize = Size(width: 175, height: 22)
  1291. let img = Mat(rows: 20 + labelSize.height, cols: 20 + labelSize.width, type: CvType.CV_8U, scalar: colorBlack)
  1292. let origin = Point(x: 10, y: 10)
  1293. Imgproc.putText(img: img, text: text, org: origin, fontFace: .FONT_HERSHEY_SIMPLEX, fontScale: 1.0, color: colorWhite, thickness: 1, lineType: .LINE_8, bottomLeftOrigin: true)
  1294. XCTAssert(Core.countNonZero(src: img) > 0)
  1295. // check that border is not corrupted
  1296. Imgproc.rectangle(img: img, pt1: Point(x: 10, y: 10), pt2: Point(x: labelSize.width + 9, y: labelSize.height + 9), color: colorBlack, thickness: Core.FILLED)
  1297. XCTAssertEqual(0, Core.countNonZero(src: img))
  1298. }
  1299. }