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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值