二值化
import cv2
import numpy as np
import matplotlib.pyplot as mp
img=cv2.imread('opencv.jpg',0)
ret1,th1=cv2.threshold(img,127,255,cv2.THRESH_BINARY)
ret2,th2=cv2.threshold(img,127,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
blur=cv2.GaussianBlur(img,(5,5),0)
ret3,th3=cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
image=[img,0,th1,
img,0,th2,
blur,0,th3]
titles=[1,2,3,4,5,6,7,8,9]
for i in range(3):
mp.subplot(3,3,i*3+1),mp.imshow(image[i*3],'gray')
mp.title(titles[i*3]),mp.xticks([]),mp.yticks([])
mp.subplot(3,3,i*3+2),mp.hist(image[i*3].ravel(),256)
mp.title(titles[i*3+1]),mp.xticks([]),mp.yticks([])
mp.subplot(3,3,i*3+3),mp.imshow(image[i*3+2],'gray')
mp.title(titles[i*3+2]),mp.xticks([]),mp.yticks([])
mp.show()