基于Halcon学习的边缘提取【三】close_contour_gaps.hdev例程

 关闭提取的直线轮廓中的间隙


dev_close_window ()
dev_update_window ('off')


*步骤:创建合成图像
gen_rectangle1 (Rectangle, 30, 20, 100, 100)

*将区域转换为二进制字节图像。
region_to_bin (Rectangle, BinImage, 130, 100, 120, 130)

*将图像的区域缩小为矩形。
rectangle1_domain (BinImage, ImageReduced, 20, 48, 40, 52)

*均值滤波
mean_image (ImageReduced, SmoothedImage, 15, 15)

*将图像的灰度值绘制到另一个图像中。
paint_gray (SmoothedImage, BinImage, Image)
get_image_size (Image, Width, Height)
dev_open_window_fit_image (Image, 0, 0 , Width, Height, WindowID)
dev_set_draw ('margin')
dev_set_line_width (3)
dev_display (Image)
stop ()

*将图像的区域缩小为矩形。
rectangle1_domain (Image, ImageReduced, 5, 5, 125, 115)

*输出xld亚像素轮廓
edges_sub_pix (ImageReduced, Edges, 'lanser2', 1.1, 22, 30)
dev_display (Image)
dev_display (Edges)
stop ()

*将xld等高线分为线段和圆弧或椭圆弧。
segment_contours_xld (Edges, LineSegments, 'lines', 5, 4, 2)

*计算回归线到xld等高线的参数
regress_contours_xld (LineSegments, RegressContours, 'no', 1)


*联合近似共线轮廓。
union_collinear_contours_xld (RegressContours, UnionContours, 10, 1, 2, 0.1, 'attr_keep')
sort_contours_xld (UnionContours, SortedContours, 'upper_left', 'true', 'column')
dev_display (Image)

*以可定义的交替颜色显示标志性对象
colored_display (SortedContours, ['yellow','white','white','yellow'])
dev_update_window ('on')

dev_close_window ()
dev_update_window ('off')

*步骤:创建合成图像
gen_rectangle1 (Rectangle, 30, 20, 100, 100)

*将区域转换为二进制字节图像。
region_to_bin (Rectangle, BinImage, 130, 100, 120, 130)

*将图像的区域抠出一个矩形
rectangle1_domain (BinImage, ImageReduced, 20, 48, 40, 52)

*均值滤波
mean_image (ImageReduced, SmoothedImage, 15, 15)

就是把SmoothedImage叠加到BinImage上
目的是为了断开这个矩形
---------------------

*将图像的灰度值绘制到另一个图像中。
paint_gray (SmoothedImage, BinImage, Image)
get_image_size (Image, Width, Height)
dev_open_window_fit_image (Image, 0, 0 , Width, Height, WindowID)
dev_set_draw ('margin')
dev_set_line_width (3)
dev_display (Image)
stop ()

*将图像的区域缩小为矩形。
rectangle1_domain (Image, ImageReduced, 5, 5, 125, 115)

*输出xld亚像素轮廓
edges_sub_pix (ImageReduced, Edges, 'lanser2', 1.1, 22, 30)
dev_display (Image)
dev_display (Edges)
stop ()

*将xld等高线分为线段和圆弧或椭圆弧。
segment_contours_xld (Edges, LineSegments, 'lines', 5, 4, 2)

*计算回归线到xld等高线的参数
regress_contours_xld (LineSegments, RegressContours, 'no', 1)

*联合近似共线轮廓。
union_collinear_contours_xld (RegressContours, UnionContours, 10, 1, 2, 0.1, 'attr_keep')
sort_contours_xld (UnionContours, SortedContours, 'upper_left', 'true', 'column')

dev_display (Image)

*以可定义的交替颜色显示标志性对象
colored_display (SortedContours, ['yellow','white','white','yellow'])
dev_update_window ('on')


region_to_bin(Region : BinImage : ForegroundGray, BackgroundGray, Width, Height : )

功能:将区域转换为二进制字节图像。

Region:输入要转换的区域;BinImage:输出二值图像; ForegroundGray:前景灰度值,;BackgroundGray:背景灰度值;Width, Height输出宽高 

rectangle1_domain(Image : ImageReduced : Row1, Column1, Row2, Column2 : )

功能:将图像的域缩小为矩形。

paint_gray(ImageSource, ImageDestination : MixedImage : : )

功能: 绘制图像的灰度值到另一个图像。

regress_contours_xld(Contours : RegressContours : Mode, Iterations : )

功能:计算输入xld等高线的以下参数,并将其与生成的等高线一起存储为全局属性:

Mode:异常处理的类型;Iterations :迭代的次数

union_collinear_contours_xld(Contours : UnionContours : MaxDistAbs, MaxDistRel, MaxShift, MaxAngle, Mode : )

功能:联合近似共线轮廓。

MaxDistAbs:沿参考轮廓回归线测量的两个轮廓之间间隙的最大长度;MaxDistRel:两条等高线之间间隙的最大长度,相对于参考等高线的长度,均沿参考等高线的回归线测量;MaxShift:第二条等高线与参考等高线回归线的最大距离;MaxAngle:两条等高线回归线之间的最大角度;Mode:定义轮廓属性处理的模式,即如果轮廓属性被保留或丢弃
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Halcon中的fit_line_contour_xld算子可以用OpenCV中的fitLine函数来实现。fitLine函数可以拟合一条直线,从而实现对轮廓的拟合。具体步骤如下: 1. 读入图像,并将其转换为灰度图像。 2. 对图像进行二值化处理。 3. 使用findContours函数查找轮廓。 4. 对轮廓进行拟合,使用fitLine函数。 5. 计算拟合直线的斜率和截距。 6. 绘制检测结果,显示图像。 下面是一个简单的示代码: ``` import cv2 import numpy as np # 读入图像并转换为灰度图像 img = cv2.imread('image.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 二值化图像 ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) # 查找轮廓 contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) # 对轮廓进行拟合 rows,cols = img.shape[:2] vx,vy,x,y = cv2.fitLine(contours[0], cv2.DIST_L2, 0, 0.01, 0.01) lefty = int((-x*vy/vx) + y) righty = int(((cols-x)*vy/vx)+y) # 绘制检测结果 cv2.line(img,(cols-1,righty),(0,lefty),(0,255,0),2) # 显示图像 cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在此示中,我们首先读取一张图像,然后将其转换为灰度图像并进行二值化处理。接下来,我们使用findContours函数查找轮廓,并使用fitLine函数对轮廓进行拟合。然后,我们计算拟合直线的斜率和截距,并使用cv2.line函数在图像上绘制该直线。最后,我们显示图像并输出结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值