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()
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/223621.html原文链接:https://javaforall.net
