org.openpnp.vision.pipeline.stages.FilterContours
文章目录org.openpnp.vision.pipeline.stages.FilterContours功能参数例子生成测试图片cv-pipeline config效果ENDorg.openpnp.vision.pipeline.stages.FilterContours功能根据轮廓面积对轮廓进行过滤。它从前序阶段获取轮廓列表List计算每个轮廓的面积只保留面积在指定范围内的轮廓输出过滤后的轮廓列表。该阶段常用于去除噪声、小斑点或过大干扰区域只保留感兴趣的目标轮廓以便后续处理如拟合椭圆、旋转矩形等。参数参数类型必需说明contoursStageNameString否指定流水线中提供轮廓列表ListMatOfPoint的那个阶段的名称。如果为空或找不到对应结果则跳过处理。minAreadouble是轮廓的最小面积阈值。只有面积 ≥minArea的轮廓才被保留。设置为-1表示无下限。maxAreadouble是轮廓的最大面积阈值。只有面积 ≤maxArea的轮廓才被保留。设置为-1表示无上限。propertyNameString否用于动态覆盖minArea和maxArea的属性名前缀。格式propertyName.minArea、propertyName.maxArea。默认值为FilterContours。例子生成测试图片importcv2importnumpy as np# 创建白色背景 (600x600)imgnp.ones((600,600,3),dtypenp.uint8)*255# 绘制三个不同面积的黑色圆形实心# 小圆 (面积 ≈ π*20² ≈ 1256)cv2.circle(img,(150,150),20,(0,0,0), -1)# 中圆 (面积 ≈ π*40² ≈ 5026)cv2.circle(img,(300,300),40,(0,0,0), -1)# 大圆 (面积 ≈ π*70² ≈ 15394)cv2.circle(img,(500,500),70,(0,0,0), -1)# 添加一个极小的噪声点面积很小用于演示过滤cv2.circle(img,(100,500),3,(0,0,0), -1)# 保存图片cv2.imwrite(filter_test.png, img)print(测试图片已生成: filter_test.png (包含小圆、中圆、大圆和一个小噪点))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.Thresholdnamethreshenabledtruethreshold127autofalseinverttrue/cv-stageclassorg.openpnp.vision.pipeline.stages.FindContoursnamecontoursenabledtrueretrieval-modeExternalapproximation-methodSimple/cv-stageclassorg.openpnp.vision.pipeline.stages.ImageRecallnamerecallenabledtrueimage-stage-nameread/cv-stageclassorg.openpnp.vision.pipeline.stages.DrawContoursnamedrawenabledtruecontours-stage-namecontoursthickness2index-1colorr255g0b0a255//cv-stagecv-stageclassorg.openpnp.vision.pipeline.stages.ImageWritenamesaveenabledtruefileoutput_one_contour.png//stages/cv-pipeline效果index -1 圈住所有目标index 0 圈住第1个目标。index n - 1, 圈住最后一个目标index n, 报错END