(一)图像直方图
-
直方图可以统计的不仅仅是颜色灰度, 它可以统计任何图像特征 (如 梯度, 方向等等)。
这是需要我们明确的,但是本文主要考虑的是灰度直方图。
以下参考:https://docs.opencv.org/3.0-beta/modules/imgproc/doc/histograms.html#calchist
Python: cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]]) → hist
Parameters: |
|
---|
使用示例
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread("D:\\software_my_programming\
\\relate_to_irisrecog\\CASIA-Iris-Lamp\\001\\L\\S2001L01.jpg",0)
#读取图像,0为灰度图像,1为彩色图像
#opencv方法读取-cv2.calcHist(速度最快)
#图像,通道[0]-灰度图,掩膜-无,灰度级,像素范围
hist_cv = cv2.calcHist([img],[0],None,[256],[0,256])
#numpy方法读取-np.histogram()
hist_np,bins = np.histogram(img.ravel(),256,[0,256])
#numpy的另一种方法读取-np.bincount()(速度=10倍法2)
hist_np2 = np.bincount(img.ravel(),minlength=256)
plt.subplot(221),plt.imshow(img,'gray')
plt.subplot(222),plt.plot(hist_cv)
plt.subplot(223),plt.plot(hist_np)
plt.subplot(224),plt.plot(hist_np2)
plt.show()
使用掩模示例
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread("D:\\software_my_programming\
\\relate_to_irisrecog\\CASIA-Iris-Lamp\\001\\L\\S2001L01.jpg",0)
#读取图像,0为灰度图像,1为彩色图像
mask = np.zeros(img.shape[:2],np.uint8)
mask[100:200,100:200] = 255
masked_img = cv2.bitwise_and(img,img,mask=mask)
#opencv方法读取-cv2.calcHist(速度最快)
#图像,通道[0]-灰度图,掩膜-无,灰度级,像素范围
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.show()
(二)直方图均衡化
图像的直方图是对图像对比度效果上的一种处理,旨在使得图像整体效果均匀,黑与白之间的各个像素级之间的点更均匀一点。
直方图均衡化只要包括三个步骤:
- 统计直方图中每个灰度级出现的次数;
- 计算累计归一化直方图;
- 重新计算像素点的像素值;
关于原理的详细部分给一个参考:
整体均衡化
示例:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread("D:\\software_my_programming\
\\relate_to_irisrecog\\CASIA-Iris-Lamp\\001\\L\\S2001L01.jpg",0)
#读取图像,0为灰度图像,1为彩色图像
res = cv2.equalizeHist(img)
plt.subplot(121),plt.imshow(img,'gray')
plt.subplot(122),plt.imshow(res,'gray')
plt.show()
局部均衡化
示例:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread("D:\\software_my_programming\
\\relate_to_irisrecog\\CASIA-Iris-Lamp\\001\\L\\S2001L01.jpg",0)
#读取图像,0为灰度图像,1为彩色图像
clahe = cv2.createCLAHE(clipLimit=2,tileGridSize=(10,10))
cl1 = clahe.apply(img)
plt.subplot(121),plt.imshow(img,'gray')
plt.subplot(122),plt.imshow(cl1,'gray')
plt.show()
对于彩色图像,直方图局部均衡化:
限制对比度自适应直方图均衡(CLAHE算法)进行预处理有时可以增加识别的准确率。使用方法如下:
import cv2
path = '1.jpg'
image = cv2.imread(path,cv2.IMREAD_COLOR)
cv2.imwrite('image.jpg',image,[cv2.IMWRITE_JPEG_QUALITY, 50])
b,g,r = cv2.split(image)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
b = clahe.apply(b)
g = clahe.apply(g)
r = clahe.apply(r)
image = cv2.merge([b,g,r])
cv2.imwrite('clahe.jpg',image,[cv2.IMWRITE_JPEG_QUALITY, 50])
函数介绍:
createCLAHE(double clipLimit=40.0, Size tileGridSize=Size(8, 8))¶
Parameters: |
|
---|
目前对于第一个参数并不是很懂。。。
相关文章: