b,g,r = cv2.split(img_dog) # 对用cv2读取的数据进行切片,BGR
plt_img_dog = cv2.merge([r,g,b]) # 将BGR格式转换为RGB格式,用于plt的图片预览
Img_cat = cv2.imread('cat.jpg')
b,g,r = cv2.split(Img_cat) # 对用cv2读取的数据进行切片,BGR
plt_img_cat = cv2.merge([r,g,b]) # 将BGR格式转换为RGB格式,用于plt的图片预览
gray_cat = cv2.imread('gray_cat.jpg')
# 超过阈值部分取maxval(最大值),否则取0
ret,thresh1 = cv2.threshold(gray_cat,127,255,cv2.THRESH_BINARY)
# THRESH_BINARY 的反转
ret,thresh2 = cv2.threshold(gray_cat,127,255,cv2.THRESH_BINARY_INV)
# 大于阈值的部分设为阈值,否则不变
ret,thresh3 = cv2.threshold(gray_cat,127,255,cv2.THRESH_TRUNC)
# 大于阈值的部分不改变,否则设为0
ret,thresh4 = cv2.threshold(gray_cat,127,255,cv2.THRESH_TOZERO)
# THRESH_TOZERO 的反转
ret,thresh5 = cv2.threshold(gray_cat,127,255,cv2.THRESH_TOZERO_INV)
titles = ['Dog Image','Orignal Image','Gray Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
images = [plt_img_dog,plt_img_cat,gray_cat,thresh1,thresh2,thresh3,thresh4,thresh5]
for i in range(8):
# 2,4为2x4,2行4列排序
ax = plt.subplot(2,4,(i+1))
# imshow(img,camp)
# camp是控制绘制的格式,常见有四种:plt.cm.gray、plt.cm.gray_r、'gray'、'gray_r'
plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([])
plt.yticks([]) # 设置X,Y轴
plt.show()
python使用OpenCV,进行图像处理,五种灰度处理方式
最新推荐文章于 2024-08-22 06:00:00 发布