例子还是手册中的,例子叫fin.hdev;是检测一个圆形结构上出的“小尾巴”;步骤是,第一,分割出背景和目标物;第二,分割并关闭想要的圆形物体的后的背景图(a);第三,用背景(a)减去分割关闭圆结构的图(b),得到想多余的小尾巴(c);第四,优化小尾巴(c),去除干扰,得到优化后的小尾巴(d),下图即为a-b=c,c优化后得到d处理图:
原图如下:
下面是代码:
* fin.hdev: Detection of a fin
*
dev_update_window ('off')
read_image (Fin, 'fin2')
*read_image (Fin, 'fin1')
*read_image (Fin, 'fin3')
get_image_size (Fin, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowID)
dev_display (Fin)
set_display_font (WindowID, 14, 'mono', 'true', 'false')
disp_continue_message (WindowID, 'black', 'true')
stop ()
threshold (Fin, Regions, 0, 244)
bin_threshold (Fin, Dark)
*自动阈值分割,通过各种内部运算,分割值较低的一部分
difference (Fin, Dark, Background)
*比较图像的不同,比较fin和阈值后分割后图像Dark,目的是分割出背景
dev_set_color ('blue')
dev_set_draw ('margin')
dev_set_line_width (4)
dev_display (Background)
disp_continue_message (WindowID, 'black', 'true')
stop ()
closing_circle (Background, ClosedBackground, 250)
*关闭圆形结构图形,小于半径250的圆形圆形结构将被忽略,也就是去那个小尾巴
dev_set_color ('green')
dev_display (ClosedBackground)
disp_continue_message (WindowID, 'black', 'true')
stop ()
difference (ClosedBackground, Background, RegionDifference)
*还是比较,用灰度图像Background和去掉小尾巴的图像ClosedBackground进行求差运算
opening_circle (RegionDifference,FinRegion, 2.5)
*与关闭相反,打开这部分图像中半径大于参数的图像,并分割出来
*opening_rectangle1 (RegionDifference, FinRegion, 5, 5)
*打开矩形结构,效果和圆形差不多
dev_display (Fin)
dev_set_color ('red')
dev_display (FinRegion)
area_center (FinRegion, FinArea, Row, Column)
dev_set_draw ('fill')
dev_set_line_width (1)
dev_update_window ('on')
disp_continue_message (WindowID, 'black', 'true')
stop ()