Halcon实现卡尺找圆

Halcon实现卡尺找圆

最近在学习halcon算子,在此纪录一下学习内容,卡尺找圆的实现。

运行结果图

在这里插入图片描述

粗略注释

dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
*读取图像
read_image (Image, 'D:/Image/1.png')
*显示图像
dev_display (Image)
*图像灰度转换
rgb1_to_gray (Image, GrayImage)
*绘制圆形
draw_circle (WindowHandle, Row, Column, Radius)
*创建计量模型
create_metrology_model (MetrologyHandle)
*添加圆形计量模型
add_metrology_object_circle_measure (MetrologyHandle, Row, Column, Radius, 20, 5, 1, 30, [], [], Index)
*获取计量对象模型轮廓
get_metrology_object_model_contour (Contour1, MetrologyHandle, Index, 1.5)
*获取计量模型测量结果
get_metrology_object_measures (Contours, MetrologyHandle, 'all', 'all', Row1, Column1)
*应用计量模型到图像
apply_metrology_model (GrayImage, MetrologyHandle)
*获取计量模型结果对象
get_metrology_object_result (MetrologyHandle, Index, 'all', 'result_type', 'all_param', Parameter)
*获取计量模型对象结果轮廓
get_metrology_object_result_contour (Contour, MetrologyHandle, Index, 'all', 1.5)
*显示轮廓
dev_set_color ('green')
dev_display (Contour)

查看官方帮助文档注释

dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
read_image (Image, 'D:/Image/1.png')
dev_display (Image)
**灰度转换
rgb1_to_gray (Image, GrayImage)

draw_circle (WindowHandle, Row, Column, Radius)
**Create the data structure that is needed to measure geometric shapes.
**创建计量模型:创建测量几何形状所需的数据结构,
create_metrology_model (MetrologyHandle)

** Add a circle or a circular arc to a metrology model.
****添加圆测量计量对象:添加一个圆或圆弧到计量模型,输出Index
**MetrologyHandle:计量模型句柄
*Row :所在行
*Column :所在列
*Radius :半径
*MesureLength1 :测量长度1
*MesureLength2 :测量长度2
*MesureSigma :测量平滑系数
*MesureThreshold :测量阈值
*GenparamName :参数名
*GenparamValue :参数值
*Index :索引
add_metrology_object_circle_measure (MetrologyHandle, Row, Column, Radius, 20, 5, 1, 30, [], [], Index)


****Query the model contour of a metrology object in image coordinates.
****获取计量对象模型轮廓: 在图像坐标系中查询计量对象的模型轮廓
**Contour :输出轮廓
**MetrologyHandle:计量模型句柄
**Index : 计量对象索引(Index of the metrology object.**Resolution:相邻轮廓点之间的距离(Distance between neighboring contour points.)。
get_metrology_object_model_contour (Contour1, MetrologyHandle, Index, 1.5)

**Get the measure regions and the results of the edge location for the metrology objects of a metrology model.
**获取一个计量模型中,计量对象的测量区域和边缘位置定位结果 
**Contours :测量区域的矩形xld轮廓(Rectangular XLD Contours of measure regions.)
**MetrologyHandle :计量对象句柄
**Index :计量对象索引
**Transition :极性选择: 任意极性/由暗到明(0->255/由明到暗(255->0)(Select light/dark or dark/light edges.**Row1:所在行
**Column1:所在列
get_metrology_object_measures (Contours, MetrologyHandle, 'all', 'all', Row1, Column1)

**Measure and fit the geometric shapes of all metrology objects of a metrology model.
**应用计量模型:测量和拟合一个计量模型中所有计量对象的几何形状
**确定边缘位置
**拟合边缘位置几何形状
apply_metrology_model (GrayImage, MetrologyHandle)

****get the results of the measurement of a metrology model.
****获取计量对象结果:获取计量模型的测量结果
**MetrologyHandle:计量句柄
**Index:计量索引
**Instance:计量对象实例(Instance of the metrology object.**GenParamName:通用参数名
**GenParamValue:通用参数值
**Parameter:返回结果
get_metrology_object_result (MetrologyHandle, Index, 'all', 'result_type', 'all_param', Parameter)
* Query the result contour of a metrology object.
*获取计量对象结果轮廓:查询计量对象的结果轮廓
**Contour :获取的结果轮廓
**MetrologyHandle:计量句柄
**Instance:计量对象实例(Instance of the metrology object.**Resolution:相邻轮廓点之间的距离(Distance between neighboring contour points.)。
get_metrology_object_result_contour (Contour, MetrologyHandle, Index, 'all', 1.5)
dev_set_color ('green')
dev_display (Contour)
OpenCV和Halcon都是常用的机器视觉开发工具,它们都提供了丰富的图像处理和分析功能。在OpenCV中,可以使用Hough变换来进行圆形检测,而在Halcon中,可以使用卡尺算法找到圆形。 下面是一个简单的OpenCV实现Halcon卡尺找圆的示例代码: ```python import cv2 import numpy as np # 读取图像并转换为灰度图像 img = cv2.imread('circle.png') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 高斯模糊和Canny边缘检测 blurred = cv2.GaussianBlur(gray, (5, 5), 0) edges = cv2.Canny(blurred, 50, 150) # Hough圆检测 circles = cv2.HoughCircles(edges, cv2.HOUGH_GRADIENT, dp=1, minDist=100, param1=50, param2=30, minRadius=0, maxRadius=0) # 绘制圆 if circles is not None: circles = np.round(circles[0, :]).astype("int") for (x, y, r) in circles: cv2.circle(img, (x, y), r, (0, 255, 0), 2) cv2.putText(img, "Circle", (x-r, y-r), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) cv2.imshow("Image", img) cv2.waitKey(0) ``` 在Halcon中,可以使用卡尺算法找到圆形的边缘。下面是一个简单的Halcon实现卡尺找圆的示例代码: ```python import halcon as ha # 读取图像并转换为灰度图像 img = ha.read_image('circle.png') gray = ha.convert_image_type(img, 'byte') # 边缘检测和二值化 edges = ha.edges_image(gray, 'canny', 5, 20) bin_img = ha.threshold(edges, 128, 255) # 卡尺找圆 contours = ha.contour(bin_img) circles = ha.select_shape(contours, 'circle', 'all', 1, 100) circles = ha.gen_circle_contour_xld(ha.tuple(circles[0]), ha.tuple(circles[1]), ha.tuple(circles[2])) # 绘制圆 if len(circles) > 0: for circle in circles: row, col, radius = ha.tuple(circle) ha.draw_circle(img, row, col, radius, 'green', 2) ha.disp_image(img) ha.wait_seconds(0) ``` 以上代码仅供参考,具体实现可能需要根据实际情况进行调整和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值