- 博客主页:https://loewen.blog.csdn.net/?type=blog
- 欢迎点赞 收藏 ⭐留言 如有错误敬请指正!
- 本文由 Loewen丶原创,首发于 CSDN,转载注明出处
- 现在的付出,都会是一种沉淀,只为让你成为更好的人✨
1、封装的函数介绍:将整个算法进行封装,得到函数MS_LineDetecter。
2、函数源码:
gen_cross_contour_xld (Cross1, iLineRow1, iLineCol1, 6, 0.785398)
gen_cross_contour_xld (Cross2, iLineRow2, iLineCol2, 6, 0.785398)
distance_pp (iLineRow1, iLineCol1, iLineRow2, iLineCol2, Distance)
angle_lx (iLineRow1, iLineCol1, iLineRow2, iLineCol2, Angle)
tuple_deg (Angle, Deg)
measureRectWidth := iRectWidth
measureRectHeight := iRectHeight
measureRectNum := Distance/measureRectWidth
get_image_size (ImageIn, Width, Height)
row := []
col := []
gen_empty_region (outDetectRectangle)
*这里把前1个测量矩形和后1个测量砍掉的目的是避免两条相临边拟合的边缘出现交叉情况,导致最终拟合的矩形出现不准确
for Index := 0+1 to measureRectNum-1 by 1
gen_rectangle2 (Rectangle, iLineRow1+cos(Angle+rad(90))*measureRectWidth*Index, iLineCol1+sin(Angle+rad(90))*measureRectWidth*Index, Angle+rad(90), measureRectHeight, measureRectWidth/2)
concat_obj (outDetectRectangle, Rectangle, outDetectRectangle)
gen_measure_rectangle2 (iLineRow1+cos(Angle+rad(90))*measureRectWidth*Index, iLineCol1+sin(Angle+rad(90))*measureRectWidth*Index, Angle+rad(90), measureRectHeight, measureRectWidth/2, Width, Height, 'nearest_neighbor', MeasureHandle)
measure_pos (ImageIn, MeasureHandle, iSigma, iThreshold, 'all', 'all', RowEdge, ColumnEdge, Amplitude, Distance1)
row := [row,RowEdge]
col := [col,ColumnEdge]
gen_cross_contour_xld (Cross, RowEdge, ColumnEdge, 6, Angle)
endfor
gen_contour_polygon_xld (Contour, row, col)
fit_line_contour_xld (Contour, 'tukey', -1, 0, 5, 2, oLineRow1, oLineCol1, oLineRow2, oLineCol2, Nr, Nc, Dist)
gen_contour_polygon_xld (Contour1, [oLineRow1,oLineRow2], [oLineCol1,oLineCol2])
3、检测效果:
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
dev_set_draw ('margin')
read_image (Image, '3.bmp')
dev_set_color ('green')
draw_line (WindowHandle, Row11, Column11, Row21, Column21)
MS_LineDetecter (Image, outDetectRectangle, Row11, Column11, Row21, Column21, 15, 15, 1, 50, oLineRow1, oLineCol1, oLineRow2, oLineCol2)
gen_contour_polygon_xld (Contour1, [oLineRow1,oLineRow2], [oLineCol1,oLineCol2])
dev_display (Image)
dev_display (outDetectRectangle)
dev_set_color ('red')
dev_display (Contour1)
1、封装的函数介绍:将整个算法进行封装,得到函数MS_FitRectangle。
2、检测效果:
dev_close_window ()
dev_open_window (0, 0, 700, 700, 'black', WindowHandle)
dev_set_draw ('margin')
read_image (Image, '3.bmp')
dev_set_color ('green')
draw_region (Region, WindowHandle)
MS_FitRectangle (Image, Region, outRegion, outContour, 25, 20, 40, 1, CenterRowlid, CenterCollid, CenterAnglelid)
gen_cross_contour_xld (Cross, CenterRowlid, CenterCollid, 26, 0.785398)
dev_display (Image)
dev_set_color ('red')
dev_display (outContour)
dev_display (Cross)
1、封装的函数介绍:将整个算法进行封装,得到函数fitRectangle。
注:封装的函数接口和上面使用测量矩形方法封装的接口类似,只是新增了拟合出的矩形宽、高输出,测量矩形方法经过优化后的检测精度也和二维计量模型方法差不多,差别是两者算法所在的halcon模块不同,前者是一维,后者是二维。
2、检测效果:
dev_close_window ()
dev_open_window (0, 0, 700, 700, 'black', WindowHandle)
dev_set_draw ('margin')
read_image (Image, '3.bmp')
dev_set_color ('green')
draw_region (Region, WindowHandle)
fitRectangle (Image, Region, outRegion, outContour, 25, 20, 40, 1, CenterRowlid, CenterCollid, RectWidth, RectHeight, CenterAnglelid)
gen_cross_contour_xld (Cross, CenterRowlid, CenterCollid, 26, 0.785398)
dev_display (Image)
dev_set_color ('red')
dev_display (outContour)
dev_display (Cross)
1、封装的函数介绍:将整个算法进行封装,得到函数MS_FitCircle。
2、检测效果:
dev_close_window ()
dev_open_window (0, 0, 700, 700, 'black', WindowHandle)
dev_set_draw ('margin')
read_image (Image, '4.bmp')
dev_set_color ('green')
draw_circle (WindowHandle, Row1, Column, Radius)
gen_circle (Circle, Row1, Column, Radius)
*darkToLight参数:true表示从圆外侧指向圆心方向开始找第一个边缘, false表示从圆心方向往外侧找第一个边缘
MS_FitCircle (Image, Circle, outDetectCircle, outContour, 10, 30, 50, 1, true, RowF, ColF, RadiusF)
gen_cross_contour_xld (Cross2, RowF, ColF, 20, 0.785398)
dev_display (Image)
dev_set_color ('red')
dev_display (outContour)
dev_display (Cross2)
1、封装的函数介绍:将整个算法进行封装,得到函数fitCircle。
2、检测效果:
dev_close_window ()
dev_open_window (0, 0, 700, 700, 'black', WindowHandle)
dev_set_draw ('margin')
read_image (Image, '4.bmp')
dev_set_color ('green')
draw_circle (WindowHandle, Row1, Column, Radius)
gen_circle (Circle, Row1, Column, Radius)
fitCircle (Image, Circle, outRegion, measureRect, 3, 10, 1.5, 90, CenterRow, CenterCol, CenterRadius)
gen_cross_contour_xld (Cross2, RowF, ColF, 20, 0.785398)
dev_display (Image)
dev_set_color ('red')
dev_display (outContour)
dev_display (Cross2)
注:
测量矩形和二维计量模型分别拟合矩形和圆的时候,之前我有专门做过精度测试,图像效果差不多的前提下,拟合精度差别不大,都可以放心用。
唯一不同的是二维计量模型算子属于Halcon
的二维模块,加密狗新增一个二维模块会贵一些,考虑成本问题,所以自己又重新开发封装了使用测量矩形的算法函数来拟合圆和矩形。
由于封装的函数代码较多,后面的MS_FitRectangle、fitRectangle、MS_FitCircle和fitCircle函数源码、仿真代码以及测试原图我单独上传,有需要的可以下载,下载地址链接:https://download.csdn.net/download/weixin_43197380/72290385。
戳戳小手帮忙点个免费的赞吧,嘿嘿。 |