Histogram: Get number of pixels for specified range of pixel values-java [closed]
I have a histogram of a grayscale image with a histSize= 256. What I want is to get the sum of the pixels values found in the ranges: 0 - 64 and 70 - 255. I am calling a for loop (i = 0; I < histSize * 0.25) but then I cannot find a way to calculate what I want. I am new in OpenCV and Java so any help would be appreciated.
Code to calculate the histogram (code found here):
Mat lrgba = new Mat();
Utils.bitmapToMat(grayBitmap, lrgba);
//get the bitmap size of the lines array
Size crgbaSize = lrgba.size();
int histSize = 256;
MatOfInt histogramSize = new MatOfInt(histSize); //Set the amount of bars in the histogram
int histogramHeight = (int) crgbaSize.height; //Set the height of the histogram
double getBinLowValue = 0;
double getBinHighValue = 0;
double binWidth = 1.76; //width of the bar for each pixel
MatOfFloat histogramRange = new MatOfFloat(0f, 256f);//Set the value range
//Create two separate lists: one for colors and one for channels
Scalar[] colorsRgb = new Scalar[]{new Scalar(200, 200, 250, 0)};
MatOfInt[] channels = new MatOfInt[]{new MatOfInt(0)};
//Create an array to be saved in the histogram and a second array, on which the histogram chart will be drawn.
Mat[] histograms = new Mat[]{new Mat(), new Mat(), new Mat()};
Mat histMatBitmap = new Mat(crgbaSize, lrgba.type());
//Calculate histogram for each channel, standardize the channels and then draw it
for (int k = 0; k < channels.length; k++) {
Imgproc.calcHist(Collections.singletonList(lrgba), channels[k], new Mat(), histograms[k], histogramSize, histogramRange);
Core.normalize(histograms[k], histograms[k], histogramHeight, 0, Core.NORM_MINMAX, -1, new Mat());//normalize the data
//Draw the chart
for (int l = 0; l < histSize; l++) {
Point h1 = new Point(binWidth * (l - 1), histogramHeight - Math.round(histograms[k].get(l - 1, 0)[0]));
Point h2 = new Point(binWidth * l, histogramHeight - Math.round(histograms[k].get(l, 0)[0]));
Imgproc.line(histMatBitmap, h1, h2, colorsRgb[k], 1, 8, 0);
Thanks in advance, EleDiam
can you show the piece of code, where you calculate the histogram ?
(it's probably something fairly easy, like extracting a range from the histogram mat)
I have added it to my question. Sure I think that this should be something easy but I am getting confused between Mat() and MatOfPoints. Thanks a lot for your quick response
untried (so not an answer yet), but as we can see from the code above, your 3 seperate (per channel) histograms are simple column Mat's. thus:
and:
(and don't get scared by the points, they're only for drawing !)
I have tried it and the sub matrix is created successfully but the sum gives error: required double, found Scalar..
apologies, the sum is ofc. the sum per (expected) channel, a Mat can have more than one.
so in java it is:
Thank you very much it works perfectly now!!!