数字图像处理相关的小练习

一  基础部分

1  产生右图所示图像f1(m,n),其中图像大小为256×256,中间亮条为128×32,暗处=0,亮处=100。对其进行FFT:

①同屏显示原图f1(m,n)和FFT(f1)的幅度谱图;

②若令f2(m,n)=(-1)(m+n)f1(m,n),重复以上过程,

比较二者幅度谱的异同,简述理由;

③若将f2(m,n)顺时针旋转90度得到f3(m,n),试显示FFT(f3)的幅度谱,

并与FFT(f2)的幅度谱进行比较;

④若将f1(m,n)顺时针旋转90度得到f4(m,n),令f5(m,n)=f1(m,n)+f4(m,n),

试显示FFT(f5)的幅度谱,并指出其与FFT(f1)和FFT(f4)的关系;

⑤若令f6(m,n)=f2(m,n)+f3(m,n),试显示FFT(f6)的幅度谱,并指出其与FFT(f2)和FFT(f3)的关系,比较FFT(f6)和FFT(f5)的幅度谱。

数字图像处理相关的小练习_第1张图片

Matlab源代码:
问题1:
I1=zeros(256);
for i=112:144
    for j=64:192
        I1(j,i)=100;
    end
end
figure;
subplot(121);
imshow(I1);
title('原图像');
subplot(122);
imshow(abs(fft2(histeq(I1))));
title('原图像的幅度谱');
数字图像处理相关的小练习_第2张图片

问题2:
I2=zeros(256);
for i=112:144
    for j=64:192
        I2(j,i)=(-1).^(i+j)*I1(j,i);
    end
end
figure;
subplot(221);
imshow(I1);
title('原图像f1');
subplot(222);
imshow(abs(fft2(histeq(I1))));
title('原图像的幅度谱');
subplot(223);
imshow(I2);
title('中心化后图像f2');
subplot(224);
imshow(abs(fft2(histeq(I2))));
title('中心化后图像f2的幅度谱');
分析:f2(m,n)相较f1(m,n)频谱中心化
数字图像处理相关的小练习_第3张图片

问题3:
I3=imrotate(I2,90,'bilinear');
figure;
subplot(131);
imshow(I2);
title('中心化后图像f2');
subplot(132);
imshow(abs(fft2(histeq(I2))));
title('中心化后图像f2的幅度谱');
subplot(133);
imshow(abs(fft2(histeq(I3))));
title('旋转后图像f3的幅度谱');
分析:f3(m,n)的FFT转换图像也是f2(m,n)的FFT转换图像左转90°所得。
数字图像处理相关的小练习_第4张图片

问题4:
I4=imrotate(I1,90,'bilinear');
I5=I4+I1;
figure;
subplot(221);
imshow(I1);
title('原图像f1');
subplot(222);
imshow(abs(fft2(histeq(I5))));
title('相加后图像f5幅度谱');
subplot(223);
imshow(abs(fft2(histeq(I1))));
title('原图像的幅度谱');
subplot(224);
imshow(abs(fft2(histeq(I4))));
title('旋转后图像f4的幅度谱');
分析:f4(m,n)相较f1(m,n)频谱顺时针旋转90度
数字图像处理相关的小练习_第5张图片

问题5:
I6=I2+I3;
figure;
subplot(221);
imshow(abs(fft2(histeq(I6))));
title('相加后图像f6幅度谱');
subplot(222);
imshow(abs(fft2(histeq(I5))));
title('相加后图像f5的幅度谱');
subplot(223);
imshow(abs(fft2(histeq(I2))));
title('中心化后图像f2的幅度谱');
subplot(224);
imshow(abs(fft2(histeq(I3))));
title('旋转后图像f3幅度谱');
分析:f6(m,n)是对f5(m,n)实现了频谱中心化
数字图像处理相关的小练习_第6张图片

2  产生教材104页题图4.18(右图)所示的二值图像(白为1,黑为0),编程实现习题4.18所要求的处理(3*3的平均滤波和中值滤波)功能(图像四周边界不考虑,处理结果按四舍五入仍取0或1),显示处理前后的图像,比较其异同。

Halcon源代码:
dev_close_window()
ROW_GEN_DEMO := 30
ROW_GEN_DEMO_0 := ROW_GEN_DEMO * 0
ROW_GEN_DEMO_1 := ROW_GEN_DEMO * 1
ROW_GEN_DEMO_2 := ROW_GEN_DEMO * 2
ROW_GEN_DEMO_3 := ROW_GEN_DEMO * 3
ROW_GEN_DEMO_4 := ROW_GEN_DEMO * 4
ROW_GEN_DEMO_5 := ROW_GEN_DEMO * 5
ROW_GEN_DEMO_6 := ROW_GEN_DEMO * 6
ROW_GEN_DEMO_7 := ROW_GEN_DEMO * 7
ROW_GEN_DEMO_8 := ROW_GEN_DEMO * 8
ROW_GEN_DEMO_9 := ROW_GEN_DEMO * 9
ROW_GEN_DEMO_10 := ROW_GEN_DEMO * 10
dev_open_window (0, 0, ROW_GEN_DEMO_10, ROW_GEN_DEMO_10, 'black', WindowHandle1)
gen_rectangle1 (ROI_0, ROW_GEN_DEMO_0, ROW_GEN_DEMO_0, ROW_GEN_DEMO_1, ROW_GEN_DEMO_1)
gen_rectangle1 (ROI_1, ROW_GEN_DEMO_2, ROW_GEN_DEMO_0, ROW_GEN_DEMO_3, ROW_GEN_DEMO_1)
gen_rectangle1 (ROI_2, ROW_GEN_DEMO_4, ROW_GEN_DEMO_0, ROW_GEN_DEMO_5, ROW_GEN_DEMO_1)
gen_rectangle1 (ROI_3, ROW_GEN_DEMO_6, ROW_GEN_DEMO_0, ROW_GEN_DEMO_7, ROW_GEN_DEMO_1)
gen_rectangle1 (ROI_4, ROW_GEN_DEMO_8, ROW_GEN_DEMO_0, ROW_GEN_DEMO_9, ROW_GEN_DEMO_1)
gen_rectangle1 (ROI_5, ROW_GEN_DEMO_1, ROW_GEN_DEMO_1, ROW_GEN_DEMO_2, ROW_GEN_DEMO_2)
gen_rectangle1 (ROI_6, ROW_GEN_DEMO_3, ROW_GEN_DEMO_1, ROW_GEN_DEMO_4, ROW_GEN_DEMO_2)
gen_rectangle1 (ROI_7, ROW_GEN_DEMO_5, ROW_GEN_DEMO_1, ROW_GEN_DEMO_6, ROW_GEN_DEMO_2)
gen_rectangle1 (ROI_8, ROW_GEN_DEMO_7, ROW_GEN_DEMO_1, ROW_GEN_DEMO_8, ROW_GEN_DEMO_2)
gen_rectangle1 (ROI_9, ROW_GEN_DEMO_9, ROW_GEN_DEMO_1, ROW_GEN_DEMO_10, ROW_GEN_DEMO_2)
gen_rectangle1 (ROI_10, ROW_GEN_DEMO_0, ROW_GEN_DEMO_2, ROW_GEN_DEMO_1, ROW_GEN_DEMO_3)
gen_rectangle1 (ROI_11, ROW_GEN_DEMO_2, ROW_GEN_DEMO_2, ROW_GEN_DEMO_3, ROW_GEN_DEMO_3)
gen_rectangle1 (ROI_12, ROW_GEN_DEMO_4, ROW_GEN_DEMO_2, ROW_GEN_DEMO_5, ROW_GEN_DEMO_3)
gen_rectangle1 (ROI_13, ROW_GEN_DEMO_6, ROW_GEN_DEMO_2, ROW_GEN_DEMO_7, ROW_GEN_DEMO_3)
gen_rectangle1 (ROI_14, ROW_GEN_DEMO_8, ROW_GEN_DEMO_2, ROW_GEN_DEMO_9, ROW_GEN_DEMO_3)
gen_rectangle1 (ROI_15, ROW_GEN_DEMO_1, ROW_GEN_DEMO_3, ROW_GEN_DEMO_2, ROW_GEN_DEMO_4)
gen_rectangle1 (ROI_16, ROW_GEN_DEMO_3, ROW_GEN_DEMO_3, ROW_GEN_DEMO_4, ROW_GEN_DEMO_4)
gen_rectangle1 (ROI_17, ROW_GEN_DEMO_5, ROW_GEN_DEMO_3, ROW_GEN_DEMO_6, ROW_GEN_DEMO_4)
gen_rectangle1 (ROI_18, ROW_GEN_DEMO_7, ROW_GEN_DEMO_3, ROW_GEN_DEMO_8, ROW_GEN_DEMO_4)
gen_rectangle1 (ROI_19, ROW_GEN_DEMO_9, ROW_GEN_DEMO_3, ROW_GEN_DEMO_10, ROW_GEN_DEMO_4)
gen_rectangle1 (ROI_20, ROW_GEN_DEMO_0, ROW_GEN_DEMO_4, ROW_GEN_DEMO_1, ROW_GEN_DEMO_5)
gen_rectangle1 (ROI_21, ROW_GEN_DEMO_2, ROW_GEN_DEMO_4, ROW_GEN_DEMO_3, ROW_GEN_DEMO_5)
gen_rectangle1 (ROI_22, ROW_GEN_DEMO_4, ROW_GEN_DEMO_4, ROW_GEN_DEMO_5, ROW_GEN_DEMO_5)
gen_rectangle1 (ROI_23, ROW_GEN_DEMO_6, ROW_GEN_DEMO_4, ROW_GEN_DEMO_7, ROW_GEN_DEMO_5)
gen_rectangle1 (ROI_24, ROW_GEN_DEMO_8, ROW_GEN_DEMO_4, ROW_GEN_DEMO_9, ROW_GEN_DEMO_5)
gen_rectangle1 (ROI_25, ROW_GEN_DEMO_1, ROW_GEN_DEMO_5, ROW_GEN_DEMO_2, ROW_GEN_DEMO_6)
gen_rectangle1 (ROI_26, ROW_GEN_DEMO_3, ROW_GEN_DEMO_5, ROW_GEN_DEMO_4, ROW_GEN_DEMO_6)
gen_rectangle1 (ROI_27, ROW_GEN_DEMO_5, ROW_GEN_DEMO_5, ROW_GEN_DEMO_6, ROW_GEN_DEMO_6)
gen_rectangle1 (ROI_28, ROW_GEN_DEMO_7, ROW_GEN_DEMO_5, ROW_GEN_DEMO_8, ROW_GEN_DEMO_6)
gen_rectangle1 (ROI_29, ROW_GEN_DEMO_9, ROW_GEN_DEMO_5, ROW_GEN_DEMO_10, ROW_GEN_DEMO_6)
gen_rectangle1 (ROI_30, ROW_GEN_DEMO_0, ROW_GEN_DEMO_6, ROW_GEN_DEMO_1, ROW_GEN_DEMO_7)
gen_rectangle1 (ROI_31, ROW_GEN_DEMO_2, ROW_GEN_DEMO_6, ROW_GEN_DEMO_3, ROW_GEN_DEMO_7)
gen_rectangle1 (ROI_32, ROW_GEN_DEMO_4, ROW_GEN_DEMO_6, ROW_GEN_DEMO_5, ROW_GEN_DEMO_7)
gen_rectangle1 (ROI_33, ROW_GEN_DEMO_6, ROW_GEN_DEMO_6, ROW_GEN_DEMO_7, ROW_GEN_DEMO_7)
gen_rectangle1 (ROI_34, ROW_GEN_DEMO_8, ROW_GEN_DEMO_6, ROW_GEN_DEMO_9, ROW_GEN_DEMO_7)
gen_rectangle1 (ROI_35, ROW_GEN_DEMO_1, ROW_GEN_DEMO_7, ROW_GEN_DEMO_2, ROW_GEN_DEMO_8)
gen_rectangle1 (ROI_36, ROW_GEN_DEMO_3, ROW_GEN_DEMO_7, ROW_GEN_DEMO_4, ROW_GEN_DEMO_8)
gen_rectangle1 (ROI_37, ROW_GEN_DEMO_5, ROW_GEN_DEMO_7, ROW_GEN_DEMO_6, ROW_GEN_DEMO_8)
gen_rectangle1 (ROI_38, ROW_GEN_DEMO_7, ROW_GEN_DEMO_7, ROW_GEN_DEMO_8, ROW_GEN_DEMO_8)
gen_rectangle1 (ROI_39, ROW_GEN_DEMO_9, ROW_GEN_DEMO_7, ROW_GEN_DEMO_10, ROW_GEN_DEMO_8)
gen_rectangle1 (ROI_40, ROW_GEN_DEMO_0, ROW_GEN_DEMO_8, ROW_GEN_DEMO_1, ROW_GEN_DEMO_9)
gen_rectangle1 (ROI_41, ROW_GEN_DEMO_2, ROW_GEN_DEMO_8, ROW_GEN_DEMO_3, ROW_GEN_DEMO_9)
gen_rectangle1 (ROI_42, ROW_GEN_DEMO_4, ROW_GEN_DEMO_8, ROW_GEN_DEMO_5, ROW_GEN_DEMO_9)
gen_rectangle1 (ROI_43, ROW_GEN_DEMO_6, ROW_GEN_DEMO_8, ROW_GEN_DEMO_7, ROW_GEN_DEMO_9)
gen_rectangle1 (ROI_44, ROW_GEN_DEMO_8, ROW_GEN_DEMO_8, ROW_GEN_DEMO_9, ROW_GEN_DEMO_9)
gen_rectangle1 (ROI_45, ROW_GEN_DEMO_1, ROW_GEN_DEMO_9, ROW_GEN_DEMO_2, ROW_GEN_DEMO_10)
gen_rectangle1 (ROI_46, ROW_GEN_DEMO_3, ROW_GEN_DEMO_9, ROW_GEN_DEMO_4, ROW_GEN_DEMO_10)
gen_rectangle1 (ROI_47, ROW_GEN_DEMO_5, ROW_GEN_DEMO_9, ROW_GEN_DEMO_6, ROW_GEN_DEMO_10)
gen_rectangle1 (ROI_48, ROW_GEN_DEMO_7, ROW_GEN_DEMO_9, ROW_GEN_DEMO_8, ROW_GEN_DEMO_10)
gen_rectangle1 (ROI_49, ROW_GEN_DEMO_9, ROW_GEN_DEMO_9, ROW_GEN_DEMO_10, ROW_GEN_DEMO_10)
dev_clear_window()
union2 (ROI_0, ROI_1, Region_Union)
union2 (ROI_2, Region_Union, Region_Union)
union2 (ROI_3, Region_Union, Region_Union)
union2 (ROI_4, Region_Union, Region_Union)
union2 (ROI_5, Region_Union, Region_Union)
union2 (ROI_6, Region_Union, Region_Union)
union2 (ROI_7, Region_Union, Region_Union)
union2 (ROI_8, Region_Union, Region_Union)
union2 (ROI_9, Region_Union, Region_Union)
union2 (ROI_10, Region_Union, Region_Union)
union2 (ROI_11, Region_Union, Region_Union)
union2 (ROI_12, Region_Union, Region_Union)
union2 (ROI_13, Region_Union, Region_Union)
union2 (ROI_14, Region_Union, Region_Union)
union2 (ROI_15, Region_Union, Region_Union)
union2 (ROI_16, Region_Union, Region_Union)
union2 (ROI_17, Region_Union, Region_Union)
union2 (ROI_18, Region_Union, Region_Union)
union2 (ROI_19, Region_Union, Region_Union)
union2 (ROI_20, Region_Union, Region_Union)
union2 (ROI_21, Region_Union, Region_Union)
union2 (ROI_22, Region_Union, Region_Union)
union2 (ROI_23, Region_Union, Region_Union)
union2 (ROI_24, Region_Union, Region_Union)
union2 (ROI_25, Region_Union, Region_Union)
union2 (ROI_26, Region_Union, Region_Union)
union2 (ROI_27, Region_Union, Region_Union)
union2 (ROI_28, Region_Union, Region_Union)
union2 (ROI_29, Region_Union, Region_Union)
union2 (ROI_30, Region_Union, Region_Union)
union2 (ROI_31, Region_Union, Region_Union)
union2 (ROI_32, Region_Union, Region_Union)
union2 (ROI_33, Region_Union, Region_Union)
union2 (ROI_34, Region_Union, Region_Union)
union2 (ROI_35, Region_Union, Region_Union)
union2 (ROI_36, Region_Union, Region_Union)
union2 (ROI_37, Region_Union, Region_Union)
union2 (ROI_38, Region_Union, Region_Union)
union2 (ROI_39, Region_Union, Region_Union)
union2 (ROI_40, Region_Union, Region_Union)
union2 (ROI_41, Region_Union, Region_Union)
union2 (ROI_42, Region_Union, Region_Union)
union2 (ROI_43, Region_Union, Region_Union)
union2 (ROI_44, Region_Union, Region_Union)
union2 (ROI_45, Region_Union, Region_Union)
union2 (ROI_46, Region_Union, Region_Union)
union2 (ROI_47, Region_Union, Region_Union)
union2 (ROI_48, Region_Union, Region_Union)
union2 (ROI_49, Region_Union, Region_Union)
stop()
region_to_bin (Region_Union, Bin_Image_255, 255, 0, ROW_GEN_DEMO_10, ROW_GEN_DEMO_10)
median_image (Bin_Image_255, Image_Median_bin_255, 'square', 3, 'mirrored')
mean_image (Bin_Image_255, Image_Mean_bin_255, 3, 3)
region_to_label (Region_Union, Image_Label_bin_2, 'int2', ROW_GEN_DEMO_10, ROW_GEN_DEMO_10)
median_image (Image_Label_bin_2, Image_Median_bin_2, 'square', 3, 'mirrored')
mean_image (Image_Label_bin_2, Image_Mean_bin_2, 3, 3)
dev_open_window (0, 0, ROW_GEN_DEMO_10, ROW_GEN_DEMO_10, 'black', WindowHandle1)
set_display_font (WindowHandle1, 14, 'mono', 'true', 'false')
dev_display (Bin_Image_255)
disp_message (WindowHandle1, '灰度图像---原图', 'window', 0, 0, 'red', 'true')
dev_open_window (0, 0, ROW_GEN_DEMO_10, ROW_GEN_DEMO_10, 'black', WindowHandle2)
set_display_font (WindowHandle2, 14, 'mono', 'true', 'false')
dev_display (Image_Median_bin_255)
disp_message (WindowHandle2, '灰度图像---中值滤波(3*3)', 'window', 0, 0, 'red', 'true')
dev_open_window (0, 0, ROW_GEN_DEMO_10, ROW_GEN_DEMO_10, 'black', WindowHandle3)
set_display_font (WindowHandle3, 14, 'mono', 'true', 'false')
dev_display (Image_Mean_bin_255)
disp_message (WindowHandle3, '灰度图像---平均滤波(3*3)', 'window', 0, 0, 'red', 'true')
dev_open_window (0, 0, ROW_GEN_DEMO_10, ROW_GEN_DEMO_10, 'black', WindowHandle4)
set_display_font (WindowHandle4, 14, 'mono', 'true', 'false')
dev_display (Image_Label_bin_2)
disp_message (WindowHandle4, '二值图像---原图', 'window', 0, 0, 'red', 'true')
dev_open_window (0, 0, ROW_GEN_DEMO_10, ROW_GEN_DEMO_10, 'black', WindowHandle5)
set_display_font (WindowHandle5, 14, 'mono', 'true', 'false')
dev_display (Image_Median_bin_2)
disp_message (WindowHandle5, '二值图像---中值滤波(3*3)', 'window', 0, 0, 'red', 'true')
dev_open_window (0, 0, ROW_GEN_DEMO_10, ROW_GEN_DEMO_10, 'black', WindowHandle6)
set_display_font (WindowHandle6, 14, 'mono', 'true', 'false')
dev_display (Image_Mean_bin_2)
disp_message (WindowHandle6, '二值图像---平均滤波(3*3)', 'window', 0, 0, 'red', 'true')
数字图像处理相关的小练习_第7张图片
数字图像处理相关的小练习_第8张图片

3  产生教材104页题图4.16所示的灰度图像(白为255,黑为0),分别加入高斯白噪声和椒盐噪声,再分别进行3*3的平均滤波和中值滤波,显示原图像、加噪图像和滤波结果图像,并比较四种滤波结果。

Halcon源代码:
dev_close_window()
ROW_GENT_TOP    := 22
ROW_GENT_BUTTON := 232
COLUMN_GENT_WIDTH_W   := 7
COLUMN_GENT_WIDTH_B   := 17
COLUMN_INIT           :=27
ROW_GENT_DEMO_0 := COLUMN_INIT + COLUMN_GENT_WIDTH_W
ROW_GENT_DEMO_1 := ROW_GENT_DEMO_0 + COLUMN_GENT_WIDTH_B
ROW_GENT_DEMO_2 := ROW_GENT_DEMO_1 + COLUMN_GENT_WIDTH_W 
ROW_GENT_DEMO_3 := ROW_GENT_DEMO_2 + COLUMN_GENT_WIDTH_B
ROW_GENT_DEMO_4 := ROW_GENT_DEMO_3 + COLUMN_GENT_WIDTH_W 
ROW_GENT_DEMO_5 := ROW_GENT_DEMO_4 + COLUMN_GENT_WIDTH_B
ROW_GENT_DEMO_6 := ROW_GENT_DEMO_5 + COLUMN_GENT_WIDTH_W 
ROW_GENT_DEMO_7 := ROW_GENT_DEMO_6 + COLUMN_GENT_WIDTH_B
ROW_GENT_DEMO_8 := ROW_GENT_DEMO_7 + COLUMN_GENT_WIDTH_W 
ROW_GENT_DEMO_9 := ROW_GENT_DEMO_8 + COLUMN_GENT_WIDTH_B
ROW_GENT_DEMO_10 := ROW_GENT_DEMO_9 + COLUMN_GENT_WIDTH_W 
ROW_GENT_DEMO_11 := ROW_GENT_DEMO_10 + COLUMN_GENT_WIDTH_B
ROW_GENT_DEMO_12 := ROW_GENT_DEMO_11 + COLUMN_GENT_WIDTH_W 
ROW_GENT_DEMO_13 := ROW_GENT_DEMO_12 + COLUMN_GENT_WIDTH_B
ROW_GENT_DEMO_14 := ROW_GENT_DEMO_13 + COLUMN_GENT_WIDTH_W 
ROW_GENT_DEMO_15 := ROW_GENT_DEMO_14 + COLUMN_GENT_WIDTH_B
ROW_GENT_DEMO_16 := ROW_GENT_DEMO_15 + COLUMN_GENT_WIDTH_W 
dev_open_window (0, 0, 256, 256, 'black', WindowHandle)
gen_rectangle1 (ROI_0, ROW_GENT_TOP, COLUMN_INIT, ROW_GENT_BUTTON, ROW_GENT_DEMO_0)
gen_rectangle1 (ROI_1, ROW_GENT_TOP, ROW_GENT_DEMO_1, ROW_GENT_BUTTON, ROW_GENT_DEMO_2)
gen_rectangle1 (ROI_2, ROW_GENT_TOP, ROW_GENT_DEMO_3, ROW_GENT_BUTTON, ROW_GENT_DEMO_4)
gen_rectangle1 (ROI_3, ROW_GENT_TOP, ROW_GENT_DEMO_5, ROW_GENT_BUTTON, ROW_GENT_DEMO_6)
gen_rectangle1 (ROI_4, ROW_GENT_TOP, ROW_GENT_DEMO_7, ROW_GENT_BUTTON, ROW_GENT_DEMO_8)
gen_rectangle1 (ROI_5, ROW_GENT_TOP, ROW_GENT_DEMO_9, ROW_GENT_BUTTON, ROW_GENT_DEMO_10)
gen_rectangle1 (ROI_6, ROW_GENT_TOP, ROW_GENT_DEMO_11, ROW_GENT_BUTTON, ROW_GENT_DEMO_12)
gen_rectangle1 (ROI_7, ROW_GENT_TOP, ROW_GENT_DEMO_13, ROW_GENT_BUTTON, ROW_GENT_DEMO_14)
gen_rectangle1 (ROI_8, ROW_GENT_TOP, ROW_GENT_DEMO_15, ROW_GENT_BUTTON, ROW_GENT_DEMO_16)
union2 (ROI_0, ROI_1, Region_Union)
union2 (ROI_2, Region_Union, Region_Union)
union2 (ROI_3, Region_Union, Region_Union)
union2 (ROI_4, Region_Union, Region_Union)
union2 (ROI_5, Region_Union, Region_Union)
union2 (ROI_6, Region_Union, Region_Union)
union2 (ROI_7, Region_Union, Region_Union)
union2 (ROI_8, Region_Union, Region_Union)
stop()
region_to_bin (Region_Union, BinImage, 255, 0, 256, 256) 
**add a GAUSS noise
for INDEX_gauss_distribution:=0 to 200 by 1
    gauss_distribution (INDEX_gauss_distribution, Distribution_GAUSS)
    add_noise_distribution (BinImage, Image_Noise_Gauss, Distribution_GAUSS)
    wait_seconds (0.03)
endfor
stop()
dev_close_window()
gauss_distribution (50, Distribution_GAUSS)
add_noise_distribution (BinImage, Image_Noise_Gauss, Distribution_GAUSS)
median_image (Image_Noise_Gauss, Image_Median_Noise_Gauss, 'square', 3, 'mirrored')
mean_image (Image_Noise_Gauss, Image_Mean_Noise_Gauss, 3, 3)
dev_open_window (0, 0, 256, 256, 'black', WindowHandle1)
set_display_font (WindowHandle1, 14, 'mono', 'true', 'false')
dev_display (BinImage)
disp_message (WindowHandle1, '灰度图像---原图', 'window', 0, 0, 'red', 'true')
dev_open_window (0, 0, 256, 256, 'black', WindowHandle2)
set_display_font (WindowHandle2, 14, 'mono', 'true', 'false')
dev_display (Image_Noise_Gauss)
disp_message (WindowHandle2, '灰度图像---加入高斯噪声(50)', 'window', 0, 0, 'red', 'true')
dev_open_window (0, 0, 256, 256, 'black', WindowHandle3)
set_display_font (WindowHandle3, 14, 'mono', 'true', 'false')
dev_display (Image_Median_Noise_Gauss)
disp_message (WindowHandle3, '灰度图像---加入高斯噪声(中)', 'window', 0, 0, 'red', 'true')
dev_open_window (0, 0, 256, 256, 'black', WindowHandle4)
set_display_font (WindowHandle4, 14, 'mono', 'true', 'false')
dev_display (Image_Mean_Noise_Gauss)
disp_message (WindowHandle4, '灰度图像---加入高斯噪声(平)', 'window', 0, 0, 'red', 'true')
**add a salt-and-pepper noise
sp_distribution (5, 5, Distribution_S_And_P)
add_noise_distribution (BinImage, Image_Noise_S_And_P, Distribution_S_And_P)
median_image (Image_Noise_S_And_P, Image_Median_Noise_S_And_P, 'square', 3, 'mirrored')
mean_image (Image_Noise_S_And_P, Image_Mean_Noise_S_And_P, 3, 3)
disp_message (WindowHandle4, '灰度图像---加入高斯噪声(平)', 'window', 0, 0, 'red', 'true')
dev_open_window (0, 0, 256, 256, 'black', WindowHandle5)
set_display_font (WindowHandle5, 14, 'mono', 'true', 'false')
dev_display (BinImage)
disp_message (WindowHandle5, '灰度图像---原图', 'window', 0, 0, 'red', 'true')
dev_open_window (0, 0, 256, 256, 'black', WindowHandle6)
set_display_font (WindowHandle6, 14, 'mono', 'true', 'false')
dev_display (Image_Noise_S_And_P)
disp_message (WindowHandle6, '灰度图像--加入椒盐噪声(5*5)', 'window', 0, 0, 'red', 'true')
dev_open_window (0, 0, 256, 256, 'black', WindowHandle7)
set_display_font (WindowHandle7, 14, 'mono', 'true', 'false')
dev_display (Image_Median_Noise_S_And_P)
disp_message (WindowHandle7, '灰度图像---加入椒盐噪声(中)', 'window', 0, 0, 'red', 'true')
dev_open_window (0, 0, 256, 256, 'black', WindowHandle8)
set_display_font (WindowHandle8, 14, 'mono', 'true', 'false')
dev_display (Image_Mean_Noise_S_And_P)
disp_message (WindowHandle8, '灰度图像---加入椒盐噪声(平)', 'window', 0, 0, 'red', 'true')
数字图像处理相关的小练习_第9张图片
数字图像处理相关的小练习_第10张图片

数字图像处理相关的小练习_第11张图片

分析:由图像可以看出,对于均值滤波,由以上处理后的图像可以看出:均值滤波对高斯噪声的抑制比较好,处理后的图像边缘迷糊较少。但对椒盐噪声的影响不大,因为在削弱噪声的同时整幅图像也变得模糊,其噪声依然存在。

对于中值滤波,有图像处理的结果可以看出,它只影响了图像的基本信息,说明中值滤波对高斯噪声的抑制效果不明显。这是因为高斯噪声使用随机大小的幅值污染所有的点,因为无论怎样进行数据选择,得到的始终还是被污染的值。而由图还可以看出,中值滤波对除去椒盐噪声可以起到很好的效果因为椒盐噪声只在图画中的部分点随机出现所以根据中值滤波原理可知,通过数据排序的方法,将图像中未被噪声污染的点代替噪声点的值的概率比较大,因此噪声的抑制效果很好,同时画面的轮廓依然比较清晰。由此看来,对于椒盐噪声密度较小时,尤其是孤立噪声点,对中值滤波的效果非常好的。

均值滤波对高斯噪声有较好的抑制作用,而对于椒盐噪声的处理中值滤波要略有优势。

4  (第 4 、5  题选做一题)对某一灰度图像,进行如下处理:

(1)分别利用 Roberts 、Prewitt  和 Sobel  边缘检测算子进行边缘检测;

(2)将 Roberts 、Prewitt 和 和 Sobel  边缘检测算子修改为锐化算子,对原图像进行锐化,同屏显示原图像、边缘检测结果和锐化后图像,说明三者之间的关系。

Halcon源代码:
read_image (Image, 'C:/Users/Administrator/Desktop/许老师图像大作业/20161109210038_rUR35.thumb.224_0.jpeg')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width*2, Height*2, 'black', WindowHandle)
rgb1_to_gray (Image, Image)
invert_image (Image, Image)
dev_display (Image)
**Roberts edge detection
roberts (Image, Image_Roberts, 'gradient_sum')
threshold (Image_Roberts, Region_roberts, 100, 255)
skeleton (Region_roberts, Skeleton_roberts)
dev_display (Image)
dev_set_color ('green')
dev_display (Skeleton_roberts)

**Prewitt edge detection
prewitt_amp (Image, Image_prewitt)
threshold (Image_prewitt, Region_prewitt, 100, 255)
skeleton (Region_prewitt, Skeleton_prewitt)
dev_display (Image)
dev_set_color ('blue')
dev_display (Skeleton_prewitt)
**Sobel edge detection
sobel_amp (Image, EdgeAmplitude, 'sum_abs', 3)
threshold (EdgeAmplitude, Region_sobel, 100, 255)
skeleton (Region_sobel, Skeleton_sobel)
dev_display (Image)
dev_set_color ('green')
dev_display(Skeleton_sobel)
stop()
数字图像处理相关的小练习_第12张图片






你可能感兴趣的:(algorithm,halcon)