In: Computer Science
What to do
This assignment is going to be pretty simple - we're just going compute frequency counts of certain colors in certain ranges (Histogram of the image).
Because there is no natural way of segregating RGB color ranges, we usually do this color by color. So you'll be computing frequency counts for individual color components individually.
The expectation is that you will write a function as described by a python function signature below (while you are free to do whatever you want, I think doing future parts of the assignment will be easier if the code is structured this way) -
Think about what happens when one is comparing images of different sizes -
To make sure similar images have similar histograms regardless of image size, it is good to normalise frequencies by dividing each frequency count by the total number of pixel in the image.
# PLEASE LIKE THE SOLUTION
# FEEL FREE TO DISCUSS IN COMMENT SECTION
# Python Program
# Assure that matplot and cv2 are installed in your ide
used for execution of this program
# if you not require plot of histogram you may remove optional code
mention below and remove import matplotlib.pyplot as
plt
import matplotlib.pyplot as plt
import cv2
# this function calulates frequency and draw histogram
and print in terminal
def draw_histogram(image):
# initilaise the key value pair
freqForRed = {}
freqForGreen = {}
freqForBlue = {}
# get height of image
height =
image.shape[0]
width = image.shape[1]
# initilaising with 0 for all 0 to 255
pixel frequency
for i in range(256):
freqForRed.update({i : 0})
freqForGreen.update({i : 0})
freqForBlue.update({i : 0})
# for each pixel get bgr value and update
frequency
for i in range(0,height):
for j in range(0,width):
color =
image[i][j]
blueValue =
int(color[0])
greenValue =
int(color[1])
redValue =
int(color[2])
# update
red frequency for this pixel
value = freqForBlue[blueValue]
freqForBlue.update({freqForBlue[blueValue] : value + 1})
value =
freqForGreen[greenValue]
freqForGreen.update({freqForGreen[greenValue] : value + 1})
value =
freqForRed[redValue]
freqForRed.update({freqForRed[redValue] : value + 1})
redList = []
blueList = []
greenList = []
# now normalising by dividing by its
size
size = float((height * width))
for i in range(256):
value = freqForRed[i];
redList.append((value/size))
value = freqForGreen[i];
blueList.append((value/size))
value = freqForBlue[i]
greenList.append((value/size))
#print key and normalised
frequency
for i in range(256):
print("Normalized Frequency
Red"+str(i)+" is "+str(redList[i])+" Green"+str(i)+" is
"+str(greenList[i])+" Blue"+str(i)+" is "+str(blueList[i]))
# OPTIONAL CODE FOR PLOTING
# PLOT RED
x =
list(freqForRed.keys())
plt.plot(x, redList)
plt.gcf().canvas.set_window_title("RED")
plt.show()
# PLOT GREEN
plt.plot(x, greenList)
plt.gcf().canvas.set_window_title("GREEN")
plt.show()
# PLOT BLUE
plt.plot(x, blueList)
plt.gcf().canvas.set_window_title("BLUE")
plt.show()
img = cv2.imread('./image.jpg')
draw_histogram(img)
# SAMPLE OUTPUT
# HISTOGRAM