A function evaluation image histogram: cv2.calcHist ()
cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate ]]) ->hist
imaes: Input image
channels: A channel selection image
mask: Mask, and is a size the same as np image array, wherein the specified portion to be processed is 1, specifies that no part of the processing is zero, typically set to None, showing the entire image processing
histSize: Use the number of bin (pillars), typically 256
ranges: Range of pixel values, usually [0, 255] is 0 to 255
behind the two basic parameters of the tube.
Note that in addition mask, the other four parameters had to take [] number.
img = cv2.imread('cat.jpg',0) #0表示灰度图
hist = cv2.calcHist([img],[0],None,[256],[0,256])
hist.shape#(256, 1)
1, no mask parameters
img = cv2.imread('cat.jpg')
color = ('b','g','r')
for i,col in enumerate(color):
histr = cv2.calcHist([img],[i],None,[256],[0,256])
plt.plot(histr,color = col)
plt.xlim([0,256])
2, using the mask parameter
# 创建mask
mask = np.zeros(img.shape[:2], np.uint8)
mask[100:300, 100:400] = 255
img = cv2.imread('cat.jpg', 0)
masked_img = cv2.bitwise_and(img, img, mask=mask)#与操作
hist_full = cv2.calcHist([img], [0], None, [256], [0, 256])
hist_mask = cv2.calcHist([img], [0], mask, [256], [0, 256])
plt.subplot(221), plt.imshow(img, 'gray')
plt.subplot(222), plt.imshow(mask, 'gray')
plt.subplot(223), plt.imshow(masked_img, 'gray')
plt.subplot(224), plt.plot(hist_full), plt.plot(hist_mask)
plt.xlim([0, 256])
plt.show()
Second, the image histogram equalization function: cv2.equalizeHist () and cv2.createCLAHA ()
Histogram equalization described
histogram equalization is a simple and effective image enhancement technique, an image is changed by changing the gradation of each pixel of the image histogram, the dynamic range used to enhance the contrast of the image is small. Due to its original image intensity distribution may be concentrated in a narrow range, resulting image is not clear enough. For example, over-exposure image gray levels is concentrated in a high luminance range, but will cause underexposed image gray level in the low luminance range concentrated. Using the histogram equalization, the histogram may be converted original image is a uniform distribution (balanced) form, thus increasing the value of the difference between the pixel gray scale dynamic range, so as to achieve the effect of enhancing the contrast of the entire image. In other words, the basic principle of histogram equalization is: The number of pixels in the image gradation value (i.e., the picture play a major role gradation value) for broadening, and the smaller number of pixel grayscale values (i.e. not play a major role in the picture gradation value) merge, thereby increasing contrast, image clarity, achieve the purpose of enhanced.
cv2.equalizeHist(img)
Parameters: img representation of the input picture
img = cv2.imread('clahe.jpg',0) #0表示灰度图
plt.hist(img.ravel(),256);
plt.show()
equ = cv2.equalizeHist(img)
plt.hist(equ.ravel(),256)
plt.show()
res = np.hstack((img,equ))
cv_show(res,'res')
This global equalization will be some problems, due to the overall brightness of the lift, so that the details of the partial image will become blurred; so we can use better adaptive equalization
cv2.createCLAHA(clipLimit=8.0, titleGridSize=(8, 8))
Parameters: clipLimit color contrast threshold, titleGridSize equalizing pixel grid size, i.e., a histogram equalization operation at much mesh
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
res_clahe = clahe.apply(img)
res = np.hstack((img,equ,res_clahe))
cv_show(res,'res')
It can be seen adaptive equalization did not make the details of people's faces disappear