// // StitchAppTests.swift // // Created by Giles Payne on 2020/01/19. // import XCTest import OpenCV class MatTests: OpenCVTestCase { override func setUp() { super.setUp() } override func tearDown() { super.tearDown() } func testAdjustROI() throws { let roi = gray0.submat(rowStart: 3, rowEnd: 5, colStart: 7, colEnd: 10) let originalroi = roi.clone() let adjusted = roi.adjustRoi(top: 2, bottom: 2, left: 2, right: 2) try assertMatEqual(adjusted, roi) assertSizeEquals(Size(width: 5, height: 6), adjusted.size()) XCTAssertEqual(originalroi.type(), adjusted.type()) XCTAssertTrue(adjusted.isSubmatrix()) XCTAssertFalse(adjusted.isContinuous()) let offset = Point() let size = Size() adjusted.locateROI(wholeSize: size, offset: offset) assertPointEquals(Point(x: 5, y: 1), offset); assertSizeEquals(gray0.size(), size); } func testAssignToMat() throws { gray0.assign(to: dst) try assertMatEqual(gray0, dst) gray255.assign(to: dst) try assertMatEqual(gray255, dst) } func testAssignToMatInt() throws { gray255.assign(to: dst, type: CvType.CV_32F) try assertMatEqual(gray255_32f, dst, OpenCVTestCase.EPS) } func testChannels() { XCTAssertEqual(1, gray0.channels()) XCTAssertEqual(3, rgbLena.channels()) XCTAssertEqual(4, rgba0.channels()) } func testCheckVectorInt() { // ! returns N if the matrix is 1-channel (N x ptdim) or ptdim-channel // (1 x N) or (N x 1); negative number otherwise XCTAssertEqual(2, Mat(rows: 2, cols: 10, type: CvType.CV_8U).checkVector(elemChannels: 10)) XCTAssertEqual(2, Mat(rows: 1, cols: 2, type: CvType.CV_8UC(10)).checkVector(elemChannels: 10)) XCTAssertEqual(2, Mat(rows: 2, cols: 1, type: CvType.CV_8UC(10)).checkVector(elemChannels: 10)) XCTAssertEqual(10, Mat(rows: 1, cols: 10, type: CvType.CV_8UC2).checkVector(elemChannels: 2)) XCTAssert(0 > Mat().checkVector(elemChannels: 0)) XCTAssert(0 > Mat(rows: 10, cols: 1, type: CvType.CV_8U).checkVector(elemChannels: 10)) XCTAssert(0 > Mat(rows: 10, cols: 20, type: CvType.CV_8U).checkVector(elemChannels: 10)) } func testCheckVectorIntInt() { XCTAssertEqual(2, Mat(rows: 2, cols: 10, type: CvType.CV_8U).checkVector(elemChannels: 10, depth: CvType.CV_8U)) XCTAssertEqual(2, Mat(rows: 1, cols: 2, type: CvType.CV_8UC(10)).checkVector(elemChannels: 10, depth: CvType.CV_8U)) XCTAssertEqual(2, Mat(rows: 2, cols: 1, type: CvType.CV_8UC(10)).checkVector(elemChannels: 10, depth: CvType.CV_8U)) XCTAssertEqual(10, Mat(rows: 1, cols: 10, type: CvType.CV_8UC2).checkVector(elemChannels: 2, depth: CvType.CV_8U)) XCTAssert(0 > Mat(rows: 2, cols: 10, type: CvType.CV_8U).checkVector(elemChannels: 10, depth: CvType.CV_8S)); XCTAssert(0 > Mat(rows: 1, cols: 2, type: CvType.CV_8UC(10)).checkVector(elemChannels: 10, depth: CvType.CV_8S)); XCTAssert(0 > Mat(rows: 2, cols: 1, type: CvType.CV_8UC(10)).checkVector(elemChannels: 10, depth: CvType.CV_8S)); XCTAssert(0 > Mat(rows: 1, cols: 10, type: CvType.CV_8UC2).checkVector(elemChannels: 10, depth: CvType.CV_8S)); } func testCheckVectorIntIntBoolean() { let mm = Mat(rows: 5, cols: 1, type: CvType.CV_8UC(10)) let roi = Mat(rows: 5, cols: 3, type: CvType.CV_8UC(10)).submat(rowStart: 1, rowEnd: 3, colStart: 2, colEnd: 3); XCTAssertEqual(5, mm.checkVector(elemChannels: 10, depth: CvType.CV_8U, requireContinuous: true)); XCTAssertEqual(5, mm.checkVector(elemChannels: 10, depth: CvType.CV_8U, requireContinuous: false)); XCTAssertEqual(2, roi.checkVector(elemChannels: 10, depth: CvType.CV_8U, requireContinuous: false)); XCTAssert(0 > roi.checkVector(elemChannels: 10, depth: CvType.CV_8U, requireContinuous: true)); } func testClone() throws { dst = gray0.clone() try assertMatEqual(gray0, dst) XCTAssertFalse(dst.isSameMat(gray0)) } func testCol() { let col = gray0.col(0) XCTAssertEqual(1, col.cols()) XCTAssertEqual(gray0.rows(), col.rows()) } func testColRangeIntInt() { let cols = gray0.colRange(start: 0, end: gray0.cols() / 2) XCTAssertEqual(gray0.cols() / 2, cols.cols()) XCTAssertEqual(gray0.rows(), cols.rows()) } func testColRangeRange() throws { let range = Range(start: 0, end: 5) dst = gray0.colRange(range) truth = Mat(rows: 10, cols: 5, type: CvType.CV_8UC1, scalar: Scalar(0.0)) try assertMatEqual(truth!, dst) } func testCols() { XCTAssertEqual(OpenCVTestCase.matSize, gray0.cols()) } func testConvertToMatInt() throws { gray255.convert(to: dst, rtype: CvType.CV_32F) truth = Mat(rows: OpenCVTestCase.matSize, cols: OpenCVTestCase.matSize, type: CvType.CV_32F, scalar: Scalar(255)); try assertMatEqual(truth!, dst, OpenCVTestCase.EPS) } func testConvertToMatIntDouble() throws { gray2.convert(to: dst, rtype: CvType.CV_16U, alpha: 2.0) truth = Mat(rows: OpenCVTestCase.matSize, cols: OpenCVTestCase.matSize, type: CvType.CV_16U, scalar: Scalar(4)) try assertMatEqual(truth!, dst) } func testConvertToMatIntDoubleDouble() throws { gray0_32f.convert(to: dst, rtype: CvType.CV_8U, alpha: 2.0, beta: 4.0) truth = Mat(rows: OpenCVTestCase.matSize, cols: OpenCVTestCase.matSize, type: CvType.CV_8U, scalar: Scalar(4)) try assertMatEqual(truth!, dst) } func testCopyToMat() throws { rgbLena.copy(to:dst) try assertMatEqual(rgbLena, dst) } func testCopyToMatMat() throws { let src = Mat(rows: 4, cols: 4, type: CvType.CV_8U, scalar: Scalar(5)) let mask = makeMask(src.clone()) src.copy(to: dst, mask: mask) truth = Mat(rows: 4, cols: 4, type: CvType.CV_8U) try XCTAssertEqual(truth!.put(row: 0, col: 0, data: [0, 0, 5, 5] as [Int8]), 4) try XCTAssertEqual(truth!.put(row: 1, col: 0, data: [0, 0, 5, 5] as [Int8]), 4) try XCTAssertEqual(truth!.put(row: 2, col: 0, data: [0, 0, 5, 5] as [Int8]), 4) try XCTAssertEqual(truth!.put(row: 3, col: 0, data: [0, 0, 5, 5] as [Int8]), 4) try assertMatEqual(truth!, dst) } func testCreateIntIntInt() { gray255.create(rows: 4, cols: 5, type: CvType.CV_32F) XCTAssertEqual(4, gray255.rows()) XCTAssertEqual(5, gray255.cols()) XCTAssertEqual(CvType.CV_32F, gray255.type()) } func testCreateSizeInt() { let size = Size(width: 5, height: 5) dst.create(size: size, type: CvType.CV_16U) XCTAssertEqual(5, dst.rows()) XCTAssertEqual(5, dst.cols()) XCTAssertEqual(CvType.CV_16U, dst.type()) } func testCreateIntArrayInt() { dst.create(sizes:[5, 6, 7], type:CvType.CV_16U) XCTAssertEqual(5, dst.size(0)) XCTAssertEqual(6, dst.size(1)) XCTAssertEqual(7, dst.size(2)) XCTAssertEqual(CvType.CV_16U, dst.type()) } func testCross() throws { let answer = Mat(rows: 1, cols: 3, type: CvType.CV_32F) try XCTAssertEqual(answer.put(row: 0, col: 0, data: [7.0, 1.0, -5.0] as [Float]), 12) let cross = v1.cross(v2) try assertMatEqual(answer, cross, OpenCVTestCase.EPS) } func testDepth() { XCTAssertEqual(CvType.CV_8U, gray0.depth()) XCTAssertEqual(CvType.CV_32F, gray0_32f.depth()) } func testDiag() throws { dst = gray0.diag() truth = Mat(rows: 10, cols: 1, type: CvType.CV_8UC1, scalar: Scalar(0)) try assertMatEqual(truth!, dst) } func testDiagInt() throws { dst = gray255.diag(2) truth = Mat(rows: 8, cols: 1, type: CvType.CV_8UC1, scalar: Scalar(255)) try assertMatEqual(truth!, dst) } func testDiagMat() throws { let diagVector = Mat(rows: OpenCVTestCase.matSize, cols: 1, type: CvType.CV_32F, scalar: Scalar(1)) dst = Mat.diag(diagVector) try assertMatEqual(grayE_32f, dst, OpenCVTestCase.EPS); } func testDot() { let s = v1.dot(v2) XCTAssertEqual(11.0, s) } func testDump() { XCTAssertEqual("[1, 3, 2]", v1.dump()) } func testElemSize() { XCTAssertEqual(MemoryLayout.size * Int(gray0.channels()), gray0.elemSize()) XCTAssertEqual(MemoryLayout.size * Int(gray0_32f.channels()), gray0_32f.elemSize()) XCTAssertEqual(MemoryLayout.size * Int(rgbLena.channels()), rgbLena.elemSize()) } func testElemSize1() { XCTAssertEqual(MemoryLayout.size, gray255.elemSize1()) XCTAssertEqual(MemoryLayout.size, gray0_64f.elemSize1()) XCTAssertEqual(MemoryLayout.size, rgbLena.elemSize1()) } func testEmpty() { XCTAssert(dst.empty()) XCTAssertFalse(gray0.empty()) } func testEyeIntIntInt() throws { let eye = Mat.eye(rows: 3, cols: 3, type: CvType.CV_32FC1) try assertMatEqual(eye, eye.inv(), OpenCVTestCase.EPS) } func testEyeSizeInt() { let size = Size(width: 5, height: 5) let eye = Mat.eye(size: size, type: CvType.CV_32S) XCTAssertEqual(5, Core.countNonZero(src: eye)) } func getTestMat(size:Int32, type:Int32) throws -> Mat { let ret = Mat(rows: size, cols: size, type: type) let ch = CvType.channels(type) var buff:[Double] = [] for i: Int32 in (0.. 4D let src = Mat(rows: 6, cols: 5, type: CvType.CV_8UC3, scalar: Scalar(0)) XCTAssertEqual(2, src.dims()) XCTAssertEqual(src.rows(), src.size(0)) XCTAssertEqual(src.cols(), src.size(1)) let newShape = [1, src.channels() * src.cols(), 1, src.rows()] dst = src.reshape(channels: 1, newshape: newShape as [NSNumber]) XCTAssertEqual(newShape.count, Int(dst.dims())) for i in 0.. 2D let src2 = Mat(sizes: [4, 6, 7], type: CvType.CV_8UC3, scalar: Scalar(0)) XCTAssertEqual(3, src2.dims()) XCTAssertEqual(4, src2.size(0)) XCTAssertEqual(6, src2.size(1)) XCTAssertEqual(7, src2.size(2)) let newShape2 = [src2.channels() * src2.size(2), src2.size(0) * src2.size(1)] dst = src2.reshape(channels: 1, newshape: newShape2 as [NSNumber]) XCTAssertEqual(newShape2.count, Int(dst.dims())) for i in 0..