[Halcon&拟合] 直线、矩形和圆的边缘提取

您所在的位置:网站首页 边缘和轮廓 [Halcon&拟合] 直线、矩形和圆的边缘提取

[Halcon&拟合] 直线、矩形和圆的边缘提取

2024-07-11 09:18| 来源: 网络整理| 查看: 265

📢博客主页:https://loewen.blog.csdn.net/?type=blog📢欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!📢本文由 Loewen丶原创,首发于 CSDN,转载注明出处🙉📢现在的付出,都会是一种沉淀,只为让你成为更好的人✨ 一、测量矩形拟合提取直线边缘

1、封装的函数介绍:将整个算法进行封装,得到函数MS_LineDetecter。

MS_LineDetecter (ImageIn : outDetectRectangle : iLineRow1, iLineCol1, iLineRow2, iLineCol2, iRectWidth, iRectHeight, iSigma, iThreshold : oLineRow1, oLineCol1, oLineRow2, oLineCol2) — 提取图像的直线边缘 参数: ImageIn(in):输入图像。 outDetectRectangle (out):边缘测量矩形合集。 iLineRow1, iLineCol1, iLineRow2, iLineCol2(in):输入直线。 iRectWidth, iRectHeight(in):测量矩形和宽、高。 iSigma(in) :滤波尺寸。 iThreshold(in) :测量矩形阈值。 oLineRow1, oLineCol1, oLineRow2, oLineCol2(out) :拟合得到的直线。

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。

MS_FitRectangle (inImage, inRoi : outDetectRectangle, outContour : inRectWidth, inRectHeight, inThreshold, inSigma : CenterRow, CenterCol, RectAngel) — 拟合提取图像的距形边缘和中心 参数: ImageIn(in):输入图像。 inRoi(in):待检测的区域(矩形)。 outDetectRectangle(out):边缘检测矩形合集。 outContour(out):拟合出的矩形。 iRectWidth, iRectHeight(in):测量矩形和宽、高。 iThreshold(in) :测量矩形阈值。 iSigma(in) :滤波尺寸。 CenterRow, CenterCol(out) :拟合出的矩形中心行、列坐标。 RectAngel(out) :拟合出的矩形角度。

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。

fitRectangle (inImg, inRoi : measureRect, outContour : iRectWidth, iRectHeight, iThreshold, iSigma: CenterRow, CenterCol, RectWidth, RectHeight, CenterAngle)— 拟合提取图像的距形边缘和中心 参数: ImageIn(in):输入图像。 inRoi(in):待检测的区域(矩形)。 outDetectRectangle(out):边缘检测矩形合集。 outContour(out):拟合出的矩形。 iRectWidth, iRectHeight(in):测量矩形和宽、高。 iThreshold(in) :测量矩形阈值。 iSigma(in) :滤波尺寸。 CenterRow, CenterCol(out) :拟合出的矩形中心行、列坐标。 RectWidth, RectHeight(out) :拟合出的矩形宽、高长度。 RectAngel(out) :拟合出的矩形角度。

注:封装的函数接口和上面使用测量矩形方法封装的接口类似,只是新增了拟合出的矩形宽、高输出,测量矩形方法经过优化后的检测精度也和二维计量模型方法差不多,差别是两者算法所在的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。

MS_FitCircle (inImg, inReg : outDetectCircle, outContour : iRectWidth, iRectHeight, iThreshold, iSigma, darkToLight : CenterRow, CenterCol, CenterRadius) — 拟合提取图像的圆形边缘和中心 参数: inImg(in):输入图像。 inReg(in):待检测的区域(矩形)。 outDetectCircle(out):边缘检测矩形合集。 outContour(out):拟合出的圆形。 iRectWidth, iRectHeight(in):测量矩形和宽、高。 iThreshold(in) :测量矩形阈值。 iSigma(in) :滤波尺寸。 darkToLight(in) :表示找边的方向,true表从圆外侧指向圆心方向开始找第一个边缘;false表示从圆心方向往外侧找第一个边缘。 CenterRow, CenterCol(out) :拟合出的圆的中心行、列坐标。 CenterRadius(out) :拟合出的圆的半径。

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。

fitCircle (inImg, inReg: outContour, outDetectCircle: iRectWidth, iRectHeight, iSigma, iThreshold: CenterRow, CenterCol, CenterRadius) — 拟合提取图像的圆形边缘和中心 参数: inImg(in):输入图像。 inReg(in):待检测的区域(矩形)。 outContour(out):拟合出的圆形。 outDetectCircle(out):边缘检测矩形合集。 iRectWidth, iRectHeight(in):测量矩形和宽、高。 iSigma(in) :滤波尺寸。 iThreshold(in) :测量矩形阈值。 CenterRow, CenterCol(out) :拟合出的圆的中心行、列坐标。 CenterRadius(out) :拟合出的圆的半径。

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。

戳戳小手帮忙点个免费的赞吧,嘿嘿。


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3