package org.opencv.test.core; import java.util.Arrays; import java.nio.ByteBuffer; import org.opencv.core.Core; import org.opencv.core.CvException; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.core.Point; import org.opencv.core.Range; import org.opencv.core.Rect; import org.opencv.core.Scalar; import org.opencv.core.Size; import org.opencv.test.OpenCVTestCase; public class MatTest extends OpenCVTestCase { public void testAdjustROI() { Mat roi = gray0.submat(3, 5, 7, 10); Mat originalroi = roi.clone(); Mat adjusted = roi.adjustROI(2, 2, 2, 2); assertMatEqual(adjusted, roi); assertSizeEquals(new Size(5, 6), adjusted.size(), EPS); assertEquals(originalroi.type(), adjusted.type()); assertTrue(adjusted.isSubmatrix()); assertFalse(adjusted.isContinuous()); Point offset = new Point(); Size size = new Size(); adjusted.locateROI(size, offset); assertPointEquals(new Point(5, 1), offset, EPS); assertSizeEquals(gray0.size(), size, EPS); } public void testAssignToMat() { gray0.assignTo(dst); assertMatEqual(gray0, dst); gray255.assignTo(dst); assertMatEqual(gray255, dst); } public void testAssignToMatInt() { gray255.assignTo(dst, CvType.CV_32F); assertMatEqual(gray255_32f, dst, EPS); } public void testChannels() { assertEquals(1, gray0.channels()); assertEquals(3, rgbLena.channels()); assertEquals(4, rgba0.channels()); } public void 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 assertEquals(2, new Mat(2, 10, CvType.CV_8U).checkVector(10)); assertEquals(2, new Mat(1, 2, CvType.CV_8UC(10)).checkVector(10)); assertEquals(2, new Mat(2, 1, CvType.CV_8UC(10)).checkVector(10)); assertEquals(10, new Mat(1, 10, CvType.CV_8UC2).checkVector(2)); assertTrue(0 > new Mat().checkVector(0)); assertTrue(0 > new Mat(10, 1, CvType.CV_8U).checkVector(10)); assertTrue(0 > new Mat(10, 20, CvType.CV_8U).checkVector(10)); } public void testCheckVectorIntInt() { assertEquals(2, new Mat(2, 10, CvType.CV_8U).checkVector(10, CvType.CV_8U)); assertEquals(2, new Mat(1, 2, CvType.CV_8UC(10)).checkVector(10, CvType.CV_8U)); assertEquals(2, new Mat(2, 1, CvType.CV_8UC(10)).checkVector(10, CvType.CV_8U)); assertEquals(10, new Mat(1, 10, CvType.CV_8UC2).checkVector(2, CvType.CV_8U)); assertTrue(0 > new Mat(2, 10, CvType.CV_8U).checkVector(10, CvType.CV_8S)); assertTrue(0 > new Mat(1, 2, CvType.CV_8UC(10)).checkVector(10, CvType.CV_8S)); assertTrue(0 > new Mat(2, 1, CvType.CV_8UC(10)).checkVector(10, CvType.CV_8S)); assertTrue(0 > new Mat(1, 10, CvType.CV_8UC2).checkVector(10, CvType.CV_8S)); } public void testCheckVectorIntIntBoolean() { Mat mm = new Mat(5, 1, CvType.CV_8UC(10)); Mat roi = new Mat(5, 3, CvType.CV_8UC(10)).submat(1, 3, 2, 3); assertEquals(5, mm.checkVector(10, CvType.CV_8U, true)); assertEquals(5, mm.checkVector(10, CvType.CV_8U, false)); assertEquals(2, roi.checkVector(10, CvType.CV_8U, false)); assertTrue(0 > roi.checkVector(10, CvType.CV_8U, true)); } public void testClone() { dst = gray0.clone(); assertMatEqual(gray0, dst); assertFalse(gray0.getNativeObjAddr() == dst.getNativeObjAddr()); assertFalse(gray0.dataAddr() == dst.dataAddr()); } public void testCol() { Mat col = gray0.col(0); assertEquals(1, col.cols()); assertEquals(gray0.rows(), col.rows()); } public void testColRangeIntInt() { Mat cols = gray0.colRange(0, gray0.cols() / 2); assertEquals(gray0.cols() / 2, cols.cols()); assertEquals(gray0.rows(), cols.rows()); } public void testColRangeRange() { Range range = new Range(0, 5); dst = gray0.colRange(range); truth = new Mat(10, 5, CvType.CV_8UC1, new Scalar(0.0)); assertMatEqual(truth, dst); } public void testCols() { assertEquals(matSize, gray0.cols()); } public void testConvertToMatInt() { gray255.convertTo(dst, CvType.CV_32F); truth = new Mat(matSize, matSize, CvType.CV_32F, new Scalar(255)); assertMatEqual(truth, dst, EPS); } public void testConvertToMatIntDouble() { gray2.convertTo(dst, CvType.CV_16U, 2.0); truth = new Mat(matSize, matSize, CvType.CV_16U, new Scalar(4)); assertMatEqual(truth, dst); } public void testConvertToMatIntDoubleDouble() { gray0_32f.convertTo(dst, CvType.CV_8U, 2.0, 4.0); truth = new Mat(matSize, matSize, CvType.CV_8U, new Scalar(4)); assertMatEqual(truth, dst); } public void testCopyToMat() { rgbLena.copyTo(dst); assertMatEqual(rgbLena, dst); } public void testCopyToMatMat() { Mat src = new Mat(4, 4, CvType.CV_8U, new Scalar(5)); Mat mask = makeMask(src.clone()); src.copyTo(dst, mask); truth = new Mat(4, 4, CvType.CV_8U) { { put(0, 0, 0, 0, 5, 5); put(1, 0, 0, 0, 5, 5); put(2, 0, 0, 0, 5, 5); put(3, 0, 0, 0, 5, 5); } }; assertMatEqual(truth, dst); } public void testCreateIntIntInt() { gray255.create(4, 5, CvType.CV_32F); assertEquals(4, gray255.rows()); assertEquals(5, gray255.cols()); assertEquals(CvType.CV_32F, gray255.type()); } public void testCreateSizeInt() { Size size = new Size(5, 5); dst.create(size, CvType.CV_16U); assertEquals(5, dst.rows()); assertEquals(5, dst.cols()); assertEquals(CvType.CV_16U, dst.type()); } public void testCreateIntArrayInt() { int[] dims = new int[] {5, 6, 7}; dst.create(dims, CvType.CV_16U); assertEquals(5, dst.size(0)); assertEquals(6, dst.size(1)); assertEquals(7, dst.size(2)); assertEquals(CvType.CV_16U, dst.type()); } public void testCross() { Mat answer = new Mat(1, 3, CvType.CV_32F); answer.put(0, 0, 7.0, 1.0, -5.0); Mat cross = v1.cross(v2); assertMatEqual(answer, cross, EPS); } public void testDataAddr() { assertTrue(0 != gray0.dataAddr()); assertEquals(0, new Mat().dataAddr()); } public void testDepth() { assertEquals(CvType.CV_8U, gray0.depth()); assertEquals(CvType.CV_32F, gray0_32f.depth()); } public void testDiag() { dst = gray0.diag(); truth = new Mat(10, 1, CvType.CV_8UC1, new Scalar(0)); assertMatEqual(truth, dst); } public void testDiagInt() { dst = gray255.diag(2); truth = new Mat(8, 1, CvType.CV_8UC1, new Scalar(255)); assertMatEqual(truth, dst); } public void testDiagMat() { Mat diagVector = new Mat(matSize, 1, CvType.CV_32F, new Scalar(1)); dst = Mat.diag(diagVector); assertMatEqual(grayE_32f, dst, EPS); } public void testDiagMat_sqrMatrix() { try { dst = Mat.diag(gray255); } catch (CvException e) { // expected } } public void testDot() { double s = v1.dot(v2); assertEquals(11.0, s); } public void testDump() { assertEquals("[1, 3, 2]", v1.dump()); } public void testElemSize() { assertEquals(Byte.SIZE / 8 * gray0.channels(), gray0.elemSize()); assertEquals(Float.SIZE / 8 * gray0_32f.channels(), gray0_32f.elemSize()); assertEquals(Byte.SIZE / 8 * rgbLena.channels(), rgbLena.elemSize()); } public void testElemSize1() { assertEquals(Byte.SIZE / 8, gray255.elemSize1()); assertEquals(Double.SIZE / 8, gray0_64f.elemSize1()); assertEquals(Byte.SIZE / 8, rgbLena.elemSize1()); } public void testEmpty() { assertTrue(dst.empty()); assertFalse(gray0.empty()); } public void testEyeIntIntInt() { Mat eye = Mat.eye(3, 3, CvType.CV_32FC1); assertMatEqual(eye, eye.inv(), EPS); } public void testEyeSizeInt() { Size size = new Size(5, 5); Mat eye = Mat.eye(size, CvType.CV_32S); assertEquals(5, Core.countNonZero(eye)); } public Mat getTestMat(int size, int type) { Mat m = new Mat(size, size, type); final int ch = CvType.channels(type); double buff[] = new double[size*size * ch]; for(int i=0; i 0); gray0.release(); assertTrue(gray0.empty()); assertEquals(0, gray0.rows()); assertEquals(0, gray0.dataAddr()); } public void testReshapeInt() { Mat src = new Mat(4, 4, CvType.CV_8U, new Scalar(0)); dst = src.reshape(4); truth = new Mat(4, 1, CvType.CV_8UC4, new Scalar(0)); assertMatEqual(truth, dst); } public void testReshapeIntInt() { Mat src = new Mat(5, 7, CvType.CV_8U, new Scalar(0)); dst = src.reshape(7, 5); truth = new Mat(5, 1, CvType.CV_8UC(7), new Scalar(0)); assertMatEqual(truth, dst); } public void testReshapeIntIntArray() { // 2D -> 4D Mat src = new Mat(6, 5, CvType.CV_8UC3, new Scalar(0)); assertEquals(2, src.dims()); assertEquals(src.rows(), src.size(0)); assertEquals(src.cols(), src.size(1)); int[] newShape = {1, src.channels() * src.cols(), 1, src.rows()}; dst = src.reshape(1, newShape); assertEquals(newShape.length, dst.dims()); for (int i = 0; i < newShape.length; ++i) assertEquals(newShape[i], dst.size(i)); // 3D -> 2D src = new Mat(new int[]{4, 6, 7}, CvType.CV_8UC3, new Scalar(0)); assertEquals(3, src.dims()); assertEquals(4, src.size(0)); assertEquals(6, src.size(1)); assertEquals(7, src.size(2)); int[] newShape2 = {src.channels() * src.size(2), src.size(0) * src.size(1)}; dst = src.reshape(1, newShape2); assertEquals(newShape2.length, dst.dims()); for (int i = 0; i < newShape2.length; ++i) assertEquals(newShape2[i], dst.size(i)); } public void testCopySize() { Mat src = new Mat(new int[]{1, 1, 10, 10}, CvType.CV_8UC1, new Scalar(1)); assertEquals(4, src.dims()); assertEquals(1, src.size(0)); assertEquals(1, src.size(1)); assertEquals(10, src.size(2)); assertEquals(10, src.size(3)); Mat other = new Mat(new int[]{10, 10}, src.type()); src.copySize(other); assertEquals(other.dims(), src.dims()); for (int i = 0; i < other.dims(); ++i) assertEquals(other.size(i), src.size(i)); } public void testRow() { Mat row = gray0.row(0); assertEquals(1, row.rows()); assertEquals(gray0.cols(), row.cols()); } public void testRowRangeIntInt() { Mat rows = gray0.rowRange(0, gray0.rows() / 2); assertEquals(gray0.rows() / 2, rows.rows()); assertEquals(gray0.cols(), rows.cols()); } public void testRowRangeRange() { Mat rows = gray255.rowRange(new Range(0, 5)); assertEquals(gray255.rows() / 2, rows.rows()); assertEquals(gray255.cols(), rows.cols()); } public void testRows() { assertEquals(matSize, gray0.rows()); } public void testSetToMat() { Mat vals = new Mat(7, 1, CvType.CV_8U) { { put(0, 0, 1, 2, 3, 4, 5, 6, 7); } }; Mat dst = new Mat(1, 1, CvType.CV_8UC(7)); dst.setTo(vals); Mat truth = new Mat(1, 1, CvType.CV_8UC(7)) { { put(0, 0, 1, 2, 3, 4, 5, 6, 7); } }; assertMatEqual(truth, dst); } public void testSetToMatMat() { Mat vals = new Mat(7, 1, CvType.CV_8U) { { put(0, 0, 1, 2, 3, 4, 5, 6, 7); } }; Mat dst = Mat.zeros(2, 1, CvType.CV_8UC(7)); Mat mask = new Mat(2, 1, CvType.CV_8U) { { put(0, 0, 0, 1); } }; dst.setTo(vals, mask); Mat truth = new Mat(2, 1, CvType.CV_8UC(7)) { { put(0, 0, 0, 0, 0, 0, 0, 0, 0); put(1, 0, 1, 2, 3, 4, 5, 6, 7); } }; assertMatEqual(truth, dst); } public void testSetToScalar() { gray0.setTo(new Scalar(127)); assertMatEqual(gray127, gray0); } public void testSetToScalarMask() { Mat mask = gray0.clone(); mask.put(1, 1, 1, 2, 3); gray0.setTo(new Scalar(1), mask); assertEquals(3, Core.countNonZero(gray0)); Core.subtract(gray0, mask, gray0); assertEquals(0, Core.countNonZero(gray0)); } public void testSize() { assertEquals(new Size(matSize, matSize), gray0.size()); assertEquals(new Size(3, 1), v1.size()); } public void testStep1() { assertEquals(matSize * CvType.channels(CvType.CV_8U), gray0.step1()); assertEquals(3, v2.step1()); } public void testStep1Int() { Mat roi = rgba0.submat(3, 5, 7, 10); Mat m = roi.clone(); assertTrue(rgba0.channels() * rgba0.cols() <= roi.step1(0)); assertEquals(rgba0.channels(), roi.step1(1)); assertTrue(m.channels() * (10 - 7) <= m.step1(0)); assertEquals(m.channels(), m.step1(1)); } public void testSubmatIntIntIntInt() { Mat submat = gray0.submat(0, gray0.rows() / 2, 0, gray0.cols() / 2); assertTrue(submat.isSubmatrix()); assertFalse(submat.isContinuous()); assertEquals(gray0.rows() / 2, submat.rows()); assertEquals(gray0.cols() / 2, submat.cols()); } public void testSubmatRangeRange() { Mat submat = gray255.submat(new Range(2, 4), new Range(2, 4)); assertTrue(submat.isSubmatrix()); assertFalse(submat.isContinuous()); assertEquals(2, submat.rows()); assertEquals(2, submat.cols()); } public void testSubmatRangeArray() { Mat submat = gray255_32f_3d.submat(new Range[]{ new Range(2, 4), new Range(2, 4), new Range(3, 6) }); assertTrue(submat.isSubmatrix()); assertFalse(submat.isContinuous()); assertEquals(2, submat.size(0)); assertEquals(2, submat.size(1)); assertEquals(3, submat.size(2)); } public void testSubmatRect() { Mat submat = gray255.submat(new Rect(5, 5, gray255.cols() / 2, gray255.rows() / 2)); assertTrue(submat.isSubmatrix()); assertFalse(submat.isContinuous()); assertEquals(gray255.rows() / 2, submat.rows()); assertEquals(gray255.cols() / 2, submat.cols()); } public void testT() { assertMatEqual(gray255, gray255.t()); Mat src = new Mat(3, 3, CvType.CV_16U) { { put(0, 0, 1, 2, 4); put(1, 0, 7, 5, 0); put(2, 0, 3, 4, 6); } }; dst = src.t(); truth = new Mat(3, 3, CvType.CV_16U) { { put(0, 0, 1, 7, 3); put(1, 0, 2, 5, 4); put(2, 0, 4, 0, 6); } }; assertMatEqual(truth, dst); } public void testToString() { assertNotNull(gray0.toString()); } public void testTotal() { int nElements = gray0.rows() * gray0.cols(); assertEquals(nElements, gray0.total()); } public void testType() { assertEquals(CvType.CV_8UC1, gray0.type()); assertEquals(CvType.CV_32FC1, gray0_32f.type()); assertEquals(CvType.CV_8UC3, rgbLena.type()); } public void testWidth() { assertEquals(gray0.cols(), gray0.width()); assertEquals(rgbLena.cols(), rgbLena.width()); assertEquals(rgba128.cols(), rgba128.width()); } public void testZerosIntIntInt() { dst = Mat.zeros(matSize, matSize, CvType.CV_32F); assertMatEqual(gray0_32f, dst, EPS); } public void testZerosSizeInt() { dst = Mat.zeros(new Size(2, 2), CvType.CV_16S); truth = new Mat(2, 2, CvType.CV_16S, new Scalar(0)); assertMatEqual(truth, dst); } public void testZerosIntArray() { dst = Mat.zeros(new int[]{2, 3, 4}, CvType.CV_16S); truth = new Mat(new int[]{2, 3, 4}, CvType.CV_16S, new Scalar(0)); assertMatEqual(truth, dst); } public void testMatFromByteBuffer() { ByteBuffer bbuf = ByteBuffer.allocateDirect(64*64); bbuf.putInt(0x01010101); Mat m = new Mat(64,64,CvType.CV_8UC1,bbuf); assertEquals(4, Core.countNonZero(m)); Core.add(m, new Scalar(1), m); assertEquals(4096, Core.countNonZero(m)); m.release(); assertEquals(2, bbuf.get(0)); assertEquals(1, bbuf.get(4095)); } public void testMatFromByteBufferWithStep() { ByteBuffer bbuf = ByteBuffer.allocateDirect(80*64); bbuf.putInt(0x01010101); bbuf.putInt(64, 0x02020202); bbuf.putInt(80, 0x03030303); Mat m = new Mat(64, 64, CvType.CV_8UC1, bbuf, 80); assertEquals(8, Core.countNonZero(m)); Core.add(m, new Scalar(5), m); assertEquals(4096, Core.countNonZero(m)); m.release(); assertEquals(6, bbuf.get(0)); assertEquals(5, bbuf.get(63)); assertEquals(2, bbuf.get(64)); assertEquals(0, bbuf.get(79)); assertEquals(8, bbuf.get(80)); assertEquals(5, bbuf.get(63*80 + 63)); } public void testMatAt() { Mat uc1 = new Mat(2, 3, CvType.CV_8S) { { put(0, 0, 1, 2, 3); put(1, 0, 4, 5, 6); } }; assertEquals((byte)1, uc1.at(Byte.class, 0, 0).getV().byteValue()); assertEquals((byte)2, uc1.at(Byte.class, 0, 1).getV().byteValue()); assertEquals((byte)3, uc1.at(Byte.class, 0, 2).getV().byteValue()); assertEquals((byte)4, uc1.at(Byte.class, 1, 0).getV().byteValue()); assertEquals((byte)5, uc1.at(Byte.class, 1, 1).getV().byteValue()); assertEquals((byte)6, uc1.at(Byte.class, 1, 2).getV().byteValue()); uc1.at(Byte.class, 0, 0).setV((byte)7); uc1.at(Byte.class, 0, 1).setV((byte)8); uc1.at(Byte.class, 0, 2).setV((byte)9); uc1.at(Byte.class, 1, 0).setV((byte)10); uc1.at(Byte.class, 1, 1).setV((byte)11); uc1.at(Byte.class, 1, 2).setV((byte)12); byte[] data = new byte[6]; uc1.get(0, 0, data); assertArrayEquals(data, new byte[] {7, 8, 9, 10, 11, 12}); Mat.Tuple3 bgr = rgbLena.at(Byte.class, 0, 0).getV3c(); assertEquals(bgr.get_0().byteValue(), (byte)128); assertEquals(bgr.get_1().byteValue(), (byte)138); assertEquals(bgr.get_2().byteValue(), (byte)225); } }