Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Histogram: Get number of pixels for specified range of pixel values-java

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 really appreciate your help as I am new in OpenCV and Java.

Thanks in advance, Ele

Histogram: Get number of pixels for specified range of pixel values-java

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 really appreciate your help as I am new in OpenCV and Java.

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 (used as separate datasets)
    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, Ele

Histogram: Get number of pixels for specified range of pixel values-java

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 really appreciate your help as I am new in OpenCV and Java.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 (used as separate datasets) Scalar[] colorsRgb = new Scalar[]{new Scalar(200, 200, 250, 0)}; MatOfInt[] channels = new MatOfInt[]{new MatOfInt(0)}; 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()); 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); 0);

Thanks in advance, Ele

Histogram: Get number of pixels for specified range of pixel values-java

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, Ele

Histogram: Get number of pixels for specified range of pixel values-java

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)};

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());

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

0);

Thanks in advance, Ele

Histogram: Get number of pixels for specified range of pixel values-java

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, EleEleDiam