org.openpnp.vision.pipeline.stages.MinAreaRect
文章目录org.openpnp.vision.pipeline.stages.MinAreaRect功能参数例子产生测试图片cv-pipeline config效果查看图片中灰度值的方法ENDorg.openpnp.vision.pipeline.stages.MinAreaRect功能在灰度图像中提取灰度值在指定范围内的像素然后拟合出包围这些像素的最小外接旋转矩形并支持仅使用部分边缘如只检测左边和上边以及角度约束。MinAreaRect要求灰度图的前景和背景值有差值设置thresholdMin~thresholdMax的值在灰度图前景色的范围内就可以忽略掉背景只将前景的灰度图像中找到最小外接矩形。如果hresholdMin~thresholdMax设置的不合适MinAreaRect就会找错最小外接矩形。参数参数名类型描述默认值thresholdMinint被视为“置位”像素的灰度最小值无默认值必须设置thresholdMaxint被视为“置位”像素的灰度最大值无默认值必须设置expectedAngledouble待检测矩形外接矩形的预期角度度0searchAngledouble在预期角度两侧的搜索范围度45leftEdgeboolean是否检测矩形的左边缘按预期角度旋转后truerightEdgeboolean是否检测矩形的右边缘按预期角度旋转后truetopEdgeboolean是否检测矩形的顶部边缘按预期角度旋转后truebottomEdgeboolean是否检测矩形的底部边缘按预期角度旋转后truediagnosticsboolean是否显示检测结果的诊断信息在图像上绘制falsepropertyNameString用于运行时覆盖属性的管道属性名前缀MinAreaRect例子产生测试图片importcv2importnumpy as np def generate_test_image():# 创建彩色渐变背景 (640x640)避免黑色imgnp.zeros((640,640,3),dtypenp.uint8)for i in range(640):# B通道从50线性增加到200b50int(i*150/640)# G通道固定80g80# R通道固定100r100img[i,:,0]b img[i,:,1]g img[i,:,2]r # 旋转矩形的参数 center(320,320)size(200,100)# 宽200高100angle35.0# 旋转角度 # 获取旋转矩形的四个顶点 rect((center[0],center[1]),size,angle)boxcv2.boxPoints(rect)boxnp.int32(box)# 在图像上绘制橙红色填充矩形(BGR:0,100,255)cv2.fillPoly(img,[box],(0,100,255))# 可选添加轻微噪声模拟真实图像noisenp.random.randint(0,10, img.shape,dtypenp.uint8)imgcv2.add(img, noise)cv2.imwrite(test_minarearect_color.png, img)print(生成测试图片: test_minarearect_color.png (蓝色渐变背景 中心旋转35°的橙红色矩形))if__name____main__:generate_test_image()cv-pipeline configcv-pipelinestagescv-stageclassorg.openpnp.vision.pipeline.stages.ImageReadnamereadImageenabledtruefileD:\3rd\openpnp_prj\openpnp-official\openpnp-test-images\my_test\test_minarearect_color.pngcolor-spaceBgrhandle-as-capturedfalse/cv-stageclassorg.openpnp.vision.pipeline.stages.ConvertColornametoGrayenabledtrueconversionBgr2Gray/cv-stageclassorg.openpnp.vision.pipeline.stages.BlurGaussiannameblurenabledtruekernel-size3property-nameBlurGaussian/cv-stageclassorg.openpnp.vision.pipeline.stages.MinAreaRectnamedetectRectenabledtruethreshold-min110threshold-max255expected-angle35.0search-angle20.0left-edgetrueright-edgetruetop-edgetruebottom-edgetruediagnosticsfalseproperty-nameMinAreaRect/cv-stageclassorg.openpnp.vision.pipeline.stages.ImageRecallnamerecallOriginalenabledtrueimage-stage-namereadImage/cv-stageclassorg.openpnp.vision.pipeline.stages.DrawRotatedRectsnamedrawRectenabledtruerotated-rects-stage-namedetectRectthickness3draw-rect-centerfalserect-center-radius20show-orientationfalsecolorr0g255b0a255//cv-stagecv-stageclassorg.openpnp.vision.pipeline.stages.ImageWritenamesaveOutputenabledtruefileD:\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了。END