数字图像处理三-1(平滑空间滤波器)
添加3000点噪声
import cv2
import numpy as np
import matplotlib as plt
def addNoise(imgPath):
# 读取图像,cv2以BGR读取彩色图像
img = cv2.imread(imgPath)
# 随机添加噪声
noiseImg = img
row, col, chn = noiseImg.shape # 获取图像大小
for i in range(3000):
x = np.random.randint(0, row) # 随机整数获取
y = np.random.randint(0, col)
noiseImg[x, y, :] = 255
return noiseImg
noiseImg = addNoise("cat.JPG")
# 显示图像
cv2.imshow("noiseImg", noiseImg)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 保存图像
cv2.imwrite("catNoiseImg.jpg", noiseImg)
盒状滤波器
cv2.blur(img, (3, 3)),模糊核大小为3 * 3,模糊核越大图像会越模糊
import cv2
import numpy as np
import matplotlib.pylab as plt
# 读取噪声图像
img = cv2.imread("catNoiseImg.jpg")
#img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # bgr转为rgb,因为matplotlib显示方式为rgb
# 盒状滤波器
mean3Img = cv2.blur(img, (3, 3)) # 模糊核大小为3*3
mean5Img = cv2.blur(img, (5, 5)) # 模糊核大小为5*5
mean10Img = cv2.blur(img, (10, 10)) # 模糊核大小为10*10
# 显示图像
cv2.imshow("noiseImg", img)
cv2.imshow("cv2_blur_3", mean3Img)
cv2.imshow("cv2_blur_5", mean5Img)
cv2.imshow("cv2_blur_10", mean10Img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 保存图像
cv2.imwrite("cv2_blur_3.jpg", mean3Img)
cv2.imwrite("cv2_blur_5.jpg", mean5Img)
cv2.imwrite("cv2_blur_10.jpg", mean10Img)
'''
# plt显示图像
titles = ['noiseImg','blur3*3','blur5*5','blur10*10']
imgs = [img,mean3Img,mean5Img,mean10Img]
for i in range(4):
plt.subplot(2,2,i+1)
plt.imshow(imgs[i])
plt.title(titles[i])
plt.xticks([])
plt.yticks([])
plt.savefig('cv2_blur.png') # plt保存图像
plt.show()
'''
# 只能奇数
import cv2
import numpy as np
import matplotlib as plt
def myMeanFilter(imgPath, len):
# 读取图像及其大小、bgr
img = cv2.imread(imgPath)
rows, cols, channels = img.shape
padNum = int((len - 1) / 2) # 填充长度
b, g, r = cv2.split(img) # 分离bgr
# bgr分别填充
bPadImg = np.zeros((rows + 2 * padNum, cols + 2 * padNum))
bPadImg[padNum:padNum + rows, padNum:padNum + cols] = b # 中间复制
gPadImg = np.zeros((rows + 2 * padNum, cols + 2 * padNum))
gPadImg[padNum:padNum + rows, padNum:padNum + cols] = g
rPadImg = np.zeros((rows + 2 * padNum, cols + 2 * padNum))
rPadImg[padNum:padNum + rows, padNum:padNum + cols] = r
# 滤波器
meanFilter = np.ones((len, len))
meanFilter = meanFilter/(len*len)
for i in range(rows):
for j in range(cols):
b[i, j] = np.sum(bPadImg[i:i+len, j:j+len] * meanFilter)
g[i, j] = np.sum(gPadImg[i:i+len, j:j+len] * meanFilter)
r[i, j] = np.sum(rPadImg[i:i+len, j:j+len] * meanFilter)
res = cv2.merge((b,g,r))
return res
imgPath = "catNoiseImg.jpg"
img = cv2.imread(imgPath)
myMean3Img = myMeanFilter(imgPath, 3)
myMean5Img = myMeanFilter(imgPath, 5)
myMean9Img = myMeanFilter(imgPath, 9)
# 显示图像
cv2.imshow("noiseImg",img)
cv2.imshow("my_mean_blur_3", myMean3Img)
cv2.imshow("my_mean_blur_5", myMean5Img)
cv2.imshow("my_mean_blur_9", myMean9Img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 保存图像
cv2.imwrite("my_mean_blur_3.jpg", myMean3Img)
cv2.imwrite("my_mean_blur_5.jpg", myMean5Img)
cv2.imwrite("my_mean_blur_9.jpg", myMean9Img)
中值滤波器
cv2.medianBlur(img, 7),size必须为奇数,表示模糊核的长度,越大越模糊
import cv2
import numpy as np
import matplotlib.pylab as plt
# 读取噪声图像
img = cv2.imread("catNoiseImg.jpg")
# 中值滤波器
median3Img = cv2.medianBlur(img, 3)
median5Img = cv2.medianBlur(img, 5)
median9Img = cv2.medianBlur(img, 9)
# 显示图像
cv2.imshow("noiseImg", img)
cv2.imshow("cv2_median_blur_3", median3Img)
cv2.imshow("cv2_median_blur_5", median5Img)
cv2.imshow("cv2_median_blur_9", median9Img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 保存图像
cv2.imwrite("cv2_median_blur_3.jpg", median3Img)
cv2.imwrite("cv2_median_blur_5.jpg", median5Img)
cv2.imwrite("cv2_median_blur_9.jpg", median9Img)
import cv2
import numpy as np
import matplotlib.pylab as plt
def myMedianBlur(imgPath, len):
img = cv2.imread(imgPath)
rows, cols, channels = img.shape
padNum = int((len - 1) / 2) # 填充长度
b, g, r = cv2.split(img) # 分离bgr
bPadImg = np.zeros((rows + 2 * padNum, cols + 2 * padNum))
bPadImg[padNum:padNum + rows, padNum:padNum + cols] = b # 中间复制
gPadImg = np.zeros((rows + 2 * padNum, cols + 2 * padNum))
gPadImg[padNum:padNum + rows, padNum:padNum + cols] = g
rPadImg = np.zeros((rows + 2 * padNum, cols + 2 * padNum))
rPadImg[padNum:padNum + rows, padNum:padNum + cols] = r
for i in range(rows):
for j in range(cols):
b[i, j] = np.median(bPadImg[i:i+len, j:j+len]) # np中矩阵寻找中位数
g[i, j] = np.median(gPadImg[i:i+len, j:j+len])
r[i, j] = np.median(rPadImg[i:i+len, j:j+len])
res = cv2.merge((b,g,r))
return res
imgPath = "catNoiseImg.jpg"
img = cv2.imread(imgPath)
myMedian3Img = myMedianBlur(imgPath, 3)
myMedian5Img = myMedianBlur(imgPath, 5)
myMedian9Img = myMedianBlur(imgPath, 9)
# 显示图像
cv2.imshow("noiseImg", img)
cv2.imshow("my_median_blur_3", myMedian3Img)
cv2.imshow("my_median_blur_5", myMedian5Img)
cv2.imshow("my_median_blur_9", myMedian9Img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 保存图像
cv2.imwrite("my_median_blur_3.jpg", myMedian3Img)
cv2.imwrite("my_median_blur_5.jpg", myMedian5Img)
cv2.imwrite("my_median_blur_9.jpg", myMedian9Img)
高斯滤波器
cv2.GaussianBlur(img,(5,5),sigmax,sigmay)
import cv2
import numpy as np
import matplotlib as plt
# 读取噪声图像
img = cv2.imread("catNoiseImg.jpg")
# 高斯滤波器
Gaussian3Img = cv2.GaussianBlur(img, (3,3), 0, 0)
Gaussian5Img = cv2.GaussianBlur(img, (5,5), 0, 0)
# 显示图像
cv2.imshow("noiseImg", img)
cv2.imshow("cv2_gaussian_blur_3", Gaussian3Img)
cv2.imshow("cv2_ganssian_blur_5", Gaussian5Img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 保存图像
cv2.imwrite("cv2_gaussian_blur_3.jpg", Gaussian3Img)
cv2.imwrite("cv2_gaussian_blur_5.jpg", Gaussian5Img)