日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

阈值分割python实现

發(fā)布時間:2024/8/1 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 阈值分割python实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1、直方圖技術(shù)法

import cv2 import numpy as np from scipy import signal import math def calcGrayHist(image):rows,cols=image.shapegrayHist=np.zeros([256],np.uint64)for r in range(rows):for c in range(cols):grayHist[image[r][c]]+=1return 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=0if firstPeak>secondPeak:temp=histogram[int(secondPeak):int(firstPeak)]minLoc=np.where(temp==np.min(temp))thresh=secondPeak+minLoc[0][0]+1else:temp=histogram[int(firstPeak):int(secondPeak)]minLoc=np.where(temp==np.min(temp))thresh=firstPeak+minLoc[0][0]+1threshImage_out=image.copy()threshImage_out[threshImage_out>thresh]=255threshImage_out[threshImage_out<=thresh]=0return (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.shapegrayHist=np.zeros([256],np.uint64)for r in range(rows):for c in range(cols):grayHist[image[r][c]]+=1return grayHist def threshEntroy(image):rows,cols=image.shapegrayHist=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]=0else: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.0totalEntroy=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=0else: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=0else: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+ft2threshLoc=np.where(fT==np.max(fT))thresh=threshLoc[0][0]threshold=np.copy(image)threshold[threshold>thresh]=255threshold[threshold<=thresh]=0return (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.shapegrayHist=np.zeros([256],np.uint64)for r in range(rows):for c in range(cols):grayHist[image[r][c]]+=1return grayHist def otsu(image):rows,cols=image.shapegrayHist=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.shapegrayHist=np.zeros([256],np.uint64)for r in range(rows):for c in range(cols):grayHist[image[r][c]]+=1return grayHist def adaptiveThresh(I,winSize,ratio=0.15):I_mean=cv2.boxFilter(I,cv2.CV_32FC1,winSize)out=I-(1.0-ratio)*I_meanout[out>=0]=255out[out<0]=0out=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]=0else: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]=255threshold[threshold<=thresh]=0return (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、自適應(yīng)閾值分割

import cv2 import numpy as np from scipy import signal import math def calcGrayHist(image):rows,cols=image.shapegrayHist=np.zeros([256],np.uint64)for r in range(rows):for c in range(cols):grayHist[image[r][c]]+=1return grayHist def adaptiveThresh(I,winSize,ratio=0.15):I_mean=cv2.boxFilter(I,cv2.CV_32FC1,winSize)out=I-(1.0-ratio)*I_meanout[out>=0]=255out[out<0]=0out=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()

總結(jié)

以上是生活随笔為你收集整理的阈值分割python实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。