【Halcon视觉】定位

[复制链接]
查看49199 | 回复0 | 2024-2-27 14:41:04 | 显示全部楼层 |阅读模式
1.前言

        Halcon中的边缘提取,可拟合直线。通过两条直线的交点,可实现工件定位。

2.效果



*关窗

dev_close_window ()

*开窗

dev_open_window (0, 0, 512, 512, 'black', WindowHandle)

*清理窗口

dev_clear_window ()

*读取图片

read_image (Image, 'C:/Users/Administrator/Desktop/图片/电池1.jpg')

*灰度化

rgb1_to_gray (Image, GrayImage)

*阈值化

threshold (GrayImage, Regions, 32, 161)

*裁剪

reduce_domain (GrayImage, Regions, MyImageReduced)

**********第一条直线**********

*画矩形

draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)

*生成矩形

gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)

*裁剪

reduce_domain (MyImageReduced, Rectangle, ImageReduced)

*轮廓

edges_sub_pix (ImageReduced, Edges, 'canny', 1, 20, 40)

*分割轮廓

segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 4, 2)

*选择轮廓

select_contours_xld (ContoursSplit, SelectedContours, 'contour_length', 100, 20000, -0.5, 0.5)

*拟合直线

fit_line_contour_xld (SelectedContours, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)

*生成直线

gen_contour_polygon_xld (Contour, [RowBegin,RowEnd], [ColBegin,ColEnd])

**********第二条直线**********

*画矩形

draw_rectangle1 (WindowHandle, Row11, Column11, Row21, Column21)

*生成矩形

gen_rectangle1 (Rectangle1, Row11, Column11, Row21, Column21)

*裁剪

reduce_domain (MyImageReduced, Rectangle1, ImageReduced1)

*轮廓

edges_sub_pix (ImageReduced1, Edges1, 'canny', 1, 20, 40)

*分割轮廓

segment_contours_xld (Edges1, ContoursSplit1, 'lines_circles', 5, 4, 2)

*选择轮廓

select_contours_xld (ContoursSplit1, SelectedContours1, 'contour_length', 100, 20000, -0.5, 0.5)

*拟合直线

fit_line_contour_xld (SelectedContours1, 'tukey', -1, 0, 5, 2, RowBegin1, ColBegin1, RowEnd1, ColEnd1, Nr1, Nc1, Dist1)

*生成直线

gen_contour_polygon_xld (Contour1, [RowBegin1,RowEnd1], [ColBegin1,ColEnd1])

**********计算两条直线的交点**********

*直线交点

intersection_lines (RowBegin, ColBegin, RowEnd, ColEnd, RowBegin1, ColBegin1, RowEnd1, ColEnd1, MyRow, MyColumn, IsOverlapping)

*生成交叉点

gen_cross_contour_xld (Cross, MyRow, MyColumn, 30, 0.785398)

*显示坐标信息

disp_message (WindowHandle, '交点坐标( '+MyRow+'  ,'+MyColumn+'  )', 'window', 50, 50, 'red', 'true')



  Halcon的定位功能,广泛应用于工业。使制造业智能化。

原文链接:https://blog.csdn.net/NickStudent/article/details/123734444

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册哦

x
您需要登录后才可以回帖 登录 | 注册哦

本版积分规则