ret,dst = cv2.threshold(src,thresh,maxval,type)
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):
plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
原图:存在许多噪音点(白点)
img = cv2.imread(r"girl.png")
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#均值滤波
#简单的平均卷积操作
blur = cv2.blur(img,(3,3))
cv2.imshow(‘blur’,blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
#执行完后噪音减少了
#方框滤波
#基本等同于均值滤波,不过可以选择归一化
box = cv2.boxFilter(img,-1,(3,3),normalize=True)
cv2.imshow(‘box’,box)
cv2.waitKey(0)
cv2.destroyAllWindows()
#方框滤波
#基本等同于均值滤波,不过可以选择归一化
box2 = cv2.boxFilter(img,-1,(3,3),normalize=False)
cv2.imshow(‘box2’,box2)
cv2.waitKey(0)
cv2.destroyAllWindows()
#高斯滤波
#满足高斯分布,相当于更加重视中间的
aussian = cv2.GaussianBlur(img,(5,5),1)
cv2.imshow(“aussian”,aussian)
cv2.waitKey(0)
cv2.destroyAllWindows()
#中值分布
#相当于用中值代替
median = cv2.medianBlur(img,5)
cv2.imshow(“median”,median)
cv2.waitKey(0)
cv2.destroyAllWindows()
#展示所有的
res = np.hstack((blur,aussian,median))
print(res[:2,:2])
cv2.imshow(“blur,aussian,median”,res)
cv2.waitKey(0)
cv2.destroyAllWindows()