Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I will try to explain. I recommend you to read OpenCV-Python tutorials on histograms. I will give a short explanation below.

Case 1: finding histogram of a single channel, say histogram of intensity values

hist = cv2.calcHist([img],[0],None,[256],[0,256])

You get a simple histogram of 256 bins where each element denotes number of pixels with particular intensity in that image. (Good to calculate histogram of a grayscale image.)

Case 2: finding histogram of two channels, say B&G channels

hist = cv2.calcHist([img],[0],None,[256, 256],[0,256,0,256])

Now you get a 256x256 array, where each element corresponds to number of pixels with particular (B,G) values. For example, hist[50,100] denotes number of pixels who have B=50 and G=100. (Good to calculate Hue-Saturation histogram during backprojection.

Case 3: finding histogram of three channels, say BGR channels

hist = cv2.calcHist([img],[0],None,[256, 256],[0,256,0,256])

Extend the idea from case 2. Now you have 256x256x256 array. hist[50,100,150] denotes number of pixels who have the values B=50, G=100, R=150.

Case 4: How to find histogram of B,G,R channels separately?

Put a for loop for case 1.

color = ('b','g','r')
for i,col in enumerate(color):
    histr = cv2.calcHist([img],[i],None,[256],[0,256])
    plt.plot(histr,color = col)
    plt.xlim([0,256])
plt.show()

I will try to explain. I recommend you to read OpenCV-Python tutorials on histograms. I will give a short explanation below.

Case 1: finding histogram of a single channel, say histogram of intensity values

hist = cv2.calcHist([img],[0],None,[256],[0,256])

You get a simple histogram of 256 bins where each element denotes number of pixels with particular intensity in that image. (Good to calculate histogram of a grayscale image.)

Case 2: finding histogram of two channels, say B&G channels

hist = cv2.calcHist([img],[0],None,[256, 256],[0,256,0,256])

Now you get a 256x256 array, where each element corresponds to number of pixels with particular (B,G) values. For example, hist[50,100] denotes number of pixels who have B=50 and G=100. (Good to calculate Hue-Saturation histogram during backprojection.

Case 3: finding histogram of three channels, say BGR channels

hist = cv2.calcHist([img],[0],None,[256, 256],[0,256,0,256])

Extend the idea from case 2. Now you have 256x256x256 array. array (more like a CUBE). hist[50,100,150] denotes number of pixels who have the values B=50, G=100, R=150.

Case 4: How to find histogram of B,G,R channels separately?

Put a for loop for case 1.

color = ('b','g','r')
for i,col in enumerate(color):
    histr = cv2.calcHist([img],[i],None,[256],[0,256])
    plt.plot(histr,color = col)
    plt.xlim([0,256])
plt.show()