org.openpnp.vision.pipeline.stages.SizeCheck
文章目录
- org.openpnp.vision.pipeline.stages.SizeCheck
- 功能
- 参数
- 例子
- 产生测试图像
- cv-pipeline config
- 效果
- END
org.openpnp.vision.pipeline.stages.SizeCheck
功能
用于检查检测到的旋转矩形(RotatedRect)的宽度和高度是否在允许的容差范围内。它通常放在返回 RotatedRect 的 Stage 之后(如 MinAreaRect),用于过滤不符合尺寸要求的检测结果。
尺寸比较:将输入矩形的宽度和高度(不考虑方向,取最大值和最小值)与预设的 sizeW、sizeH 进行比较。
容差判断:允许的绝对误差由 tolerance 参数指定。
输出控制:如果尺寸符合要求(两个维度分别都在容差内),则输出原模型;否则返回 null,表示该阶段无有效结果。
参数
| 参数名 | 数据类型 | 默认值 | 功能描述 |
|---|---|---|---|
tolerance | int | 5 | 允许的像素容差(绝对值)。实际尺寸与期望尺寸之差 ≤ 此值时视为有效。 |
sizeW | int | 7 | 期望的宽度(像素)。内部会与矩形尺寸的最大值/最小值进行比较。 |
sizeH | int | 28 | 期望的高度(像素)。内部会与矩形尺寸的最大值/最小值进行比较。 |
例子
产生测试图像
importcv2importnumpy as np def generate_color_rects_image(output_path):"""生成彩色背景图像,包含多个不同尺寸、颜色、角度的旋转矩形(无网格线)""" width, height=800,600img=np.full((height, width,3),(240,240,240),dtype=np.uint8)# 浅灰色背景# 定义矩形列表:(中心x,中心y, 宽w,高h, 角度, 颜色BGR, 标签)rects=[(200,150,80,50,30,(0,0,255),"R1"),# 红色,80x50(600,150,60,40, -20,(0,255,0),"R2"),# 绿色,60x40(200,400,100,70,15,(255,0,0),"R3"),# 蓝色,100x70(600,400,90,60, -45,(0,255,255),"R4"),# 黄色,90x60(400,300,120,80,0,(255,255,0),"R5"),# 青色,120x80(太大)(150,80,30,20,60,(255,0,255),"R6"),# 紫色,30x20(太小)]for(cx, cy, w, h, angle, color, label)inrects: rect=((cx, cy),(w, h), angle)box=cv2.boxPoints(rect)box=np.array(box,dtype=np.int32)overlay=img.copy()cv2.fillPoly(overlay,[box], color)cv2.addWeighted(overlay,0.6, img,0.4,0, img)cv2.drawContours(img,[box],0,(0,0,0),2)cv2.putText(img, label,(cx-20, cy-10), cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,0),1)print(f"{label}: 宽={w}, 高={h}, 角度={angle}")cv2.imwrite(output_path, img)print(f"已生成彩色测试图像: {output_path}")if__name__=="__main__":generate_color_rects_image("color_rects_test.png")cv-pipeline config
<cv-pipeline><stages><cv-stageclass="org.openpnp.vision.pipeline.stages.ImageRead"name="loadImage"enabled="true"file="D:\3rd\openpnp_prj\openpnp-official\openpnp-test-images\my_test\color_rects_test.png"color-space="Bgr"handle-as-captured="false"/><cv-stageclass="org.openpnp.vision.pipeline.stages.ConvertColor"name="toGray"enabled="true"conversion="Bgr2Gray"/><cv-stageclass="org.openpnp.vision.pipeline.stages.BlurMedian"name="dilate"enabled="true"kernel-size="5"/><cv-stageclass="org.openpnp.vision.pipeline.stages.DetectEdgesCanny"name="edges"enabled="true"threshold-1="50.0"threshold-2="150.0"/><cv-stageclass="org.openpnp.vision.pipeline.stages.FindContours"name="findContours"enabled="true"retrieval-mode="External"approximation-method="Simple"/><cv-stageclass="org.openpnp.vision.pipeline.stages.MinAreaRectContours"name="minAreaRects"enabled="true"contours-stage-name="findContours"/><cv-stageclass="org.openpnp.vision.pipeline.stages.SelectSingleRect"name="selectRect"enabled="true"position="0"rotated-rects-stage-name="minAreaRects"/><cv-stageclass="org.openpnp.vision.pipeline.stages.SizeCheck"name="sizeCheck"enabled="true"tolerance="5"size-w="100"size-h="70"/><cv-stageclass="org.openpnp.vision.pipeline.stages.ImageRecall"name="recallOriginal"enabled="true"image-stage-name="loadImage"/><cv-stageclass="org.openpnp.vision.pipeline.stages.DrawRotatedRects"name="drawValid"enabled="true"rotated-rects-stage-name="sizeCheck"thickness="10"draw-rect-center="false"rect-center-radius="20"show-orientation="false"><colorr="0"g="255"b="0"a="255"/></cv-stage><cv-stageclass="org.openpnp.vision.pipeline.stages.ImageWrite"name="saveResult"enabled="true"file="D:\3rd\openpnp_prj\openpnp-official\openpnp-test-images\my_test\sizecheck_result.png"/></stages></cv-pipeline>