目录
实现图像的均值滤波、中值滤波、高斯滤波一、均值滤波二、中值滤波三、高斯滤波小结实现图像卷积边缘检测Harries角点检测
实现图像的均值滤波、中值滤波、高斯滤波
一、均值滤波
实现:blur函数
blur()的作用是:对输入的图像src进行均值滤波后用dst输出。
代码如下:
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('D:\\flower.jpg',0)
blur = cv2.blur(img,(3,5))
plt.subplot(1,2,1),plt.imshow(img,'gray')
plt.subplot(1,2,2),plt.imshow(blur,'gray')
plt.show()
cv2
import matplotlib.pyplot as plt
img = cv2.imread('D:\\flower.jpg',0)
blur = cv2.blur(img,(3,5))
plt.subplot(1,2,1),plt.imshow(img,'gray')
plt.subplot(1,2,2),plt.imshow(blur,'gray')
plt.show()
函数理解:
blur():网上没有找到python中这个函数的相关介绍,以后再了解吧,在C++中blur是有5个参数的,在这里只用了两个。
subplot(numRows, numCols, plotNum):划分一个numRows行,numCols列的区域。第三个参数plotNum指定了子图所在的区域。此处应注意逗号的存在。(不懂什么用)
imshow():第二个参数可选,如果没有参数则默认彩色。
show():将图片显示出来。如果没有加上plt.show(),命令行将显示图片的地址,而不会弹出图片窗口。
实现如下:
有点
二、中值滤波
实现:medianBlur函数
medianBlur函数使用中值滤波器来平滑(模糊)处理一张图片,从src输入,结果从dst输出。对于多通道图片,它对每一个通道都单独进行处理,并且支持就地操作。
代码如下:
import numpy as np
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('D:\\woman.jpg',0)
for i in range(2000):
temp_x = np.random.randint(0, img.shape[0])
temp_y = np.random.randint(0, img.shape[1])
img[temp_x][temp_y] = 255
newImg = cv2.medianBlur(img,5)
plt.subplot(1,2,1),plt.imshow(img,'gray')
plt.subplot(1,2,2),plt.imshow(newImg,'gray')
plt.show()
numpy as np
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('D:\\woman.jpg',0)
for i in range(2000):
temp_x = np.random.randint(0, img.shape[0])
temp_y = np.random.randint(0, img.shape[1])
img[temp_x][temp_y] = 255
newImg = cv2.medianBlur(img,5)
plt.subplot(1,2,1),plt.imshow(img,'gray')
plt.subplot(1,2,2),plt.imshow(newImg,'gray')
plt.show()
函数理解:
np.random.randint(a,b):生成a,b之间的随机数,然后再以a,b分别为x,y坐标画点噪声。255为白色。
实现如下:
三、高斯滤波
实现:GaussianBlur函数
代码如下:
import numpy as np
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('D:\\woman.jpg')
for i in range(2000):
Axis_x = np.random.randint(0, img.shape[0])
Axis_y = np.random.randint(0, img.shape[1])
img[temp_x][temp_y] = 255
newImg = cv2.GaussianBlur(img,(5,5),0)
plt.subplot(1,2,1),plt.imshow(img,'gray')
plt.subplot(1,2,2),plt.imshow(newImg,'gray')
plt.show()
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('D:\\woman.jpg')
for i in range(2000):
Axis_x = np.random.randint(0, img.shape[0])
Axis_y = np.random.randint(0, img.shape[1])
img[temp_x][temp_y] = 255
newImg = cv2.GaussianBlur(img,(5,5),0)
plt.subplot(1,2,1),plt.imshow(img,'gray')
plt.subplot(1,2,2),plt.imshow(newImg,'gray')
plt.show()
高斯滤波和中值滤波的代码差不多,唯一的区别是在使用的滤波函数不同。
实现如下:
版权声明:本文为ChanKi_原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。