Ask Your Question

Converting histogram Mat to BitMap - Getting OpenCV Error (src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4)

asked 2016-06-26 09:15:09 -0500

Solace gravatar image

updated 2016-06-26 09:17:38 -0500

I am trying to calculate the histogram from this image and trying to convert it to a Bitmap and display it.

image description

And I am getting the following error. The question is why, and how can I fix it?

06-26 14:07:18.972: E/cv::error()(1278): 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 /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp, line 98
06-26 14:07:18.992: A/libc(1278): Fatal signal 11 (SIGSEGV) at 0x997fcff4 (code=2), thread 1278 (tests.testfive)

Following is the relevant part of my code.

    //Get the image from file path
    rgbaMat = new Mat();
    rgbaMat = Highgui.imread(filePath);

    Bitmap objectSubmatBitmap = Bitmap.createBitmap(rgbaMat.cols(), rgbaMat.rows(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(rgbaMat, objectSubmatBitmap);

    //Preprocessing - color space conversion
    Imgproc.cvtColor(rgbaMat, rgbaMat, Imgproc.COLOR_BGR2RGB);
    Mat hsvMat = new Mat();
    Imgproc.cvtColor(rgbaMat, hsvMat, Imgproc.COLOR_RGB2HSV);

    //Calculate Histogram
    List<Mat> sourceImageList = new ArrayList<Mat>();
    Mat histogram = new Mat();
    //histogramMat.convertTo(histogramMat, CvType.CV_8UC1);
    Imgproc.calcHist(sourceImageList, new MatOfInt(2), new Mat(), histogram, new MatOfInt(256), 
                    new MatOfFloat(0f, 256f));

    //Highgui.imwrite("/mnt/sdcard/histogram.bmp", histogram);// check
    Bitmap histogramBitmap = 
    Bitmap.createBitmap(histogram.cols(), histogram.rows(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(histogram, histogramBitmap);
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-06-28 00:02:26 -0500

berak gravatar image

the histogram in your case is a single 256 x 1 float Mat (not ARGB_8888).

apart from the type, i don't think, it makes any sense, to convert that to a Bitmap (it's a single row only).

instead, e.g. make a new 256 x 256 uchar image, and draw something like bars into it, like in this example

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2016-06-26 09:15:09 -0500

Seen: 1,826 times

Last updated: Jun 28 '16