我用过公式: ((L-1)/ MN)的 NI 哪里 L是graylevels的总数,M N是图像的大小,ni是累积频率
但我总是得到完整的黑色图像。我也尝试过其他图像。
import numpy as np
import cv2
path="C:/Users/Arun Nambiar/Downloads/fingerprint256by256 (1).pgm"
img=cv2.imread(path,0)
#To display image before equalization
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
a=np.zeros((256,),dtype=np.float16)
b=np.zeros((256,),dtype=np.float16)
height,width=img.shape
#finding histogram
for i in range(width):
for j in range(height):
g=img[j,i]
a[g]=a[g]+1
print(a)
#performing histogram equalization
tmp=255/(height*width)
a[0]=tmp*a[0]
b[0]=round(a[0])
for g in range(1,width):
a[g]=(a[g]*tmp)+(a[g-1]*tmp)
b[g]=round(a[g])
print(b)
b=b.astype(np.uint8)
print(b)
for i in range(width):
for j in range(height):
g=img[j,i]
img[j,i]=b[g]
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
答案 0 :(得分:0)
均衡步骤的实施有些不正确。概率分布函数(PDF)的计算应该高达箱的数量而不是图像宽度(尽管在这种特定情况下它们是相等的)。请参阅以下代码,并更正均衡步骤。
import numpy as np
import cv2
path = "fingerprint256by256.pgm"
img = cv2.imread(path,0)
#To display image before equalization
cv2.imshow('image',img)
cv2.waitKey(0)
a = np.zeros((256,),dtype=np.float16)
b = np.zeros((256,),dtype=np.float16)
height,width=img.shape
#finding histogram
for i in range(width):
for j in range(height):
g = img[j,i]
a[g] = a[g]+1
print(a)
#performing histogram equalization
tmp = 1.0/(height*width)
b = np.zeros((256,),dtype=np.float16)
for i in range(256):
for j in range(i+1):
b[i] += a[j] * tmp;
b[i] = round(b[i] * 255);
# b now contains the equalized histogram
b=b.astype(np.uint8)
print(b)
#Re-map values from equalized histogram into the image
for i in range(width):
for j in range(height):
g = img[j,i]
img[j,i]= b[g]
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
使用Python 3.4和OpenCV 3.4在Ubuntu 14.04上测试和验证。