MSRA-TD500数据集坐标转换

1.根据MSRA-TD500原旋转坐标,转换成多边形坐标

import os
from math import *
import math



def rotate(angle, x, y):
    """
    基于原点的弧度旋转

    :param angle:   弧度
    :param x:       x
    :param y:       y
    :return:
    """
    rotatex = math.cos(angle) * x - math.sin(angle) * y
    rotatey = math.cos(angle) * y + math.sin(angle) * x
    return rotatex, rotatey

def xy_rorate(theta, x, y, centerx, centery):
    """
    针对中心点进行旋转

    :param theta:
    :param x:
    :param y:
    :param centerx:
    :param centery:
    :return:
    """
    r_x, r_y = rotate(theta, x - centerx, y - centery)
    return centerx+r_x, centery+r_y

def rec_rotate(x, y, width, height, theta):
    """
    传入矩形的x,y和宽度高度,弧度,转成QUAD格式
    :param x:
    :param y:
    :param width:
    :param height:
    :param theta:
    :return:
    """
    centerx = x + width / 2
    centery = y + height / 2

    x1, y1 = xy_rorate(theta, x, y, centerx, centery)
    x2, y2 = xy_rorate(theta, x+width, y, centerx, centery)
    x3, y3 = xy_rorate(theta, x, y+height, centerx, centery)
    x4, y4 = xy_rorate(theta, x+width, y+height, centerx, centery)

    return x1, y1,  x3, y3, x4, y4,x2, y2


fname='IMG_0692.gt'
f=open(fname,'r')
savestr=''
for line in f:
    line=line.strip()
    line=line.split(' ')
    line=list(map(float,line))
    x,y=line[2],line[3]
    w,h=line[4],line[5]
    # centralx=x+w/2
    # centraly = y + h / 2
    points=[x,y,x,y+h,x+w,y+h,x+w,y]
    pointsrotate=rec_rotate(x,y,w,h,line[-1])
    savestr=savestr+str(int(pointsrotate[0]))+','+str(int(pointsrotate[1]))+','+str(int(pointsrotate[2]))+','+str(int(pointsrotate[3]))+','+str(int(pointsrotate[4]))+','+str(int(pointsrotate[5]))+','+str(int(pointsrotate[6]))+','+str(int(pointsrotate[7]))+','+'text\n'
savename=fname[0:-2]+'txt'
savef=open(savename,'w')
savef.write(savestr)
savef.close()

2.根据保存的txt坐标(逆时针8坐标)在原图上绘制多边形

from PIL import Image, ImageDraw

im = Image.open("IMG_0692.jpg")
draw = ImageDraw.Draw(im)
fname='IMG_0692.txt'
f=open(fname,'r')
savestr=''
for line in f:
    line=line.strip()
    line=line.split(',')
    line=list(map(float,line[0:-1]))
    print(line)
    draw.polygon(line, outline='red')
im.show('t')

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AICVer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值