org.openpnp.vision.pipeline.stages.MinAreaRect
文章目录
- org.openpnp.vision.pipeline.stages.MinAreaRect
- 功能
- 参数
- 例子
- 产生测试图片
- cv-pipeline config
- 效果
- 查看图片中灰度值的方法
- END
org.openpnp.vision.pipeline.stages.MinAreaRect
功能
在灰度图像中,提取灰度值在指定范围内的像素,然后拟合出包围这些像素的最小外接旋转矩形,并支持仅使用部分边缘(如只检测左边和上边)以及角度约束。
MinAreaRect要求灰度图的前景和背景值有差值,设置thresholdMin~thresholdMax的值在灰度图前景色的范围内,就可以忽略掉背景,只将前景的灰度图像中找到最小外接矩形。
如果hresholdMin~thresholdMax设置的不合适,MinAreaRect就会找错最小外接矩形。
参数
| 参数名 | 类型 | 描述 | 默认值 |
|---|---|---|---|
thresholdMin | int | 被视为“置位”像素的灰度最小值 | 无默认值(必须设置) |
thresholdMax | int | 被视为“置位”像素的灰度最大值 | 无默认值(必须设置) |
expectedAngle | double | 待检测矩形外接矩形的预期角度(度) | 0 |
searchAngle | double | 在预期角度两侧的搜索范围(度) | 45 |
leftEdge | boolean | 是否检测矩形的左边缘(按预期角度旋转后) | true |
rightEdge | boolean | 是否检测矩形的右边缘(按预期角度旋转后) | true |
topEdge | boolean | 是否检测矩形的顶部边缘(按预期角度旋转后) | true |
bottomEdge | boolean | 是否检测矩形的底部边缘(按预期角度旋转后) | true |
diagnostics | boolean | 是否显示检测结果的诊断信息(在图像上绘制) | false |
propertyName | String | 用于运行时覆盖属性的管道属性名前缀 | "MinAreaRect" |
例子
产生测试图片
importcv2importnumpy as np def generate_test_image():# 创建彩色渐变背景 (640x640),避免黑色img=np.zeros((640,640,3),dtype=np.uint8)for i in range(640):# B通道从50线性增加到200b=50+int(i*150/640)# G通道固定80g=80# R通道固定100r=100img[i,:,0]=b img[i,:,1]=g img[i,:,2]=r # 旋转矩形的参数 center=(320,320)size=(200,100)# 宽200,高100angle=35.0# 旋转角度 # 获取旋转矩形的四个顶点 rect=((center[0],center[1]),size,angle)box=cv2.boxPoints(rect)box=np.int32(box)# 在图像上绘制橙红色填充矩形(BGR:0,100,255)cv2.fillPoly(img,[box],(0,100,255))# 可选:添加轻微噪声(模拟真实图像)noise=np.random.randint(0,10, img.shape,dtype=np.uint8)img=cv2.add(img, noise)cv2.imwrite("test_minarearect_color.png", img)print("生成测试图片: test_minarearect_color.png (蓝色渐变背景 + 中心旋转35°的橙红色矩形)")if__name__=="__main__":generate_test_image()cv-pipeline config
<cv-pipeline><stages><cv-stageclass="org.openpnp.vision.pipeline.stages.ImageRead"name="readImage"enabled="true"file="D:\3rd\openpnp_prj\openpnp-official\openpnp-test-images\my_test\test_minarearect_color.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.BlurGaussian"name="blur"enabled="true"kernel-size="3"property-name="BlurGaussian"/><cv-stageclass="org.openpnp.vision.pipeline.stages.MinAreaRect"name="detectRect"enabled="true"threshold-min="110"threshold-max="255"expected-angle="35.0"search-angle="20.0"left-edge="true"right-edge="true"top-edge="true"bottom-edge="true"diagnostics="false"property-name="MinAreaRect"/><cv-stageclass="org.openpnp.vision.pipeline.stages.ImageRecall"name="recallOriginal"enabled="true"image-stage-name="readImage"/><cv-stageclass="org.openpnp.vision.pipeline.stages.DrawRotatedRects"name="drawRect"enabled="true"rotated-rects-stage-name="detectRect"thickness="3"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="saveOutput"enabled="true"file="D:\3rd\openpnp_prj\openpnp-official\openpnp-test-images\my_test\output_minarearect_result.png"/></stages></cv-pipeline>效果
查看图片中灰度值的方法
在MinAreaRect阶段,将鼠标移动进前景图形和背景图形,就能看到灰度值。
从上图中可以看出,背景色的灰度值为99,前景色的灰度值为138.
所以 stages.MinAreaRect 阶段的threshold-min ~ hreshold-max值范围只要>99 && < 138就可以。
我只修改了 threshold-min = 110,就检测ok了。
