OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Thu, 08 Feb 2018 07:55:45 -0600Trying to calculate histogram on Android and find the median. Unsure how to access histogram data from Mathttp://answers.opencv.org/question/3650/trying-to-calculate-histogram-on-android-and-find-the-median-unsure-how-to-access-histogram-data-from-mat/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);Thu, 01 Nov 2012 00:10:52 -0500http://answers.opencv.org/question/3650/trying-to-calculate-histogram-on-android-and-find-the-median-unsure-how-to-access-histogram-data-from-mat/Comment by ejehardenberg for <p>I'm trying to find the median, which I believe can be done by using the code given here: <a href="http://tech.dir.groups.yahoo.com/group/OpenCV/message/23809">http://tech.dir.groups.yahoo.com/group/OpenCV/message/23809</a></p>
<p>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. (<a href="http://docs.opencv.org/2.4.3rc/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html">http://docs.opencv.org/2.4.3rc/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html</a>)</p>
<p>The method there says to do hist.at<float>(num), but I cannot do that in java.</p>
<p>I'm also not 100% sure if I should be normalizing the hist like this or not.</p>
<p>Any help is appreciated, thank you. </p>
<pre><code> 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);
</code></pre>
http://answers.opencv.org/question/3650/trying-to-calculate-histogram-on-android-and-find-the-median-unsure-how-to-access-histogram-data-from-mat/?comment=40890#post-id-40890hist.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 moreTue, 02 Sep 2014 09:18:56 -0500http://answers.opencv.org/question/3650/trying-to-calculate-histogram-on-android-and-find-the-median-unsure-how-to-access-histogram-data-from-mat/?comment=40890#post-id-40890Answer by kaba for <p>I'm trying to find the median, which I believe can be done by using the code given here: <a href="http://tech.dir.groups.yahoo.com/group/OpenCV/message/23809">http://tech.dir.groups.yahoo.com/group/OpenCV/message/23809</a></p>
<p>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. (<a href="http://docs.opencv.org/2.4.3rc/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html">http://docs.opencv.org/2.4.3rc/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html</a>)</p>
<p>The method there says to do hist.at<float>(num), but I cannot do that in java.</p>
<p>I'm also not 100% sure if I should be normalizing the hist like this or not.</p>
<p>Any help is appreciated, thank you. </p>
<pre><code> 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);
</code></pre>
http://answers.opencv.org/question/3650/trying-to-calculate-histogram-on-android-and-find-the-median-unsure-how-to-access-histogram-data-from-mat/?answer=184238#post-id-184238Check 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.)Thu, 08 Feb 2018 06:51:13 -0600http://answers.opencv.org/question/3650/trying-to-calculate-histogram-on-android-and-find-the-median-unsure-how-to-access-histogram-data-from-mat/?answer=184238#post-id-184238Comment by kaba for <p>Check the dimensions of your <code>hist</code>, it should be one-dimensional.</p>
<p>The following should give you access to the values:</p>
<pre><code>for( int x=0; x<25; x++ ) // 25 being your histSize
double y = hist.get(x,0)[0];
</code></pre>
<p>(And as ejehardenberg already remarked: please update the links you provided.)</p>
http://answers.opencv.org/question/3650/trying-to-calculate-histogram-on-android-and-find-the-median-unsure-how-to-access-histogram-data-from-mat/?comment=184245#post-id-184245@LBerger Ah, right, didn't see that, sorry.Thu, 08 Feb 2018 07:55:45 -0600http://answers.opencv.org/question/3650/trying-to-calculate-histogram-on-android-and-find-the-median-unsure-how-to-access-histogram-data-from-mat/?comment=184245#post-id-184245Comment by LBerger for <p>Check the dimensions of your <code>hist</code>, it should be one-dimensional.</p>
<p>The following should give you access to the values:</p>
<pre><code>for( int x=0; x<25; x++ ) // 25 being your histSize
double y = hist.get(x,0)[0];
</code></pre>
<p>(And as ejehardenberg already remarked: please update the links you provided.)</p>
http://answers.opencv.org/question/3650/trying-to-calculate-histogram-on-android-and-find-the-median-unsure-how-to-access-histogram-data-from-mat/?comment=184243#post-id-184243" please update the links you provided." this question was posted in 2012!Thu, 08 Feb 2018 07:47:01 -0600http://answers.opencv.org/question/3650/trying-to-calculate-histogram-on-android-and-find-the-median-unsure-how-to-access-histogram-data-from-mat/?comment=184243#post-id-184243