Mat Basic Processing1 (OpenCV5)

These codes are included in the OpenCVForUnity Example Unity scenes. (MatBasicProcessingExample)

shape overview example (0D / 1D / 2D / ND)

Example Code:


            // ---------------------------------------------------------------------------------------
            //  shape overview example (0D / 1D / 2D / ND)
            // ---------------------------------------------------------------------------------------
            // OpenCV 5 Mat is shape (dims) x type (depth + channels).
            // empty and 0D scalar both can report dims==0 — distinguish them with empty() / total().
            // A true 1D vector is NOT the same as a 2D Nx1 (or 1xN) matrix.
            //
            // kind     dims  empty  total  rows  cols  note
            // empty    0     true   0      0     0     no data
            // 0D       0     false  1      1     1     one scalar value
            // 1D       1     false  N      1     N     true vector (OpenCV 5)
            // 2D       2     false  R*C    R     C     image / matrix
            // ND       >=3   false  prod   -1    -1    use size(i)
            //

            Log("kind     dims  empty  total  rows  cols  note");
            Log("-------- ----- ------ ------ ----- ----- ----");

            // Empty Mat (no data)
            Mat empty = new Mat();
            Log(FormatShapeRow("empty", empty, "no data"));
            Log("empty = " + empty.dump());

            // 0D scalar (one value; empty()==false)
            Mat scalar = new Mat(Array.Empty<int>(), CvType.CV_64FC1, new Scalar(3.14));
            Log(FormatShapeRow("0D", scalar, "one scalar value"));
            Log("scalar = " + scalar.dump());

            // True 1D vector of length 4
            Mat vec = new Mat(new int[] { 4 }, CvType.CV_64FC1);
            vec.put(0, 0, 1, 2, 3, 4);
            Log(FormatShapeRow("1D", vec, "true vector; put(0,i) / get(0,i)"));
            Log("vec = " + vec.dump());

            // Contrast: 2D column vector (Nx1) — same element count, different dims
            Mat colVec2d = new Mat(4, 1, CvType.CV_64FC1);
            colVec2d.put(0, 0, 1, 2, 3, 4);
            Log(FormatShapeRow("2D Nx1", colVec2d, "NOT a true 1D; put(i,0)"));
            Log("colVec2d = " + colVec2d.dump());
            Log("vec.step1() = " + vec.step1());
            Log("colVec2d.step1() = " + colVec2d.step1());

            // 2D matrix / image
            Mat m2d = new Mat(2, 3, CvType.CV_64FC1);
            m2d.put(0, 0, 1, 2, 3, 4, 5, 6);
            Log(FormatShapeRow("2D", m2d, "image / matrix"));
            Log("m2d = " + m2d.dump());

            // ND tensor
            Mat nd = new Mat(new int[] { 2, 2, 3 }, CvType.CV_8UC1, Scalar.all(0));
            Log(FormatShapeRow("ND", nd, "use size(i); rows/cols are -1"));
            Log("nd = " + nd);

            Log("");
            Log("OpenCV 4 vs 5 tip for length-N vectors:");
            Log("  OpenCV 4 often used Nx1 (dims=2, rows=N, cols=1, put(i,0)).");
            Log("  OpenCV 5 true 1D: new Mat(new int[]{ N }, type) -> dims=1, rows=1, cols=N, put(0,i).");
            Log("  Prefer total() or checkVector() for element count — do not assume rows()==N.");
            Log("  channels() is independent of dims (e.g. CV_8UC3 has 3 channels on any shape).");
            Log("  dump() works for dims<=2 (empty/0D/1D/2D). For dims>2, reshape to 2D first.");

            Log("");
            Log("empty vs 0D — when to use which:");
            Log("  empty (new Mat()): no data yet; common as an output destination before Core ops fill it.");
            Log("  0D scalar: a real one-element Mat (empty()==false, total()==1); not the same as empty.");
            Log("  Tip: if (m.empty()) means no buffer; a 0D Mat is valid data with one element.");

            Log("");
            Log("checkVector(elemChannels) — element count if the Mat is a vector-like layout, else -1:");
            // True 1D length-4, 1 channel -> 4 elements
            Log("vec.checkVector(1) = " + vec.checkVector(1));
            // 2D Nx1 is also accepted as a column vector of 1-channel elements
            Log("colVec2d.checkVector(1) = " + colVec2d.checkVector(1));
            // General 2x3 matrix is not a vector for elemChannels==1
            Log("m2d.checkVector(1) = " + m2d.checkVector(1) + " // -1: not a row/column vector");
            // Multi-channel column vector: Nx1 CV_32FC2 -> checkVector(2) == N
            Mat ch2col = new Mat(3, 1, CvType.CV_32FC2);
            Log("ch2col (3x1 CV_32FC2).checkVector(2) = " + ch2col.checkVector(2));
            Log("ch2col.checkVector(1) = " + ch2col.checkVector(1) + " // -1: channel count mismatch");
            

Execution Result:

kind     dims  empty  total  rows  cols  note
-------- ----- ------ ------ ----- ----- ----
empty        0   True      0     0     0  no data
empty = []
0D           0  False      1     1     1  one scalar value
scalar = [3.14]
1D           1  False      4     1     4  true vector; put(0,i) / get(0,i)
vec = [1, 2, 3, 4]
2D Nx1       2  False      4     4     1  NOT a true 1D; put(i,0)
colVec2d = [1;
 2;
 3;
 4]
vec.step1() = 1
colVec2d.step1() = 1
2D           2  False      6     2     3  image / matrix
m2d = [1, 2, 3;
 4, 5, 6]
ND           3  False     12    -1    -1  use size(i); rows/cols are -1
nd = Mat [ 2*2*3*CV_8UC1, isCont=True, isSubmat=False, nativeObj=0x1807068817904, dataAddr=0x1819125132288 ]

OpenCV 4 vs 5 tip for length-N vectors:
  OpenCV 4 often used Nx1 (dims=2, rows=N, cols=1, put(i,0)).
  OpenCV 5 true 1D: new Mat(new int[]{ N }, type) -> dims=1, rows=1, cols=N, put(0,i).
  Prefer total() or checkVector() for element count — do not assume rows()==N.
  channels() is independent of dims (e.g. CV_8UC3 has 3 channels on any shape).
  dump() works for dims<=2 (empty/0D/1D/2D). For dims>2, reshape to 2D first.

empty vs 0D — when to use which:
  empty (new Mat()): no data yet; common as an output destination before Core ops fill it.
  0D scalar: a real one-element Mat (empty()==false, total()==1); not the same as empty.
  Tip: if (m.empty()) means no buffer; a 0D Mat is valid data with one element.

checkVector(elemChannels) — element count if the Mat is a vector-like layout, else -1:
vec.checkVector(1) = 4
colVec2d.checkVector(1) = 4
m2d.checkVector(1) = -1 // -1: not a row/column vector
ch2col (3x1 CV_32FC2).checkVector(2) = 3
ch2col.checkVector(1) = -1 // -1: channel count mismatch

initialization example

Example Code:


            // ---------------------------------------------------------------------------------------
            //  initialization example
            // ---------------------------------------------------------------------------------------
            // Showcase initialization methods for different matrix types and sizes,
            // including OpenCV 5 true 0D / 1D constructors.
            //

            // 3x3 matrix (set array value)
            // CvType.CV_64FC1 = 64-bit float, 1 channel per pixel.
            Mat mat1 = new Mat(3, 3, CvType.CV_64FC1);
            mat1.put(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
            Log("mat1 = " + mat1.dump());

            // 2x2 rotation matrix
            double angle = 30, a = Math.Cos(angle * Math.PI / 180), b = Math.Sin(angle * Math.PI / 180);
            Mat mat2 = new Mat(2, 2, CvType.CV_64FC1);
            mat2.put(0, 0, a, -b, b, a);
            Log("mat2 = " + mat2.dump());

            // 5x5 all 1's matrix
            Mat mat3 = Mat.ones(5, 5, CvType.CV_64FC1);
            Log("mat3 = " + mat3.dump());

            // 5x5 all zero's matrix
            Mat mat4 = Mat.zeros(5, 5, CvType.CV_64FC1);
            Log("mat4 = " + mat4.dump());

            // 5x5 identity matrix
            Mat mat5 = Mat.eye(5, 5, CvType.CV_64FC1);
            Log("mat5 = " + mat5.dump());

            // 3x3 initialize with a constant
            Mat mat6 = new Mat(3, 3, CvType.CV_64FC1, new Scalar(5));
            Log("mat6 = " + mat6.dump());

            // 3x2 initialize with a uniform distribution random number
            Mat mat7 = new Mat(3, 2, CvType.CV_8UC1);
            Core.randu(mat7, 0, 256);
            Log("mat7 = " + mat7.dump());

            // 3x2 initialize with a normal distribution random number
            Mat mat8 = new Mat(3, 2, CvType.CV_8UC1);
            Core.randn(mat8, 128, 10);
            Log("mat8 = " + mat8.dump());

            // 0D scalar (OpenCV 5): one value, dims==0, empty()==false
            Mat mat0d = new Mat(Array.Empty<int>(), CvType.CV_64FC1, new Scalar(42));
            Log("mat0d.dims() = " + mat0d.dims());
            Log("mat0d.empty() = " + mat0d.empty());
            Log("mat0d.total() = " + mat0d.total());
            Log("mat0d = " + mat0d.dump());

            // True 1D vector (OpenCV 5): dims==1, rows==1, cols==N
            // Note: new Mat(5, 1, type) creates a 2D 5x1 matrix — not a true 1D Mat.
            Mat mat1d = new Mat(new int[] { 5 }, CvType.CV_64FC1);
            mat1d.put(0, 0, 1, 2, 3, 4, 5);
            Log("mat1d.dims() = " + mat1d.dims());
            Log("mat1d.rows() = " + mat1d.rows());
            Log("mat1d.cols() = " + mat1d.cols());
            Log("mat1d = " + mat1d.dump());
            Mat mat1dOnes = Mat.ones(new int[] { 5 }, CvType.CV_64FC1);
            Mat mat1dZeros = Mat.zeros(new int[] { 5 }, CvType.CV_64FC1);
            Log("mat1dOnes = " + mat1dOnes.dump());
            Log("mat1dZeros = " + mat1dZeros.dump());

            // 2x2x3x4 matrix (4 dimensional array)
            // For ndim > 2, rows()/cols() return (-1, -1); use size(i) and dims() instead.
            int[] sizes = new int[] { 2, 2, 3, 4 };
            Mat mat9 = new Mat(sizes, CvType.CV_8UC1, Scalar.all(0));
            Log("mat9.dims() = " + mat9.dims());
            Log("mat9.rows() = " + mat9.rows() + " //When the matrix is more than 2-dimensional, the returned size is (-1, -1).");
            Log("mat9.cols() = " + mat9.cols());
            

Execution Result:

mat1 = [1, 2, 3;
 4, 5, 6;
 7, 8, 9]
mat2 = [0.8660254037844387, -0.4999999999999999;
 0.4999999999999999, 0.8660254037844387]
mat3 = [1, 1, 1, 1, 1;
 1, 1, 1, 1, 1;
 1, 1, 1, 1, 1;
 1, 1, 1, 1, 1;
 1, 1, 1, 1, 1]
mat4 = [0, 0, 0, 0, 0;
 0, 0, 0, 0, 0;
 0, 0, 0, 0, 0;
 0, 0, 0, 0, 0;
 0, 0, 0, 0, 0]
mat5 = [1, 0, 0, 0, 0;
 0, 1, 0, 0, 0;
 0, 0, 1, 0, 0;
 0, 0, 0, 1, 0;
 0, 0, 0, 0, 1]
mat6 = [5, 5, 5;
 5, 5, 5;
 5, 5, 5]
mat7 = [ 89, 144;
 210,  51;
  31, 234]
mat8 = [141, 108;
 109, 123;
 121, 147]
mat0d.dims() = 0
mat0d.empty() = False
mat0d.total() = 1
mat0d = [42]
mat1d.dims() = 1
mat1d.rows() = 1
mat1d.cols() = 5
mat1d = [1, 2, 3, 4, 5]
mat1dOnes = [1, 1, 1, 1, 1]
mat1dZeros = [0, 0, 0, 0, 0]
mat9.dims() = 4
mat9.rows() = -1 //When the matrix is more than 2-dimensional, the returned size is (-1, -1).
mat9.cols() = -1

multi channel example

Example Code:


            // ---------------------------------------------------------------------------------------
            //  multi channel example
            // ---------------------------------------------------------------------------------------
            // Initialization of matrices with various numbers of channels, including those with four or more channels.
            // channels() is independent of dims(): e.g. a 1D Mat can still be CV_8UC3.
            //

            // 64F, channels=1, 3x3
            Mat mat1 = new Mat(3, 3, CvType.CV_64FC1);
            Log("mat1.dims() = " + mat1.dims());
            Log("mat1.elemSize1() = " + mat1.elemSize1());
            Log("mat1.channels() = " + mat1.channels());

            // 64F, channels=10, 3x3 — CV_64FC(n) creates n channels interleaved per pixel.
            Mat mat2 = new Mat(3, 3, CvType.CV_64FC(10));
            Log("mat2.dims() = " + mat2.dims());
            Log("mat2.elemSize1() = " + mat2.elemSize1());
            Log("mat2.channels() = " + mat2.channels());

            // 64F, channels=1, 2x2x3x4 (4 dimensional array)
            int[] sizes = new int[] { 2, 2, 3, 4 };
            Mat mat3 = new Mat(sizes, CvType.CV_64FC1);
            Log("mat3.dims() = " + mat3.dims());
            Log("mat3.elemSize1() = " + mat3.elemSize1());
            Log("mat3.channels() = " + mat3.channels());

            // 1D + multi-channel: shape is still 1D; each element has 3 channels
            Mat mat4 = new Mat(new int[] { 4 }, CvType.CV_8UC3, new Scalar(1, 2, 3));
            Log("mat4.dims() = " + mat4.dims());
            Log("mat4.cols() = " + mat4.cols());
            Log("mat4.channels() = " + mat4.channels());
            Log("mat4 = " + mat4.dump());

            // Color / RGBA images are still dims==2; channels are not Mat dimensions.
            Mat rgba = new Mat(2, 2, CvType.CV_8UC4, new Scalar(255, 128, 64, 255));
            Log("rgba.dims() = " + rgba.dims() + " // color image remains dims==2");
            Log("rgba.channels() = " + rgba.channels());
            Log("rgba = " + rgba.dump());
            

Execution Result:

mat1.dims() = 2
mat1.elemSize1() = 8
mat1.channels() = 1
mat2.dims() = 2
mat2.elemSize1() = 8
mat2.channels() = 10
mat3.dims() = 4
mat3.elemSize1() = 8
mat3.channels() = 1
mat4.dims() = 1
mat4.cols() = 4
mat4.channels() = 3
mat4 = [  1,   2,   3,   1,   2,   3,   1,   2,   3,   1,   2,   3]
rgba.dims() = 2 // color image remains dims==2
rgba.channels() = 4
rgba = [255, 128,  64, 255, 255, 128,  64, 255;
 255, 128,  64, 255, 255, 128,  64, 255]

MatOf* example (OpenCVForUnity / Java-wrapper typed Mats)

Example Code:


            // ---------------------------------------------------------------------------------------
            //  MatOf* example (OpenCVForUnity / Java-wrapper typed Mats)
            // ---------------------------------------------------------------------------------------
            // MatOf* classes (MatOfInt, MatOfPoint, MatOfByte, ...) are Mat subclasses from the
            // OpenCV Java bindings. C++ OpenCV has no separate MatOf* types — they exist to
            // round-trip C# arrays/lists with a known depth/channel layout.
            //
            // Shape notes (OpenCV 5):
            // - fromArray / alloc normally create a true 1D Mat (dims==1, rows==1, cols==N).
            // - You can also wrap an existing Mat (including compatible 2D Nx1 / 1xN) via new MatOf*(mat)
            //   when checkVector(channels, depth) succeeds; otherwise CvException("Incompatible Mat").
            // - Do not assume rows()==N (OpenCV 4 Nx1 habit); use checkVector / toArray length / cols() for 1D.
            //

            // Typical path: fromArray -> true 1D
            MatOfInt moi = new MatOfInt(10, 20, 30, 40);
            Log("moi.dims() = " + moi.dims());
            Log("moi.rows() = " + moi.rows());
            Log("moi.cols() = " + moi.cols());
            Log("moi.checkVector(1, CvType.CV_32S) = " + moi.checkVector(1, CvType.CV_32S));
            Log("moi = " + moi.dump());
            int[] moiArr = moi.toArray();
            Log("moi.toArray() length = " + moiArr.Length);

            // Round-trip
            moi.fromArray(1, 2, 3);
            Log("moi after fromArray = " + moi.dump());

            // Wrap a compatible 2D Mat (Nx1) — still valid MatOfInt, but dims==2
            Mat col2d = new Mat(4, 1, CvType.CV_32SC1);
            col2d.put(0, 0, 5, 6, 7, 8);
            MatOfInt moiFrom2d = new MatOfInt(col2d);
            Log("moiFrom2d.dims() = " + moiFrom2d.dims() + " // wrapped 2D Nx1, not true 1D");
            Log("moiFrom2d.checkVector(1, CvType.CV_32S) = " + moiFrom2d.checkVector(1, CvType.CV_32S));
            Log("moiFrom2d = " + moiFrom2d.dump());

            // Multi-channel typed Mat: MatOfPoint uses CV_32SC2 (x,y per element)
            MatOfPoint mop = new MatOfPoint(new Point(1, 2), new Point(3, 4), new Point(5, 6));
            Log("mop.dims() = " + mop.dims());
            Log("mop.channels() = " + mop.channels());
            Log("mop.checkVector(2, CvType.CV_32S) = " + mop.checkVector(2, CvType.CV_32S));
            Log("mop = " + mop.dump());

            // Same pattern as transposeND / mixChannels demos (MatOfInt as int parameter Mat)
            MatOfInt order = new MatOfInt(0, 2, 1, 3);
            Log("order (transposeND-style) = " + order.dump());
            

Execution Result:

moi.dims() = 1
moi.rows() = 1
moi.cols() = 4
moi.checkVector(1, CvType.CV_32S) = 4
moi = [10, 20, 30, 40]
moi.toArray() length = 4
moi after fromArray = [1, 2, 3]
moiFrom2d.dims() = 2 // wrapped 2D Nx1, not true 1D
moiFrom2d.checkVector(1, CvType.CV_32S) = 4
moiFrom2d = [5;
 6;
 7;
 8]
mop.dims() = 1
mop.channels() = 2
mop.checkVector(2, CvType.CV_32S) = 3
mop = [1, 2, 3, 4, 5, 6]
order (transposeND-style) = [0, 2, 1, 3]

dump example

Example Code:


            // ---------------------------------------------------------------------------------------
            //  dump example
            // ---------------------------------------------------------------------------------------
            // Output the elements of the matrix as a string.
            // Native dump() supports dims <= 2 (empty, 0D scalar, 1D, and 2D).
            // Mats with dims > 2 throw CvException — reshape to 2D first.
            //

            // 8U, channels=1, 3x3
            Mat mat1 = new Mat(3, 3, CvType.CV_8UC1, new Scalar(1));

            // 8U, channels=4, 3x3
            Mat mat2 = new Mat(3, 3, CvType.CV_8UC4, new Scalar(1, 2, 3, 4));

            // 0D scalar
            Mat mat0d = new Mat(Array.Empty<int>(), CvType.CV_8UC1, new Scalar(7));

            // True 1D
            Mat mat1d = new Mat(new int[] { 3 }, CvType.CV_32F);
            mat1d.put(0, 0, 1f, 3f, 2f);

            // 32F, channels=1, 1x3x4x3 (dims=4)
            Mat mat3 = new Mat(new int[] { 1, 3, 4, 3 }, CvType.CV_32FC1);
            mat3.put(new int[] { 0, 0, 0, 0 }, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);

            // dump
            Log("mat1 = " + mat1);
            Log("mat1.dump() = " + mat1.dump());
            Log("mat2 = " + mat2);
            Log("mat2.dump() = " + mat2.dump());
            Log("mat0d.dims() = " + mat0d.dims());
            Log("mat0d = " + mat0d.dump());
            Log("mat1d.dims() = " + mat1d.dims());
            Log("mat1d = " + mat1d.dump());
            Log("mat3 = " + mat3);
            // dump() requires dims<=2; reshape ND Mats to 2D to inspect contents.
            Log("mat3.reshape(3, new int[] { 3, 4 }).dump() = " + mat3.reshape(3, new int[] { 3, 4 }).dump() + " // dims>2: reshape to 2D before dump()");
            

Execution Result:

mat1 = Mat [ 3*3*CV_8UC1, isCont=True, isSubmat=False, nativeObj=0x1819712920112, dataAddr=0x1818885691392 ]
mat1.dump() = [  1,   1,   1;
   1,   1,   1;
   1,   1,   1]
mat2 = Mat [ 3*3*CV_8UC4, isCont=True, isSubmat=False, nativeObj=0x1819712919888, dataAddr=0x1816289763392 ]
mat2.dump() = [  1,   2,   3,   4,   1,   2,   3,   4,   1,   2,   3,   4;
   1,   2,   3,   4,   1,   2,   3,   4,   1,   2,   3,   4;
   1,   2,   3,   4,   1,   2,   3,   4,   1,   2,   3,   4]
mat0d.dims() = 0
mat0d = [  7]
mat1d.dims() = 1
mat1d = [1, 3, 2]
mat3 = Mat [ 1*3*4*3*CV_32FC1, isCont=True, isSubmat=False, nativeObj=0x1819712921008, dataAddr=0x1819712920384 ]
mat3.reshape(3, new int[] { 3, 4 }).dump() = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12;
 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12;
 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] // dims>2: reshape to 2D before dump()

CVException handling example

Example Code:


            // ---------------------------------------------------------------------------------------
            //  CVException handling example
            // ---------------------------------------------------------------------------------------
            // How to display Native-side OpenCV error logs in the Unity Editor Console.
            // Common causes: mismatched depth/type, incompatible sizes/shapes, invalid arguments.
            // OpenCVDebug.SetDebugMode(true, throwException) controls LogError vs thrown CvException.
            //

            // 32F, channels=1, 3x3
            Mat m1 = new Mat(3, 3, CvType.CV_32FC1);
            m1.put(0, 0, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f);

            // 8U, channels=1, 3x3
            Mat m2 = new Mat(3, 3, CvType.CV_8UC1);
            m2.put(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

            // dump
            Log("m1 = " + m1);
            Log("m1.dump() = " + m1.dump());
            Log("m2 = " + m2);
            Log("m2.dump() = " + m2.dump());

            // CVException handling
            // Publish CVException to Debug.LogError.
            OpenCVDebug.SetDebugMode(true, false);

            Mat m3 = new Mat();
            // Core.divide requires matching element types; CV_32FC1 vs CV_8UC1 triggers a native error.
            Core.divide(m1, m2, m3); // element type is different.
            Log("m3 = " + m3);

            OpenCVDebug.SetDebugMode(false);

            // Throw CVException.
            OpenCVDebug.SetDebugMode(true, true);
            try
            {
                Mat m4 = new Mat();
                Core.divide(m1, m2, m4); // element type is different.
                Log("m4 = " + m4);
            }
            catch (Exception e)
            {
                Log("CVException: " + e);
            }
            OpenCVDebug.SetDebugMode(false);
            

Execution Result:

m1 = Mat [ 3*3*CV_32FC1, isCont=True, isSubmat=False, nativeObj=0x1819712919664, dataAddr=0x1816289762752 ]
m1.dump() = [1, 2, 3;
 4, 5, 6;
 7, 8, 9]
m2 = Mat [ 3*3*CV_8UC1, isCont=True, isSubmat=False, nativeObj=0x1819712918096, dataAddr=0x1818885688128 ]
m2.dump() = [  1,   2,   3;
   4,   5,   6;
   7,   8,   9]
m3 = Mat [ -1*-1*CV_8UC1, isCont=False, isSubmat=False, nativeObj=0x1819712919888, dataAddr=0x0 ]
CVException: OpenCVForUnity.CoreModule.CvException: core::divide_12() : OpenCV(5.0.0) C:\Users\xxxxx\opencv\modules\core\src\arithm.cpp:715: error: (-5:Bad argument) When the input arrays in add/subtract/multiply/divide functions have different types, the output array type must be explicitly specified in function 'cv::arithm_op'

property example

Example Code:


            // ---------------------------------------------------------------------------------------
            //  property example
            // ---------------------------------------------------------------------------------------
            // List the properties of an OpenCV matrix, including empty / 0D / true 1D / 2D / ND contrasts.
            //

            // 64F, channels=1, 3x4
            Mat mat1 = new Mat(3, 4, CvType.CV_64FC1);

            // number of rows
            Log("mat1.rows() = " + mat1.rows());
            // number of columns
            Log("mat1.cols() = " + mat1.cols());
            // number of dimensions
            Log("mat1.dims() = " + mat1.dims());
            // size
            Log("mat1.size() = " + mat1.size().width + ", " + mat1.size().height);
            // bit depth ID
            Log("mat1.depth() = " + mat1.depth() + "( = " + CvType.CV_64F + ")");
            // number of channels
            Log("mat1.channels() = " + mat1.channels());
            // size of one element
            Log("mat1.elemSize() = " + mat1.elemSize() + "[byte]");
            // size for one channel in one element
            Log("mat1.elemSize1() = " + mat1.elemSize1() + "[byte]");
            // total number of elements
            Log("mat1.total() = " + mat1.total());
            // size of step
            Log("mat1.step1()*elemSize1() = " + mat1.step1() * mat1.elemSize1() + "[byte]");
            // total number of channels within one step
            Log("mat1.step1() = " + mat1.step1());
            // is the data continuous?
            Log("mat1.isContinuous() = " + mat1.isContinuous());
            // is it a submatrix?
            Log("mat1.isSubmatrix() = " + mat1.isSubmatrix());
            // is the data empty?
            Log("mat1.empty() = " + mat1.empty());

            Log("==============================");

            // 32FC, channels=5, 4x5, 3x4 Submatrix
            Mat mat2 = new Mat(4, 5, CvType.CV_32FC(5));
            OpenCVForUnity.CoreModule.Rect roi_rect = new OpenCVForUnity.CoreModule.Rect(0, 0, 3, 4);
            Mat r1 = new Mat(mat2, roi_rect);

            // number of rows
            Log("r1.rows() = " + r1.rows());
            // number of columns
            Log("r1.cols() = " + r1.cols());
            // number of dimensions
            Log("r1.dims() = " + r1.dims());
            // size
            Log("r1.size() = " + r1.size().width + ", " + r1.size().height);
            // bit depth ID
            Log("r1.depth() = " + r1.depth() + "( = " + CvType.CV_32F + ")");
            // number of channels
            Log("r1.channels() = " + r1.channels());
            // size of one element
            Log("r1.elemSize() = " + r1.elemSize() + "[byte]");
            // size for one channel in one element
            Log("r1.elemSize1() = " + r1.elemSize1() + "[byte]");
            // total number of elements
            Log("r1.total() = " + r1.total());
            // size of step
            Log("r1.step1()*elemSize1() = " + r1.step1() * r1.elemSize1() + "[byte]");
            // total number of channels within one step
            Log("r1.step1() = " + r1.step1());
            // is the data continuous?
            Log("r1.isContinuous() = " + r1.isContinuous());
            // is it a submatrix?
            Log("r1.isSubmatrix() = " + r1.isSubmatrix());
            // is the data empty?
            Log("r1.empty() = " + r1.empty());

            Log("==============================");

            // 32S, channles=2, 2x3x3x4x6 (5 dimensional array)
            int[] sizes = new int[] { 2, 3, 3, 4, 6 };
            Mat mat3 = new Mat(sizes, CvType.CV_32SC2);

            // number of rows
            Log("mat3.rows() = " + mat3.rows());
            // number of columns
            Log("mat3.cols() = " + mat3.cols());
            // number of dimensions
            Log("mat3.dims() = " + mat3.dims());
            // size
            string size = "";
            for (int i = 0; i < mat3.dims(); ++i)
            {
                size += mat3.size(i) + ", ";
            }
            Log("mat3.size() = " + size);
            // bit depth ID
            Log("mat3.depth() = " + mat3.depth() + "( = " + CvType.CV_32S + ")");
            // number of channels
            Log("mat3.channels() = " + mat3.channels());
            // size of one element
            Log("mat3.elemSize() = " + mat3.elemSize() + "[byte]");
            // size for one channel in one element
            Log("mat3.elemSize1() = " + mat3.elemSize1() + "[byte]");
            // total number of elements
            Log("mat3.total() = " + mat3.total());
            // size of step
            string step = "";
            for (int i = 0; i < mat3.dims(); ++i)
            {
                step += mat3.step1(i) * mat3.elemSize1() + ", ";
            }
            Log("mat3.step1()*elemSize1() = " + step + "[byte]");
            // total number of channels within one step
            Log("mat3.step1() = " + mat3.step1());
            // is the data continuous?
            Log("mat3.isContinuous() = " + mat3.isContinuous());
            // is it a submatrix?
            Log("mat3.isSubmatrix() = " + mat3.isSubmatrix());
            // is the data empty?
            Log("mat3.empty() = " + mat3.empty());

            Log("==============================");

            // 0D scalar vs empty Mat (both can report dims==0)
            Mat emptyMat = new Mat();
            Mat scalar0d = new Mat(Array.Empty<int>(), CvType.CV_8UC1, new Scalar(7));
            Log("emptyMat.dims() = " + emptyMat.dims());
            Log("emptyMat.empty() = " + emptyMat.empty());
            Log("emptyMat.total() = " + emptyMat.total());
            Log("emptyMat.rows() = " + emptyMat.rows());
            Log("emptyMat.cols() = " + emptyMat.cols());
            Log("scalar0d.dims() = " + scalar0d.dims());
            Log("scalar0d.empty() = " + scalar0d.empty());
            Log("scalar0d.total() = " + scalar0d.total());
            Log("scalar0d.rows() = " + scalar0d.rows());
            Log("scalar0d.cols() = " + scalar0d.cols());
            Log("scalar0d = " + scalar0d.dump());

            Log("==============================");

            // True 1D vs 2D Nx1 (same element count, different dims / step1)
            Mat vec1d = new Mat(new int[] { 4 }, CvType.CV_32FC1, new Scalar(0));
            Mat col2d = new Mat(4, 1, CvType.CV_32FC1, new Scalar(0));
            Log("vec1d.dims() = " + vec1d.dims());
            Log("vec1d.rows() = " + vec1d.rows());
            Log("vec1d.cols() = " + vec1d.cols());
            Log("vec1d.size(0) = " + vec1d.size(0));
            Log("vec1d.total() = " + vec1d.total());
            Log("vec1d.step1() = " + vec1d.step1());
            Log("col2d.dims() = " + col2d.dims());
            Log("col2d.rows() = " + col2d.rows());
            Log("col2d.cols() = " + col2d.cols());
            Log("col2d.total() = " + col2d.total());
            Log("col2d.step1() = " + col2d.step1());
            Log("vec1d.size() = " + vec1d.size().width + ", " + vec1d.size().height + " // Size looks like (N,1) but dims==1");

            

Execution Result:

mat1.rows() = 3
mat1.cols() = 4
mat1.dims() = 2
mat1.size() = 4, 3
mat1.depth() = 6( = 6)
mat1.channels() = 1
mat1.elemSize() = 8[byte]
mat1.elemSize1() = 8[byte]
mat1.total() = 12
mat1.step1()*elemSize1() = 32[byte]
mat1.step1() = 4
mat1.isContinuous() = True
mat1.isSubmatrix() = False
mat1.empty() = False
==============================
r1.rows() = 4
r1.cols() = 3
r1.dims() = 2
r1.size() = 3, 4
r1.depth() = 5( = 5)
r1.channels() = 5
r1.elemSize() = 20[byte]
r1.elemSize1() = 4[byte]
r1.total() = 12
r1.step1()*elemSize1() = 100[byte]
r1.step1() = 25
r1.isContinuous() = False
r1.isSubmatrix() = True
r1.empty() = False
==============================
mat3.rows() = -1
mat3.cols() = -1
mat3.dims() = 5
mat3.size() = 2, 3, 3, 4, 6, 
mat3.depth() = 4( = 4)
mat3.channels() = 2
mat3.elemSize() = 8[byte]
mat3.elemSize1() = 4[byte]
mat3.total() = 432
mat3.step1()*elemSize1() = 1728, 576, 192, 48, 8, [byte]
mat3.step1() = 432
mat3.isContinuous() = True
mat3.isSubmatrix() = False
mat3.empty() = False
==============================
emptyMat.dims() = 0
emptyMat.empty() = True
emptyMat.total() = 0
emptyMat.rows() = 0
emptyMat.cols() = 0
scalar0d.dims() = 0
scalar0d.empty() = False
scalar0d.total() = 1
scalar0d.rows() = 1
scalar0d.cols() = 1
scalar0d = [  7]
==============================
vec1d.dims() = 1
vec1d.rows() = 1
vec1d.cols() = 4
vec1d.size(0) = 4
vec1d.total() = 4
vec1d.step1() = 1
col2d.dims() = 2
col2d.rows() = 4
col2d.cols() = 1
col2d.total() = 4
col2d.step1() = 1
vec1d.size() = 4, 1 // Size looks like (N,1) but dims==1

four arithmetic operation example

Example Code:


            // ---------------------------------------------------------------------------------------
            //  four arithmetic operation example
            // ---------------------------------------------------------------------------------------
            // Performs four arithmetic methods on matrices.
            // This demo uses 2D Mats; element-wise Core ops also work on matching shapes (use total() for element count on any rank).
            //

            // 3x3 matrix
            Mat m1 = new Mat(3, 3, CvType.CV_64FC1);
            m1.put(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
            Mat m2 = new Mat(3, 3, CvType.CV_64FC1);
            m2.put(0, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18);
            // Scalar
            Scalar s = new Scalar(5);
            // alpha
            double alpha = 3;

            Log("m1 = " + m1.dump());
            Log("m2 = " + m2.dump());
            Log("s = " + s);
            Log("alpha = " + alpha);

            Mat mat_dst = new Mat();

            // Addition, subtraction, negation: A+B, A-B, A+s, A-s, s+A, s-A, -A
            Core.add(m1, m2, mat_dst);
            Log("m1+m2 = " + mat_dst.dump());
            Core.add(m1, s, mat_dst);
            Log("m1+s = " + mat_dst.dump());

            Core.subtract(m1, m2, mat_dst);
            Log("m1-m2 = " + mat_dst.dump());
            Core.subtract(m1, s, mat_dst);
            Log("m1-s = " + mat_dst.dump());

            Core.multiply(m1, Scalar.all(-1), mat_dst);
            Log("-m1 = " + mat_dst.dump());

            // Scaling: A*alpha A/alpha
            Core.multiply(m1, Scalar.all(3), mat_dst);
            Log("m1*alpha = " + mat_dst.dump());
            Core.divide(m1, Scalar.all(3), mat_dst);
            Log("m1/alpha = " + mat_dst.dump());

            // Per-element multiplication and division: A.mul(B), A/B, alpha/A
            Log("m1.mul(m2) = " + (m1.mul(m2)).dump());

            Core.divide(m1, m2, mat_dst);
            Log("m1/m2 = " + mat_dst.dump());

            Core.divide(new Mat(m1.size(), m1.type(), Scalar.all(3)), m1, mat_dst);
            Log("alpha/m2 = " + mat_dst.dump());

            // Matrix multiplication: A*B
            Core.gemm(m1, m2, 1, new Mat(), 0, mat_dst);
            Log("m1*m2 = " + mat_dst.dump());

            // Bitwise logical operations: A logicop B, A logicop s, s logicop A, ~A, where logicop is one of :  &, |, ^.
            Core.bitwise_and(m1, m2, mat_dst);
            Log("m1&m2 = " + mat_dst.dump());

            Core.bitwise_or(m1, m2, mat_dst);
            Log("m1|m2 = " + mat_dst.dump());

            Core.bitwise_xor(m1, m2, mat_dst);
            Log("m1^m2 = " + mat_dst.dump());

            Core.bitwise_not(m1, mat_dst);
            Log("~m1 = " + mat_dst.dump());
            

Execution Result:

m1 = [1, 2, 3;
 4, 5, 6;
 7, 8, 9]
m2 = [10, 11, 12;
 13, 14, 15;
 16, 17, 18]
s = [5, 0, 0, 0]
alpha = 3
m1+m2 = [11, 13, 15;
 17, 19, 21;
 23, 25, 27]
m1+s = [6, 7, 8;
 9, 10, 11;
 12, 13, 14]
m1-m2 = [-9, -9, -9;
 -9, -9, -9;
 -9, -9, -9]
m1-s = [-4, -3, -2;
 -1, 0, 1;
 2, 3, 4]
-m1 = [-1, -2, -3;
 -4, -5, -6;
 -7, -8, -9]
m1*alpha = [3, 6, 9;
 12, 15, 18;
 21, 24, 27]
m1/alpha = [0.3333333333333333, 0.6666666666666666, 1;
 1.333333333333333, 1.666666666666667, 2;
 2.333333333333333, 2.666666666666667, 3]
m1.mul(m2) = [10, 22, 36;
 52, 70, 90;
 112, 136, 162]
m1/m2 = [0.1, 0.1818181818181818, 0.25;
 0.3076923076923077, 0.3571428571428572, 0.4;
 0.4375, 0.4705882352941176, 0.5]
alpha/m2 = [3, 1.5, 1;
 0.75, 0.6, 0.5;
 0.4285714285714285, 0.375, 0.3333333333333333]
m1*m2 = [84, 90, 96;
 201, 216, 231;
 318, 342, 366]
m1&m2 = [4.450147717014403e-308, 2, 3;
 2, 2.5, 3;
 4, 8, 9]
m1|m2 = [nan(snan), 11, 12;
 26, 28, 30;
 28, 17, 18]
m1^m2 = [5.617791046444737e+307, 6.118953110894804e-308, 4.450147717014403e-308;
 1.446298008029681e-307, 1.335044315104321e-307, 1.223790622178961e-307;
 7.787758504775205e-308, 2.364140974663901e-308, 2.225073858507201e-308]
~m1 = [-4, -2, -1.5;
 -0.9999999999999999, -0.8749999999999999, -0.7499999999999999;
 -0.6249999999999999, -0.4999999999999999, -0.4687499999999999]

convertTo example

Example Code:


            // ---------------------------------------------------------------------------------------
            //  convertTo example
            // ---------------------------------------------------------------------------------------
            // Mat.convertTo changes depth (and optional alpha/beta scale) while keeping the same shape
            // (including 0D/1D/ND). Channel count stays the same unless you change type to another Cn.
            //

            // 64F, channels=1, 3x3
            Mat m1 = new Mat(3, 3, CvType.CV_64FC1);
            m1.put(0, 0, 1.1, 1.2, 1.3, 2.1, 2.2, 2.3, 3.1, 3.2, 3.3);
            Log("m1 = " + m1.dump());

            // 64F -> 8U (dst mat, type)
            Mat m2 = new Mat();
            m1.convertTo(m2, CvType.CV_8U);
            Log("m2 = " + m2.dump());

            // 64F -> 8U (dst mat, type, scale factor, added to the scaled value)
            Mat m3 = new Mat();
            m1.convertTo(m3, CvType.CV_8U, 2, 10);
            Log("m3 = " + m3.dump());
            

Execution Result:

m1 = [1.1, 1.2, 1.3;
 2.1, 2.2, 2.3;
 3.1, 3.2, 3.3]
m2 = [  1,   1,   1;
   2,   2,   2;
   3,   3,   3]
m3 = [ 12,  12,  13;
  14,  14,  15;
  16,  16,  17]

reshape example

Example Code:


            // ---------------------------------------------------------------------------------------
            //  reshape example
            // ---------------------------------------------------------------------------------------
            // Changes the shape and/or the number of channels of a  matrix without copying the data.
            // The method makes a new matrix header for this elements.The new matrix may have a different size and / or different number of channels.Any combination is possible if:
            // - No extra elements are included into the new matrix and no elements are excluded.Consequently, the product rows* cols*channels() must stay the same after the transformation.
            // - No data is copied.That is, this is an O(1) operation.Consequently, if you change the number of rows, or the operation changes the indices of elements row in some other way, the matrix must be continuous.See "Mat.isContinuous".
            //

            // 64F, channels=1, 3x4
            Mat m1 = new Mat(3, 4, CvType.CV_64FC1);
            m1.put(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
            Log("m1 = " + m1.dump());
            Log("m1.channels() = " + m1.channels());

            // channels=1, 3x4 -> channels=2, 3x2
            Mat m2 = m1.reshape(2);
            Log("m2 = " + m2.dump());
            Log("m2.channels() = " + m2.channels());

            // channels=1, 3x4 -> channels=1, 2x6
            Mat m3 = m1.reshape(1, 2);
            Log("m3 = " + m3.dump());
            Log("m3.channels() = " + m3.channels());

            // 2D -> 4D
            Mat src = new Mat(6, 5, CvType.CV_8UC3, new Scalar(0));
            Mat m4 = src.reshape(1, new int[] { 1, src.channels() * src.cols(), 1, src.rows() });
            Log("m4.dims() = " + m4.dims());
            string size = "";
            for (int i = 0; i < m4.dims(); ++i)
            {
                size += m4.size(i) + ", ";
            }
            Log("m4.size() = " + size);
            Log("m4.channels() = " + m4.channels());

            // 3D -> 2D
            src = new Mat(new int[] { 4, 6, 7 }, CvType.CV_8UC3, new Scalar(0));
            Mat m5 = src.reshape(1, new int[] { src.channels() * src.size(2), src.size(0) * src.size(1) });
            Log("m5 = " + m5);
            Log("m5.channels() = " + m5.channels());

            // 1D <-> 2D (reshape changes dims; keep total()*channels the same)
            Mat v1d = new Mat(new int[] { 6 }, CvType.CV_8UC1, new Scalar(0));
            v1d.put(0, 0, new byte[] { 1, 2, 3, 4, 5, 6 });
            Mat vAs2d = v1d.reshape(1, new int[] { 2, 3 });
            Mat backTo1d = vAs2d.reshape(1, new int[] { 6 });
            Log("vAs2d.dims() = " + vAs2d.dims());
            Log("vAs2d = " + vAs2d.dump());
            Log("backTo1d.dims() = " + backTo1d.dims());
            Log("backTo1d = " + backTo1d.dump());

            // 0D scalar -> 1x1 2D (handy when an API expects a 2D header)
            Mat s0d = new Mat(Array.Empty<int>(), CvType.CV_8UC1, new Scalar(7));
            Mat s11 = s0d.reshape(1, new int[] { 1, 1 });
            Log("s11.dims() = " + s11.dims());
            Log("s11 = " + s11.dump());
            

Execution Result:

m1 = [1, 2, 3, 4;
 5, 6, 7, 8;
 9, 10, 11, 12]
m1.channels() = 1
m2 = [1, 2, 3, 4;
 5, 6, 7, 8;
 9, 10, 11, 12]
m2.channels() = 2
m3 = [1, 2, 3, 4, 5, 6;
 7, 8, 9, 10, 11, 12]
m3.channels() = 1
m4.dims() = 4
m4.size() = 1, 15, 1, 6, 
m4.channels() = 1
m5 = Mat [ 21*24*CV_8UC1, isCont=True, isSubmat=False, nativeObj=0x1819712920336, dataAddr=0x1807042803200 ]
m5.channels() = 1
vAs2d.dims() = 2
vAs2d = [  1,   2,   3;
   4,   5,   6]
backTo1d.dims() = 1
backTo1d = [  1,   2,   3,   4,   5,   6]
s11.dims() = 2
s11 = [  7]

transpose example

Example Code:


            // ---------------------------------------------------------------------------------------
            //  transpose example
            // ---------------------------------------------------------------------------------------
            // The Core.transpose function can be used for various image processing tasks such as rotating images by 90 degrees and changing the shape of matrices by swapping rows and columns of Mat.
            // - The Core.transpose function is a function that performs a transposition operation on a two-dimensional matrix.
            // - The Core.transposeND function is a function that performs a transposition operation on a tensor of arbitrary dimensions.For example, it can be used to swap specific dimensions of a 3D tensor(such as video data).
            //

            // Transposes a matrix.
            // 8U, channels=1, 3x4
            Mat m1 = new Mat(3, 4, CvType.CV_8UC1);
            m1.put(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
            Log("m1 = " + m1.dump());

            // [3x4] -> [4x3]
            Mat m1_t = new Mat();
            Core.transpose(m1, m1_t);
            Log("Core.transpose(m1, m1_t) = " + m1_t.dump());

            // Transpose for n-dimensional matrices.
            // 32F, channels=1, 1x3x4x3
            Mat m2 = new Mat(new int[] { 1, 3, 4, 3 }, CvType.CV_32FC1);
            m2.put(new int[] { 0, 0, 0, 0 }, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
            string m2_size = "";
            for (int i = 0; i < m2.dims(); ++i)
            {
                m2_size += m2.size(i) + ", ";
            }
            Log("m2 = " + m2.reshape(3, new int[] { 3, 4 }).dump());
            Log("m2 size[] = " + m2_size);

            // [1x3x4x3] -> [1x4x3x3]
            Mat m2_t = new Mat();
            MatOfInt order = new MatOfInt(0, 2, 1, 3); // See MatOf* example: typed 1D int Mat for permute order
            Core.transposeND(m2, order, m2_t);
            string m2_t_size = "";
            for (int i = 0; i < m2_t.dims(); ++i)
            {
                m2_t_size += m2_t.size(i) + ", ";
            }
            Log("Core.transposeND(m2, m2_t) = " + m2_t.reshape(3, new int[] { 4, 3 }).dump());
            Log("m2_t size[] = " + m2_t_size);
            

Execution Result:

m1 = [  1,   2,   3,   4;
   5,   6,   7,   8;
   9,  10,  11,  12]
Core.transpose(m1, m1_t) = [  1,   5,   9;
   2,   6,  10;
   3,   7,  11;
   4,   8,  12]
m2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12;
 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12;
 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
m2 size[] = 1, 3, 4, 3, 
Core.transposeND(m2, m2_t) = [1, 2, 3, 1, 2, 3, 1, 2, 3;
 4, 5, 6, 4, 5, 6, 4, 5, 6;
 7, 8, 9, 7, 8, 9, 7, 8, 9;
 10, 11, 12, 10, 11, 12, 10, 11, 12]
m2_t size[] = 1, 4, 3, 3, 

range example

Example Code:


            // ---------------------------------------------------------------------------------------
            //  range example
            // ---------------------------------------------------------------------------------------
            // Mat.rowRange and Mat.colRange efficiently extract submatrices from a Mat by creating new Mat headers that point to specified row or column ranges of the original data, without copying the underlying data.
            //

            // 64F, channels=1, 3x3
            Mat m1 = new Mat(3, 3, CvType.CV_64FC1);
            m1.put(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
            Log("m1 = " + m1.dump());

            // all rows
            Log("m1.rowRange(Range.all()) = " + m1.rowRange(Range.all()).dump());

            // rowRange(0,2)
            Log("m1.rowRange(new Range(0,2)) = " + m1.rowRange(new Range(0, 2)).dump());

            // row(0)
            Log("m1.row(0) = " + m1.row(0).dump());

            // all cols
            Log("m1.colRange(Range.all()) = " + m1.colRange(Range.all()).dump());

            // colRange(0,2)
            Log("m1.colRange(new Range(0,2)) = " + m1.colRange(new Range(0, 2)).dump());

            // col(0)
            Log("m1.col(0) = " + m1.col(0).dump());

            // True 1D: prefer colRange / submat(Range[]); rowRange is a 2D-oriented API.
            Mat v = new Mat(new int[] { 5 }, CvType.CV_8UC1);
            v.put(0, 0, new byte[] { 10, 20, 30, 40, 50 });
            Log("v = " + v.dump());
            Log("v.colRange(1,4) = " + v.colRange(1, 4).dump());
            Log("v.submat(new Range[] { new Range(2, 5) }) = " + v.submat(new Range[] { new Range(2, 5) }).dump());
            

Execution Result:

m1 = [1, 2, 3;
 4, 5, 6;
 7, 8, 9]
m1.rowRange(Range.all()) = [1, 2, 3;
 4, 5, 6;
 7, 8, 9]
m1.rowRange(new Range(0,2)) = [1, 2, 3;
 4, 5, 6]
m1.row(0) = [1, 2, 3]
m1.colRange(Range.all()) = [1, 2, 3;
 4, 5, 6;
 7, 8, 9]
m1.colRange(new Range(0,2)) = [1, 2;
 4, 5;
 7, 8]
m1.col(0) = [1;
 4;
 7]
v = [ 10,  20,  30,  40,  50]
v.colRange(1,4) = [ 20,  30,  40]
v.submat(new Range[] { new Range(2, 5) }) = [ 30,  40,  50]

submatrix (ROI) example

Example Code:


            // ---------------------------------------------------------------------------------------
            //  submatrix (ROI) example
            // ---------------------------------------------------------------------------------------
            // A submatrix (Region of Interest, ROI) is a region cut out of an image or matrix. OpenCV allows you to create a submatrix that manipulates only that region without copying the original data.
            //

            // 3x3 matrix
            Mat m1 = new Mat(3, 3, CvType.CV_64FC1);
            m1.put(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
            Log("m1 = " + m1.dump());

            // Submatrix (ROI) shares the parent Mat's memory; modifying m2 also changes m1.
            Mat m2 = new Mat(m1, new OpenCVForUnity.CoreModule.Rect(0, 0, 2, 2));
            Log("m2 = " + m2.dump());
            Log("m2.submat() = " + m2.submat(0, 2, 0, 2).dump());

            // find the parent matrix size of the submatrix (ROI) m2 and its position in it
            Size wholeSize = new Size();
            Point ofs = new Point();
            m2.locateROI(wholeSize, ofs);
            Log("wholeSize = " + wholeSize.width + "x" + wholeSize.height);
            Log("offset = " + ofs.x + ", " + ofs.y);

            // expand the range of submatrix (ROI)
            m2.adjustROI(0, 1, 0, 1);
            Log("m2.rows() = " + m2.rows());
            Log("m2.cols() = " + m2.cols());
            Log("m2 = " + m2.dump());

            // True 1D submatrix via colRange / submat(Range[]) — remains continuous
            Mat v = new Mat(new int[] { 5 }, CvType.CV_8UC1);
            v.put(0, 0, new byte[] { 10, 20, 30, 40, 50 });
            Mat vSlice = v.colRange(1, 4);
            Log("v = " + v.dump());
            Log("vSlice.dims() = " + vSlice.dims());
            Log("vSlice.isSubmatrix() = " + vSlice.isSubmatrix());
            Log("vSlice.isContinuous() = " + vSlice.isContinuous());
            Log("vSlice = " + vSlice.dump());
            

Execution Result:

m1 = [1, 2, 3;
 4, 5, 6;
 7, 8, 9]
m2 = [1, 2;
 4, 5]
m2.submat() = [1, 2;
 4, 5]
wholeSize = 3x3
offset = 0, 0
m2.rows() = 3
m2.cols() = 3
m2 = [1, 2, 3;
 4, 5, 6;
 7, 8, 9]
v = [ 10,  20,  30,  40,  50]
vSlice.dims() = 1
vSlice.isSubmatrix() = True
vSlice.isContinuous() = True
vSlice = [ 20,  30,  40]