当前位置:   article > 正文

熵算法阈值_包络熵的阈值是什么

包络熵的阈值是什么

在这里插入图片描述
在这里插入图片描述

#熵算法阈值
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()
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

结果

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]]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

在这里插入图片描述
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/340996
推荐阅读
相关标签
  

闽ICP备14008679号