Calib3dTest.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. //
  2. // Calib3dTest.swift
  3. //
  4. // Created by Giles Payne on 2020/05/26.
  5. //
  6. import XCTest
  7. import OpenCV
  8. class Calib3dTest: OpenCVTestCase {
  9. var size = Size()
  10. override func setUp() {
  11. super.setUp()
  12. size = Size(width: 3, height: 3)
  13. }
  14. override func tearDown() {
  15. super.tearDown()
  16. }
  17. func testComposeRTMatMatMatMatMatMat() throws {
  18. let rvec1 = Mat(rows: 3, cols: 1, type: CvType.CV_32F)
  19. try rvec1.put(row: 0, col: 0, data: [0.5302828, 0.19925919, 0.40105945] as [Float])
  20. let tvec1 = Mat(rows: 3, cols: 1, type: CvType.CV_32F)
  21. try tvec1.put(row: 0, col: 0, data: [0.81438506, 0.43713298, 0.2487897] as [Float])
  22. let rvec2 = Mat(rows: 3, cols: 1, type: CvType.CV_32F)
  23. try rvec2.put(row: 0, col: 0, data: [0.77310503, 0.76209372, 0.30779448] as [Float])
  24. let tvec2 = Mat(rows: 3, cols: 1, type: CvType.CV_32F)
  25. try tvec2.put(row: 0, col: 0, data: [0.70243168, 0.4784472, 0.79219002] as [Float])
  26. let rvec3 = Mat()
  27. let tvec3 = Mat()
  28. let outRvec = Mat(rows: 3, cols: 1, type: CvType.CV_32F)
  29. try outRvec.put(row: 0, col: 0, data: [1.418641, 0.88665926, 0.56020796])
  30. let outTvec = Mat(rows: 3, cols: 1, type: CvType.CV_32F)
  31. try outTvec.put(row: 0, col: 0, data: [1.4560841, 1.0680628, 0.81598103])
  32. Calib3d.composeRT(rvec1: rvec1, tvec1: tvec1, rvec2: rvec2, tvec2: tvec2, rvec3: rvec3, tvec3: tvec3)
  33. try assertMatEqual(outRvec, rvec3, OpenCVTestCase.EPS)
  34. try assertMatEqual(outTvec, tvec3, OpenCVTestCase.EPS)
  35. }
  36. func testFilterSpecklesMatDoubleIntDouble() throws {
  37. gray_16s_1024.copy(to: dst)
  38. let center = Point(x: gray_16s_1024.rows() / 2, y: gray_16s_1024.cols() / 2)
  39. Imgproc.circle(img: dst, center: center, radius: 1, color: Scalar.all(4096))
  40. try assertMatNotEqual(gray_16s_1024, dst)
  41. Calib3d.filterSpeckles(img: dst, newVal: 1024.0, maxSpeckleSize: 100, maxDiff: 0.0)
  42. try assertMatEqual(gray_16s_1024, dst)
  43. }
  44. func testFindChessboardCornersMatSizeMat() {
  45. let patternSize = Size(width: 9, height: 6)
  46. let corners = MatOfPoint2f()
  47. Calib3d.findChessboardCorners(image: grayChess, patternSize: patternSize, corners: corners)
  48. XCTAssertFalse(corners.empty())
  49. }
  50. func testFindChessboardCornersMatSizeMatInt() {
  51. let patternSize = Size(width: 9, height: 6)
  52. let corners = MatOfPoint2f()
  53. Calib3d.findChessboardCorners(image: grayChess, patternSize: patternSize, corners: corners, flags: Calib3d.CALIB_CB_ADAPTIVE_THRESH + Calib3d.CALIB_CB_NORMALIZE_IMAGE + Calib3d.CALIB_CB_FAST_CHECK)
  54. XCTAssertFalse(corners.empty())
  55. }
  56. func testFind4QuadCornerSubpix() {
  57. let patternSize = Size(width: 9, height: 6)
  58. let corners = MatOfPoint2f()
  59. let region_size = Size(width: 5, height: 5)
  60. Calib3d.findChessboardCorners(image: grayChess, patternSize: patternSize, corners: corners)
  61. Calib3d.find4QuadCornerSubpix(img: grayChess, corners: corners, region_size: region_size)
  62. XCTAssertFalse(corners.empty())
  63. }
  64. func testFindCirclesGridMatSizeMat() {
  65. let size = 300
  66. let img = Mat(rows:Int32(size), cols:Int32(size), type:CvType.CV_8U)
  67. img.setTo(scalar: Scalar(255))
  68. let centers = Mat()
  69. XCTAssertFalse(Calib3d.findCirclesGrid(image: img, patternSize: Size(width: 5, height: 5), centers: centers))
  70. for i in 0..<5 {
  71. for j in 0..<5 {
  72. let x = Int32(size * (2 * i + 1) / 10)
  73. let y = Int32(size * (2 * j + 1) / 10)
  74. let pt = Point(x: x, y: y)
  75. Imgproc.circle(img: img, center: pt, radius: 10, color: Scalar(0), thickness: -1)
  76. }
  77. }
  78. XCTAssert(Calib3d.findCirclesGrid(image: img, patternSize:Size(width:5, height:5), centers:centers))
  79. XCTAssertEqual(25, centers.rows())
  80. XCTAssertEqual(1, centers.cols())
  81. XCTAssertEqual(CvType.CV_32FC2, centers.type())
  82. }
  83. func testFindCirclesGridMatSizeMatInt() {
  84. let size:Int32 = 300
  85. let img = Mat(rows:size, cols: size, type: CvType.CV_8U)
  86. img.setTo(scalar: Scalar(255))
  87. let centers = Mat()
  88. XCTAssertFalse(Calib3d.findCirclesGrid(image: img, patternSize: Size(width: 3, height: 5), centers: centers, flags: Calib3d.CALIB_CB_CLUSTERING | Calib3d.CALIB_CB_ASYMMETRIC_GRID))
  89. let step = size * 2 / 15
  90. let offsetx = size / 6
  91. let offsety = (size - 4 * step) / 2
  92. for i:Int32 in 0...2 {
  93. for j:Int32 in 0...4 {
  94. let pt = Point(x: offsetx + (2 * i + j % 2) * step, y: offsety + step * j)
  95. Imgproc.circle(img: img, center: pt, radius: 10, color: Scalar(0), thickness: -1)
  96. }
  97. }
  98. XCTAssert(Calib3d.findCirclesGrid(image: img, patternSize: Size(width: 3, height: 5), centers: centers, flags: Calib3d.CALIB_CB_CLUSTERING | Calib3d.CALIB_CB_ASYMMETRIC_GRID))
  99. XCTAssertEqual(15, centers.rows())
  100. XCTAssertEqual(1, centers.cols())
  101. XCTAssertEqual(CvType.CV_32FC2, centers.type())
  102. }
  103. func testFindHomographyListOfPointListOfPoint() throws {
  104. let NUM:Int32 = 20
  105. let originalPoints = MatOfPoint2f()
  106. originalPoints.alloc(NUM)
  107. let transformedPoints = MatOfPoint2f()
  108. transformedPoints.alloc(NUM)
  109. for i:Int32 in 0..<NUM {
  110. let x:Float = Float.random(in: -50...50)
  111. let y:Float = Float.random(in: -50...50)
  112. try originalPoints.put(row:i, col:0, data:[x, y])
  113. try transformedPoints.put(row:i, col:0, data:[y, x])
  114. }
  115. let hmg = Calib3d.findHomography(srcPoints: originalPoints, dstPoints: transformedPoints)
  116. truth = Mat(rows: 3, cols: 3, type: CvType.CV_64F)
  117. try truth!.put(row:0, col:0, data:[0, 1, 0, 1, 0, 0, 0, 0, 1] as [Double])
  118. try assertMatEqual(truth!, hmg, OpenCVTestCase.EPS)
  119. }
  120. func testReprojectImageTo3DMatMatMat() throws {
  121. let transformMatrix = Mat(rows: 4, cols: 4, type: CvType.CV_64F)
  122. try transformMatrix.put(row:0, col:0, data:[0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1] as [Double])
  123. let disparity = Mat(rows: OpenCVTestCase.matSize, cols: OpenCVTestCase.matSize, type: CvType.CV_32F)
  124. var disp = [Float].init(repeating: 0.0, count: Int(OpenCVTestCase.matSize * OpenCVTestCase.matSize))
  125. for i in 0..<Int(OpenCVTestCase.matSize) {
  126. for j in 0..<Int(OpenCVTestCase.matSize) {
  127. disp[i * Int(OpenCVTestCase.matSize) + j] = Float(i - j)
  128. }
  129. }
  130. try disparity.put(row:0, col:0, data:disp)
  131. let _3dPoints = Mat()
  132. Calib3d.reprojectImageTo3D(disparity: disparity, _3dImage: _3dPoints, Q: transformMatrix)
  133. XCTAssertEqual(CvType.CV_32FC3, _3dPoints.type())
  134. XCTAssertEqual(OpenCVTestCase.matSize, _3dPoints.rows())
  135. XCTAssertEqual(OpenCVTestCase.matSize, _3dPoints.cols())
  136. truth = Mat(rows: OpenCVTestCase.matSize, cols: OpenCVTestCase.matSize, type: CvType.CV_32FC3)
  137. var _truth = [Float](repeating: 0.0, count: Int(OpenCVTestCase.matSize * OpenCVTestCase.matSize * 3))
  138. for i:Int in 0..<Int(OpenCVTestCase.matSize) {
  139. for j:Int in 0..<Int(OpenCVTestCase.matSize) {
  140. let start:Int = (i * Int(OpenCVTestCase.matSize) + j) * 3
  141. _truth[start + 0] = Float(i)
  142. _truth[start + 1] = Float(j)
  143. _truth[start + 2] = Float(i - j)
  144. }
  145. }
  146. try truth!.put(row: 0, col: 0, data: _truth)
  147. try assertMatEqual(truth!, _3dPoints, OpenCVTestCase.EPS)
  148. }
  149. func testReprojectImageTo3DMatMatMatBoolean() throws {
  150. let transformMatrix = Mat(rows: 4, cols: 4, type: CvType.CV_64F)
  151. try transformMatrix.put(row: 0, col: 0, data: [0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1] as [Double])
  152. let disparity = Mat(rows: OpenCVTestCase.matSize, cols: OpenCVTestCase.matSize, type: CvType.CV_32F)
  153. var disp = [Float](repeating: 0.0, count: Int(OpenCVTestCase.matSize * OpenCVTestCase.matSize))
  154. for i in 0..<Int(OpenCVTestCase.matSize) {
  155. for j in 0..<Int(OpenCVTestCase.matSize) {
  156. disp[i * Int(OpenCVTestCase.matSize) + j] = Float(i - j)
  157. }
  158. }
  159. disp[0] = -.greatestFiniteMagnitude
  160. try disparity.put(row: 0, col: 0, data: disp)
  161. let _3dPoints = Mat()
  162. Calib3d.reprojectImageTo3D(disparity: disparity, _3dImage: _3dPoints, Q: transformMatrix, handleMissingValues: true)
  163. XCTAssertEqual(CvType.CV_32FC3, _3dPoints.type())
  164. XCTAssertEqual(OpenCVTestCase.matSize, _3dPoints.rows())
  165. XCTAssertEqual(OpenCVTestCase.matSize, _3dPoints.cols())
  166. truth = Mat(rows: OpenCVTestCase.matSize, cols: OpenCVTestCase.matSize, type: CvType.CV_32FC3)
  167. var _truth = [Float](repeating: 0.0, count:Int(OpenCVTestCase.matSize * OpenCVTestCase.matSize * 3))
  168. for i in 0..<Int(OpenCVTestCase.matSize) {
  169. for j in 0..<Int(OpenCVTestCase.matSize) {
  170. _truth[(i * Int(OpenCVTestCase.matSize) + j) * 3 + 0] = Float(i)
  171. _truth[(i * Int(OpenCVTestCase.matSize) + j) * 3 + 1] = Float(j)
  172. _truth[(i * Int(OpenCVTestCase.matSize) + j) * 3 + 2] = Float(i - j)
  173. }
  174. }
  175. _truth[2] = 10000
  176. try truth!.put(row: 0, col: 0, data: _truth)
  177. try assertMatEqual(truth!, _3dPoints, OpenCVTestCase.EPS)
  178. }
  179. func testReprojectImageTo3DMatMatMatBooleanInt() throws {
  180. let transformMatrix = Mat(rows: 4, cols: 4, type: CvType.CV_64F)
  181. try transformMatrix.put(row: 0, col: 0, data: [0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1] as [Double])
  182. let disparity = Mat(rows: OpenCVTestCase.matSize, cols: OpenCVTestCase.matSize, type: CvType.CV_32F)
  183. var disp = [Float](repeating: 0.0, count: Int(OpenCVTestCase.matSize * OpenCVTestCase.matSize))
  184. for i in 0..<Int(OpenCVTestCase.matSize) {
  185. for j in 0..<Int(OpenCVTestCase.matSize) {
  186. disp[i * Int(OpenCVTestCase.matSize) + j] = Float(i - j)
  187. }
  188. }
  189. try disparity.put(row:0, col:0, data:disp)
  190. let _3dPoints = Mat()
  191. Calib3d.reprojectImageTo3D(disparity: disparity, _3dImage: _3dPoints, Q: transformMatrix, handleMissingValues: false, ddepth: CvType.CV_16S)
  192. XCTAssertEqual(CvType.CV_16SC3, _3dPoints.type())
  193. XCTAssertEqual(OpenCVTestCase.matSize, _3dPoints.rows())
  194. XCTAssertEqual(OpenCVTestCase.matSize, _3dPoints.cols())
  195. truth = Mat(rows: OpenCVTestCase.matSize, cols: OpenCVTestCase.matSize, type: CvType.CV_16SC3)
  196. var _truth = [Int16](repeating: 0, count: Int(OpenCVTestCase.matSize * OpenCVTestCase.matSize * 3))
  197. for i in 0..<Int(OpenCVTestCase.matSize) {
  198. for j in 0..<Int(OpenCVTestCase.matSize) {
  199. let start = (i * Int(OpenCVTestCase.matSize) + j) * 3
  200. _truth[start + 0] = Int16(i)
  201. _truth[start + 1] = Int16(j)
  202. _truth[start + 2] = Int16(i - j)
  203. }
  204. }
  205. try truth!.put(row: 0, col: 0, data: _truth)
  206. try assertMatEqual(truth!, _3dPoints, OpenCVTestCase.EPS)
  207. }
  208. func testRodriguesMatMat() throws {
  209. let r = Mat(rows: 3, cols: 1, type: CvType.CV_32F)
  210. let R = Mat(rows: 3, cols: 3, type: CvType.CV_32F)
  211. try r.put(row:0, col:0, data:[.pi, 0, 0] as [Float])
  212. Calib3d.Rodrigues(src: r, dst: R)
  213. truth = Mat(rows: 3, cols: 3, type: CvType.CV_32F)
  214. try truth!.put(row:0, col:0, data:[1, 0, 0, 0, -1, 0, 0, 0, -1] as [Float])
  215. try assertMatEqual(truth!, R, OpenCVTestCase.EPS)
  216. let r2 = Mat()
  217. Calib3d.Rodrigues(src: R, dst: r2)
  218. try assertMatEqual(r, r2, OpenCVTestCase.EPS)
  219. }
  220. func testSolvePnPListOfPoint3ListOfPointMatMatMatMat() throws {
  221. let intrinsics = Mat.eye(rows: 3, cols: 3, type: CvType.CV_64F)
  222. try intrinsics.put(row: 0, col: 0, data: [400] as [Double])
  223. try intrinsics.put(row: 1, col: 1, data: [400] as [Double])
  224. try intrinsics.put(row: 0, col: 2, data: [640 / 2] as [Double])
  225. try intrinsics.put(row: 1, col: 2, data: [480 / 2] as [Double])
  226. let minPnpPointsNum: Int32 = 4
  227. let points3d = MatOfPoint3f()
  228. points3d.alloc(minPnpPointsNum)
  229. let points2d = MatOfPoint2f()
  230. points2d.alloc(minPnpPointsNum)
  231. for i in 0..<minPnpPointsNum {
  232. let x = Float.random(in: -50...50)
  233. let y = Float.random(in: -50...50)
  234. try points2d.put(row: i, col: 0, data: [x, y]) //add(Point(x, y))
  235. try points3d.put(row: i, col: 0, data: [0, y, x]) // add(Point3(0, y, x))
  236. }
  237. let rvec = Mat()
  238. let tvec = Mat()
  239. Calib3d.solvePnP(objectPoints: points3d, imagePoints: points2d, cameraMatrix: intrinsics, distCoeffs: MatOfDouble(), rvec: rvec, tvec: tvec)
  240. let truth_rvec = Mat(rows: 3, cols: 1, type: CvType.CV_64F)
  241. try truth_rvec.put(row: 0, col: 0, data: [0, .pi / 2, 0] as [Double])
  242. let truth_tvec = Mat(rows: 3, cols: 1, type: CvType.CV_64F)
  243. try truth_tvec.put(row: 0, col: 0, data: [-320, -240, 400] as [Double])
  244. try assertMatEqual(truth_rvec, rvec, OpenCVTestCase.EPS)
  245. try assertMatEqual(truth_tvec, tvec, OpenCVTestCase.EPS)
  246. }
  247. func testComputeCorrespondEpilines() throws {
  248. let fundamental = Mat(rows: 3, cols: 3, type: CvType.CV_64F)
  249. try fundamental.put(row: 0, col: 0, data: [0, -0.577, 0.288, 0.577, 0, 0.288, -0.288, -0.288, 0])
  250. let left = MatOfPoint2f()
  251. left.alloc(1)
  252. try left.put(row: 0, col: 0, data: [2, 3] as [Float]) //add(Point(x, y))
  253. let lines = Mat()
  254. let truth = Mat(rows: 1, cols: 1, type: CvType.CV_32FC3)
  255. try truth.put(row: 0, col: 0, data: [-0.70735186, 0.70686162, -0.70588124])
  256. Calib3d.computeCorrespondEpilines(points: left, whichImage: 1, F: fundamental, lines: lines)
  257. try assertMatEqual(truth, lines, OpenCVTestCase.EPS)
  258. }
  259. func testConstants()
  260. {
  261. // calib3d.hpp: some constants have conflict with constants from 'fisheye' namespace
  262. XCTAssertEqual(1, Calib3d.CALIB_USE_INTRINSIC_GUESS)
  263. XCTAssertEqual(2, Calib3d.CALIB_FIX_ASPECT_RATIO)
  264. XCTAssertEqual(4, Calib3d.CALIB_FIX_PRINCIPAL_POINT)
  265. XCTAssertEqual(8, Calib3d.CALIB_ZERO_TANGENT_DIST)
  266. XCTAssertEqual(16, Calib3d.CALIB_FIX_FOCAL_LENGTH)
  267. XCTAssertEqual(32, Calib3d.CALIB_FIX_K1)
  268. XCTAssertEqual(64, Calib3d.CALIB_FIX_K2)
  269. XCTAssertEqual(128, Calib3d.CALIB_FIX_K3)
  270. XCTAssertEqual(0x0800, Calib3d.CALIB_FIX_K4)
  271. XCTAssertEqual(0x1000, Calib3d.CALIB_FIX_K5)
  272. XCTAssertEqual(0x2000, Calib3d.CALIB_FIX_K6)
  273. XCTAssertEqual(0x4000, Calib3d.CALIB_RATIONAL_MODEL)
  274. XCTAssertEqual(0x8000, Calib3d.CALIB_THIN_PRISM_MODEL)
  275. XCTAssertEqual(0x10000, Calib3d.CALIB_FIX_S1_S2_S3_S4)
  276. XCTAssertEqual(0x40000, Calib3d.CALIB_TILTED_MODEL)
  277. XCTAssertEqual(0x80000, Calib3d.CALIB_FIX_TAUX_TAUY)
  278. XCTAssertEqual(0x100000, Calib3d.CALIB_USE_QR)
  279. XCTAssertEqual(0x200000, Calib3d.CALIB_FIX_TANGENT_DIST)
  280. XCTAssertEqual(0x100, Calib3d.CALIB_FIX_INTRINSIC)
  281. XCTAssertEqual(0x200, Calib3d.CALIB_SAME_FOCAL_LENGTH)
  282. XCTAssertEqual(0x400, Calib3d.CALIB_ZERO_DISPARITY)
  283. XCTAssertEqual((1 << 17), Calib3d.CALIB_USE_LU)
  284. XCTAssertEqual((1 << 22), Calib3d.CALIB_USE_EXTRINSIC_GUESS)
  285. }
  286. func testSolvePnPGeneric_regression_16040() throws {
  287. let intrinsics = Mat.eye(rows: 3, cols: 3, type: CvType.CV_64F)
  288. try intrinsics.put(row: 0, col: 0, data: [400] as [Double])
  289. try intrinsics.put(row: 1, col: 1, data: [400] as [Double])
  290. try intrinsics.put(row: 0, col: 2, data: [640 / 2] as [Double])
  291. try intrinsics.put(row: 1, col: 2, data: [480 / 2] as [Double])
  292. let minPnpPointsNum: Int32 = 4
  293. let points3d = MatOfPoint3f()
  294. points3d.alloc(minPnpPointsNum)
  295. let points2d = MatOfPoint2f()
  296. points2d.alloc(minPnpPointsNum)
  297. for i in 0..<minPnpPointsNum {
  298. let x = Float.random(in: -50...50)
  299. let y = Float.random(in: -50...50)
  300. try points2d.put(row: i, col: 0, data: [x, y]) //add(Point(x, y))
  301. try points3d.put(row: i, col: 0, data: [0, y, x]) // add(Point3(0, y, x))
  302. }
  303. var rvecs = [Mat]()
  304. var tvecs = [Mat]()
  305. let rvec = Mat()
  306. let tvec = Mat()
  307. let reprojectionError = Mat(rows: 2, cols: 1, type: CvType.CV_64FC1)
  308. Calib3d.solvePnPGeneric(objectPoints: points3d, imagePoints: points2d, cameraMatrix: intrinsics, distCoeffs: MatOfDouble(), rvecs: &rvecs, tvecs: &tvecs, useExtrinsicGuess: false, flags: .SOLVEPNP_IPPE, rvec: rvec, tvec: tvec, reprojectionError: reprojectionError)
  309. let truth_rvec = Mat(rows: 3, cols: 1, type: CvType.CV_64F)
  310. try truth_rvec.put(row: 0, col: 0, data: [0, .pi / 2, 0] as [Double])
  311. let truth_tvec = Mat(rows: 3, cols: 1, type: CvType.CV_64F)
  312. try truth_tvec.put(row: 0, col: 0, data: [-320, -240, 400] as [Double])
  313. try assertMatEqual(truth_rvec, rvecs[0], 10 * OpenCVTestCase.EPS)
  314. try assertMatEqual(truth_tvec, tvecs[0], 1000 * OpenCVTestCase.EPS)
  315. }
  316. func testGetDefaultNewCameraMatrixMat() {
  317. let mtx = Calib3d.getDefaultNewCameraMatrix(cameraMatrix: gray0)
  318. XCTAssertFalse(mtx.empty())
  319. XCTAssertEqual(0, Core.countNonZero(src: mtx))
  320. }
  321. func testGetDefaultNewCameraMatrixMatSizeBoolean() {
  322. let mtx = Calib3d.getDefaultNewCameraMatrix(cameraMatrix: gray0, imgsize: size, centerPrincipalPoint: true)
  323. XCTAssertFalse(mtx.empty())
  324. XCTAssertFalse(0 == Core.countNonZero(src: mtx))
  325. // TODO_: write better test
  326. }
  327. func testUndistortMatMatMatMat() throws {
  328. let src = Mat(rows: 3, cols: 3, type: CvType.CV_32F, scalar: Scalar(3))
  329. let cameraMatrix = Mat(rows: 3, cols: 3, type: CvType.CV_32F)
  330. try cameraMatrix.put(row: 0, col: 0, data: [1, 0, 1] as [Float])
  331. try cameraMatrix.put(row: 1, col: 0, data: [0, 1, 2] as [Float])
  332. try cameraMatrix.put(row: 2, col: 0, data: [0, 0, 1] as [Float])
  333. let distCoeffs = Mat(rows: 1, cols: 4, type: CvType.CV_32F)
  334. try distCoeffs.put(row: 0, col: 0, data: [1, 3, 2, 4] as [Float])
  335. Calib3d.undistort(src: src, dst: dst, cameraMatrix: cameraMatrix, distCoeffs: distCoeffs)
  336. truth = Mat(rows: 3, cols: 3, type: CvType.CV_32F)
  337. try truth!.put(row: 0, col: 0, data: [0, 0, 0] as [Float])
  338. try truth!.put(row: 1, col: 0, data: [0, 0, 0] as [Float])
  339. try truth!.put(row: 2, col: 0, data: [0, 3, 0] as [Float])
  340. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  341. }
  342. func testUndistortMatMatMatMatMat() throws {
  343. let src = Mat(rows: 3, cols: 3, type: CvType.CV_32F, scalar: Scalar(3))
  344. let cameraMatrix = Mat(rows: 3, cols: 3, type: CvType.CV_32F)
  345. try cameraMatrix.put(row: 0, col: 0, data: [1, 0, 1] as [Float])
  346. try cameraMatrix.put(row: 1, col: 0, data: [0, 1, 2] as [Float])
  347. try cameraMatrix.put(row: 2, col: 0, data: [0, 0, 1] as [Float])
  348. let distCoeffs = Mat(rows: 1, cols: 4, type: CvType.CV_32F)
  349. try distCoeffs.put(row: 0, col: 0, data: [2, 1, 4, 5] as [Float])
  350. let newCameraMatrix = Mat(rows: 3, cols: 3, type: CvType.CV_32F, scalar: Scalar(1))
  351. Calib3d.undistort(src: src, dst: dst, cameraMatrix: cameraMatrix, distCoeffs: distCoeffs, newCameraMatrix: newCameraMatrix)
  352. truth = Mat(rows: 3, cols: 3, type: CvType.CV_32F, scalar: Scalar(3))
  353. try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
  354. }
  355. //undistortPoints(List<Point> src, List<Point> dst, Mat cameraMatrix, Mat distCoeffs)
  356. func testUndistortPointsListOfPointListOfPointMatMat() {
  357. let src = MatOfPoint2f(array: [Point2f(x: 1, y: 2), Point2f(x: 3, y: 4), Point2f(x: -1, y: -1)])
  358. let dst = MatOfPoint2f()
  359. let cameraMatrix = Mat.eye(rows: 3, cols: 3, type: CvType.CV_64FC1)
  360. let distCoeffs = Mat(rows: 8, cols: 1, type: CvType.CV_64FC1, scalar: Scalar(0))
  361. Calib3d.undistortPoints(src: src, dst: dst, cameraMatrix: cameraMatrix, distCoeffs: distCoeffs)
  362. XCTAssertEqual(src.toArray(), dst.toArray())
  363. }
  364. }