// // ImgprocTest.swift // // Created by Giles Payne on 2020/02/08. // import XCTest import OpenCV class ImgprocTest: OpenCVTestCase { let anchorPoint = Point(x: 2, y: 2) let imgprocSz: Int32 = 2 let size = Size(width: 3, height: 3) func testAccumulateMatMat() throws { let src = getMat(CvType.CV_64F, vals: [2]) let dst = getMat(CvType.CV_64F, vals: [0]) let dst2 = src.clone() Imgproc.accumulate(src: src, dst: dst) Imgproc.accumulate(src: src, dst: dst2) try assertMatEqual(src, dst, OpenCVTestCase.EPS) try assertMatEqual(getMat(CvType.CV_64F, vals: [4]), dst2, OpenCVTestCase.EPS) } func testAccumulateMatMatMat() throws { let src = getMat(CvType.CV_64F, vals: [2]) let mask = makeMask(getMat(CvType.CV_8U, vals: [1])) let dst = getMat(CvType.CV_64F, vals: [0]) let dst2 = src.clone() Imgproc.accumulate(src: src, dst: dst, mask: mask) Imgproc.accumulate(src: src, dst: dst2, mask: mask) try assertMatEqual(makeMask(getMat(CvType.CV_64F, vals: [2])), dst, OpenCVTestCase.EPS) try assertMatEqual(makeMask(getMat(CvType.CV_64F, vals: [4]), vals: [2]), dst2, OpenCVTestCase.EPS) } func testAccumulateProductMatMatMat() throws { let src = getMat(CvType.CV_64F, vals: [2]) let dst = getMat(CvType.CV_64F, vals: [0]) let dst2 = src.clone() Imgproc.accumulateProduct(src1: src, src2: src, dst: dst) Imgproc.accumulateProduct(src1: src, src2: dst, dst: dst2) try assertMatEqual(getMat(CvType.CV_64F, vals:[4]), dst, OpenCVTestCase.EPS) try assertMatEqual(getMat(CvType.CV_64F, vals:[10]), dst2, OpenCVTestCase.EPS) } func testAccumulateProductMatMatMatMat() throws { let src = getMat(CvType.CV_64F, vals: [2]) let mask = makeMask(getMat(CvType.CV_8U, vals: [1])) let dst = getMat(CvType.CV_64F, vals: [0]) let dst2 = src.clone() Imgproc.accumulateProduct(src1: src, src2: src, dst: dst, mask: mask) Imgproc.accumulateProduct(src1: src, src2: dst, dst: dst2, mask: mask) try assertMatEqual(makeMask(getMat(CvType.CV_64F, vals: [4])), dst, OpenCVTestCase.EPS) try assertMatEqual(makeMask(getMat(CvType.CV_64F, vals: [10]), vals:[2]), dst2, OpenCVTestCase.EPS) } func testAccumulateSquareMatMat() throws { let src = getMat(CvType.CV_64F, vals: [2]) let dst = getMat(CvType.CV_64F, vals: [0]) let dst2 = src.clone() Imgproc.accumulateSquare(src: src, dst: dst) Imgproc.accumulateSquare(src: src, dst: dst2) try assertMatEqual(getMat(CvType.CV_64F, vals: [4]), dst, OpenCVTestCase.EPS) try assertMatEqual(getMat(CvType.CV_64F, vals: [6]), dst2, OpenCVTestCase.EPS) } func testAccumulateSquareMatMatMat() throws { let src = getMat(CvType.CV_64F, vals: [2]) let mask = makeMask(getMat(CvType.CV_8U, vals: [1])) let dst = getMat(CvType.CV_64F, vals: [0]) let dst2 = src.clone() Imgproc.accumulateSquare(src: src, dst: dst, mask: mask) Imgproc.accumulateSquare(src: src, dst: dst2, mask: mask) try assertMatEqual(makeMask(getMat(CvType.CV_64F, vals: [4])), dst, OpenCVTestCase.EPS) try assertMatEqual(makeMask(getMat(CvType.CV_64F, vals: [6]), vals: [2]), dst2, OpenCVTestCase.EPS) } func testAccumulateWeightedMatMatDouble() throws { let src = getMat(CvType.CV_64F, vals: [2]) let dst = getMat(CvType.CV_64F, vals: [4]) let dst2 = src.clone() Imgproc.accumulateWeighted(src: src, dst: dst, alpha: 0.5) Imgproc.accumulateWeighted(src: src, dst: dst2, alpha: 2) try assertMatEqual(getMat(CvType.CV_64F, vals: [3]), dst, OpenCVTestCase.EPS) try assertMatEqual(getMat(CvType.CV_64F, vals: [2]), dst2, OpenCVTestCase.EPS) } func testAccumulateWeightedMatMatDoubleMat() throws { let src = getMat(CvType.CV_64F, vals: [2]) let mask = makeMask(getMat(CvType.CV_8U, vals: [1])) let dst = getMat(CvType.CV_64F, vals: [4]) let dst2 = src.clone() Imgproc.accumulateWeighted(src: src, dst: dst, alpha: 0.5, mask: mask) Imgproc.accumulateWeighted(src: src, dst: dst2, alpha: 2, mask: mask) try assertMatEqual(makeMask(getMat(CvType.CV_64F, vals: [3]), vals: [4]), dst, OpenCVTestCase.EPS) try assertMatEqual(getMat(CvType.CV_64F, vals: [2]), dst2, OpenCVTestCase.EPS) } func testAdaptiveThreshold() { let src = makeMask(getMat(CvType.CV_8U, vals: [50]), vals:[20]) let dst = Mat() Imgproc.adaptiveThreshold(src: src, dst: dst, maxValue: 1, adaptiveMethod: .ADAPTIVE_THRESH_MEAN_C, thresholdType: .THRESH_BINARY, blockSize: 3, C: 0) XCTAssertEqual(src.rows(), Core.countNonZero(src: dst)) } func testApproxPolyDP() { 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)] var approxCurve = [Point2f]() Imgproc.approxPolyDP(curve: curve, approxCurve: &approxCurve, epsilon: OpenCVTestCase.EPS, closed: true) let approxCurveGold = [Point2f(x: 1, y: 3), Point2f(x: 3, y: 5), Point2f(x: 5, y: 3)] XCTAssert(approxCurve == approxCurveGold) } func testArcLength() { 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)] let arcLength = Imgproc.arcLength(curve: curve, closed: false) XCTAssertEqual(5.656854249, arcLength, accuracy:0.000001) } func testBilateralFilterMatMatIntDoubleDouble() throws { Imgproc.bilateralFilter(src: gray255, dst: dst, d: 5, sigmaColor: 10, sigmaSpace: 5) try assertMatEqual(gray255, dst) } func testBilateralFilterMatMatIntDoubleDoubleInt() throws { Imgproc.bilateralFilter(src: gray255, dst: dst, d: 5, sigmaColor: 10, sigmaSpace: 5, borderType: .BORDER_REFLECT) try assertMatEqual(gray255, dst) } func testBlurMatMatSize() throws { Imgproc.blur(src: gray0, dst: dst, ksize: size) try assertMatEqual(gray0, dst) Imgproc.blur(src: gray255, dst: dst, ksize: size) try assertMatEqual(gray255, dst) } func testBlurMatMatSizePoint() throws { Imgproc.blur(src: gray0, dst: dst, ksize: size, anchor: anchorPoint) try assertMatEqual(gray0, dst) } func testBlurMatMatSizePointInt() throws { Imgproc.blur(src: gray0, dst: dst, ksize: size, anchor: anchorPoint, borderType: .BORDER_REFLECT) try assertMatEqual(gray0, dst) } func testBoundingRect() { let points = [Point(x: 0, y: 0), Point(x: 0, y: 4), Point(x: 4, y: 0), Point(x: 4, y: 4)] let p1 = Point(x: 1, y: 1) let p2 = Point(x: -5, y: -2) let bbox = Imgproc.boundingRect(array: MatOfPoint(array: points)) XCTAssert(bbox.contains(p1)) XCTAssertFalse(bbox.contains(p2)) } func testBoxFilterMatMatIntSize() throws { let size = Size(width: 3, height: 3) Imgproc.boxFilter(src: gray0, dst: dst, ddepth: 8, ksize: size) try assertMatEqual(gray0, dst) } func testBoxFilterMatMatIntSizePointBoolean() throws { Imgproc.boxFilter(src: gray255, dst: dst, ddepth: 8, ksize: size, anchor: anchorPoint, normalize: false) try assertMatEqual(gray255, dst) } func testBoxFilterMatMatIntSizePointBooleanInt() throws { Imgproc.boxFilter(src: gray255, dst: dst, ddepth: 8, ksize: size, anchor: anchorPoint, normalize: false, borderType: .BORDER_REFLECT) try assertMatEqual(gray255, dst) } func testCalcBackProject() { let images = [grayChess] let channels:[Int32] = [0] let histSize:[Int32] = [10] let ranges:[Float] = [0, 256] let hist = Mat() Imgproc.calcHist(images: images, channels: channels, mask: Mat(), hist: hist, histSize: histSize, ranges: ranges) Core.normalize(src: hist, dst: hist) Imgproc.calcBackProject(images: images, channels: channels, hist: hist, dst: dst, ranges: ranges, scale: 255) XCTAssertEqual(grayChess.size(), dst.size()) XCTAssertEqual(grayChess.depth(), dst.depth()) XCTAssertFalse(0 == Core.countNonZero(src: dst)) } func testCalcHistListOfMatListOfIntegerMatMatListOfIntegerListOfFloat() throws { let images = [gray128] let channels:[Int32] = [0] let histSize:[Int32] = [10] let ranges:[Float] = [0, 256] let hist = Mat() Imgproc.calcHist(images: images, channels: channels, mask: Mat(), hist: hist, histSize: histSize, ranges: ranges) truth = Mat(rows: 10, cols: 1, type: CvType.CV_32F, scalar: Scalar.all(0)) try truth!.put(row: 5, col: 0, data: [100] as [Float]) try assertMatEqual(truth!, hist, OpenCVTestCase.EPS) } func testCalcHistListOfMatListOfIntegerMatMatListOfIntegerListOfFloat2D() throws { let images = [gray255, gray128] let channels:[Int32] = [0, 1] let histSize:[Int32] = [10, 10] let ranges:[Float] = [0, 256, 0, 256] let hist = Mat() Imgproc.calcHist(images: images, channels: channels, mask: Mat(), hist: hist, histSize: histSize, ranges: ranges) truth = Mat(rows: 10, cols: 10, type: CvType.CV_32F, scalar: Scalar.all(0)) try truth!.put(row: 9, col: 5, data: [100] as [Float]) try assertMatEqual(truth!, hist, OpenCVTestCase.EPS) } func testCalcHistListOfMatListOfIntegerMatMatListOfIntegerListOfFloat3D() throws { let images = [rgbLena] let hist3D = Mat() let histList = [Mat(), Mat(), Mat()] let histSize: [Int32] = [10] let ranges: [Float] = [0, 256] for i:Int in 0.. 0) Imgproc.polylines(img: img, pts: polyline2, isClosed: true, color: Scalar(0), thickness: 2, lineType: .LINE_8, shift: 1) XCTAssertEqual(0, Core.countNonZero(src: img)) } func testPutTextMatStringPointIntDoubleScalar() { let text = "Hello World" let labelSize = Size(width: 175, height: 22) let img = Mat(rows: 20 + labelSize.height, cols: 20 + labelSize.width, type: CvType.CV_8U, scalar: colorBlack) let origin = Point(x: 10, y: labelSize.height + 10) Imgproc.putText(img: img, text: text, org: origin, fontFace: .FONT_HERSHEY_SIMPLEX, fontScale: 1.0, color: colorWhite) XCTAssert(Core.countNonZero(src: img) > 0) // check that border is not corrupted 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) XCTAssertEqual(0, Core.countNonZero(src: img)) } func testPutTextMatStringPointIntDoubleScalarInt() { let text = "Hello World" let labelSize = Size(width: 176, height: 22) let img = Mat(rows: 20 + labelSize.height, cols: 20 + labelSize.width, type: CvType.CV_8U, scalar: colorBlack) let origin = Point(x: 10, y: labelSize.height + 10) Imgproc.putText(img: img, text: text, org: origin, fontFace: .FONT_HERSHEY_SIMPLEX, fontScale: 1.0, color: colorWhite, thickness: 2) XCTAssert(Core.countNonZero(src: img) > 0) // check that border is not corrupted 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) XCTAssertEqual(0, Core.countNonZero(src: img)) } func testPutTextMatStringPointIntDoubleScalarIntIntBoolean() { let text = "Hello World" let labelSize = Size(width: 175, height: 22) let img = Mat(rows: 20 + labelSize.height, cols: 20 + labelSize.width, type: CvType.CV_8U, scalar: colorBlack) let origin = Point(x: 10, y: 10) Imgproc.putText(img: img, text: text, org: origin, fontFace: .FONT_HERSHEY_SIMPLEX, fontScale: 1.0, color: colorWhite, thickness: 1, lineType: .LINE_8, bottomLeftOrigin: true) XCTAssert(Core.countNonZero(src: img) > 0) // check that border is not corrupted 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) XCTAssertEqual(0, Core.countNonZero(src: img)) } }