阈值分割python实现
生活随笔
收集整理的這篇文章主要介紹了
阈值分割python实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、直方圖技術法
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、自適應閾值分割
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()總結
以上是生活随笔為你收集整理的阈值分割python实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 太阳能小屋的设计
- 下一篇: java日期字符串排序_对字符串格式的日