import cv2
import numpy as np
#1.单独使用rayleigh噪声
img = cv2.imread('D:/PmridProject/flower.jpg',0)
#读取图片
print(img.shape)
img_height=img.shape[0]
img_width=img.shape[1]
#随机生成一个服从分布的噪声
noise = np.random.rayleigh(0.4, size=img.shape)
#给图片添加speckle噪声
noisy_img = img + img * noise
#归一化图像的像素值
noisy_img = np.clip(noisy_img,a_min=0,a_max=255)
#保存图片
cv2.imwrite("noisy_reli.jpg",noisy_img)
#2.添加高斯与斑点噪声 运用在花图片上验证
import cv2
import numpy as np
img = cv2.imread('D:/PmridProject/flower.jpg',0)
mu, sigma = 0.0, 0.8
noiseGause = np.random.normal(mu, sigma, img.shape)
imgGaussNoise = img + noiseGause
imgGaussNoise = np.uint8(cv2.normalize(imgGaussNoise, None, 0, 255, cv2.NORM_MINMAX))
cv2.imwrite("noisy_gauss.jpg",imgGaussNoise)
img = cv2.imread('D:/PmridProject/noisy_gauss.jpg',0)
noise = np.random.rayleigh(0.2, size=img.shape)
#给图片添加speckle噪声
noisy_img = img + img * noise
#归一化图像的像素值
noisy_img = np.clip(noisy_img,a_min=0,a_max=255)
cv2.imwrite("noisyall.jpg",noisy_img)
#3.添加高斯与斑点噪声 运用在EUS图像上验证
import cv2
import numpy as np
img = cv2.imread('F:/img2test/1_654.bmp',0)
mu, sigma = 0.0, 0.8
noiseGause = np.random.normal(mu, sigma, img.shape)
imgGaussNoise = img + noiseGause
imgGaussNoise = np.uint8(cv2.normalize(imgGaussNoise, None, 0, 255, cv2.NORM_MINMAX))
noise = np.random.rayleigh(0.2, size=img.shape)
noisy_img = imgGaussNoise+ imgGaussNoise * noise
#归一化图像的像素值
# noisy_img = np.clip(noisy_img,a_min=0,a_max=255)
#使用归一化像素且使用 normalize
noisy_img = np.uint8(cv2.normalize(noisy_img, None, 0, 255, cv2.NORM_MINMAX))
cv2.imwrite("654_normalize.jpg",noisy_img)
#4.添加运动模糊
def motion_blur(image):
degree = 7
angle = 0
image = np.array(image)
# 这里生成任意角度的运动模糊kernel的矩阵, degree越大,模糊程度越高
M = cv2.getRotationMatrix2D((degree / 2, degree / 2), angle, 1)
motion_blur_kernel = np.diag(np.ones(degree))
motion_blur_kernel = cv2.warpAffine(motion_blur_kernel, M, (degree, degree))
motion_blur_kernel = motion_blur_kernel / degree
blurred = cv2.filter2D(image, -1, motion_blur_kernel)
# convert to uint8
cv2.normalize(blurred, blurred, 0, 255, cv2.NORM_MINMAX)
blurred = np.array(blurred, dtype=np.uint8)
return blurred
image=cv2.imread('654_normalize.jpg')
image1=motion_blur(image)
cv2.imwrite("motion.jpg",image1)
注意点:
a.
#归一化图像的像素值
noisy_img = np.clip(noisy_img,a_min=0,a_max=255)
#使用归一化像素且使用 normalize
noisy_img = np.uint8(cv2.normalize(noisy_img, None, 0, 255, cv2.NORM_MINMAX))
b.斑点噪声与瑞利噪声都是乘性噪声
#给图片添加speckle噪声
noisy_img = img + img * noise