- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
We used python for creating a histogram of the image. I used the imread() function from the OpenCV library and matplotlib library to plot the histogram of the image. Also, we used the NumPy library to store the pixel value of the image and to do various operations on the image.
Note: At the end of the article, I provided the whole code ...... and also join the telegram channel for more information : Join
This is the input image that I used :
- I used the below function(read_image()) to read the image from the source and convert into a row-major format from a matrix format.
def read_image():
image = cv2.imread("/content/lena_face.jpg")
print(image.shape)
image_1d =image.flatten()
print(image_1d.shape)
return image_1d
To generate a bin, I used one data structure of python that, is Dictionary.
def generate_intensity():
intensity = dict()
for i in range(0,256):
intensity[i] = 0
return intensity
Find the probability for each bin.
def findProbability(intensity,image):
p = [i for i in range(256)]
for i in range(256):
p[i] = intensity[i]/image.shape[0]
return pHere, we generated bins and find the probability for each bin and plot it using the matplotlib library.
#generate the x axis(bins)
x = [i for i in range(256)]
y = [ intensity_1[i] for i in range(256)]
#find the probability for each bins
p =findProbability(intensity_1,image_1)
print(intensity_1)
print(p)
plt.plot(x,p)library
After completing the above task, you should add the 100 constant value to each pixel, plot a histogram for it. Your histogram looks like the one below if you use an input image I used.
If you want to do a different variety of tasks, please go through the below link where all kinds of operations have been explained and copy the whole program then also go through the below link.
Article link :
What kind of arithmetic operation moves the whole histogram of the image to the left-hand side. Can you guess what happens with an image? I have provided the result below for the same input image.
- Divide operation gives result. Once try it and comment below..
What kind of changes will happen while doing down-sampling by factor-2. ?
- If you do the sampling nothing changes happen with the histogram except the count of pixels in each bin. Pixels in the histogram bin decrease respectively, therefore you couldn't see any changes in the histogram of the image.
Code :
#Library import numpy as np import cv2 import math from google.colab.patches import cv2_imshow import matplotlib.pyplot as plt #read image def read_image(): image = cv2.imread("/content/lena_face.jpg") print(image.shape) image_1d =image.flatten() print(image_1d.shape) return image_1d def generate_intensity(): intensity = dict() for i in range(0,256): intensity[i] = 0 return intensity def findProbability(intensity,image): p = [i for i in range(256)] for i in range(256): p[i] = intensity[i]/image.shape[0] return p image_1 =read_image() intensity_1 = generate_intensity() print(type(intensity_1)) for i in range(image_1.shape[0]): intensity_1[image_1[i]] = intensity_1[image_1[i]] + 1 #generate the x axis(bins) x = [i for i in range(256)] y = [ intensity_1[i] for i in range(256)] #find the probability for each bins p =findProbability(intensity_1,image_1) print(intensity_1) print(p) plt.plot(x,p) #add the 100 constant value on this image. image_2 = read_image() intensity_2 = generate_intensity() image_2 =image_2 +100 imag_2=255*image_2/np.max(image_2-np.min(image_2)) for i in range(image_2.shape[0]): intensity_2[image_2[i]] = intensity_2[image_2[i]] +1 y_probability = findProbability(intensity_2,image_2) plt.plot(x,y_probability) #move histogram toward left image_3 = read_image() intensity_3 = generate_intensity() for i in range(image_3.shape[0]): image_3[i] = math.ceil(image_3[i]/10) # imag_1d_left=255*image_3/np.max(image_3-np.min(image_3)) for i in range(image_3.shape[0]): intensity_3[image_3[i]] = intensity_3[image_3[i]] +1 y_probability =findProbability(intensity_3,image_3) plt.plot(x,y_probability) image_4 = cv2.imread('/content/lena_face.jpg') intensity_4 = generate_intensity() image_4 = cv2.resize(image_4,None,fx=1/2,fy=1/2,interpolation=cv2.INTER_CUBIC) print(image_4.shape) image_4 = image_4.flatten() print(image_4.shape) for i in range(image_4.shape[0]): intensity_4[image_4[i]] = intensity_4[image_4[i]] +1 y_probability_4 =findProbability(intensity_4,image_4) plt.plot(x,y_probability_4)
The whole code runs into the google colab. It is the best platform where we can easily run the program and analyze the output.
I also write stories on medium platform which is free for all users. Just visit once :
Medium with Debugger and follow it.
Thanks for giving your golden time......
(Harsh Prajapati)
Comments
I appreciate you taking the time and effort to share your knowledge. This material proved to be really efficient and beneficial to me. Thank you very much for providing this information. Continue to write your blog.
ReplyDeleteData Engineering Services
Artificial Intelligence Services
Data Analytics Services
Data Modernization Services