关于DOTA斜框数据格式数据处理(数据格式转换、还原框)

该代码示例展示了如何将Dota数据格式的五个值(中心点坐标,宽,高,角度)转换为表示旋转框的四个点的八个值。通过rotatePoint函数进行坐标旋转,然后在图像上绘制旋转框。最后,使用OpenCV显示带有绘制旋转框的图像。
摘要由CSDN通过智能技术生成

再做斜框检测时遇到数据转换问题,需要把dota数据五个值转换为4个点8个值的数据格式,以下为编写的demo代码

import os,json,math,cv2
import numpy as np

dir = r'C:\Users\Sunyj\Desktop\P0041.png'

# 读取图像文件
image = cv2.imread(dir)

def rotatePoint(xc, yc, xp, yp, theta):
    xoff = xp - xc
    yoff = yp - yc

    cosTheta = math.cos(theta)
    sinTheta = math.sin(theta)
    pResx = cosTheta * xoff + sinTheta * yoff
    pResy = - sinTheta * xoff + cosTheta * yoff
    return str(int(xc + pResx)), str(int(yc + pResy))

mdict = {}
score = 0.5

outputs = mdict['output']


res_dict1 = {}
mlsit = []
for output in outputs:

    for img_name,img_bboxs in output.items():
        res_dict = {}
        res_list1 = []

        # print(img_name)
        for img_label,img_bbox_lists in img_bboxs.items():
            for img_bbox_list in img_bbox_lists:
                res_list = []

                if img_bbox_list[-1] >= score:
                    # print(img_bbox_list)

                    cx = img_bbox_list[0]
                    cy = img_bbox_list[1]
                    w = img_bbox_list[2]
                    h = img_bbox_list[3]
                    angle = img_bbox_list[4]

                    # 先转换成4个点 做尺寸缩放 然后在转换成robndbox
                    x1text, y1text = rotatePoint(cx, cy, cx - w / 2, cy - h / 2, -angle)
                    x2text, y2text = rotatePoint(cx, cy, cx + w / 2, cy - h / 2, -angle)
                    x3text, y3text = rotatePoint(cx, cy, cx + w / 2, cy + h / 2, -angle)
                    x4text, y4text = rotatePoint(cx, cy, cx - w / 2, cy + h / 2, -angle)

                    x1 = float(x1text)
                    x2 = float(x2text)
                    x3 = float(x3text)
                    x4 = float(x4text)
                    y1 = float(y1text)
                    y2 = float(y2text)
                    y3 = float(y3text)
                    y4 = float(y4text)

                    res_list.append(img_label)
                    res_list.append(float(img_bbox_list[-1]))
                    res_list.append([float(x1),float(y1),float(x2),float(y2),float(x3),float(y3),float(x4),float(y4)])
                    res_list1.append(res_list)


                    # 定义斜框坐标
                    coords = [int(x1),int(y1),int(x2),int(y2),int(x3),int(y3),int(x4),int(y4),]

                    print(coords)
                    # 将坐标转换为 Numpy 数组
                    points = np.array(coords).reshape(4, 2)
                    print(points)
                    # 在图像上绘制斜框
                    cv2.drawContours(image, [points], -1, (0, 255, 0), 2)

        # 显示图像
        cv2.imshow('image', image)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
        exit()

        res_dict['image_path'] = img_name
        if len(res_list1) == 0:
            res_dict['boxes'] = []
        else:
            res_dict['boxes'] = res_list1
        mlsit.append(res_dict)

    res_dict1['outputs'] = mlsit
print(res_dict1)

单独详细解释:五个值转8个值

def rotatePoint(xc, yc, xp, yp, theta):
    xoff = xp - xc
    yoff = yp - yc

    cosTheta = math.cos(theta)
    sinTheta = math.sin(theta)
    pResx = cosTheta * xoff + sinTheta * yoff
    pResy = - sinTheta * xoff + cosTheta * yoff
    return str(int(xc + pResx)), str(int(yc + pResy))

cx = img_bbox_list[0]
cy = img_bbox_list[1]
w = img_bbox_list[2]
h = img_bbox_list[3]
angle = img_bbox_list[4]

# 先转换成4个点 做尺寸缩放 然后在转换成robndbox
x1text, y1text = rotatePoint(cx, cy, cx - w / 2, cy - h / 2, -angle)
x2text, y2text = rotatePoint(cx, cy, cx + w / 2, cy - h / 2, -angle)
x3text, y3text = rotatePoint(cx, cy, cx + w / 2, cy + h / 2, -angle)
x4text, y4text = rotatePoint(cx, cy, cx - w / 2, cy + h / 2, -angle)

x1 = float(x1text)
x2 = float(x2text)
x3 = float(x3text)
x4 = float(x4text)
y1 = float(y1text)
y2 = float(y2text)
y3 = float(y3text)
y4 = float(y4text)

将八个值还原到原图像上

dir = r'C:\Users\Sunyj\Desktop\P0041.png'

# 定义斜框坐标
coords = [int(x1),int(y1),int(x2),int(y2),int(x3),int(y3),int(x4),int(y4),]

print(coords)
# 将坐标转换为 Numpy 数组
points = np.array(coords).reshape(4, 2)
print(points)
# 在图像上绘制斜框
cv2.drawContours(image, [points], -1, (0, 255, 0), 2)

# 显示图像
cv2.imshow('image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值