Basic Open-CV, GoogleColab, and Histogram
At first, this article is from my image processing class in the open-ended question for understanding about histogram and code. I used google colab and openCV that is python base in this article.
Reach to the picture
Because to coding with googleColab is not the same as others. We need to upload files or picture to it. There are two easiest ways to import picture to GoogleColab. First is uploading directly by run the code and second is using google drive.
this code for uploading image.
from google.colab import files
uploaded = files.upload()
When you run this section the upload button will appear. Then you choose your image from you local drive. After that the uploaded files will appear on your left side.
this code for access google drive.
from google.colab import drive
drive.mount('/content/drive')
Then your files in google drive will appear on your left side same as uploaded files.
Set up
Next, we need to import library that will use in calculating histogram. First is openCV for using function “calcHist” and second is matplot for plotting the histogram.
import cv2
from matplotlib import pyplot as plt
In this article I using OpenCV version 3.4.3.
You can check version by using this code
cv2.__version__
Calculate and plot
jpg = cv2.imread('image.jpg',0)
This code is for reading your image files.
The number after file name is to select type of image.
1 for load image in color
0 for load image in gray scale
-1 for load image that include alpha channel in pixel values
Then we will using calcHist function from openCV and plot it by using matplotlib for comparing between picture and graph.
Hist = cv2.calcHist([jpg],[0],none,[256],[0,256])
plt.subplot(121), plt.imshow(jpg, 'gray')
plt.subplot(122), plt.plot(Hist)
plt.xlim([0,256])
in the brackets of calcHist function.
[jpg] — your image
[0] — channel color of histogram chose [0] if your image is in gray scale. in case of color image [0] is for blue, [1] for green and [2] for red.
none — this is for select point of the picture that you want to calculate. none is for full frame. If you want to choose the frame, you need to made it before but in this article I didn’t do that.
[256] — this is for “bin” or we can call “brackets” so you can read more about bin here. Anywhere for full of gray scale we use 256
[0,256] — this is the ranges of pixel values
Beware, openCV does not support GIF files. Check out how to change GIF to pic by python here.
Histogram equalization
Histogram equalization is for improving the contrast of images. In the theory, It talks about changing the curve cumulative graph in to linear cumulative graph.
more information about cumulative theory here
We can check cumulative graph by using “cumsum()” and change it to normalization by multiply by max of hist and divide by max of cumsum in hist.
cdf = hist.cumsum()
cdfnmhist = cdf * hist.max()/ cdf.max()
OpenCV has function for equalization called “equalizeHist”.
hist = cv2.calcHist([img],[0],None,[256],[0,256])
eq = cv2.equalizeHist(img)
The dark zone of image after equalization changing to brighter than before. Because we improve contrast of this image by using “equalizeHist” function. Then in cumulative graph is going to linear as the purpose.
Check out the full code of this method.
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('JPG.jpg',0)hist = cv2.calcHist([img],[0],None,[256],[0,256])
eq = cv2.equalizeHist(img)
cdf = hist.cumsum()
cdfnmhist = cdf * hist.max()/ cdf.max()
histeq = cv2.calcHist([eq],[0],None,[256],[0,256])
cdfeq = histeq.cumsum()
cdfnmhisteq = cdfeq * histeq.max()/ cdf.max()plt.subplot(221), plt.imshow(img,'gray')
plt.subplot(222), plt.plot(hist), plt.plot(cdfnmhist)
plt.subplot(223), plt.imshow(eq,'gray')
plt.subplot(224), plt.plot(histeq), plt.plot(cdfnmhisteq)
plt.xlim([0,256])
That is all of my stuffs. In my opinion, I like Google.Colab for some reason. May be because I don’t have to set a lot of software environment for writing and compiling my code. However, thank you for reading my article. I hope this article will helpful for someone that want to start coding in image processing like me.