再做斜框检测时遇到数据转换问题,需要把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()