首次完成时间:2024 年 10 月 22 日
去除图像噪声
一.实验内容
1. 根据均匀噪声的原理,设计添加均匀噪声。
1)程序代码:
import cv2
import random as rd
import numpy as np
def avg_noise(src, noise_num):
img_noise = src.copy()
rows, cols, chn = img_noise.shape
for i in range(noise_num):
x = np.random.uniform(0, rows)
y = np.random.uniform(0, cols)
img_noise[int(x), int(y), :] = 255
return img_noise
if __name__ == '__main__':
# 读取图像
src = cv2.imread('../cat.png')
# 加噪声
avgnoised = avg_noise(src, 1000)
cv2.imshow('input', src)
cv2.imshow('avg', avgnoised)
cv2.waitKey(0)
cv2.destroyAllWindows()
2)代码分析:
2. 根据高斯噪声的原理,设计添加高斯噪声。
1)程序代码:
import cv2
import numpy as np
import random
# (2)添加高斯噪声
def GaussianNoise(src, mean, sigma, percetage):
NoiseImg = src.copy()
NoiseNum = int(percetage * src.shape[0] * src.shape[1])
for i in range(NoiseNum):
randX = random.randint(0, src.shape[0] - 1)
randY = random.randint(0, src.shape[1] - 1)
randomGauss = np.random.normal(mean, sigma)
for channel in range(NoiseImg.shape[2]):
NoiseImg[randX, randY, channel] = NoiseImg[randX, randY, channel] + randomGauss
if NoiseImg[randX, randY, channel] < 0:
NoiseImg[randX, randY, channel] = 0
elif NoiseImg[randX, randY, channel] > 255:
NoiseImg[randX, randY, channel] = 255
return NoiseImg
if __name__ == '__main__':
src = cv2.imread('../cat.png')
gdst = GaussianNoise(src, 10, 20, 0.1)
cv2.imshow('src', src)
cv2.imshow('gaussian', gdst)
cv2.waitKey(0)
cv2.destroyAllWindows()
2)代码分析:
3. 根据椒盐噪声的原理,设计添加椒盐噪声。
1)程序代码:
import cv2
from numpy import *
def PepperandSalt(src, percetage):
NoiseImg = src.copy()
NoiseNum = int(percetage * src.shape[0] * src.shape[1])
for i in range(NoiseNum):
randX = random.randint(0, src.shape[0] - 1)
randY = random.randint(0, src.shape[1] - 1)
if random.randint(0, 1) <= 0.5:
NoiseImg[randX, randY] = 0
else:
NoiseImg[randX, randY] = 255
return NoiseImg
if __name__ == '__main__':
src = cv2.imread('../cat.png')
imnoised = PepperandSalt(src, 0.05)
cv2.imshow('src', src)
cv2.imshow('imnoised', imnoised)
cv2.waitKey(0)
cv2.destroyAllWindows()
2)代码分析:
4. 根据泊松噪声噪声的原理,设计添加泊松噪声。
1)程序代码:
import cv2
import numpy as np
def PoissonNoise(src, lam):
NoiseImg = src.copy()
# lam >= 0 值越小,噪声频率越少
# 生成与图像尺寸相同的泊松噪声
poissonNoise = np.random.poisson(lam=lam, size=NoiseImg.shape[:2]).astype(np.uint8)
# 为每个颜色通道添加噪声
for i in range(NoiseImg.shape[2]):
NoiseImg[:, :, i] = NoiseImg[:, :, i] + poissonNoise
# 确保像素值在0-255范围内
NoiseImg = np.clip(NoiseImg, 0, 255).astype(np.uint8)
return NoiseImg
if __name__ == '__main__':
src = cv2.imread('../cat.png') # 确保路径正确
psnoised = PoissonNoise(src, 20)
cv2.imshow('input', src)
cv2.imshow('poisson', psnoised)
cv2.waitKey(0)
cv2.destroyAllWindows()
2)代码分析:
5. 分别使用均值滤波、高斯滤波、中值滤波对图像做平滑处理。
1)程序代码:
import cv2
src = cv2.imread('gaussian_noise.jpg')
dst = cv2.blur(src, (5, 5))
gdst = cv2.GaussianBlur(src, (5, 5), 0, 0)
mdst = cv2.medianBlur(src, 5)
cv2.imshow('inosed', src)
cv2.imshow('blur', dst)
cv2.imshow('GaussianBlur', gdst)
cv2.imshow('medianBlur', mdst)
cv2.waitKey(0)
cv2.destroyAllWindows()
2)代码分析:
6. 使用erode()和dilate()函数对图像做腐蚀操作和膨胀操作实验,比实验效果。
1)程序代码:
import cv2
src = cv2.imread("gaussian_noise.jpg")
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10))
gray_res = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
# 腐蚀操作
erode = cv2.erode(gray_res, kernel)
# 膨胀操作
dilate = cv2.dilate(gray_res, kernel)
cv2.imshow('src', src)
cv2.imshow("erode", erode)
cv2.imshow('dilate', dilate)
cv2.waitKey(0)
cv2.destroyAllWindows()
2)代码分析:
7. 使用morphologyEx()对图像做开运算和闭运算,比运算效果。
1)程序代码:
import cv2
src = cv2.imread('cat.png')
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
gray_res = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
# 开运算
opened = cv2.morphologyEx(gray_res, cv2.MORPH_OPEN, kernel, iterations=1)
# 闭运算
closed = cv2.morphologyEx(gray_res, cv2.MORPH_DILATE, kernel, iterations=1)
cv2.imshow('input', src)
cv2.imshow('opened', opened)
cv2.imshow('closed', closed)
cv2.waitKey(0)
cv2.destroyAllWindows()