下面看看具体步骤:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('bird.png')
#平均滤波器
blur1 = cv2.blur(img,(5,5))
#中值滤波器
blur2 = cv2.medianBlur(img,5)
#高斯滤波器
blur3 = cv2.GaussianBlur(img,(5,5,),0)
#双边滤波器,同时考虑像素的空间位置和邻域的像素灰度相似性,在滤波的同时较好的保留图片的边缘
blur4 = cv2.bilateralFilter(img,9,75,75)
plt.figure(figsize=(10,10))
plt.subplot(321),plt.imshow(img),plt.title('img')
plt.xticks([]), plt.yticks([])
plt.subplot(322),plt.imshow(blur1),plt.title('blurl')
plt.xticks([]), plt.yticks([])
plt.subplot(323),plt.imshow(blur2),plt.title('blur2')
plt.xticks([]), plt.yticks([])
plt.subplot(324),plt.imshow(blur3),plt.title('blur3')
plt.xticks([]), plt.yticks([])
plt.subplot(325),plt.imshow(blur4),plt.title('blur4')
plt.xticks([]), plt.yticks([])
plt.show()
hsv = cv2.cvtColor(blur4,cv2.COLOR_BGR2HSV)
# 颜色阈值分割
low_blue = np.array([55,0,0])
high_blue = np.array([118,255,255])
mask = cv2.inRange(hsv,low_blue,high_blue)
cv2.imshow('mask',mask)
cv2.waitKey(0)
cv2.destroyAllWindows()
res = cv2.bitwise_and(img,img,mask=mask)
cv2.imshow('res',res)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.bitwise_and()参考链接:
https://blog.csdn.net/yl_best/article/details/87877110?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param