在Android中将Mat转换为位图时出现OpenCv错误

时间:2016-07-06 14:52:04

标签: android opencv bitmap

我正在尝试将Mat转换为Bitmap但是收到如下异常:

E/cv::error(): OpenCV Error: Assertion failed (src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4) in void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean), file /Volumes/Linux/builds/master_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp, line 98

以下是将摄像头捕获的位图转换为Mat的代码。

public static Mat calculateHistogram(Bitmap bitmap) {
    MatOfFloat ranges = new MatOfFloat(0f, 255f);
    MatOfInt histSize = new MatOfInt(25);
    Mat imageMat = new Mat(bitmap.getWidth(), bitmap.getHeight(), CvType.CV_8UC1);
    Utils.bitmapToMat(bitmap, imageMat);
    Imgproc.cvtColor(imageMat, imageMat, Imgproc.COLOR_RGB2GRAY);

    Mat histoGramMat = new Mat();

    Imgproc.calcHist(Arrays.asList(imageMat), new MatOfInt(0), new Mat(), histoGramMat,
            histSize, ranges);
    return histoGramMat;
}

现在,当我尝试将此mat转换回位图时,我得到了上面提到的异常。我将mat转换回位图的代码如下:

Bitmap resultBitmap = null;
            try {
                Mat temp = new Mat(comparisonMat.cols(), comparisonMat.rows(), CvType.CV_8UC1);
                comparisonMat.copyTo(temp);
                Imgproc.cvtColor(temp, temp, Imgproc.COLOR_GRAY2RGB);
                resultBitmap = Bitmap.createBitmap(temp.cols(),
                        temp.rows(), Bitmap.Config.ARGB_8888);
                Utils.matToBitmap(temp, resultBitmap, true);
            }
            catch (CvException e){
                Log.d("Exception",e.getMessage());
            }

对此有任何帮助将不胜感激。

0 个答案:

没有答案