Histogram in Opencv

asked 2017-06-20 04:16:16 -0600

Nani gravatar image

I have two questions:

1) I have segmented an image from the background and the result is a colored circle with a black background. When I do histogram using calcHist(), is the result will be the histogram of the colored circle or it will include even the black background.

2) After calculating the histogram for lab image for each channel l, a, and b and calculate the mean for each histogram, it gave me same mean for each channel while the standard deviation and the mode are different. Is this correct or I am doing something wrong.

Any help would be appreciated.

edit retag flag offensive close merge delete

Comments

First question : calcHist calculates histogram using all pixels in image. If you don't want all pixels you have to use a mask

If you want the histogram original of pixels inside connectComponent you can create a specific mask (label 10 for example):

Mat mask=connected==10;

LBerger gravatar imageLBerger ( 2017-06-20 04:28:07 -0600 )edit

Thank you for your comments, I create a mask which is the same size like the image with a white circle instead of colored circle in the src image but not a white rectangle and it gave me different result (less smaller). Is this correct?.

Nani gravatar imageNani ( 2017-06-20 04:35:07 -0600 )edit

When you use a mask, number of pixels in histogram (sum(h(i)) is equal to number of white pixels in mask

LBerger gravatar imageLBerger ( 2017-06-20 04:40:40 -0600 )edit

Thanks. this makes sense. Do you know what is the range of value in histogram for Lab color space. I explored the web and I found the range should be between 0-255 but after calculating the image histogram, the mode is very large around 600.

Nani gravatar imageNani ( 2017-06-20 04:57:30 -0600 )edit

no idea but may be you will an answer here http://docs.opencv.org/master/de/d25/...

how do you find mode ? did you use minMaxLoc?

LBerger gravatar imageLBerger ( 2017-06-20 05:04:20 -0600 )edit

I just loop through the histogram result for each channel:

// Calc mode

            maxVal = l_hist.get(0, 0)[0];
            for (int j = 0; j < 256; j++) {
                double tempVal;
                tempVal = l_hist.get(j, 0)[0];
                if (maxVal < tempVal) {
                    maxVal = tempVal;
                    //index=contourIdx;
                }
            }
Nani gravatar imageNani ( 2017-06-20 05:19:40 -0600 )edit

@Nani, histograms count occurences , not pixel values, so a number of 600 means, 600 pixels had the value of this bin

berak gravatar imageberak ( 2017-06-21 01:54:20 -0600 )edit

@berak, thank you, this is making sense now. I have another inquiry if you do not mind, I used only the parameter hist size = 256 and range 0-255 then what this means 600 pixels had the value of bin 256 or I need to define smaller bin to see which pixels has the highest color in relation to bins. Sorry I seems very confused can you please clear my confusion. Thanks in advance

Nani gravatar imageNani ( 2017-06-21 03:29:35 -0600 )edit

unfortunately,you cannot make the bins "smaller than 1 pixel".

what's wrong about having a count of 600 in the largest bin ?

berak gravatar imageberak ( 2017-06-21 03:34:13 -0600 )edit

there is no wrong, just i would like to understand what it means.

Nani gravatar imageNani ( 2017-06-21 04:52:18 -0600 )edit