#熵算法阈值
import sys,cv2,math
import numpy as np
import matplotlib.pyplot as plt
def calcGrayHist(image):
rows,cols = image.shape
grayHist = np.zeros([256],np.uint64)
for r in range(rows):
for c in range(cols):
grayHist[image[r][c]] +=1#把图像灰度值作为索引
return(grayHist)
def threshEntroy(image):
rows,cols = image.shape
#灰度直方图
grayHist = calcGrayHist(image)
#归一化灰度直方图
normGrayHist = grayHist/float(rows*cols)
#计算累加直方图
zeroCumuMoment = np.zeros([256],np.float32)
for k in range(256):
if k==0:
zeroCumuMoment[k] = normGrayHist[k]
else:
zeroCumuMoment[k] = zeroCumuMoment[k-1] + normGrayHist[k]
#计算各个灰度级的熵
entropy = np.zeros([256],np.float32)
for k in range(256):
if k==0:
if normGrayHist[k] ==0:
entropy[k]==0
else:
entropy[k] = -normGrayHist[k]*math.log10(normGrayHist[k])
else:
if normGrayHist[k] ==0:
entropy[k]=entropy[k-1]
else:
entropy[k]=entropy[k-1]-normGrayHist[k]*math.log10(normGrayHist[k])
#找阈值
fT = np.zeros([256],np.float32)
ft1,ft2 = 0.0,0.0
totalEntroy = entropy[255]
for k in range(255):
#找最大值
maxFront = np.max(normGrayHist[0:k+1])
maxBack = np.max(normGrayHist[k+1:256])
if (maxFront==0 or zeroCumuMoment[k]==0 or maxFront==1 or zeroCumuMoment[k]==1 or totalEntroy==0):
ft1 = 0
else:
ft1=entropy[k]/totalEntroy*(math.log10(zeroCumuMoment[k])/math.log10(maxFront))
if (maxBack==0 or 1-zeroCumuMoment[k]==0 or maxBack==1 or 1-zeroCumuMoment[k]==1 ):
ft2 = 0
else:
if totalEntroy==0:
ft2 = (math.log10(1-zeroCumuMoment[k])/math.log10(maxBack))
else:
ft2=(1-entropy[k]/totalEntroy)*(math.log10(1-zeroCumuMoment[k])/math.log10(maxBack))
fT[k] = ft1+ft2
#找最大值的索引,作为得到的阈值
threshLoc = np.where(fT==np.max(fT))
thresh = threshLoc[0][0]
#阈值处理
threshold = np.copy(image)
threshold[threshold>thresh]=255
threshold[threshold<=thresh]=0
return(thresh,threshold)
if __name__ =='__main__':
src = cv2.imread('E:/sy2/6/img7.jpg',cv2.IMREAD_GRAYSCALE)
re,ra = threshEntroy(src)
print(re)
print(ra)
cv2.imshow('ra',ra)
cv2.waitKey(0)
cv2.destroyAllWindows()
结果
95
[[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 255 255 255]
...
[ 0 0 0 ... 0 255 255]
[ 0 0 0 ... 0 0 255]
[255 0 0 ... 0 0 0]]