直方图及均衡化(opencv_python下)

本文详细介绍了图像处理中的直方图统计方法,包括灰度直方图的计算及应用,以及直方图均衡化的步骤与实现。通过OpenCV库的calcHist函数和equalizeHist函数,展示了如何在Python中进行直方图计算与图像对比度增强。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(一)图像直方图

  • 直方图可以统计的不仅仅是颜色灰度, 它可以统计任何图像特征 (如 梯度, 方向等等)。

这是需要我们明确的,但是本文主要考虑的是灰度直方图。

以下参考: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:
  • images – Source arrays. They all should have the same depth, CV_8U or CV_32F , and the same size. Each of them can have an arbitrary number of channels.
  • nimages – Number of source images.
  • channels – List of the dims channels used to compute the histogram. The first array channels are numerated from 0 to images[0].channels()-1 , the second array channels are counted from images[0].channels() to images[0].channels() + images[1].channels()-1, and so on.
  • mask – Optional mask. If the matrix is not empty, it must be an 8-bit array of the same size as images[i] . The non-zero mask elements mark the array elements counted in the histogram.
  • hist – Output histogram, which is a dense or sparse dims -dimensional array.
  • dims – Histogram dimensionality that must be positive and not greater than CV_MAX_DIMS (equal to 32 in the current OpenCV version).
  • histSize – Array of histogram sizes in each dimension.
  • ranges – Array of the dims arrays of the histogram bin boundaries in each dimension. When the histogram is uniform ( uniform =true), then for each dimension i it is enough to specify the lower (inclusive) boundary L_0 of the 0-th histogram bin and the upper (exclusive) boundary U_{\texttt{histSize}[i]-1} for the last histogram bin histSize[i]-1 . That is, in case of a uniform histogram each of ranges[i] is an array of 2 elements. When the histogram is not uniform ( uniform=false ), then each of ranges[i] contains histSize[i]+1 elements: L_0, U_0=L_1, U_1=L_2, ..., U_{\texttt{histSize[i]}-2}=L_{\texttt{histSize[i]}-1}, U_{\texttt{histSize[i]}-1} . The array elements, that are not between L_0 and U_{\texttt{histSize[i]}-1} , are not counted in the histogram.
  • uniform – Flag indicating whether the histogram is uniform or not (see above).
  • accumulate – Accumulation flag. If it is set, the histogram is not cleared in the beginning when it is allocated. This feature enables you to compute a single histogram from several sets of arrays, or to update the histogram in time.

使用示例

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:
  • clipLimit – Threshold for contrast limiting.
  • tileGridSize – Size of grid for histogram equalization. Input image will be divided into equally sized rectangular tiles. tileGridSize defines the number of tiles in row and column.

目前对于第一个参数并不是很懂。。。

相关文章:

CLAHE的实现与研究

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值