Reputation:
I want to find the mean value for each channel (R,G,B) from a histogram. The histogram I'm working with is from a cropped area of a picture. I'm using Python and openCV, but I'm willing to use a different library.
So first I load the image and then crop the area of interest. Then I use calcHist for each channel (BGR). Here's the Histogram for the Blue Channel. So from this the mean value should be around 140. But when using histrB.mean() for the Blue Channel I get 17. What I found is that mean() is doing the following: sum(histB)/len(np.count_nonzero(histR)). Each hist is a vector with a length of 256 and has a lot of zeros, just like it's hown in the picture. Here's my code so far:
image='image.jpg'
img= cv.imread(image)
areaofinterest = img[145:193, 430:455]
histB = cv.calcHist([areaofinterest],[0],None,[256],[0,256])
histG = cv.calcHist([areaofinterest],[1],None,[256],[0,256])
histR = cv.calcHist([areaofinterest],[2],None,[256],[0,256])
I also wanted to find the max value from each channel and used max(histG) which actually returned the max value from the vector.
Thank you in advance
Upvotes: 0
Views: 2778
Reputation: 28994
A histogram shows you the distribution of values in your image.
histrB.mean()
calculates the mean of the vector histrB which is the sum of all vector elements divided by the number of elements.
This would be the average number of values per bin in your histogram.
If you want to calculate the mean of your blue channel you either calculate it from the image itself or if you want to do it from the histogram you have to multiply each value-bin by the number of elements in that bin and sum those values up and then divide that sum by the number of pixels
Same for the max and min. The max of that histogram would give you the highest pixel count of all bins.
Instead you are looking for the highest bin that has > 0 elements. or the lowest bin with > 0 elements for the minimum blue value.
Make sure you understand what a histogram is!
Upvotes: 1