Error on creating a wrapper class for Mat Object (Getting “multiple of channels count” error)

205 views Asked by At

I am Using a Wrapper Class in Java for Mat Object and am getting an error on the following line mat.put(0, 0, data); as

Caught Error: java.lang.UnsupportedOperationException: Provided data element number (0) should be multiple of the Mat channels count (4)

The following is the code which I have used

import java.io.Serializable;

public class MatWrapper implements Serializable {
    int rows;
    int cols;
    int type;
    byte[] data;

    public MatWrapper() {
    }

    public MatWrapper(Mat mat)
    {
        if (mat.isContinuous()) {

            int elemSize = (int) mat.elemSize();
            rows = mat.rows();
            cols = mat.cols();
            type = mat.type();

            byte[] data = new byte[cols * rows * elemSize];
            mat.get(0, 0, data);
        }
    }

    public Mat toMat()
    {
        Mat mat = new Mat(rows, cols, type);
        mat.put(0, 0, data);
        return mat;
    }
}

For Using it I am making following calls:

// convert Mat object to a wrapper object
MatWrapper wrapper = new MatWrapper(mat);

// this wrapper object is serializable

Mat mat = wrapper.toMat();
0

There are 0 answers