对图像增强 添加高斯噪声和possion噪声

       核心的思想: 对图片中的某个元素增加噪声,然后返回添加噪声以后的图片。

代码:

# 对图像增加高斯噪声,possion噪声
from numpy import *
from scipy import *
import numpy as np
import cv2
srcImage=cv2.imread("lena.jpg")
print(srcImage.shape)
cv2.namedWindow("origin image")
cv2.imshow("origin image",srcImage)
k=cv2.waitKey(0)

# 把原始的图像转化为灰度的图像
gray_image=cv2.cvtColor(srcImage,cv2.COLOR_BGR2GRAY)
print(gray_image.shape)
cv2.imshow("gray image",gray_image)
k=cv2.waitKey(0)

image=np.array(gray_image/255,dtype=float)
percent=0.01
num=int(percent*image.shape[0]*image.shape[1])
print(num)

for i in range(num):
    # 获得随机的宽和高
    temp1=np.random.randint(image.shape[0])
    temp2=np.random.randint(image.shape[1])
    mean=0
    var=0.04
    noise=np.random.normal(mean,var**0.5,1)
    image[temp1][temp2]+=noise
out=image
print(out)
if out.min()<0:
    low_clip=-1
else:
    low_clip=0
out=np.clip(out,low_clip,1)
gauss_image=np.array(out*255,dtype='uint8')
print(gauss_image.shape)
cv2.imshow("gauss image",gauss_image)
k=cv2.waitKey(0)


image=np.array(gray_image/255,dtype=float)
percent=0.001
num=int(percent*image.shape[0]*image.shape[1])
print(num)

for i in range(num):
    # 获得随机的宽和高
    temp1=np.random.randint(image.shape[0])
    temp2=np.random.randint(image.shape[1])
    scale=150
    noise=np.random.poisson(scale,1)
    image[temp1][temp2]+=noise
out=image
print(out)
if out.min()<0:
    low_clip=-1
else:
    low_clip=0
out=np.clip(out,low_clip,1)
possion_image=np.array(out*255,dtype='uint8')
print(possion_image.shape)
cv2.imshow("possion image",possion_image)
k=cv2.waitKey(0)

 

你可能感兴趣的:(对图像增强 添加高斯噪声和possion噪声)