halcon 获取轮廓

*读取图片
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 650, 485, 'black', WindowHandle)
read_image (Image, './3.bmp')
get_image_size (Image, Width, Height)
dev_set_part (0, 0, Height-1, Width-1)
binary_threshold (Image, Region, 'max_separability', 'dark', UsedThreshold)
fill_up_shape (Region, RegionFillUp, 'area', 1, 7000)
closing_circle (RegionFillUp, RegionClosing, 10.5)
boundary (RegionClosing, RegionBorder, 'inner')
gen_contour_region_xld (RegionBorder, Contours, 'border')
*提取亚像素精密边缘轮廓
* edges_sub_pix (Image, Edges, 'canny', 2, 20, 60)
*将一个XLD轮廓分割为直线段、圆(圆弧)、椭圆弧
segment_contours_xld (Contours, ContoursSplit, 'lines_circles', 5, 10, 20)
*计算个数
count_obj (ContoursSplit, Number)
dev_set_line_width (2)
dev_display (Image)
dev_display (ContoursSplit)

*写入轮廓点数据
open_file ('轮廓数据.txt', 'append', FileHandle)
for I := 1 to Number by 1
    corx := []
    cory := []
    *选择一个元素
    select_obj (ContoursSplit, ObjectSelected, I)
    length_xld(ObjectSelected, len)
    *获取轮廓点的个数
    contour_point_num_xld(ObjectSelected, num_1)
    *获取轮廓点坐标
    get_contour_xld(ObjectSelected, Row1, Col1)
    
    fwrite_string (FileHandle, '轮廓'+I+'      长度:'+len+'     轮廓点的个数:'+num_1)
    fnew_line (FileHandle)
    fwrite_string (FileHandle, '轮廓点坐标如下:')
    fnew_line (FileHandle)
    fwrite_string (FileHandle, 'Row'+Row1+'   Col'+Col1+'\n')
endfor

close_file (FileHandle)
dev_disp_text ('轮廓数据写入成功!', 'window', 12, 12, 'black', [], [])

posted on 2022-10-17 21:05  qq1151219115  阅读(314)  评论(0编辑  收藏  举报