HDDevelop13 writes the halcon library to realize the contour detection of the center of the circle

renderings

 

Note that there is stop() in the internal code to stop the operation

All of them can be commented. Every time you run it without commenting, you can only output one operation. You need to keep clicking and running until the final effect is achieved.

dev_update_window ('off')
dev_close_window ()
*打开一个原点(左上角)在(0,0)的,宽:高:,黑色背景的图形窗口
dev_open_window (0, 0, 512, 512, 'black', WindowID)
read_image (Image, 'D:/Besktop/faimage/测试原图/23_50_12.bmp')
 
dev_display (Image)
set_display_font (WindowID, 22, 'mono', 'true', 'false')
disp_continue_message (WindowID, 'black', 'true')
stop ()
threshold (Image, Bright, 90, 255)
*画出最小外接矩形
shape_trans (Bright, rigontran, 'rectangle2')
dev_set_color ('green')
dev_set_line_width (2)
dev_set_draw ('margin')
dev_display (rigontran)
disp_continue_message (WindowID, 'black', 'true')
stop ()
*获得矩形内图像
reduce_domain (Image, rigontran, DieGrey)
threshold (DieGrey, Wires, 0, 50)
*填充区域内给定特征的孔
fill_up_shape (Wires, WiresFilled, 'area', 1, 100)
dev_display (Image)
*设置所画图形的形状,fill为填满
dev_set_draw ('margin')
dev_set_color ('red')
dev_display (WiresFilled)
disp_continue_message (WindowID, 'black', 'true')
stop ()
*开运算,除去孤立的小点、毛刺,总位置大小形状不变;输入控制半径
opening_circle (WiresFilled, Balls, 15.5)
dev_set_color ('green')
dev_display (Balls)
disp_continue_message (WindowID, 'black', 'true')
stop ()
connection (Balls, SingleBalls)
*根据特征选择区域;‘and’全部特征,‘or’特征之一;这里通过圆率进行筛选
select_shape (SingleBalls, IntermediateBalls, 'circularity', 'and', 0.85, 1.0)
*根据区域相关位置进行排序,这里选择‘column’,按照先列后行进行排序;递增
sort_region (IntermediateBalls, FinalBalls, 'first_point', 'true', 'column')
dev_display (Image)
dev_set_color('orange')
dev_display (FinalBalls)
disp_continue_message (WindowID, 'black', 'true')
stop ()
*计算给定区域的最小外接圆
smallest_circle (FinalBalls, Row, Column, Radius)
NumBalls := |Radius|
Diameter := 2 * Radius
meanDiameter := sum(Diameter) / NumBalls
mimDiameter := min(Diameter)
dev_display (Image)
*显示描述了圆心坐标及半径的圆
disp_circle (WindowID, Row, Column, Radius)
dev_set_color ('white')
 
*求数量用||
Row_length:=|Row|
Col_lenght:=|Column|
*遍历所有的圆心坐标
for i := 0 to Row_length-1 by 1 
    *圆心坐标
    R:=[Row[i],Column[i]]
    *用十字画出圆心位置
    disp_cross (WindowID, R[0], R[1], 10, 10)
    *disp_message(WindowID, 'Cc: ' + R, 'image',R[0],R[1], 'white', 'false')
endfor
stop()
 
for i := 0 to Row_length-1 by 1
    *圆心坐标
    R:=[Row[i],Column[i]]
    R1:=Row[i]
    W1:=Column[i]
    disp_message(WindowID, '(' + W1 +','+ R1 + ')', 'image',R[0],R[1], 'white', 'false')
    
endfor
stop()
 
for i := 0 to Row_length-1 by 1
    *圆心坐标
    R:=[Row[i],Column[i]]
    for k := 1 to Row_length-1 by 1
    W:=[Row[k],Column[k]]
    distance_pp(R[0],R[1],W[0],W[1], Distance)
    disp_line(WindowID,R[0],R[1],W[0],W[1])
    disp_message (WindowID, 'Dp: ' + Distance, 'image',(R[0]+W[0])/2,(R[1]+W[1])/2, 'white', 'false')
    endfor
endfor
 
 
for i := 1 to NumBalls by 1
    *判断余数是否为1
    if (fmod(i,2) != 1)
        disp_message (WindowID, 'D: ' + Diameter[i - 1], 'image', Row[i - 1] - 1.8 * Radius[i - 1], max([Column[i - 1] - 60,0]), 'white', 'false')
        *disp_message (WindowID, 'D: ' + Diameter[i - 1], 'image', Row[i - 1] - 2.0 * Radius[i - 1], max([Column[i - 1] - 60,0]), 'white', 'false')
    else
        disp_message (WindowID, 'D: ' + Diameter[i - 1], 'image', Row[i - 1] + 1.2 * Radius[i - 1], max([Column[i - 1] - 60,0]), 'white', 'false')
    endif
endfor
 
dev_set_color ('green')
dev_update_window ('on')
*disp_continue_message (WindowID, 'black', 'true')
stop ()
dev_close_window ()

Guess you like

Origin blog.csdn.net/qq_65356682/article/details/129860696