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

上篇针对结构元素全部相同的情况
本篇针对结构元素有一个非零值,其余全为零的腐蚀情况

结论
一:结构元素为
1 0 0
0 0 0
0 0 0
当结构元素区域最小值位置和1(唯一非零值)位置重合时,结果为最小位置灰度值-1(唯一非零值)

二:结构元素为
5 0 0
0 0 0
0 0 0
当结构元素区域最小值位置和5(唯一非零值)位置重合时,结果为最小位置灰度值-5(唯一非零值)

三:结构元素为
1 0 0
0 0 0
0 0 0
当结构元素区域最小值位置和1(唯一非零值)位置不重合时,结果为最小位置灰度值

halcon代码如下

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
* 1 0 0
* 0 0 0
* 0 0 0
gen_image_const (ImageSE1, 'byte', 3, 3)
set_grayval (ImageSE1, 0, 0, 1)
set_grayval (ImageSE1, 0, 1, 0)
set_grayval (ImageSE1, 0, 2, 0)
set_grayval (ImageSE1, 1, 0, 0)
set_grayval (ImageSE1, 1, 1, 0)
set_grayval (ImageSE1, 1, 2, 0)
set_grayval (ImageSE1, 2, 0, 0)
set_grayval (ImageSE1, 2, 1, 0)
set_grayval (ImageSE1, 2, 2, 0)

*创建结构元素3
* 0 0 0
* 0 1 0
* 0 0 0
gen_image_const (ImageSE2, 'byte', 3, 3)
set_grayval (ImageSE2, 0, 0, 0)
set_grayval (ImageSE2, 0, 1, 0)
set_grayval (ImageSE2, 0, 2, 0)
set_grayval (ImageSE2, 1, 0, 0)
set_grayval (ImageSE2, 1, 1, 1)
set_grayval (ImageSE2, 1, 2, 0)
set_grayval (ImageSE2, 2, 0, 0)
set_grayval (ImageSE2, 2, 1, 0)
set_grayval (ImageSE2, 2, 2, 0)


*创建结构元素4
* 0 0 0
* 0 0 0
* 0 0 1
gen_image_const (ImageSE3, 'byte', 3, 3)
set_grayval (ImageSE3, 0, 0, 0)
set_grayval (ImageSE3, 0, 1, 0)
set_grayval (ImageSE3, 0, 2, 0)
set_grayval (ImageSE3, 1, 0, 0)
set_grayval (ImageSE3, 1, 1, 0)
set_grayval (ImageSE3, 1, 2, 0)
set_grayval (ImageSE3, 2, 0, 0)
set_grayval (ImageSE3, 2, 1, 0)
set_grayval (ImageSE3, 2, 2, 1)

stop ()

*一:使用结构元素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腐蚀原图并显示前后像素对比
dev_set_window (WindowHandle)
dev_display (Image)
gray_erosion (Image, ImageSE2, ImageErosion2)

*显示腐蚀前后像素对比
dev_open_window (0, 0, 480, 640, 'black', WindowHandle3)
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 (WindowHandle3, Grayval, 'window', Row, Column, 'red', 'true')
    endfor
endfor

Row:=Row+40
disp_line (WindowHandle3, 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 (ImageErosion2, Index, Index1, Grayval)
        Row:=BeginRow+Index*30
        Column:=BeginColumn+Index1*40
        disp_message (WindowHandle3, Grayval, 'window', Row, Column, 'red', 'true')
    endfor
endfor
stop ()

*四:使用结构元素4腐蚀原图并显示前后像素对比
dev_set_window (WindowHandle)
dev_display (Image)
gray_erosion (Image, ImageSE3, ImageErosion3)

*显示腐蚀前后像素对比
dev_open_window (0, 0, 480, 640, 'black', WindowHandle5)
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 (WindowHandle5, Grayval, 'window', Row, Column, 'red', 'true')
    endfor
endfor

Row:=Row+40
disp_line (WindowHandle5, 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 (ImageErosion3, Index, Index1, Grayval)
        Row:=BeginRow+Index*30
        Column:=BeginColumn+Index1*40
        disp_message (WindowHandle5, Grayval, 'window', Row, Column, 'red', 'true')
    endfor
endfor

stop ()

*创建结构元素4
* 5 0 0
* 0 0 0
* 0 0 0
gen_image_const (ImageSE4, 'byte', 3, 3)
set_grayval (ImageSE4, 0, 0, 5)
set_grayval (ImageSE4, 0, 1, 0)
set_grayval (ImageSE4, 0, 2, 0)
set_grayval (ImageSE4, 1, 0, 0)
set_grayval (ImageSE4, 1, 1, 0)
set_grayval (ImageSE4, 1, 2, 0)
set_grayval (ImageSE4, 2, 0, 0)
set_grayval (ImageSE4, 2, 1, 0)
set_grayval (ImageSE4, 2, 2, 0)

*五:使用结构元素5腐蚀原图并显示前后像素对比
dev_set_window (WindowHandle)
dev_display (Image)
gray_erosion (Image, ImageSE4, ImageErosion4)

*显示腐蚀前后像素对比
dev_open_window (0, 0, 480, 640, 'black', WindowHandle5)
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 (WindowHandle5, Grayval, 'window', Row, Column, 'red', 'true')
    endfor
endfor

Row:=Row+40
disp_line (WindowHandle5, 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 (ImageErosion4, Index, Index1, Grayval)
        Row:=BeginRow+Index*30
        Column:=BeginColumn+Index1*40
        disp_message (WindowHandle5, Grayval, 'window', Row, Column, 'red', 'true')
    endfor
endfor

stop ()


效果图片如下
在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值