Ask Your Question

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

asked 2012-11-01 00:10:52 -0500

Jdban gravatar image

updated 2012-11-01 00:24:05 -0500

I'm trying to find the median, which I believe can be done by using the code given here:

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. (

The method there says to do<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>();

        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);
edit retag flag offensive close merge delete

Comments<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 ( 2014-09-02 09:18:56 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-02-08 06:51:13 -0500

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

edit flag offensive delete link more


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

LBerger gravatar imageLBerger ( 2018-02-08 07:47:01 -0500 )edit

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

kaba gravatar imagekaba ( 2018-02-08 07:55:45 -0500 )edit

Question Tools

1 follower


Asked: 2012-11-01 00:10:52 -0500

Seen: 3,201 times

Last updated: Feb 08 '18