// // Calib3dTest.swift // // Created by Giles Payne on 2020/05/26. // import XCTest import OpenCV class Calib3dTest: OpenCVTestCase { var size = Size() override func setUp() { super.setUp() size = Size(width: 3, height: 3) } override func tearDown() { super.tearDown() } func testComposeRTMatMatMatMatMatMat() throws { let rvec1 = Mat(rows: 3, cols: 1, type: CvType.CV_32F) try rvec1.put(row: 0, col: 0, data: [0.5302828, 0.19925919, 0.40105945] as [Float]) let tvec1 = Mat(rows: 3, cols: 1, type: CvType.CV_32F) try tvec1.put(row: 0, col: 0, data: [0.81438506, 0.43713298, 0.2487897] as [Float]) let rvec2 = Mat(rows: 3, cols: 1, type: CvType.CV_32F) try rvec2.put(row: 0, col: 0, data: [0.77310503, 0.76209372, 0.30779448] as [Float]) let tvec2 = Mat(rows: 3, cols: 1, type: CvType.CV_32F) try tvec2.put(row: 0, col: 0, data: [0.70243168, 0.4784472, 0.79219002] as [Float]) let rvec3 = Mat() let tvec3 = Mat() let outRvec = Mat(rows: 3, cols: 1, type: CvType.CV_32F) try outRvec.put(row: 0, col: 0, data: [1.418641, 0.88665926, 0.56020796]) let outTvec = Mat(rows: 3, cols: 1, type: CvType.CV_32F) try outTvec.put(row: 0, col: 0, data: [1.4560841, 1.0680628, 0.81598103]) Calib3d.composeRT(rvec1: rvec1, tvec1: tvec1, rvec2: rvec2, tvec2: tvec2, rvec3: rvec3, tvec3: tvec3) try assertMatEqual(outRvec, rvec3, OpenCVTestCase.EPS) try assertMatEqual(outTvec, tvec3, OpenCVTestCase.EPS) } func testFilterSpecklesMatDoubleIntDouble() throws { gray_16s_1024.copy(to: dst) let center = Point(x: gray_16s_1024.rows() / 2, y: gray_16s_1024.cols() / 2) Imgproc.circle(img: dst, center: center, radius: 1, color: Scalar.all(4096)) try assertMatNotEqual(gray_16s_1024, dst) Calib3d.filterSpeckles(img: dst, newVal: 1024.0, maxSpeckleSize: 100, maxDiff: 0.0) try assertMatEqual(gray_16s_1024, dst) } func testFindChessboardCornersMatSizeMat() { let patternSize = Size(width: 9, height: 6) let corners = MatOfPoint2f() Calib3d.findChessboardCorners(image: grayChess, patternSize: patternSize, corners: corners) XCTAssertFalse(corners.empty()) } func testFindChessboardCornersMatSizeMatInt() { let patternSize = Size(width: 9, height: 6) let corners = MatOfPoint2f() Calib3d.findChessboardCorners(image: grayChess, patternSize: patternSize, corners: corners, flags: Calib3d.CALIB_CB_ADAPTIVE_THRESH + Calib3d.CALIB_CB_NORMALIZE_IMAGE + Calib3d.CALIB_CB_FAST_CHECK) XCTAssertFalse(corners.empty()) } func testFind4QuadCornerSubpix() { let patternSize = Size(width: 9, height: 6) let corners = MatOfPoint2f() let region_size = Size(width: 5, height: 5) Calib3d.findChessboardCorners(image: grayChess, patternSize: patternSize, corners: corners) Calib3d.find4QuadCornerSubpix(img: grayChess, corners: corners, region_size: region_size) XCTAssertFalse(corners.empty()) } func testFindCirclesGridMatSizeMat() { let size = 300 let img = Mat(rows:Int32(size), cols:Int32(size), type:CvType.CV_8U) img.setTo(scalar: Scalar(255)) let centers = Mat() XCTAssertFalse(Calib3d.findCirclesGrid(image: img, patternSize: Size(width: 5, height: 5), centers: centers)) for i in 0..<5 { for j in 0..<5 { let x = Int32(size * (2 * i + 1) / 10) let y = Int32(size * (2 * j + 1) / 10) let pt = Point(x: x, y: y) Imgproc.circle(img: img, center: pt, radius: 10, color: Scalar(0), thickness: -1) } } XCTAssert(Calib3d.findCirclesGrid(image: img, patternSize:Size(width:5, height:5), centers:centers)) XCTAssertEqual(25, centers.rows()) XCTAssertEqual(1, centers.cols()) XCTAssertEqual(CvType.CV_32FC2, centers.type()) } func testFindCirclesGridMatSizeMatInt() { let size:Int32 = 300 let img = Mat(rows:size, cols: size, type: CvType.CV_8U) img.setTo(scalar: Scalar(255)) let centers = Mat() XCTAssertFalse(Calib3d.findCirclesGrid(image: img, patternSize: Size(width: 3, height: 5), centers: centers, flags: Calib3d.CALIB_CB_CLUSTERING | Calib3d.CALIB_CB_ASYMMETRIC_GRID)) let step = size * 2 / 15 let offsetx = size / 6 let offsety = (size - 4 * step) / 2 for i:Int32 in 0...2 { for j:Int32 in 0...4 { let pt = Point(x: offsetx + (2 * i + j % 2) * step, y: offsety + step * j) Imgproc.circle(img: img, center: pt, radius: 10, color: Scalar(0), thickness: -1) } } XCTAssert(Calib3d.findCirclesGrid(image: img, patternSize: Size(width: 3, height: 5), centers: centers, flags: Calib3d.CALIB_CB_CLUSTERING | Calib3d.CALIB_CB_ASYMMETRIC_GRID)) XCTAssertEqual(15, centers.rows()) XCTAssertEqual(1, centers.cols()) XCTAssertEqual(CvType.CV_32FC2, centers.type()) } func testFindHomographyListOfPointListOfPoint() throws { let NUM:Int32 = 20 let originalPoints = MatOfPoint2f() originalPoints.alloc(NUM) let transformedPoints = MatOfPoint2f() transformedPoints.alloc(NUM) for i:Int32 in 0.. src, List dst, Mat cameraMatrix, Mat distCoeffs) func testUndistortPointsListOfPointListOfPointMatMat() { let src = MatOfPoint2f(array: [Point2f(x: 1, y: 2), Point2f(x: 3, y: 4), Point2f(x: -1, y: -1)]) let dst = MatOfPoint2f() let cameraMatrix = Mat.eye(rows: 3, cols: 3, type: CvType.CV_64FC1) let distCoeffs = Mat(rows: 8, cols: 1, type: CvType.CV_64FC1, scalar: Scalar(0)) Calib3d.undistortPoints(src: src, dst: dst, cameraMatrix: cameraMatrix, distCoeffs: distCoeffs) XCTAssertEqual(src.toArray(), dst.toArray()) } }