org.openpnp.vision.pipeline.stages.FilterRects

张开发
2026/4/17 18:08:50 15 分钟阅读

分享文章

org.openpnp.vision.pipeline.stages.FilterRects
文章目录org.openpnp.vision.pipeline.stages.FilterRects功能参数例子生成测试图片cv-pipeline config效果更换测试图片效果ENDorg.openpnp.vision.pipeline.stages.FilterRects功能用于过滤旋转矩形RotatedRect。它从指定的前一阶段获取矩形列表然后根据用户设置的宽度、长度和宽高比长边/短边限制筛选出满足条件的矩形。参数参数名类型默认值描述widthMaxdouble50.0过滤矩形的最大宽度长边。若为 0 且lengthMax非 0则根据宽高比推导。widthMindouble25.0过滤矩形的最小宽度长边。lengthMaxdouble50.0过滤矩形的最大长度短边。若为 0 且widthMax非 0则根据宽高比推导。lengthMindouble25.0过滤矩形的最小长度短边。aspectRatioMaxdouble0.0最大宽高比长边/短边。当宽度或长度限制为 0 时用于推导另一维。aspectRatioMindouble0.0最小宽高比。enableLoggingbooleanfalse是否启用详细日志输出每个矩形的尺寸、宽高比及判定结果。rotatedRectsStageNameStringnull前一阶段的名称该阶段必须输出RotatedRect或ListRotatedRect。例子生成测试图片importcv2importnumpy as np def generate_test_image(output_pathfilter_test.png,size(800,600)): 生成带有旋转矩形的测试图像。 矩形参数:(中心x, 中心y, 宽度, 高度, 角度°) imgnp.zeros((size[1], size[0]),dtypenp.uint8)# 黑色背景# 定义矩形: (center_x, center_y, width, height, angle_deg)rects[(200,150,80,40,30),# 宽80高40角度30°(400,150,60,60,0),# 正方形角度0°(600,150,100,30,45),# 宽100高30角度45°(200,400,50,120,15),# 宽50高120角度15°(400,400,70,70,60),# 正方形角度60°(600,400,40,90, -20),# 宽40高90角度-20°(100,100,30,30,10),# 小正方形角度10°(700,500,200,50,80),# 大扁矩形角度80°]for(cx, cy, w, h, angle_deg)inrects: rect((cx, cy),(w, h), angle_deg)boxcv2.boxPoints(rect)boxnp.int32(box)# 修正使用 np.int32 替代 np.int0cv2.fillPoly(img,[box],255)cv2.imwrite(output_path, img)print(f测试图片已生成: {output_path})if__name____main__:generate_test_image()cv-pipeline configcv-pipelinestagescv-stageclassorg.openpnp.vision.pipeline.stages.ImageReadnamereadenabledtruefileD:\3rd\openpnp_prj\openpnp-official\openpnp-test-images\my_test\filter_test.pngcolor-spaceBgrhandle-as-capturedfalse/cv-stageclassorg.openpnp.vision.pipeline.stages.ConvertColornamegrayenabledtrueconversionBgr2Gray/cv-stageclassorg.openpnp.vision.pipeline.stages.Thresholdnamethreshenabledtruethreshold127autofalseinvertfalse/cv-stageclassorg.openpnp.vision.pipeline.stages.FindContoursnamecontoursenabledtrueretrieval-modeExternalapproximation-methodSimple/cv-stageclassorg.openpnp.vision.pipeline.stages.MinAreaRectContoursnametoRectsenabledtruecontours-stage-namecontours/cv-stageclassorg.openpnp.vision.pipeline.stages.FilterRectsnamefilterRectsenabledtruewidth-max200.0width-min10.0length-max3000.0length-min1.0aspect-ratio-max0.0aspect-ratio-min0.0enable-loggingtruerotated-rects-stage-nametoRects/cv-stageclassorg.openpnp.vision.pipeline.stages.ImageRecallnamerecallenabledtrueimage-stage-nameread/cv-stageclassorg.openpnp.vision.pipeline.stages.DrawRotatedRectsnamedrawFilteredenabledtruerotated-rects-stage-namefilterRectsthickness2draw-rect-centerfalserect-center-radius20show-orientationfalsecolorr0g255b0a255//cv-stagecv-stageclassorg.openpnp.vision.pipeline.stages.ImageWritenamesaveenabledtruefileoutput_filtered_rects.png//stages/cv-pipeline效果右下角的矩形出了图片范围没有圈住是正常的。更换测试图片importcv2importnumpy as np def generate_circles_test_image(output_pathcircles_test.png,size(800,600)): imgnp.zeros((size[1], size[0]),dtypenp.uint8)circles[(200,150,40),(400,150,30),(600,150,20),(200,400,50),(400,400,35),(600,400,25),(100,100,15),(700,500,60),]for(cx, cy, radius)incircles: cv2.circle(img,(cx, cy), radius,255, -1)cv2.imwrite(output_path, img)print(f圆形测试图片已生成: {output_path})if__name____main__:generate_circles_test_image()效果虽然是圆形但是也是找最小外接矩形所以也能圈出来。但是因为从圆形上找最小外接矩形没有角度值所以找出的最小外接矩形的角度就是随机的。圆形在数学上虽然是完美的旋转对称图形但实际图像中的圆形是由离散像素组成的轮廓点并非完美圆。MinAreaRectContours 会基于这些轮廓点计算最小外接矩形对于圆形轮廓最小外接矩形实际上是一个正方形边长等于直径。而正方形的旋转角度在算法上是不确定的——任何角度下正方形的外接矩形面积都相同但 OpenCV 的 minAreaRect 函数会根据轮廓点的分布或数值计算的稳定性返回一个角度例如 45°、63.4° 等END

更多文章