文章目录
- org.openpnp.vision.pipeline.stages.DetectLinesHough
- 功能
- 参数
- 例子
- 测试图像
- generate_line_test_image.py
- cv-pipeline
- 效果
- END
org.openpnp.vision.pipeline.stages.DetectLinesHough
功能
在图像中检测直线段
在DetectLinesHough之前,需要执行DetectEdgesCanny来加强执行DetectLinesHough后的效果。
参数
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
rho | double | 0.5 | 距离分辨率(像素)。累加器在极坐标空间中的单位距离步长。值越小精度越高,但计算量越大。 |
theta | double | π/180 ≈ 0.01745 | 角度分辨率(弧度)。累加器中的角度步长,默认 π/180 表示 1° 分辨率。 |
threshold | int | 1000 | 累加器阈值。只有得票数 ≥ 该值的直线才会被检测为候选线段。值越大,检测的线段越少(只保留最明显的线)。 |
minLineLength | double | 30.0 | 最小线段长度,以图像对角线长度的百分比表示(如 30%)。短于此值的线段被丢弃。 |
maxLineGap | double | 5.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”,再往大了调,字体行就会被判定为直线。