Halcon calculus principle generates calipers to measure the width of special-shaped products

1. In ordinary measurement projects, we can use halcon's measurement model, such as add_metrology_object_line_measure. It is very convenient to measure straight lines, circles, ellipses, rectangles, etc. One disadvantage of these tools is that they need to draw the measurement position in advance and then follow it using affine transformation, or dynamically generate the measurement position in the project. However, when measuring special-shaped products, these methods are not suitable. For the shape as shown below, it is necessary to generate a caliper in real time based on the principle of calculus for measurement.
Insert image description here

2. Implement the code

dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
read_image (Image, 'C:/Users/haoyu.dou/Desktop/圆弧胶.bmp')
decompose3 (Image, R, G, B)
mean_image (R, ImageMean, 255, 255)
dyn_threshold (R, ImageMean, RegionDynThresh, 25, 'light')
opening_circle (RegionDynThresh, RegionOpening, 15)
connection (RegionOpening, ConnectedRegions)
skeleton (ConnectedRegions, Skeleton)
get_region_points (Skeleton, Rows, Columns)
gen_cross_contour_xld (Cross, Rows, Columns, 6, 0.785398)
tuple_sort_index (Columns, Indices)
dev_display (R)
dev_display (Cross)
get_image_size (ImageMean, Width, Height)
gen_empty_obj (Edge_MeasureOut)
gen_empty_obj (Rectangle_MeasureOut)
*积分间距
RecDis:=50
for Index := 0 to |Rows|-RecDis-1 by RecDis
    *生成测量卡尺,隔50个像素,计算一小段区域角度
    angle_lx (Rows[Indices[Index]], Columns[Indices[Index]], Rows[Indices[Index+RecDis]], Columns[Indices[Index+RecDis]], Angle)
    *把每一个卡尺轮廓画出来
    gen_rectangle2_contour_xld (Rectangle, (Rows[Indices[Index]]+Rows[Indices[Index+RecDis]])/2, (Columns[Indices[Index]]+Columns[Indices[Index+RecDis]])/2, Angle-rad(90), 150, RecDis/2)
    concat_obj (Rectangle_MeasureOut, Rectangle, Rectangle_MeasureOut)
    *生成垂直待测区域卡尺
    gen_measure_rectangle2 ((Rows[Indices[Index]]+Rows[Indices[Index+RecDis]])/2, (Columns[Indices[Index]]+Columns[Indices[Index+RecDis]])/2, Angle-rad(90), 150, RecDis/2, Width, Height, 'nearest_neighbor', MeasureHandle)
    measure_pairs (R, MeasureHandle, 1, 20, 'all', 'all', RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, IntraDistance, InterDistance)
    *获取测量上边缘
    Get_MeasureEdge (Edge_MeasureOut, Marker, Edge_MeasureOut, RowEdgeFirst, ColumnEdgeFirst, RecDis/2, Angle-rad(90))
    *获取测量下边缘
    Get_MeasureEdge (Edge_MeasureOut, Marker, Edge_MeasureOut, RowEdgeSecond, ColumnEdgeSecond, RecDis/2, Angle-rad(90))
            
           
endfor
dev_display (R)
dev_set_color ('green')
dev_display (Rectangle_MeasureOut)
dev_set_color ('red')
dev_display (Edge_MeasureOut)

Insert image description here

Guess you like

Origin blog.csdn.net/Douhaoyu/article/details/131911476