Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Finding Largest Histogram in Android

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