阈值化
import cv2
import numpy as np
import matplotlib.pyplot as mp
img=cv2.imread('opencv.jpg',0)
ret,thresh1=cv2.threshold(img,127,255,cv2.THRESH_BINARY)
ret,thresh2=cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)
ret,thresh3=cv2.threshold(img,127,255,cv2.THRESH_TRUNC)
ret,thresh4=cv2.threshold(img,127,255,cv2.THRESH_TOZERO)
ret,thresh5=cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV)
titles = ['original Image', 'binary','BINARY_INV', 'TRUNC','TOZERO','tozero_inv']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in range(6):
mp.subplot(2,3,i+1),mp.imshow(images[i],'gray')
mp.title(titles[i])
mp.xticks([]),mp.yticks([])
mp.show()