org.openpnp.vision.pipeline.stages.DetectLinesHough
文章目录org.openpnp.vision.pipeline.stages.DetectLinesHough功能参数例子测试图像generate_line_test_image.pycv-pipeline效果ENDorg.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, height800,600# 纯蓝色背景 (BGR: 255, 0, 0) 非黑白色imgnp.full((height,width,3),(255,0,0),dtypenp.uint8)#1.一条完整的红色水平线段(无间隙)cv2.line(img,(50,100),(300,100),(0,0,255),thickness4)#2.一条完整的绿色垂直线段 cv2.line(img,(600,50),(600,250),(0,255,0),thickness4)#3.一条完整的黄色斜线段 cv2.line(img,(50,500),(350,300),(0,255,255),thickness4)#4.一条有间隙的青色线段测试 maxLineGap # 线段整体从(400,400)到(750,550)中间留出40像素的缺口 line_start(400,400)line_end(750,550)# 计算方向 dxline_end[0]-line_start[0] dyline_end[1]-line_start[1] lengthint(np.hypot(dx,dy))# 缺口起始位置距离起点 100 像素缺口长度 40 像素gap_start100gap_end140# 逐段绘制prev_endline_startfordistinrange(0, length 1,10):# 每 10 像素采样一次tdist / length xint(line_start[0] t * dx)yint(line_start[1] t * dy)ifgap_startdistgap_end:# 缺口区域不绘制continueifprev_end is not None: cv2.line(img, prev_end,(x, y),(255,255,0),thickness4)prev_end(x, y)# 最后补上缺口后的最后一段final_start_xint(line_start[0](gap_end / length)* dx)final_start_yint(line_start[1](gap_end / length)* dy)cv2.line(img,(final_start_x, final_start_y), line_end,(255,255,0),thickness4)# 可选添加文字说明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_pathtest_lines.pngcv2.imwrite(output_path, img)print(f测试图像已生成: {output_path})cv-pipelinecv-pipelinestagescv-stageclassorg.openpnp.vision.pipeline.stages.ImageReadnamereadenabledtruefileD:\3rd\openpnp_prj\openpnp-official\openpnp-test-images\my_test\test_lines.pngcolor-spaceBgrhandle-as-capturedfalse/cv-stageclassorg.openpnp.vision.pipeline.stages.ConvertColornamegrayenabledtrueconversionBgr2Gray/cv-stageclassorg.openpnp.vision.pipeline.stages.BlurGaussiannameblurenabledtruekernel-size3property-nameBlurGaussian/cv-stageclassorg.openpnp.vision.pipeline.stages.DetectEdgesCannynameedgesenabledtruethreshold-125.0threshold-275.0/cv-stageclassorg.openpnp.vision.pipeline.stages.DetectLinesHoughnamelinesenabledtruerho0.5theta0.01745threshold50min-line-length10.0max-line-gap0.85/cv-stageclassorg.openpnp.vision.pipeline.stages.ImageRecallname0enabledtrueimage-stage-nameread/cv-stageclassorg.openpnp.vision.pipeline.stages.DrawContoursnamedrawenabledtruecontours-stage-namelinesthickness2index-1colorr0g0b0a255//cv-stagecv-stageclassorg.openpnp.vision.pipeline.stages.ImageWritenamesaveenabledtruefileoutput_lines_fixed.png//stages/cv-pipeline效果DetectLinesHough参数是 max-line-gap“0.85”再往大了调字体行就会被判定为直线。END
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2519018.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!