阈值分割python实现

1、直方图技术法

import cv2
import numpy as np
from scipy import signal
import math
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 threshTwoPeaks(image):
    histogram=calcGrayHist(image)
    maxLoc=np.where(histogram==np.max(histogram))
    firstPeak=maxLoc[0][0]
    measureDists=np.zeros([256],np.float32)
    for k in range(256):
        measureDists[k]=pow(k-firstPeak,2)*histogram[k]
    maxLoc2=np.where(measureDists==np.max(measureDists))
    secondPeak=maxLoc2[0][0]
    thresh=0
    if firstPeak>secondPeak:
        temp=histogram[int(secondPeak):int(firstPeak)]
        minLoc=np.where(temp==np.min(temp))
        thresh=secondPeak+minLoc[0][0]+1
    else:
        temp=histogram[int(firstPeak):int(secondPeak)]
        minLoc=np.where(temp==np.min(temp))
        thresh=firstPeak+minLoc[0][0]+1
    threshImage_out=image.copy()
    threshImage_out[threshImage_out>thresh]=255
    threshImage_out[threshImage_out<=thresh]=0
    return (thresh,threshImage_out)
image=cv2.imread("/home/xiaomingming/profile/xmm.jpg",cv2.IMREAD_GRAYSCALE)
cv2.imshow("image",image)
thresh,out=threshTwoPeaks(image)
print(thresh)
cv2.imshow("out",out)
cv2.waitKey(0)
cv2.destroyAllWindows()

2、熵算法

import cv2
import numpy as np
from scipy import signal
import math
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(zeroCumuMoment[0:k+1])
        maxBack=np.max(zeroCumuMoment[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)
image=cv2.imread("/home/xiaomingming/profile/dog.jpg",cv2.IMREAD_GRAYSCALE)
cv2.imshow("image",image)
thresh,out=threshEntroy(image)
print(thresh)
out=np.round(out)
out.astype(np.uint8)
cv2.imshow("out",out)
cv2.waitKey(0)
cv2.destroyAllWindows()

3、otsu阈值处理

import cv2
import numpy as np
from scipy import signal
import math
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 otsu(image):
    rows,cols=image.shape
    grayHist=calcGrayHist(image)
    uniformGrayHist=grayHist/float(rows*cols)
    zeroCumuMoment=np.zeros([256],np.float32)
    oneCumuMoment=np.zeros([256],np.float32)
    for k in range(256):
        if k==0:
            zeroCumuMoment[k]=uniformGrayHist[0]
            oneCumuMoment[k]=k*uniformGrimport cv2
import numpy as np
from scipy import signal
import math
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 adaptiveThresh(I,winSize,ratio=0.15):
    I_mean=cv2.boxFilter(I,cv2.CV_32FC1,winSize)
    out=I-(1.0-ratio)*I_mean
    out[out>=0]=255
    out[out<0]=0
    out=np.round(out)
    out=out.astype(np.uint8)
    return out
image=cv2.imread("/home/xiaomingming/profile/xmm.jpg",cv2.IMREAD_GRAYSCALE)
cv2.imshow("image",image)
out=adaptiveThresh(image,(11,11))
#out=np.round(out)
#out.astype(np.uint8)
cv2.imshow("out",out)
cv2.waitKey(0)
cv2.destroyAllWindows()ayHist[0]
        else:
            zeroCumuMoment[k]=zeroCumuMoment[k-1]+uniformGrayHist[k]
            oneCumuMoment[k]=oneCumuMoment[k-1]+k*uniformGrayHist[k]
    mean=oneCumuMoment[255]
    variance=np.zeros([256],np.float32)
    for k in range(255):
        if zeroCumuMoment[k]==0 or zeroCumuMoment[k]==1:
            variance[k]=0
        else:
            variance[k]=math.pow(mean*zeroCumuMoment[k]-oneCumuMoment[k],2.0)/(zeroCumuMoment[k]*(1-zeroCumuMoment[k]))
    threshLoc=np.where(variance[0:255]==np.max(variance[0:255]))
    thresh=threshLoc[0][0]
    threshold=np.copy(image)
    threshold[threshold>thresh]=255
    threshold[threshold<=thresh]=0
    return (thresh,threshold)
image=cv2.imread("/home/xiaomingming/profile/xmm.jpg",cv2.IMREAD_GRAYSCALE)
cv2.imshow("image",image)
thresh,out=otsu(image)
print(thresh)
out=np.round(out)
out.astype(np.uint8)
cv2.imshow("out",out)
cv2.waitKey(0)
cv2.destroyAllWindows()

4、自适应阈值分割

import cv2
import numpy as np
from scipy import signal
import math
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 adaptiveThresh(I,winSize,ratio=0.15):
    I_mean=cv2.boxFilter(I,cv2.CV_32FC1,winSize)
    out=I-(1.0-ratio)*I_mean
    out[out>=0]=255
    out[out<0]=0
    out=np.round(out)
    out=out.astype(np.uint8)
    return out
image=cv2.imread("/home/xiaomingming/profile/xmm.jpg",cv2.IMREAD_GRAYSCALE)
cv2.imshow("image",image)
out=adaptiveThresh(image,(11,11))
#out=np.round(out)
#out.astype(np.uint8)
cv2.imshow("out",out)
cv2.waitKey(0)
cv2.destroyAllWindows()
很抱歉,根据提供的引用内容中没有关于漫水阈值分割Python代码。在这里提供的引用内容是关于阈值分割的代码,但没有提到漫水阈值分割。如果您有关于漫水阈值分割的具体问题,请提供更多的信息,我将尽力帮助您。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python+opencv实现阈值分割](https://download.csdn.net/download/weixin_38691739/12865456)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [阈值分割python实现](https://blog.csdn.net/weixin_41463944/article/details/101627609)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [opencv-python 详解阈值分割](https://blog.csdn.net/RayChiu757374816/article/details/119998873)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员阿明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值