Finding Largest Histogram in Android

asked 2018-06-15 11:27:06 -0600

Vishudh gravatar image

Hello, I am trying to find the largest histogram to get the mode of color of a Mat. This link gives only C++ example. But then I got another here. My code is

private Scalar histMode(Mat patchRgba){
        Size patchSize = patchRgba.size();

        int histSize = 256;
        MatOfInt histogramSize = new MatOfInt(histSize);
        int histogramHeight = (int) patchSize.height;

        Scalar maxValues = new Scalar(0,0,0);
        MatOfFloat histogramRange = new MatOfFloat(0f, 256f);
        Scalar[] colorsRgb = new Scalar[]{new Scalar(200, 0, 0, 255), new Scalar(0, 200, 0, 255), new Scalar(0, 0, 200, 255)};
        MatOfInt[] channels = new MatOfInt[]{new MatOfInt(0), new MatOfInt(1), new MatOfInt(2)};
        Mat[] histograms = new Mat[]{new Mat(), new Mat(), new Mat()};
        for (int i = 0; i < channels.length; i++) {
            Imgproc.calcHist(Collections.singletonList(patchRgba), channels[i], new Mat(), histograms[i], histogramSize, histogramRange);
            Core.normalize(histograms[i], histograms[i], histogramHeight, 0, Core.NORM_INF);
            maxValues.val[i] = Core.minMaxLoc(histograms[i]).maxVal;
        }
        return maxValues;
    }

But I am not getting the intended values. What I am getting is [192,192,192] or [150,150,150]... like that. Same value for all the channels. Thanks in Advance

edit retag flag offensive close merge delete

Comments

could you explain, why you are doing this ? what are you trying to achieve here ?

a Scalar made from the most prominent individual channel histograms is not a color existing in your image.

berak gravatar imageberak ( 2018-06-15 11:54:11 -0600 )edit
1

I have to match a color with a palette of colors. So when I take the average color of the contour the resulting color turns out to be very dark compared to what we see. So I wanted to take the mode of the contour area, as someone suggested it would be better than average color. I read on some article which suggested taking the largest histogram would be equal to median. I was trying to achieve the same

Vishudh gravatar imageVishudh ( 2018-06-15 14:52:20 -0600 )edit