首先是opencv在图像处理方面的基础应用,彩色图像的相关知识和技术以及直方图的均衡化和规定化的原理是必须提前掌握,这些我就不做过多的介绍了。 本次实验也是基于python下完成的。
老师要求的是在hsv模型里对直方图进行规定化,所以如果你是在rgb模型下进行直方图规定化,只需要将rgb和hsv互相转化的部分删除即可,重点并不是这里。
import cv2
import numpy as np
from matplotlib import pyplot as plt
img1 = cv2.imread('E:\\picSource\\Fig7A.jpg')
img2 = cv2.imread('E:\\picSource\\Fig7B.jpg')
img_hsv1 = cv2.cvtColor(img1, cv2.COLOR_BGR2HSV) # bgr转hsv
img_hsv2 = cv2.cvtColor(img2, cv2.COLOR_BGR2HSV)
color = ('h', 's', 'v')
for i, col in enumerate(color):
# histr = cv2.calcHist([img_hsv1], [i], None, [256], [0, 256])
hist1, bins = np.histogram(img_hsv1[:, :, i].ravel(), 256, [0, 256])
hist2, bins = np.histogram(img_hsv2[:, :, i].ravel(), 256, [0, 256])
cdf1 = hist1.cumsum() # 灰度值0-255的累计值数组
cdf2 = hist2.cumsum()
cdf1_hist = hist1.cumsum() / cdf1.max() # 灰度值的累计值的比率
cdf2_hist = hist2.cumsum() / cdf2.max()
diff_cdf = [[0 for j in range(256)] for k in range(256)] # diff_cdf 里是每2个灰度值比率间的差值
for j in range(256):
for k in range(256):
diff_cdf[j][k] = abs(cdf1_hist[j] - cdf2_hist[k])
lut = [0 for j in range(256)] # 映射表
for j in range(256):
min = diff_cdf[j][0]
index = 0
for k in range(256): # 直方图规定化的映射原理
if min > diff_cdf[j][k]:
min = diff_cdf[j][k]
index = k
lut[j] = ([j, index])
h = int(img_hsv1.shape[0])
w = int(img_hsv1.shape[1])
for j in range(h): # 对原图像进行灰度值的映射
for k in range(w):
img_hsv1[j, k, i] = lut[img_hsv1[j, k, i]][1]
hsv_img1 = cv2.cvtColor(img_hsv1, cv2.COLOR_HSV2BGR) # hsv转bgr
hsv_img2 = cv2.cvtColor(img_hsv2, cv2.COLOR_HSV2BGR)
cv2.namedWindow('firstpic', 0)
cv2.resizeWindow('firstpic', 670, 900)
cv2.namedWindow('targetpic', 0)
cv2.resizeWindow('targetpic', 670, 900)
cv2.namedWindow('defpic', 0)
cv2.resizeWindow('defpic', 670, 900)
cv2.imshow('firstpic', img1)
cv2.imshow('targetpic',img2)
# cv2.imshow('img1', img_hsv1)
cv2.imshow('defpic', hsv_img1)
cv2.waitKey(0)
cv2.destroyAllWindows()
以下分别是原图像 目标图像 和 经过直方图规定化后的图像: