本文主要是介绍MSRA-TD500数据集坐标转换,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.根据MSRA-TD500原旋转坐标,转换成多边形坐标
import os
from math import *
import mathdef rotate(angle, x, y):"""基于原点的弧度旋转:param angle: 弧度:param x: x:param y: y:return:"""rotatex = math.cos(angle) * x - math.sin(angle) * yrotatey = math.cos(angle) * y + math.sin(angle) * xreturn rotatex, rotateydef 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_ydef rec_rotate(x, y, width, height, theta):"""传入矩形的x,y和宽度高度,弧度,转成QUAD格式:param x::param y::param width::param height::param theta::return:"""centerx = x + width / 2centery = y + height / 2x1, 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, y2fname='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 / 2points=[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, ImageDrawim = 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')
这篇关于MSRA-TD500数据集坐标转换的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!