Pragyan93
Pragyan93

Reputation: 721

How to convert Mat to Interger Array in OpenCV java?

any suggestion on how to convert a Mat to an Integer array in OpenCV. I tried one of the suggestions from http://answers.opencv.org/question/33596/convert-mat-to-byte-in-c/ but it gives me the following error: "java.lang.UnsupportedOperationException: Mat data type is not compatible: 24" Please let me know if I am doing something wrong here. Here is my code below:

    public void onCameraViewStarted(int width, int height) {
    mRgba = new Mat(width, height, CvType.CV_32SC4);
    mYuv  = new Mat(width,height,CvType.CV_8SC4);
}

public void onCameraViewStopped() {
    mRgba.release();
    mYuv.release();
}

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    mRgba = inputFrame.rgba();
    Imgproc.cvtColor(mRgba,mYuv,Imgproc.COLOR_RGBA2YUV_IYUV);

    int[] rgba = new int[(int)(mRgba.total()*mRgba.channels())];
    mRgba.get(0,0,rgba);

    byte[] yuv = new byte[(int)(mYuv.total()*mYuv.channels())];
    mYuv.get(0,0,yuv);

    FindFeatures(mRgba.width(),mRgba.height(),yuv,rgba);

    return mRgba;
}

And when I try to convert using this piece of code:

    MatOfInt r = new MatOfInt();
    mRgba.assignTo(r);
    int[] rgba = r.toArray();

    MatOfByte y = new MatOfByte();
    mYuv.assignTo(y);
    byte[] yuv = y.toArray();

I get this error: " java.lang.RuntimeException: Native Mat has unexpected type or size: Mat [ 480*640*CV_8UC4, isCont=true, isSubmat=false, nativeObj=0x7ffc35926ca0, dataAddr=0x7ffc35a09010 ]"

Upvotes: 2

Views: 9985

Answers (1)

Pragyan93
Pragyan93

Reputation: 721

This is how I solve it :

    byte[] yuv = new byte[(int)(mYuv.total()*mYuv.channels())];
    mYuv.get(0,0,yuv);

    MatOfInt rgb = new MatOfInt(CvType.CV_32S);
    mRgba.convertTo(rgb,CvType.CV_32S);
    int[] rgba = new int[(int)(rgb.total()*rgb.channels())];
    rgb.get(0,0,rgba);

Upvotes: 2

Related Questions