def calculate_entropy(image):
entropy = image.copy()
sum = 0
i = 0
j = 0
while i < entropy.shape[0]:
j = 0
while j < entropy.shape[1]:
sub_image = entropy[i:i+10,j:j+10]
histogram = cv2.calcHist([sub_image],[0],None,[256],[0,256])
sum = 0
for k in range(256):
if histogram[k] != 0:
sum = sum + (histogram[k] * math.log(histogram[k]))
k = k + 1
entropy[i:i+10,j:j+10] = sum
j = j+10
i = i+10
ret2,th2 = cv2.threshold(entropy,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
newfin = cv2.erode(th2, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)), iterations=1)
return newfin
评论列表
文章目录