当前位置:   article > 正文

labelme 标注数据生成最小外接矩形的旋转角度_labelme 矩形 旋转

labelme 矩形 旋转

labelme 标注数据生成最小外接矩形的旋转角度

import json
import math
import cv2
import numpy as np
import math
j_file= '0011_rgb.json'
p_file='0011_rgb.jpg'
with open(j_file, "r") as j_f:
    data=json.load(j_f)
img=cv2.imread(p_file)
contours=[]
rects=[]
font = cv2.FONT_HERSHEY_SIMPLEX
for shape in data['shapes']:
    points=shape['points']
    # points=[[x] for x in points ]
    contour=np.array(points,np.int32)
    contours.append(contour)
    rect = cv2.minAreaRect(contour)
    rects.append(rect)

for rect in rects:
    x, y = rect[0]
    width,height=rect[1]
    angle=rect[2]
    if width>height:
        l=(width+100)/2
        ptStart = (int(x-l*math.cos(math.pi/(180)*angle)), int(y-l*math.sin(math.pi/(180)*angle)))
        ptEnd = (int(x+l*math.cos(math.pi/(180)*angle)), int(y+l*math.sin(math.pi/(180)*angle)))
        point_color = (0, 255, 0)  # BGR
        thickness = 1
        lineType = 4
        cv2.line(img, ptStart, ptEnd, point_color, thickness, lineType)
        cv2.putText(img, str(int(-angle)), (int(x), int(y)), font, 1.2, (255, 255, 255), 2)
    else:
        l=(height+100)/2
        ptStart = (int(x-l*math.cos(math.pi/(180)*(90+angle))), int(y-l*math.sin(math.pi/(180)*(90+angle))))
        ptEnd = (int(x+l*math.cos(math.pi/(180)*(90+angle))), int(y+l*math.sin(math.pi/(180)*(90+angle))))
        point_color = (0, 255, 0)  # BGR
        thickness = 1
        lineType = 4
        cv2.line(img, ptStart, ptEnd, point_color, thickness, lineType)
        cv2.putText(img, str(int(90-angle)), (int(x), int(y)), font, 1.2, (255, 255, 255), 2)
    cv2.circle(img, (int(x), int(y)), 3, (0, 255, 0), 5)
    print(width,height)
cv2.drawContours(img,contours,-1,(0,255,0),1)
rect = cv2.minAreaRect(contours[0])
cv2.imshow("contours",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/722340
推荐阅读
相关标签
  

闽ICP备14008679号