halcon 使用点集创建测量矩形
时间: 2023-07-30 20:07:15 浏览: 65
在Halcon中使用点集创建测量矩形,可以使用gen_rectangle2函数。具体步骤如下:
1.使用gen_region_points函数创建一个包含点集的区域对象。
2.使用gen_rectangle2函数,传入点集区域对象,以及所需的矩形中心、角度、宽度和高度等参数,生成测量矩形。
3.使用gen_contour_polygon函数将测量矩形转换为轮廓对象。
下面是一个示例代码:
```
* 生成包含点集的区域对象
gen_region_points (Points, 'random', 100, 200, 200)
* 创建测量矩形
gen_rectangle2 (Rectangle, 100, 100, 0, 100, 50)
* 使用点集区域对象和测量矩形生成轮廓对象
gen_contour_polygon (Contour, Rectangle, 'border')
```
在这个示例中,首先使用gen_region_points函数生成一个包含100个随机点的区域对象Points。然后使用gen_rectangle2函数创建一个以(100,100)为中心,宽度为100,高度为50,不旋转的测量矩形Rectangle。最后,使用gen_contour_polygon函数将Rectangle转换为轮廓对象Contour。
相关问题
halcon 使用XLD轮廓创建依次创建5*20的测量矩形
在Halcon中使用XLD轮廓创建依次创建5*20的测量矩形,可以使用gen_measure_rectangle2函数。具体步骤如下:
1.使用gen_contour_points函数创建一个包含XLD轮廓的区域对象。
2.使用gen_measure_rectangle2函数,传入XLD轮廓区域对象,以及所需的矩形中心、角度、宽度和高度等参数,生成测量矩形。
3.重复步骤2,使用不同的矩形中心和角度,创建5*20个测量矩形。
下面是一个示例代码:
```
* 生成XLD轮廓
gen_contour_points (XLDContour, 'rectangle2', 100, 100, 0, 50, 30)
* 循环创建测量矩形
for Index := 1 to 100 by 5
for Jndex := 1 to 20
CenterX := Index * 10
CenterY := Jndex * 10
Angle := (Index + Jndex) mod 360
gen_measure_rectangle2 (MeasureRectangle, XLDContour, CenterY, CenterX, Angle, 10, 5, 'nearest_neighbor')
endfor
endfor
```
在这个示例中,首先使用gen_contour_points函数生成一个包含一个50x30的不旋转矩形的XLD轮廓对象XLDContour。然后使用双重循环,分别计算每个测量矩形的中心坐标和角度,并使用gen_measure_rectangle2函数创建测量矩形。最后,可以将这些测量矩形用于不同的图像分析任务。
halcon2017 使用xld查找矩形
使用XLD查找矩形的步骤如下:
1. 定义一个输入图像。
```python
input_img = image.read_image('input_image.png')
```
2. 对输入图像进行预处理,例如灰度化、二值化、滤波等操作。
```python
gray_img = image.convert_image(input_img, 'byte')
threshold_img = image.threshold(gray_img, 'max_separability')
filtered_img = image.median_rect(gray_img, 5, 5)
```
3. 使用XLD函数查找矩形。
```python
rects = image.hough_rectangle1(filtered_img, 50, np.pi/180, 50, 20, 20)
```
其中,`hough_rectangle1`函数的参数含义如下:
- `filtered_img`:输入图像。
- `50`:矩形边缘检测的阈值。
- `np.pi/180`:角度分辨率。
- `50`:最小矩形宽度。
- `20`:最小矩形高度。
- `20`:最小矩形长度。
4. 对查找到的矩形进行可视化。
```python
output_img = input_img.copy()
for rect in rects:
output_img = image.draw_rectangle(output_img, rect, color='green', thickness=3)
image.display(output_img)
```
完整代码如下:
```python
import numpy as np
import halcon as image
input_img = image.read_image('input_image.png')
gray_img = image.convert_image(input_img, 'byte')
threshold_img = image.threshold(gray_img, 'max_separability')
filtered_img = image.median_rect(gray_img, 5, 5)
rects = image.hough_rectangle1(filtered_img, 50, np.pi/180, 50, 20, 20)
output_img = input_img.copy()
for rect in rects:
output_img = image.draw_rectangle(output_img, rect, color='green', thickness=3)
image.display(output_img)
```
注意:以上代码仅供参考,具体操作需要根据实际情况进行调整。