First time here? Check out the FAQ!

Ask Your Question
0

Trying to calculate histogram on Android and find the median. Unsure how to access histogram data from Mat

asked Nov 1 '12

Jdban gravatar image

updated Nov 1 '12

I'm trying to find the median, which I believe can be done by using the code given here: http://tech.dir.groups.yahoo.com/group/OpenCV/message/23809

What I have so far in java is the histogram calculation working fine (I assume), and at that point, I don't know what to put insude my calcMedianOfHist function. The issue I'm running into is that I don't know how to access the bins of the histogram. The closest I've come to finding out how to do this is looking at this example for C code, but that doesn't apply to the Java. (http://docs.opencv.org/2.4.3rc/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html)

The method there says to do hist.at<float>(num), but I cannot do that in java.

I'm also not 100% sure if I should be normalizing the hist like this or not.

Any help is appreciated, thank you.

        ArrayList<Mat> list = new ArrayList<Mat>();
        list.add(mGraySubmat);

        MatOfInt one = new MatOfInt(0);
        int median = 0;

        hist = new Mat();

        MatOfInt histSize = new MatOfInt(25);
        MatOfFloat range = new MatOfFloat(0f, 256f);

        Imgproc.calcHist(Arrays.asList(mGraySubmat), one, new Mat(), hist, histSize, range);

        Core.normalize(hist, hist);

        median = calcMedianOfHist(hist, mGraySubmat.cols(), mGraySubmat.rows());

        System.out.println("Median is: " + median);
Preview: (hide)

Comments

hist.at<float>(num), is the same as Mat.get I believe. The links in your question are broken otherwise I would try to answer a bit more

ejehardenberg gravatar imageejehardenberg (Sep 2 '14)edit

1 answer

Sort by » oldest newest most voted
0

answered Feb 8 '18

kaba gravatar image

Check the dimensions of your hist, it should be one-dimensional.

The following should give you access to the values:

for( int x=0; x<25; x++ )   // 25 being your histSize
    double y = hist.get(x,0)[0];

(And as ejehardenberg already remarked: please update the links you provided.)

Preview: (hide)

Comments

" please update the links you provided." this question was posted in 2012!

LBerger gravatar imageLBerger (Feb 8 '18)edit
1

@LBerger Ah, right, didn't see that, sorry.

kaba gravatar imagekaba (Feb 8 '18)edit

Question Tools

1 follower

Stats

Asked: Nov 1 '12

Seen: 3,778 times

Last updated: Feb 08 '18