1 添加椒盐噪声
原始图像文件:

过程文件:
Pro add_saltpepper
;read image
filename=dialog_pickfile(title='Please Choose the Image:')
img=read_image(filename)
;add 13% salt and pepper noise
img_noise=saltpepper(img,0.12)
;get the size of image
sz=size(img)
;get the image's numbers of columns and rows
sum_columns=sz[1] & sum_rows=sz[2]
;creat a window which can contain 2 images and some text
win1=window(dimension=[sum_columns*2+10,sum_rows+50])
;set the attributes of first image,containing position and so on
img1=image(img,position=[0,50,sum_columns-1,sum_rows+50-1],/device,/current)
;set the attributes of second image,containing position and so on
img2=image(img_noise,position=[sum_columns+10,50,sum_columns*2+10-1,sum_rows+50-1],/device,/current)
;set the title of first image
text1=text(sum_columns/2,20,'Lena Original Image',target=win1,alignment=0.5,/device)
;set the title of second image
text2=text(sum_columns*1.5+20,20,'Image Containing 12% Salt & Pepper Noise',target=win1,alignment=0.5,/device)
result=dialog_write_image(img_noise,filename=Lena_noise,type='jpeg',/fix_type,path='V:\Images',title='Save Image File:')
End
函数文件:
Function saltpepper,img,rate
;create a random array which is the same number as the image
num_array=n_elements(img)
random_array=randomu(sends,num_array)
;sort the random array
sort_array=sort(random_array)
;make the value of pepper noise be 0 and the value of salt noise be 255,each half
result=img
result[sort_array[0:num_array*rate/2-1]]=0
result[sort_array[num_array*rate/2:num_array*rate-1]]=255
return,result
End


2 图像去噪
(1)均值滤波
Pro img_smooth
;read image
;filename1=dialog_pickfile(title='Please Choose the Original Image:')
filename2=dialog_pickfile(title='Please Choose the Noise Image:')
;img=read_image(filename1)
img_noise=read_image(filename2)
;get the size of image
sz=size(img_noise)
;get the image's numbers of columns and rows
sum_columns=sz[1] & sum_rows=sz[2]
;mean smoothed of 5*5 window
img_smooth_5=smooth(img_noise,5)
;creat a window which can contain 3 images and some text
win1=window(dimension=[sum_columns*2+10,sum_rows+50])
;set the attributes of first image,containing position and so on
;img1=image(img,position=[0,50,sum_columns-1,sum_rows+50-1],/device,/current)
;set the attributes of second image,containing position and so on
img2=image(img_noise,position=[0,50,sum_columns-1,sum_rows+50-1],/device,/current)
;set the attributes of third image,containing position and so on
img3=image(img_smooth_5,position=[sum_columns+10,50,sum_columns*2+10-1,sum_rows+50-1],/device,/current)
;set the title of first image
;text1=text(sum_columns/2,20,'Lena Original Image',target=win1,alignment=0.5,/device)
;set the title of second image
text2=text(sum_columns*0.5,20,'Image Containing 12% Salt & Pepper Noise',target=win1,alignment=0.5,/device)
;set the title of first image
text1=text(sum_columns*1.5+10,20,'Lena Image After Smooth',target=win1,alignment=0.5,/device)
;save the smooth image
result=dialog_write_image(img_smooth_5,filename=Lena_smooth,type='jpeg',/fix_type,path='V:\Images',title='Save Image File:')
End
代码:
;mean smoothed of 5*5 window
img_smooth_5=smooth(img_noise,5)
中可以修改滤波窗口大小,可以看出,窗口越大,去噪效果越好,但图像越模糊。
3x3窗口:

5x5窗口:

9x9窗口:

(2)中值滤波
Pro img_median
;read image
;filename1=dialog_pickfile(title='Please Choose the Original Image:')
filename2=dialog_pickfile(title='Please Choose the Noise Image:')
;img=read_image(filename1)
img_noise=read_image(filename2)
;get the size of image
sz=size(img_noise)
;get the image's numbers of columns and rows
sum_columns=sz[1] & sum_rows=sz[2]
;mean medianed of 5*5 window
img_median_5=median(img_noise,5)
;creat a window which can contain 3 images and some text
win1=window(dimension=[sum_columns*2+10,sum_rows+50])
;set the attributes of first image,containing position and so on
;img1=image(img,position=[0,50,sum_columns-1,sum_rows+50-1],/device,/current)
;set the attributes of second image,containing position and so on
img2=image(img_noise,position=[0,50,sum_columns-1,sum_rows+50-1],/device,/current)
;set the attributes of third image,containing position and so on
img3=image(img_median_5,position=[sum_columns+10,50,sum_columns*2+10-1,sum_rows+50-1],/device,/current)
;set the title of first image
;text1=text(sum_columns/2,20,'Lena Original Image',target=win1,alignment=0.5,/device)
;set the title of second image
text2=text(sum_columns*0.5,20,'Image Containing 12% Salt & Pepper Noise',target=win1,alignment=0.5,/device)
;set the title of first image
text1=text(sum_columns*1.5+10,20,'Lena Image After Median',target=win1,alignment=0.5,/device)
;save the median image
result=dialog_write_image(img_median_5,filename=Lena_median,type='jpeg',/fix_type,path='V:\Images',title='Save Image File:')
End
代码:
;mean medianed of 5*5 window
img_median_5=median(img_noise,5)
中可以修改滤波窗口大小,可以看出,窗口越大,去噪效果越好,但图像越模糊。
3x3窗口:

5x5窗口:

9x9窗口:

(3)高斯滤波
利用卷积运算自定义高斯卷积核进行图像滤波去噪:
Pro img_convol_gauss
;read image
;filename1=dialog_pickfile(title='Please Choose the Original Image:')
filename2=dialog_pickfile(title='Please Choose the Noise Image:')
;img=read_image(filename1)
img_noise=read_image(filename2)
;get the size of image
sz=size(img_noise)
;get the image's numbers of columns and rows
sum_columns=sz[1] & sum_rows=sz[2]
;define the kernel of convol
kernel=[[1,4,7,4,1],[4,16,26,16,4],[7,26,41,26,7],[4,16,26,16,4],[1,4,7,4,1]]/273.0
;mean smoothed of 5*5 window
img_convol=convol(float(img_noise),kernel,/edge_truncate)
;creat a window which can contain 3 images and some text
win1=window(dimension=[sum_columns*2+10,sum_rows+50])
;set the attributes of first image,containing position and so on
;img1=image(img,position=[0,50,sum_columns-1,sum_rows+50-1],/device,/current)
;set the attributes of second image,containing position and so on
img2=image(img_noise,position=[0,50,sum_columns-1,sum_rows+50-1],/device,/current)
;set the attributes of third image,containing position and so on
img3=image(img_convol,position=[sum_columns+10,50,sum_columns*2+10-1,sum_rows+50-1],/device,/current)
;set the title of first image
;text1=text(sum_columns/2,20,'Lena Original Image',target=win1,alignment=0.5,/device)
;set the title of second image
text2=text(sum_columns*0.5,20,'Image Containing 12% Salt & Pepper Noise',target=win1,alignment=0.5,/device)
;set the title of first image
text1=text(sum_columns*1.5+10,20,'Lena Image After Gauss',target=win1,alignment=0.5,/device)
;save the gauss image
result=dialog_write_image(img_convol,filename=Lena_gauss,type='jpeg',/fix_type,path='V:\Images',title='Save Image File:')
End
利用卷积运算自定义高斯卷积核进行图像滤波去噪,卷积核为:
;define the kernel of convol
kernel=[[1,4,7,4,1],[4,16,26,16,4],[7,26,41,26,7],[4,16,26,16,4],[1,4,7,4,1]]/273.0

结论
综上,可以看出,三种滤波方法(均值滤波,中值滤波,高斯滤波)对椒盐噪声的响应各有不同,其中中值滤波最适合去除椒盐噪声,而且效果最好。这取决于其卷积核特性,对每个窗口的像素值取其中间值,可以直接利用窗口内的正常像素替换椒盐噪声这种突变式影响,更有利于填补这些缺口,优化图像。此图中5*5窗口效果良好,既有更好的去噪效果,且不易模糊图像。
简单书写,
希望你十分美好!
网友评论