如何在OpenCV java中将Mat转换为Interger数组?

时间:2016-06-21 04:27:58

标签: java android opencv feature-detection

有关如何在OpenCV中将Mat转换为Integer数组的任何建议。我尝试了http://answers.opencv.org/question/33596/convert-mat-to-byte-in-c/中的一条建议 但它给了我以下错误:" java.lang.UnsupportedOperationException:Mat数据类型不兼容:24" 如果我在这里做错了,请告诉我。 以下是我的代码:

    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;
}

当我尝试使用这段代码进行转换时:

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

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

我收到此错误:" java.lang.RuntimeException:Native Mat具有意外的类型或大小:Mat [480 * 640 * CV_8UC4,isCont = true,isSubmat = false,nativeObj = 0x7ffc35926ca0,dataAddr = 0x7ffc35a09010]"

1 个答案:

答案 0 :(得分:0)

我就是这样解决的:

    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);