现代数字图像处理作业---对lena.bmp图像转成灰度图后,加入90%强度的椒盐噪声。尝试任何方法去恢复图像。
作業(yè)代碼及內(nèi)容展示和分析:
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):????????????????????????????? #生成椒鹽噪聲圖函數(shù)
?????? w,h = im.shape
?????? saltNoiseNum = int(w*h*var)???????????????????????????? #噪聲總的點數(shù)
?????? 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
分析:
依據(jù)脈沖噪聲的極值準則和不連續(xù)準則,利用D-S證據(jù)理論進行信息融合,在噪聲修復階段,對于檢測窗口內(nèi)非噪聲點的中值,利用距離矩陣和偏差矩陣進行修正。
?
總結(jié)
以上是生活随笔為你收集整理的现代数字图像处理作业---对lena.bmp图像转成灰度图后,加入90%强度的椒盐噪声。尝试任何方法去恢复图像。的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Warez出品的精品动画
- 下一篇: 解决Linux下使用QQ的问题