大家好,又见面了,我是你们的朋友全栈君。
作业代码及内容展示和分析:
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证据理论进行信息融合,在噪声修复阶段,对于检测窗口内非噪声点的中值,利用距离矩阵和偏差矩阵进行修正。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/151642.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...