因为我懒,经常写了一些可视化的函数,但是又找不到了,每次都在重写整得我花费了很多不必要的时间,所以在这里记录一下。
功能希望包含,框
文章目录
给了image_path和bboxes
def show(path,b,flip=None,size=None,typ='xyxy'):
'''
b must be of size (n,4) while have type xyxy,xywh,cxcywh
path为图片路径
flip为0,1,-1代表图片的反转,None为不翻转,0垂直1水平-1对角
size为resize操作'''
img=cv.imread(path)
if size is not None:
img=cv.resize(img,size)
if flip is not None:
cv.flip(img,flip,img)
for i in b:
c=[int(j) for j in i]
if typ=='xyxy':
cv.rectangle(img,(c[0],c[1]),(c[2],c[3]),(0,255,0),2)
elif typ=='xywh':
cv.rectangle(img,(c[0],c[1]),(c[0]+c[2],c[1]+c[3]),(0,255,0),2)
elif typ=='cxcywh':
c[3]=int(c[3]/2);c[2]=int(c[2]/2)
cv.rectangle(img,(c[0]-c[2],c[1]-c[3]),(c[0]+c[2],c[1]+c[3]),(0,255,0),2)
else:
raise RuntimeError(f'Annotation type should be xyxy,xywh,cxcywh ,not {typ}')
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
plt.figure(figsize=(10.24,10.24),dpi=50, frameon=False)
plt.subplots_adjust(top=1, bottom=0, right=1, left=0)
ax=plt.gca()
ax.axis('off')
plt.imshow(img)
给了dota数据集,的img_path和anno_path
dota_ann='../data/DOTA1-split-1024/trainval1024/annfiles'
dota_img='../data/DOTA1-split-1024/trainval1024/images'
# ann_list=os.listdir(dota_ann)
img_p=dota_img+'/P0010__1__0___524.png'
ann_p=dota_ann+'/'+'P0010__1__0___524.txt'
with open(ann_p,'r')as f:
ann=f.readlines()
pts=[]
dif=[]
for i in ann:
dif.append(int(i[-2]))
cood=i.split(' ')[:8]
cood=list(map(float,cood))
cood=t.tensor(cood,dtype=t.int16)
cood=cood.view(-1,2)
cood=np.array(cood.numpy(),np.int32)
pts.append(cood)
#
img=cv.imread(img_p)
#实现旋转0垂直1水平-1对角
if pts is not None:
for i,j in zip(pts,dif):
points = i.reshape((-1,1,2))
# print(points)
if j==1:
cv.polylines(img,[points],True,(0,255,255),2)
if j==0:
cv.polylines(img,[points],True,(0,255,0),2)
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
plt.figure(figsize=(10.24,10.24),dpi=50, frameon=False)
plt.subplots_adjust(top=1, bottom=0, right=1, left=0)
ax=plt.gca()
ax.axis('off')
plt.imshow(img)
给了cx cy w h a的框标注和图片路径
#只是一个可视化,好多人性化的注释都没加,在一看一眼已经看不明白了,吗蛋
def get_rot_box_point_single(box):
'''
box have type cx cy w h a'''
th=box[-1]; o=box[0:2]; w=box[2]; h=box[3]
rotate=t.tensor([[math.cos(th),math.sin(th)],[-math.sin(th),math.cos(th)]])
#左上开始顺时针
p_box=t.tensor([[[-w/2,h/2]],[[-w/2,-h/2]],[[w/2,-h/2]],[[w/2,h/2]]])
result=[]
for i in p_box:
result.append(t.matmul(i,rotate)+o.clone().detach())
return t.cat(result,0)
def get_rot_box_point(boxes):
'''
box.shape=n*5
'''
return list(map(get_rot_box_point_single,boxes))
def show_boxes(path,points=None,flip=None):
#如果没有pts,直接展示原图
img=cv.imread(path)
#实现旋转0垂直1水平-1对角
if flip is not None:
cv.flip(img,flip,img)
if points is not None:
for i in points:
pts = np.array(i.numpy(), np.int32)
pts = pts.reshape((-1,1,2))
cv.polylines(img,[pts],True,(0,255,255),2)
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
plt.figure(figsize=(10.24,10.24),dpi=50, frameon=False)
plt.subplots_adjust(top=1, bottom=0, right=1, left=0)
ax=plt.gca()
ax.axis('off')
plt.imshow(img)
return 1
p='/home/cgm/disk1T/dataset/DOTA1-split-1024/trainval1024/images/'
path_pic=p+'P2755__1__908___2620.png'
gt_bboxes=t.tensor([
[211,975,101,63,1.52],
[210,284,106,36,-1.57]])
rot_ps=get_rot_box_point(gt_bboxes)
show_boxes(path_pic,rot_ps)
例子:下面是我常喜欢用的一张dota的图片,以及可视化它的代码
with open(osp.join(data_root,'train.json'),'r')as f:
file=json.load(f)
# 利用show,多展示一些图片
# P0010__1__0___524.png
for i in file['images']:
if i['file_name'].endswith('P0010__1__0___524.png'):
print(i['id'])
b=[]
for j in file['annotations']:
if j['image_id']==16274:
b.append(j['bbox'])
img_path=osp.join(data_root,img_root,'P0010__1__0___524.png')
show(img_path,b,typ='xywh')