1

Hello Im trying to calculate pixles of each R/G/B and create histogram of some picture, histogram is looking nice but I cannot calculate pixles of each colour. It says the same amount for each colour which I doubt is correct.

Here is my code, Im fairly new to it and Im run out of ideas

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('photo.jpg')
color = ('b','g','r')


qtdBlue = 0
qtdGreen = 0
qtdRed = 0
totalPixels = 0


for i,col in enumerate(color):
    histr = cv.calcHist([img],[i],None,[256],[0,256])
    plt.plot(histr,color = col)
    plt.xlim([0, 256])


    totalPixels+=sum(histr)
    if i==0:
        qtdBlue = sum(histr)
    elif i==1:
        qtdGreen = sum(histr)
    elif i==2:
        qtdRed = sum(histr)


print("Red Quantity")
print(qtdRed)

print("Blue Quantity")
print(qtdBlue)

print("Green Quantity")
print(qtdGreen)

plt.show()
1
  • What are you trying to do? When you sum the intensities they will always have the same value. Commented Jan 21, 2018 at 19:48

2 Answers 2

3

If I have understood you correctly you want to extract the contribution of each colour to your image. Here is how it can be using matplotlib. As you see at the end of the code, the shape (number of pixels) is the same for each colour.

import numpy as np
import matplotlib.pyplot as plt

# Load the image
img = plt.imread('C:\Documents\Roses.jpg')

# Extract each colour channel
red, green, blue = img[:,:,0], img[:,:,1], img[:,:,2]

# Total red+green+blue intensity
intensity = img.sum(axis=2)

# Function to calculate proportion of a certain channel
def colour_frac(color):
    return np.sum(color)/np.sum(intensity)

# Calculate the proportion of each colour
red_fraction = colour_frac(red)
green_fraction = colour_frac(green)
blue_fraction = colour_frac(blue)

sum_colour_fraction = red_fraction + green_fraction + blue_fraction
print('Red fraction: {}'.format(red_fraction))
print('\nGreen fraction: {}'.format(green_fraction))
print('\nBlue fraction: {}'.format(blue_fraction))
print('\nRGB sum: {}'.format(sum_colour_fraction))
print(red.shape == green.shape == blue.shape)

# Output
Red fraction: 0.3798302547713819

Green fraction: 0.33196874775790813

Blue fraction: 0.28820099747071

RGB sum: 1.0

red.shape == green.shape == blue.shape
Out[68]: True

enter image description here

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

This might not answer your question, but I will explain why the results of the sum of your histograms for different channels have the same value. A histogram is all about intensity distribution, which means at the end, alle sums will be same.

Let's look at a more simplified example: A 3x3 image filled with red pixels.

The red channels the intensity count for the bin 255 is 9. In the other two channels (b,g) the intensities are 9 as well but for the bin 0. As you can see the count doesn't change in a histogramm comparision.

Histogram Values:

b = [9, 0, 0, ..., 0]    #0 - 255
g = [9, 0, 0, ..., 0]    #0 - 255
r = [0, 0, 0, ..., 9]    #0 - 255

enter image description here

Anywho: you might actually be interested in the dominant colors of an image

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.