import copy
import numpy as np
import random
使棚轮用的是pycharm
因为最近看了《银翼杀手2049》,里面Joi实在是太好看了所以链镇信原图像就用Joi了
要求是灰度图像,所以第一步先把图像转化成灰度图像
# 读入原始图像
img = CV2.imread('joi.jpg')
# 灰度化处理
gray = CV2.cvtColor(img, CV2.COLOR_BGR2GRAY)
CV2.imwrite('img.png', gray)
第一个任务是利用分段函数增强灰度对比,我自己随便写了个函数大致是这样的
def chng(a):
if a <255/3:
b = a/2
elif a <255/3*2:
b = (a-255/3)*2 + 255/6
else:
b = (a-255/3*2)/2 + 255/6 +255/3*2
return b
rows = img.shape[0]
cols = img.shape[1]
cover = copy.deepcopy(gray)
for i in range(rows):
for j in range(cols):
cover[i][j] = chng(cover[i][j])
CV2.imwrite('cover.png', cover)
下一步是直方图均衡化
# histogram equalization
def hist_equal(img, z_max=255):
H, W = img.shape
# S is the total of pixels
S = H * W * 1.
out = img.copy()
sum_h = 0.
for i in range(1, 255):
ind = np.where(img == i)
sum_h += len(img[ind])
z_prime = z_max / S * sum_h
out[ind] = z_prime
out = out.astype(np.uint8)
return out
covereq = hist_equal(cover)
CV2.imwrite('covereq.png', covereq)
在实现滤波之前先添加高斯噪声和椒盐噪声(代码来源旅兆于网络)
不知道这个椒盐噪声的名字是谁起的感觉隔壁小孩都馋哭了
用到了random.gauss()
percentage是噪声占比
def GaussianNoise(src,means,sigma,percetage):
NoiseImg=src
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)
NoiseImg[randX, randY]=NoiseImg[randX,randY]+random.gauss(means,sigma)
if NoiseImg[randX, randY]<0:
NoiseImg[randX, randY]=0
elif NoiseImg[randX, randY]>255:
NoiseImg[randX, randY]=255
return NoiseImg
def PepperandSalt(src,percetage):
NoiseImg=src
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
covereqg = GaussianNoise(covereq, 2, 4, 0.8)
CV2.imwrite('covereqg.png', covereqg)
covereqps = PepperandSalt(covereq, 0.05)
CV2.imwrite('covereqps.png', covereqps)
下面开始均值滤波和中值滤波了
就以n x n为例,均值滤波就是用这n x n个像素点灰度值的平均值代替中心点,而中值就是中位数代替中心点,边界点周围补0;前两个函数的作用是算出这个点的灰度值,后两个是对整张图片进行
#均值滤波模板
def mean_filter(x, y, step, img):
sum_s = 0
for k in range(x-int(step/2), x+int(step/2)+1):
for m in range(y-int(step/2), y+int(step/2)+1):
if k-int(step/2) 0 or k+int(step/2)+1 >img.shape[0]
or m-int(step/2) 0 or m+int(step/2)+1 >img.shape[1]:
sum_s += 0
else:
sum_s += img[k][m] / (step*step)
return sum_s
#中值滤波模板
def median_filter(x, y, step, img):
sum_s=[]
for k in range(x-int(step/2), x+int(step/2)+1):
for m in range(y-int(step/2), y+int(step/2)+1):
if k-int(step/2) 0 or k+int(step/2)+1 >img.shape[0]
or m-int(step/2) 0 or m+int(step/2)+1 >img.shape[1]:
sum_s.append(0)
else:
sum_s.append(img[k][m])
sum_s.sort()
return sum_s[(int(step*step/2)+1)]
def median_filter_go(img, n):
img1 = copy.deepcopy(img)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
img1[i][j] = median_filter(i, j, n, img)
return img1
def mean_filter_go(img, n):
img1 = copy.deepcopy(img)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
img1[i][j] = mean_filter(i, j, n, img)
return img1
完整main代码如下:
if __name__ == "__main__":
# 读入原始图像
img = CV2.imread('joi.jpg')
# 灰度化处理
gray = CV2.cvtColor(img, CV2.COLOR_BGR2GRAY)
CV2.imwrite('img.png', gray)
rows = img.shape[0]
cols = img.shape[1]
cover = copy.deepcopy(gray)
for i in range(rows):
for j in range(cols):
cover[i][j] = chng(cover[i][j])
CV2.imwrite('cover.png', cover)
covereq = hist_equal(cover)
CV2.imwrite('covereq.png', covereq)
covereqg = GaussianNoise(covereq, 2, 4, 0.8)
CV2.imwrite('covereqg.png', covereqg)
covereqps = PepperandSalt(covereq, 0.05)
CV2.imwrite('covereqps.png', covereqps)
meanimg3 = mean_filter_go(covereqps, 3)
CV2.imwrite('medimg3.png', meanimg3)
meanimg5 = mean_filter_go(covereqps, 5)
CV2.imwrite('meanimg5.png', meanimg5)
meanimg7 = mean_filter_go(covereqps, 7)
CV2.imwrite('meanimg7.png', meanimg7)
medimg3 = median_filter_go(covereqg, 3)
CV2.imwrite('medimg3.png', medimg3)
medimg5 = median_filter_go(covereqg, 5)
CV2.imwrite('medimg5.png', medimg5)
medimg7 = median_filter_go(covereqg, 7)
CV2.imwrite('medimg7.png', medimg7)
medimg4 = median_filter_go(covereqps, 7)
CV2.imwrite('medimg4.png', medimg4)
1、打开Matlab,点击“新建脚本”,如下图所示。
2、在代码编辑区输入代码,先利用Imread函数读取图像,然后使用Imshow函数将图像展示出来,以便与添加噪声后的图像做对比。
3、Matlab中利用imnoise函数差族携可以添加各种噪声,而使用“gaussian”参数就可以添加高斯噪声。
4、选择保存位虚伏置,并为m文件重命名,穗帆如下图所示所示,设置完成,点击“保存”。
5、这样,就在Matlab中为图像添加了高斯噪声,并且把一组对比图显示在同一个窗口中,如下图所示。
一. 均值滤波简介和原理
晌扒 均值滤波,是图像处理中常用的手段,从频率域观点来看均值滤波是一种低通滤波器,高频信号将会去掉。均值滤波可以帮助消除图像尖锐噪声,实现图像平滑,模糊等功能。理想的均值滤波是用每个像素和它周围像素计算出来岁蠢的平均值替换图像中每个像素。
以3*3均值滤波器为例,均值滤波器算法宴雀昌原理如下图:
二. 用均值滤波器对椒盐噪声污染后的图像去噪
python 源码:
import cv2
import numpy as np
# mean filter
def mean_filter(img, K_size=3):
H, W, C = img.shape
# zero padding
pad = K_size // 2
out = np.zeros((H + pad * 2, W + pad * 2, C), dtype=np.float)
out[pad: pad + H, pad: pad + W] = img.copy().astype(np.float)
tmp = out.copy()
# filtering
for y in range(H):
for x in range(W):
for c in range(C):
out[pad + y, pad + x, c] = np.mean(tmp[y: y + K_size, x: x + K_size, c])
out = out[pad: pad + H, pad: pad + W].astype(np.uint8)
return out
# Read image
img = cv2.imread("../paojie_sp1.jpg")
# Mean Filter
out = mean_filter(img, K_size=5)
# Save result
cv2.imwrite("out.jpg", out)
cv2.imshow("result", out)
cv2.waitKey(0)
cv2.destroyAllWindows()
三. 实验结果:
可以看到,均值滤波后,图像中噪声虽然有所减弱,但是图像变模糊了。因为均值滤波器过滤掉了图像中的高频分量,所以图像的边缘都变模糊了。(去除一定量椒盐噪声,可以考虑使用中值滤波)
四. 参考内容:
https://www.cnblogs.com/wojianxin/p/12501891.html
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)