文章目录
- org.openpnp.vision.pipeline.stages.MaskModel
- 功能
- 参数
- 例子
- 效果
- END
org.openpnp.vision.pipeline.stages.MaskModel
功能
用于使用前一阶段产生的模型(如旋转矩形、圆形或轮廓)作为掩码,对当前工作图像进行选择性保留或遮挡。它支持以下模型类型:
单个 RotatedRect(旋转矩形)
单个 Circle(圆形)
列表(List)中的多个 Circle、RotatedRect 或 MatOfPoint(轮廓)
相当于将原图中除了找到的东西之外的图像都掩码掉。
参数
| 参数名 | 类型 | 默认值 | 描述 |
|---|---|---|---|
color | Color | black | 用于填充非保留区域的颜色。当isMask = false时,掩码外部填充此色;当isMask = true时,掩码内部填充此色。 |
modelStageName | String | null | 提供模型数据的前一阶段名称。该阶段的输出必须是RotatedRect、Circle、List<Circle>、List<RotatedRect>或List<MatOfPoint>类型。 |
isMask | boolean | false | 决定掩码的行为。若为false,保留模型区域(掩码内),外部填充color;若为true,保留模型外部(掩码外),内部填充color。 |
例子
importcv2importnumpy as np def generate_colorful_objects(output_path="colorful_objects.png",size=(640,480)):# 纯红色背景 (BGR: 0,0,255)img=np.full((size[1],size[0],3),(0,0,255),dtype=np.uint8)# 黄色圆形(BGR:0,255,255)cv2.circle(img,(200,150),60,(0,255,255),-1)# 绿色矩形(BGR:0,255,0)cv2.rectangle(img,(100,300),(200,400),(0,255,0),-1)# 蓝色三角形(BGR:255,0,0)pts=np.array([[500,100],[600,200],[400,200]],np.int32)cv2.fillPoly(img,[pts],(255,0,0))cv2.imwrite(output_path, img)print(f"彩色测试图片已生成: {output_path}")if__name__=="__main__":generate_colorful_objects()<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\colorful_objects.png"color-space="Bgr"handle-as-captured="false"/><cv-stageclass="org.openpnp.vision.pipeline.stages.ConvertColor"name="bgr2hsv"enabled="true"conversion="Bgr2HsvFull"/><cv-stageclass="org.openpnp.vision.pipeline.stages.MaskHsv"name="extractYellow"enabled="true"auto="false"fraction-to-mask="0.0"hue-min="21"hue-max="64"saturation-min="50"saturation-max="255"value-min="50"value-max="255"soft-edge="0"soft-factor="1.0"invert="true"binary-mask="true"property-name="MaskHsv"/><cv-stageclass="org.openpnp.vision.pipeline.stages.FindContours"name="contours"enabled="true"retrieval-mode="External"approximation-method="Simple"/><cv-stageclass="org.openpnp.vision.pipeline.stages.ImageRecall"name="recallColor"enabled="true"image-stage-name="read"/><cv-stageclass="org.openpnp.vision.pipeline.stages.MaskModel"name="maskYellow"enabled="true"model-stage-name="contours"is-mask="false"><colorr="0"g="0"b="0"a="255"/></cv-stage><cv-stageclass="org.openpnp.vision.pipeline.stages.ImageWrite"name="save"enabled="true"file="output_yellow_only.png"/></stages></cv-pipeline>