|
通过直方图的均衡化进行图像增加,需进行如下四个步骤:
1、计算图像的灰度直方图
2、计算灰度直方图的累加直方图
3、根据累加的直方图和直方图均衡化的原理得到输入灰度级与输出灰度级之间的映射关系
4、使用循环的方式得到输出图像的每一个像素的灰度级
实现的代码如下:
import cv2
import numpy as np
def calcGrayHist(image):
rows,clos = image.shape
grahHist = np.zeros([256],np.uint64)
for r in range(rows):
for c in range(clos):
grahHist[image[r][c]] +=1
return grahHist
if __name__ == "__main__":
image = cv2.imread("D://python/p1.jpg", cv2.IMREAD_GRAYSCALE)
rows,cols = image.shape
grayHist = calcGrayHist(image)
zeroCumuMoment = np.zeros([256], np.uint32)
for p in range(256):
if p == 0:
zeroCumuMoment[p] = grayHist[0]
else:
zeroCumuMoment[p] = zeroCumuMoment[p-1] + grayHist[p]
output = np.zeros([256],np.uint8)
cofficient = 256.0/(rows*cols)
for p in range(256):
q = cofficient * float(zeroCumuMoment[p])-1
if q >=0:
output[p] = np.math.floor(q)
else:
output[p] = 0
equalHistimg = np.zeros(image.shape,np.uint8)
for r in range(rows):
for c in range(cols):
equalHistimg[r][c] = output[image[r][c]]
cv2.imshow('image',image)
cv2.imshow('histimage',equalHistimg)
cv2.waitKey(0)
cv2.destroyAllWindows()
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|