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

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

一、测量矩形拟合提取直线边缘

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。


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


目录
相关文章
|
1月前
|
算法 计算机视觉
OpenCV(三十七):拟合直线、三角形和圆形
OpenCV(三十七):拟合直线、三角形和圆形
54 0
|
1月前
|
计算机视觉
OpenCV(三十四):轮廓外接最大、最小矩形和多边形拟合
OpenCV(三十四):轮廓外接最大、最小矩形和多边形拟合
81 0
|
4月前
[Halcon&几何] 直线的垂线与延长线的计算
[Halcon&几何] 直线的垂线与延长线的计算
91 1
|
4月前
|
算法 容器
[MFC] 将多个坐标点拟合出一条直线,并画出
[MFC] 将多个坐标点拟合出一条直线,并画出
31 0
|
4月前
|
计算机视觉 Python
OpenCV中绘制圆和直线
要在OpenCV中绘制圆和直线,可以使用以下方法: 1. 导入所需库:
29 1
|
10月前
|
Web App开发 Windows
|
11月前
|
存储 API 图形学
OpenCV_11 轮廓检测:图像的轮廓+绘制轮廓+轮廓近似+边界矩形+椭圆拟合+直线拟合
轮廓检测指检测图像中的对象边界,更偏向于关注上层语义对象。如OpenCV中的findContours()函数, 它会得到每一个轮廓并以点向量方式存储,除此也得到一个图像的拓扑信息,即一个轮廓的后一个轮廓、前一个轮廓等的索引编号。
1125 0
|
计算机视觉
五、OpenCV绘制线、矩形、圆等基本几何形状
通过javaOpenCV中的Imgproc函数进行简单几何图形的绘制
99 0
五、OpenCV绘制线、矩形、圆等基本几何形状
|
编解码 算法 计算机视觉
Canny算子与霍夫变换检测圆与直线
Canny算子与霍夫变换检测圆与直线
289 0
Canny算子与霍夫变换检测圆与直线
075.绘制余弦曲线和直线的迭加
075.绘制余弦曲线和直线的迭加
49 0