一、图像的阈值处理
ret, dst = cv2.threshold(src=,thresh=,maxval=,type=) dst:输出图 src:输入图,只能是单通道图像,通常来说为灰度图 thresh:阈值 maxval:当像素值超过了阈值(或者小于阈值,根据type来决定),所赋予的值 type:二值化操作的类型和,包含以下五种 cv2.THRESH_BINARY 超过阈值部分取maxval(最大值),否则0 cv2.THRESH_BINARY_INV 小于阈值部分取maxval(最大值) 否则0 cv2.THRESH_TRUNC 大于阈值部分设为阈值,否则不变 cv2.THRESH_TOZERO 大于阈值部分不改变,否则设为0 cv2.THRESH_TOZERO_INV 小于阈值部分不改变吗,否则设为0
import cv2
img_h = cv2.imread(r"C:\Users\admin\Desktop\yy.jpg", cv2.IMREAD_GRAYSCALE)
ret1,img1 = cv2.threshold(img_h,127,255,cv2.THRESH_BINARY)
ret2,img2 = cv2.threshold(img_h,127,255,cv2.THRESH_BINARY_INV)
ret3,img3 = cv2.threshold(img_h,127,255,cv2.THRESH_TRUNC)
ret4,img4 = cv2.threshold(img_h,127,255,cv2.THRESH_TOZERO)
ret5,img5 = cv2.threshold(img_h,127,255,cv2.THRESH_TOZERO_INV)
titles = ["ORIGINAL", "BINARY", "BINARY_INV", "TRUNC", "TOZERO", "TOZERO_INV"]
imgs = [img_h,img1,img2,img3,img4,img5]
for i in range(6):
plt.subplot(2,3,i+1),plt.imshow(imgs[i],"gray")
plt.title(titles[i])
plt.xticks([]), plt.yticks([])
plt.show()
效果如下
二、图像的平滑处理
img = cv2.imread(r"C:\Users\admin\Desktop\yy2.jpg", cv2.IMREAD_GRAYSCALE)
blur = cv2.blur(img, (3, 3))
box = cv2.boxFilter(img,-1,(3,3),normalize=True)
box = cv2.boxFilter(img,-1,(3,3),normalize=False)
aussian = cv2.GaussianBlur(img, (5,5), 1)
median = cv2.medianBlur(img, 5)
|