阈值处理
定义:指剔除图像内像素值高于一定值或者低于一定值的像素点
例如,设定阈值为127,然后将图像内所有像素值大于127的像素点的值设为255。将图像内所有像素值小于或等于127的像素点的值设为0。
cv2.threshold()
retval,dst=cv2.threshold(src,thresh,maxval,type)
retval,dst=cv2.threshold(src,thresh,maxval,type)
retval,dst=cv2.threshold(src,thresh,maxval,type)
import cv2
import numpy as np
#from matplotlib import pyplot as plt
peppa = cv2.imread('peppa.jpg')
img=cv2.cvtColor(peppa,cv2.COLOR_BGR2GRAY)
cv2.imshow('Peppa',img)
ret,thresh1 = cv2.threshold(img,200,255,cv2.THRESH_BINARY)
ret,thresh2 = cv2.threshold(img,200,255,cv2.THRESH_BINARY_INV)
ret,thresh3 = cv2.threshold(img,200,255,cv2.THRESH_TRUNC)
ret,thresh4 = cv2.threshold(img,200,255,cv2.THRESH_TOZERO)
ret,thresh5 = cv2.threshold(img,200,255,cv2.THRESH_TOZERO_INV)
cv2.imshow('BINARY',thresh1)
cv2.imshow('BINARY_INV',thresh2)
#cv2.imshow('TRUNC',thresh3)
#cv2.imshow('TOZERO',thresh4)
#cv2.imshow('TOZERO_INV',thresh5)
peppa_body=cv2.bitwise_and(peppa,peppa,mask=thresh2)
cv2.imshow('peppa_body',peppa_body)
cv2