深度理解灰度图像形态学之腐蚀3

结构元素1
0 0 0
0 0 0
0 0 0
结构元素2
0 10 0 A(00) A(01) A(02)
20 0 30 A(10) A(11) A(12)
0 40 0 A(20) A(21) A(22)

当结构体区域内最小值在A01位置时,结果为最小值-A(01)
当结构体区域内最小值在A10位置时,结果为最小值-A(10)
当结构体区域内最小值在A12位置时,结果为最小值-A(12)
当结构体区域内最小值在A21位置时,结果为最小值-A(21)

当结构体区域内最小值在其余位置(结构元素为0)时,结果为最小值。大部分是这样,有的位置找不到规律,不知道为何,如第二行第三列89,不知道为何

dev_close_window ()

*创建原图 9*9
gen_image_const (Image, 'byte', 9, 9)
get_image_size (Image, Width, Height)
dev_open_window_fit_image (Image, 0, 0, Width, Height, WindowHandle)
*生成随机数0-255,并将随机数设置成图像像素
tuple_rand (81, Rand)
Rand:=Rand*100
tuple_int (Rand, Int)
Int:=Int+120
Cnt:=9
for Index := 0 to Cnt-1 by 1
    for Index1 := 0 to Cnt-1 by 1
        Pos:=Index*Cnt+Index1
        set_grayval (Image, Index, Index1, Int[Pos])
    endfor
endfor

*创建结构元素1 内部都是0
* 0 0 0
* 0 0 0
* 0 0 0
gen_image_const (ImageSE, 'byte', 3, 3)
Cnt:=3
for Index := 0 to Cnt-1 by 1
    for Index1 := 0 to Cnt-1 by 1
        Pos:=Index*Cnt+Index1
        set_grayval (ImageSE, Index, Index1, 0)
    endfor
endfor

*创建结构元素2
* 0  10 0
* 20 0  30
* 0  40 0
gen_image_const (ImageSE1, 'byte', 3, 3)
set_grayval (ImageSE1, 0, 0, 0)
set_grayval (ImageSE1, 0, 1, 10)
set_grayval (ImageSE1, 0, 2, 0)
set_grayval (ImageSE1, 1, 0, 20)
set_grayval (ImageSE1, 1, 1, 0)
set_grayval (ImageSE1, 1, 2, 30)
set_grayval (ImageSE1, 2, 0, 0)
set_grayval (ImageSE1, 2, 1, 40)
set_grayval (ImageSE1, 2, 2, 0)

*一:使用结构元素1腐蚀原图并显示前后像素对比
dev_display (Image)
gray_erosion (Image, ImageSE, ImageErosion)

*显示腐蚀前后像素对比
dev_open_window (0, 0, 400, 640, 'black', WindowHandle1)
BeginRow:=10
BeginColumn:=10
Cnt:=9
for Index := 0 to Cnt-1 by 1
    for Index1 := 0 to Cnt-1 by 1
        Pos:=Index*Cnt+Index1
        get_grayval (Image, Index, Index1, Grayval)
        Row:=BeginRow+Index*30
        Column:=BeginColumn+Index1*40
        disp_message (WindowHandle1, Grayval, 'window', Row, Column, 'red', 'true')
    endfor
endfor

Row:=Row+40
disp_line (WindowHandle1, Row, 0, Row, 640)

BeginRow:=Row+10
BeginColumn:=10
for Index := 0 to Cnt-1 by 1
    for Index1 := 0 to Cnt-1 by 1
        Pos:=Index*Cnt+Index1
        get_grayval (ImageErosion, Index, Index1, Grayval)
        Row:=BeginRow+Index*30
        Column:=BeginColumn+Index1*40
        disp_message (WindowHandle1, Grayval, 'window', Row, Column, 'red', 'true')
    endfor
endfor
stop ()

*二:使用结构元素2腐蚀原图并显示前后像素对比
dev_set_window (WindowHandle)
gray_erosion (Image, ImageSE1, ImageErosion1)

*显示腐蚀前后像素对比
dev_open_window (0, 0, 400, 640, 'black', WindowHandle2)
BeginRow:=10
BeginColumn:=10
Cnt:=9
for Index := 0 to Cnt-1 by 1
    for Index1 := 0 to Cnt-1 by 1
        Pos:=Index*Cnt+Index1
        get_grayval (Image, Index, Index1, Grayval)
        Row:=BeginRow+Index*30
        Column:=BeginColumn+Index1*40
        disp_message (WindowHandle2, Grayval, 'window', Row, Column, 'red', 'true')
    endfor
endfor

Row:=Row+40
disp_line (WindowHandle2, Row, 0, Row, 640)
BeginRow:=Row+10
BeginColumn:=10
for Index := 0 to Cnt-1 by 1
    for Index1 := 0 to Cnt-1 by 1
        Pos:=Index*Cnt+Index1
        get_grayval (ImageErosion1, Index, Index1, Grayval)
        Row:=BeginRow+Index*30
        Column:=BeginColumn+Index1*40
        disp_message (WindowHandle2, Grayval, 'window', Row, Column, 'red', 'true')
    endfor
endfor
stop ()


深度理解灰度图像形态学之腐蚀3_第1张图片

你可能感兴趣的:(Halcon学习)