OpenCV利用python来实现图像的直方图均衡化
1.直方图
直方图: (1) 图像中不同像素等级出现的次数 (2) 图像中具有不同等级的像素关于总像素数目的比值。
我们使用cv2.calcHist
方法得到直方图
cv2.calcHist(images, channels, mask, histSize, ranges):
-img: 图像
-channels: 选取图像的哪个通道
-histSize: 直方图大小
-ranges: 直方图范围
cv2.minMaxLoc:
返回直方图的最大最小值,以及他们的索引
import cv2 import numpy as np def ImageHist(image, type): color = (255, 255,255) windowName = 'Gray' if type == 1: #判断通道颜色类型 B-G-R color = (255, 0, 0) windowName = 'B hist' elif type == 2: color = (0,255,0) windowName = 'G hist' else: color = (0,0,255) # 得到直方图 hist = cv2.calcHist([image],[0],None,[256],[0,255]) # 得到最大值和最小值 minV,maxV,minL,maxL = cv2.minMaxLoc(hist) histImg = np.zeros([256,256,3],np.uint8) #直方图归一化 for h in range(256): interNormal = int(hist[h] / maxV * 256) cv2.line(histImg, (h, 256), (h, 256 - interNormal), color) cv2.imshow(windowName, histImg) return histImg img = cv2.imread('img.jpg', 1) channels = cv2.split(img) # R-G-B for i in range(3): ImageHist(channels[i], 1 + i) cv2.waitKey(0)
2.直方图均衡化
灰色图像直方图均衡化
这里我们直接使用cv2.equalizeHist
方法来得到直方图均衡化之后的图像
import cv2 import numpy as np img = cv2.imread('img.jpg', 1) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) dat = cv2.equalizeHist(gray) cv2.imshow('gray', gray)a cv2.imshow('dat', dat) cv2.waitKey(0)
原图像:
直方图均衡化后的图像:
彩色图像直方图均衡化
彩色图像有3个通道,直方图是针对单通道上的像素统计,所以使用cv2.split
方法分离图像的颜色通道,分别得到各个通道的直方图,最后使用cv2.merge()
方法合并直方图,得到彩色图像的直方图均衡化
import cv2 import numpy as np img = cv2.imread('img.jpg', 1) cv2.imshow('img', img) (b, g, r) = cv2.split(img) bH = cv2.equalizeHist(b) gH = cv2.equalizeHist(g) rH = cv2.equalizeHist(r) dat = cv2.merge((bH, gH, rH)) cv2.imshow('dat', dat) cv2.waitKey(0)
D:\Anaconda\lib\site-packages\numpy\_distributor_init.py:32: UserWarning: loaded more than 1 DLL from .libs:
D:\Anaconda\lib\site-packages\numpy\.libs\libopenblas.NOIJJG62EMASZI6NYURL6JBKM4EVBGM7.gfortran-win_amd64.dll
D:\Anaconda\lib\site-packages\numpy\.libs\libopenblas.PYQHXLVVQ7VESDPUVUADXEVJOBGHJPAY.gfortran-win_amd64.dll
stacklevel=1)
原图像:
直方图均衡化之后的图像:
3.源代码实现直方图均衡化
下面我们用源代码来实现直方图
横坐标为像素等级,纵坐标为出现的概率
import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread('img.jpg', 1) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) count = np.zeros(256, np.float) for i in range(img.shape[0]): for j in range(img.shape[1]): count[int(gray[i, j])] += 1 # 统计该像素出现的次数 count = count / (img.shape[0] * img.shape[1]) # 得到概率 x = np.linspace(0,255,256) plt.bar(x, count,color = 'b') plt.show() # 计算累计概率 for i in range(1,256): count[i] += count[i - 1] # 映射 map1 = count * 255 for i in range(img.shape[0]): for j in range(img.shape[1]): p = gray[i, j] gray[i, j] = map1[p] cv2.imshow('gray', gray) cv2.waitKey(0)
直方图:
直方图均衡化后的图像:
彩色直方图源码
import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread('img.jpg', 1) # R-G-B三种染色直方图 countb = np.zeros(256, np.float32) countg = np.zeros(256, np.float32) countr = np.zeros(256, np.float32) for i in range(img.shape[0]): for j in range(img.shape[1]): (b,g,r) = img[i,j] b = int(b) g = int(g) r = int(r) countb[b] += 1 # 统计该像素出现的次数 countg[g] += 1 countr[r] += 1 countb = countb / (img.shape[0] * img.shape[1]) # 得到概率 countg = countg / (img.shape[0] * img.shape[1]) countr = countr / (img.shape[0] * img.shape[1]) x = np.linspace(0,255,256) plt.figure() plt.bar(x, countb,color = 'b') plt.figure() plt.bar(x, countg,color = 'g') plt.figure() plt.bar(x, countr,color = 'r') plt.show() # 计算直方图累计概率 for i in range(1,256): countb[i] += countb[i - 1] countg[i] += countg[i - 1] countr[i] += countr[i - 1] #映射表 mapb = countb * 255 mapg = countg * 255 mapr = countr * 255 dat = np.zeros(img.shape, np.uint8) for i in range(img.shape[0]): for j in range(img.shape[1]): (b,g,r) = img[i, j] dat[i, j] = (mapb[b],mapg[g],mapr[r]) cv2.imshow('dat', dat) cv2.waitKey(0)
R-G-B 3 种颜色通道的直方图如下:
图像均衡化之后的结果:
下一篇:Python+OpenCV图像处理——打印图片属性、设置存储路径、调用摄像头