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窗口效果良好,既有更好的去噪效果,且不易模糊圖像。