org.openpnp.vision.pipeline.stages.DrawRotatedRects

张开发
2026/6/27 7:08:07 15 分钟阅读
org.openpnp.vision.pipeline.stages.DrawRotatedRects
文章目录org.openpnp.vision.pipeline.stages.DrawRotatedRects功能参数例子1生成测试图片cv-pipeline config效果备注例子2产生测试图片效果ENDorg.openpnp.vision.pipeline.stages.DrawRotatedRects功能在图像上绘制旋转矩形RotatedRect以便可视化形状检测或轮廓拟合的结果.参数参数类型必需说明rotatedRectsStageNameString是指定流水线中提供旋转矩形的那个阶段的名称。如果为空则抛出异常。colorjava.awt.Color否绘制矩形所用的颜色。如果不设置会为每个矩形自动分配一个索引色循环使用。thicknessint是矩形轮廓的线宽像素。drawRectCenterboolean否是否在矩形中心绘制一个圆点。默认为false。rectCenterRadiusint否中心圆点的半径像素。默认为20。仅在drawRectCentertrue时有效。showOrientationboolean否是否绘制方向标记沿主轴的一条线。默认为false。例子1生成测试图片importcv2importnumpy as np# 创建白色背景 (600x600)width, height600,600imgnp.ones((height,width,3),dtypenp.uint8)*255# 定义多个旋转矩形:(中心x,中心y,宽度,高度,角度)rects[(150,150,100,60,30),#30度(450,150,120,80,-45),#-45度(150,450,140,70,15),(450,450,90,90,0),# 正方形0度(300,300,200,100,60)] # 绘制填充的旋转矩形黑色 for x,y,w,h,angle in rects:# 构建旋转矩形 rect((x,y),(w,h),angle)# 获取矩形四个顶点 boxcv2.boxPoints(rect)boxnp.int32(box)# 使用 np.int32 替代 np.int0 cv2.fillPoly(img,[box],(0,0,0))# 添加少许噪声使图像更自然noisenp.random.randint(0,30,(height, width,3),dtypenp.uint8)imgcv2.addWeighted(img,0.95, noise,0.05,0)# 保存图片cv2.imwrite(rotated_rects_test.png, img)print(测试图片已生成: rotated_rects_test.png)cv-pipeline configcv-pipelinestagescv-stageclassorg.openpnp.vision.pipeline.stages.ImageReadnamereadenabledtruefileD:\3rd\openpnp_prj\openpnp-official\openpnp-test-images\my_test\rotated_rects_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.FilterContoursnamefilterenabledtruecontours-stage-namecontoursmin-area500.0max-area-1.0property-nameFilterContours/cv-stageclassorg.openpnp.vision.pipeline.stages.MinAreaRectContoursnameminAreaRectenabledtruecontours-stage-namefilter/cv-stageclassorg.openpnp.vision.pipeline.stages.ImageRecallnamerecallenabledtrueimage-stage-nameread/cv-stageclassorg.openpnp.vision.pipeline.stages.DrawRotatedRectsnamedrawenabledtruerotated-rects-stage-nameminAreaRectthickness2draw-rect-centertruerect-center-radius20show-orientationtruecolorr0g255b0a255//cv-stagecv-stageclassorg.openpnp.vision.pipeline.stages.ImageWritenamesaveenabledtruefileoutput_rotated_rects.png//stages/cv-pipeline效果备注DrawRotatedRects 依赖前置stage找到的Contours数据来画旋转矩形。本身并不能指定角度来画旋转矩形。例子2画出的旋转矩形都是外接矩形。如果轮廓数据不是矩形会找到外接矩形和旋转角度将矩形画出来。换一张测试图片管道数据不变看一下效果。产生测试图片importcv2importnumpy as np def rotate_points(points, center, angle_deg):绕 center 旋转 angle_deg 度返回 int32 点集 radnp.radians(angle_deg)c, snp.cos(rad), np.sin(rad)rot_matnp.array([[c, -s],[s, c]])ptsnp.array(points)- center ptsnp.dot(pts, rot_mat.T)return(pts center).astype(np.int32)# 创建白色背景 (800x600)width, height800,600imgnp.ones((height,width,3),dtypenp.uint8)*255#1.矩形旋转矩形 rect((150,150),(120,80),30)boxcv2.boxPoints(rect)boxnp.int32(box)cv2.fillPoly(img,[box],(0,0,0))# 2. 圆形circle_center(600,150)circle_radius55cv2.circle(img, circle_center, circle_radius,(0,0,0), -1)# 3. 三角形等边三角形旋转30度triangle_center(150,400)side120h_triint(side * np.sqrt(3)/2)p1(triangle_center[0], triangle_center[1]- h_tri//2)p2(triangle_center[0]- side//2, triangle_center[1] h_tri//2)p3(triangle_center[0] side//2, triangle_center[1] h_tri//2)tri_ptsnp.array([p1, p2, p3], np.int32)tri_ptsrotate_points(tri_pts, triangle_center,30)cv2.fillPoly(img,[tri_pts],(0,0,0))# 4. 椭圆形已带角度20度ellipse_center(600,420)ellipse_axes(100,60)ellipse_angle20cv2.ellipse(img, ellipse_center, ellipse_axes, ellipse_angle,0,360,(0,0,0), -1)# 5. 五角星原始尖角向上整体旋转20度star_center(400,280)outer_r70inner_r30star_pts[]foriinrange(10): anglei *36-90# 起始-90°让一个尖角向上router_rifi %20elseinner_r xstar_center[0] r * np.cos(np.radians(angle))ystar_center[1] r * np.sin(np.radians(angle))star_pts.append((int(x),int(y)))star_ptsnp.array(star_pts, np.int32)star_ptsrotate_points(star_pts, star_center,20)cv2.fillPoly(img,[star_pts],(0,0,0))# 添加轻微噪声noisenp.random.randint(0,30,(height, width,3),dtypenp.uint8)imgcv2.addWeighted(img,0.95, noise,0.05,0)cv2.imwrite(shapes_rotated.png, img)print(测试图片已生成: shapes_rotated.png (三角形旋转30°五角星旋转20°))效果END

更多文章