工控编程吧
标题: 工业视觉halcon选择出最长最短的轮廓 [打印本页]
作者: qq263946146 时间: 2019-6-7 17:07
标题: 工业视觉halcon选择出最长最短的轮廓
当前这个实例实现从众多轮廓中选择出最长最短的轮廓
可以将下面函数打包成本地函数使用
*Contours 输入的要被提取的轮廓
*MinLengthContour,MaxLengthContour,提取出的最短和最长轮廓
*从图片中生成轮廓集
read_image (Image, 'printer_chip/printer_chip_01')
threshold (Image, Region, 128, 255)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 25000, 100000)
gen_contour_region_xld (SelectedRegions, Contours, 'border')
*打开halcon错误信息提示功能
dev_set_check ('give_error')
gen_empty_obj (MaxLengthContour)
try
count_obj (Contours, Number)
catch (Exception)
return()
endtry
if(Number<1)
return()
endif
*最长最短长度及其索引初始化
Max_Length :=0
Max_Index :=0
Min_Length :=9999999
Min_Index:=0
*遍历每个轮廓长度
for i:=1 to Number by 1
select_obj (Contours, ObjectSelected, i)
length_xld (ObjectSelected, Length)
*保留最长轮廓及绑住索引
if(Max_Length<Length)
Max_Length := Length
Max_Index := i
endif
*保留最短轮廓及其索引
if(Min_Length>Length)
Min_Length :=Length
Min_Index := i
endif
endfor
*从众多轮廓中选择出最长,最短轮廓
select_obj (Contours,MaxLengthContour , Max_Index)
select_obj (Contours,MinLengthContour , Min_Index)
*效果显示,不用可删除
dev_set_color ('red')
dev_display (Contours)
dev_set_color ('green')
dev_display (MaxLengthContour)
dev_set_color ('blue')
dev_display (MinLengthContour)
效果如图
(, 下载次数: 4)