F1sher
F1sher

Reputation: 7310

How to create and display histogram in OpenCV4Android?

Can anybody tell me what am I doing wrong or give me example how to create AND DISPLAY histogram from grayscale mat in OpenCV4 Android? There is my code...

Vector<Mat> bgr_planes = new Vector<Mat>();                                                                                                                                                                                 
Core.split(cleanFaceMatGRAY, bgr_planes);
MatOfInt histSize = new MatOfInt(256);
final MatOfFloat histRange = new MatOfFloat(0f, 256f);
boolean accumulate = false;
Mat b_hist = new  Mat();
Imgproc.calcHist(bgr_planes, new MatOfInt(0),new Mat(), b_hist, histSize, histRange, accumulate);

And then I try to set it to Image view to see it:

    ivPickedPhoto.setImageBitmap(AppTools.createBitmapFromMat(b_hist, Bitmap.Config.ARGB_8888));

Method for display was created on my own:

public class AppTools {

public static Bitmap createBitmapFromMat(Mat mat, Bitmap.Config config) {
    Bitmap bmp2 = null;

    bmp2 = Bitmap.createBitmap(mat.cols(), mat.rows(), config);
    Utils.matToBitmap(mat, bmp2);   
    return bmp2;
}

This gives me an error in the ivPickedPhoto(...) lane:

12-10 21:12:59.910: E/AndroidRuntime(10301): Caused by: CvException [org.opencv.core.CvException: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp:98: error: (-215) src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4 in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean)

I guess my problem is with the way I want to display the histogram... prolly I cant convert it to bmp. What is a good way to show histogram?

Upvotes: 2

Views: 988

Answers (1)

pdresselhaus
pdresselhaus

Reputation: 699

The OpenCV Java Documentation states that the Mat object you are passing should be of type CV_8UC1, CV_8UC3 or CV_8UC4. The documentation of calcHist doesn't really specify which type you should expect your b_Hist to be, but since it is complaining about the Mat type:

error: (-215) src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4

you should probably convert it to either of the three above formats.

Upvotes: 2

Related Questions