作业代码及内容展示和分析:
1.代码
import cv2 as cv
import random as rd
import matplotlib.pyplot as plt
import numpy as np
le = cv.imread(‘lena.jpg’,0)
lena = le.copy()
def saltNoise_image(im,var): #生成椒盐噪声图函数
w,h = im.shape
saltNoiseNum = int(w*h*var) #噪声总的点数
all_xy_pos = [[[i,j] for j in range(h)]for i in range(w)]
xy = []
for i in all_xy_pos:
xy.extend(i)
salt_xy = rd.sample(xy,saltNoiseNum)
for i,j in salt_xy:
ra = rd.choice([0,1])
if ra == 0:
im[i,j] = 0
else:
im[i,j] = 255
return im
def border_image(im):
im[im==255] = 0
h,w = im.shape
im_mask = np.zeros((h+8,w+8),dtype=np.uint8)
im_mask[4:h+4,4:w+4] = im
return im_mask
def get_median(arr):
x = arr[arr!=0]
x.sort()
half = len(x)//2
if len(x)%2 == 0:
return x[half-1]//2 + x[half]//2
else:
return x[half]
def coreMat_cmt(a,b,im):
arr = im[a-1:a+2,b-1:b+2]
if arr.sum() == 0:
arr = im[a-2:a+3,b-2:b+3]
if arr.sum() == 0:
arr = im[a-3:a+4,b-3:b+4]
if arr.sum() == 0:
arr = im[a-4:a+5,b-4:b+5]
if arr.sum() == 0:
return 0
else:
gray = get_median(arr)
return gray
else:
gray = get_median(arr)
return gray
else:
gray = get_median(arr)
return gray
else:
gray = get_median(arr)
return gray
def run_filter(im):
h,w = im.shape
im_mask = np.zeros((h-8,w-8),dtype=np.uint8)
for i in range(4,h-4):
for j in range(4,w-4):
if im[i,j] ==0:
new_gray = coreMat_cmt(i,j,im)
im_mask[i-4,j-4] = new_gray
else:
im_mask[i-4,j-4] = im[i,j]
return im_mask
def filter_image(im):
for i in range(5):
border_im = border_image(im)
im = run_filter(border_im)
return im
noise_lena = saltNoise_image(lena,0.9)
filter_lena = filter_image(noise_lena)
cv.imshow(‘0’,le)
cv.imshow(‘1’,noise_lena)
cv.imshow(‘2’,filter_lena)
x = 0
for i in range(lena.shape[0]):
for j in range(lena.shape[1]):
x += abs(int(le[i,j]) – int(filter_lena[i,j]))
print(x/(400*400))
if cv.waitKey() == ord(‘A’):
cv.destroyAllWindows()
2.lena原图、90%强度噪声lena图像、恢复后图像;MSE为85.9
分析:
依据脉冲噪声的极值准则和不连续准则,利用D-S证据理论进行信息融合,在噪声修复阶段,对于检测窗口内非噪声点的中值,利用距离矩阵和偏差矩阵进行修正。
今天的文章现代数字图像处理作业—对lena.bmp图像转成灰度图后,加入90%强度的椒盐噪声。尝试任何方法去恢复图像。分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/30810.html