news 2026/4/15 6:46:27

org.openpnp.vision.pipeline.stages.DetectLinesHough

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
org.openpnp.vision.pipeline.stages.DetectLinesHough

文章目录

  • org.openpnp.vision.pipeline.stages.DetectLinesHough
    • 功能
    • 参数
    • 例子
      • 测试图像
      • generate_line_test_image.py
      • cv-pipeline
    • 效果
    • END

org.openpnp.vision.pipeline.stages.DetectLinesHough

功能

在图像中检测直线段
在DetectLinesHough之前,需要执行DetectEdgesCanny来加强执行DetectLinesHough后的效果。

参数

参数类型默认值说明
rhodouble0.5距离分辨率(像素)。累加器在极坐标空间中的单位距离步长。值越小精度越高,但计算量越大。
thetadoubleπ/180 ≈ 0.01745角度分辨率(弧度)。累加器中的角度步长,默认 π/180 表示 1° 分辨率。
thresholdint1000累加器阈值。只有得票数 ≥ 该值的直线才会被检测为候选线段。值越大,检测的线段越少(只保留最明显的线)。
minLineLengthdouble30.0最小线段长度,以图像对角线长度的百分比表示(如 30%)。短于此值的线段被丢弃。
maxLineGapdouble5.0线段上允许的最大间隙,以图像对角线长度的百分比表示。如果同一条直线上两段之间的间隙小于此值,则合并为一条线段。

例子

测试图像

generate_line_test_image.py

# @fn generate_line_test_image.pyimportcv2importnumpy as np# 图像尺寸width, height=800,600# 纯蓝色背景 (BGR: 255, 0, 0) 非黑白色img=np.full((height,width,3),(255,0,0),dtype=np.uint8)#1.一条完整的红色水平线段(无间隙)cv2.line(img,(50,100),(300,100),(0,0,255),thickness=4)#2.一条完整的绿色垂直线段 cv2.line(img,(600,50),(600,250),(0,255,0),thickness=4)#3.一条完整的黄色斜线段 cv2.line(img,(50,500),(350,300),(0,255,255),thickness=4)#4.一条有间隙的青色线段(测试 maxLineGap) # 线段整体从(400,400)(750,550),中间留出40像素的缺口 line_start=(400,400)line_end=(750,550)# 计算方向 dx=line_end[0]-line_start[0] dy=line_end[1]-line_start[1] length=int(np.hypot(dx,dy))# 缺口起始位置(距离起点 100 像素,缺口长度 40 像素)gap_start=100gap_end=140# 逐段绘制prev_end=line_startfordistinrange(0, length +1,10):# 每 10 像素采样一次t=dist / length x=int(line_start[0]+ t * dx)y=int(line_start[1]+ t * dy)ifgap_start<=dist<=gap_end:# 缺口区域不绘制continueifprev_end is not None: cv2.line(img, prev_end,(x, y),(255,255,0),thickness=4)prev_end=(x, y)# 最后补上缺口后的最后一段final_start_x=int(line_start[0]+(gap_end / length)* dx)final_start_y=int(line_start[1]+(gap_end / length)* dy)cv2.line(img,(final_start_x, final_start_y), line_end,(255,255,0),thickness=4)# 可选:添加文字说明cv2.putText(img,"Red: full line",(50,70), cv2.FONT_HERSHEY_SIMPLEX,0.6,(0,0,255),2)cv2.putText(img,"Green: full vertical",(600,30), cv2.FONT_HERSHEY_SIMPLEX,0.6,(0,255,0),2)cv2.putText(img,"Yellow: full diagonal",(50,470), cv2.FONT_HERSHEY_SIMPLEX,0.6,(0,255,255),2)cv2.putText(img,"Cyan: with gap (middle missing)",(400,380), cv2.FONT_HERSHEY_SIMPLEX,0.6,(255,255,0),2)# 保存图像output_path="test_lines.png"cv2.imwrite(output_path, img)print(f"测试图像已生成: {output_path}")

cv-pipeline

<cv-pipeline><stages><cv-stageclass="org.openpnp.vision.pipeline.stages.ImageRead"name="read"enabled="true"file="D:\3rd\openpnp_prj\openpnp-official\openpnp-test-images\my_test\test_lines.png"color-space="Bgr"handle-as-captured="false"/><cv-stageclass="org.openpnp.vision.pipeline.stages.ConvertColor"name="gray"enabled="true"conversion="Bgr2Gray"/><cv-stageclass="org.openpnp.vision.pipeline.stages.BlurGaussian"name="blur"enabled="true"kernel-size="3"property-name="BlurGaussian"/><cv-stageclass="org.openpnp.vision.pipeline.stages.DetectEdgesCanny"name="edges"enabled="true"threshold-1="25.0"threshold-2="75.0"/><cv-stageclass="org.openpnp.vision.pipeline.stages.DetectLinesHough"name="lines"enabled="true"rho="0.5"theta="0.01745"threshold="50"min-line-length="10.0"max-line-gap="0.85"/><cv-stageclass="org.openpnp.vision.pipeline.stages.ImageRecall"name="0"enabled="true"image-stage-name="read"/><cv-stageclass="org.openpnp.vision.pipeline.stages.DrawContours"name="draw"enabled="true"contours-stage-name="lines"thickness="2"index="-1"><colorr="0"g="0"b="0"a="255"/></cv-stage><cv-stageclass="org.openpnp.vision.pipeline.stages.ImageWrite"name="save"enabled="true"file="output_lines_fixed.png"/></stages></cv-pipeline>

效果

DetectLinesHough参数是 max-line-gap=“0.85”,再往大了调,字体行就会被判定为直线。

END

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/15 6:43:13

终极AMD硬件调试革命:3大技术突破让系统稳定性提升5倍

终极AMD硬件调试革命&#xff1a;3大技术突破让系统稳定性提升5倍 【免费下载链接】SMUDebugTool A dedicated tool to help write/read various parameters of Ryzen-based systems, such as manual overclock, SMU, PCI, CPUID, MSR and Power Table. 项目地址: https://gi…

作者头像 李华
网站建设 2026/4/15 6:43:13

矿场安全高效运营必备!A-59P系列设备实战应用指南

前言&#xff1a;矿山作业环境复杂多变&#xff0c;高粉尘、强干扰、潜在爆炸风险等问题&#xff0c;一直是制约矿场安全高效运营的核心痛点。无论是井下应急通信、人员安全监护&#xff0c;还是日常调度协同&#xff0c;都需要一套可靠、适配、易落地的设备解决方案。今天&…

作者头像 李华
网站建设 2026/4/15 6:42:09

Java学习笔记_Day30(File)

FileFile对象就表示一个路径&#xff0c;可以是文件的路径&#xff0c;也可以是文件夹的路径这个路径可以是存在的&#xff0c;也可以是不存在的三种构造方法常见的成员方法1.判断和获取2.创建和删除3.获取并遍历当调用者File表示的路径不存在时&#xff0c;返回null当调用者Fi…

作者头像 李华
网站建设 2026/4/15 6:39:11

终极Windows优化指南:3分钟用Win11Debloat释放系统性能

终极Windows优化指南&#xff1a;3分钟用Win11Debloat释放系统性能 【免费下载链接】Win11Debloat A simple, lightweight PowerShell script that allows you to remove pre-installed apps, disable telemetry, as well as perform various other changes to declutter and c…

作者头像 李华
网站建设 2026/4/15 6:38:13

MySQL8.0升级到MySQL8.4避坑:密码插件问题

对于广大运维和DBA来说&#xff0c;MySQL8.0即将结束官方支持&#xff08;EOL&#xff09;已经是板上钉钉的事。按照Oracle官方的版本生命周期策略&#xff0c;MySQL8.0的常规支持周期逐步收尾&#xff0c;后续仅保留有限的安全修复&#xff0c;不再提供功能更新与全面技术支持…

作者头像 李华