%matplotlib inline
import torch
from d2l import torch as d2l
'''读取图片'''
image_path = '../data/images/cat_dog_new.jpg'
# 创建画板
figure = d2l.set_figsize()
image = d2l.plt.imread(image_path)
d2l.plt.imshow(image);
'''边界框'''
box = (60,50,460,510)
def bbox_to_rect(bbox, color):
# 将边界框(左上x,左上y,右下x,右下y)格式转换成matplotlib格式:
# ((左上x,左上y),宽,高)
return d2l.plt.Rectangle(
xy=(bbox[0], bbox[1]), width=bbox[2]-bbox[0], height=bbox[3]-bbox[1],
fill=False, edgecolor=color, linewidth=2)
# 返回一个画布,该画布上面有图画image
fig = d2l.plt.imshow(image)
# 在该画布上画一个矩形框
fig.axes.add_patch(bbox_to_rect(box,'red'));
原:(x1,y1,x2,y2)
w = x2-x1
h = y2-y1
中心坐标 = (x1+w/2,y1+h/2)
def box_corner_to_center(boxes):
# 因为以后boxes可能不止一个,所以 boxes[:,0]取出所有行的第一列
x1,y1,x2,y2 = boxes[:,0],boxes[:,1],boxes[:,2],boxes[:,3]
w = x2-x1
h = y2-y1
x_center,y_center = x1 + w/2, y1 + h/2
# axis=-1表示在最后一个维度上进行堆叠
boxes = torch.stack((x_center,y_center,w,h),axis = -1)
return boxes
创建两个左上角走下角格式的坐标boxes
dog_bbox, cat_bbox = [60.0, 45.0, 378.0, 516.0], [400.0, 112.0, 655.0, 493.0]
boxes = torch.tensor((dog_bbox,cat_bbox))
print(boxes)
boxes.shape
tensor([[ 60., 45., 378., 516.],
[400., 112., 655., 493.]])
torch.Size([2, 4])
运行结果
box_corner_to_center(boxes)
tensor([[219.0000, 280.5000, 471.0000, 318.0000],
[527.5000, 302.5000, 381.0000, 255.0000]])
torch.Size([2, 4])
原:(x_center,y_center,w,h)
x1 = x_center - 0.5w
y1 = y_center - 0.5h
x2 = x_center + 0.5w
y2 = y_center + 0.5h
def box_center_to_corner(boxes):
x_center,y_center,w,h = boxes[:,0],boxes[:,1],boxes[:,2],boxes[:,3]
x1,y1= x_center-0.5*w , y_center-0.5*h
x2,y2= x_center+0.5*w , y_center+0.5*h
boxes = torch.stack((x1,y1,x2,y2),axis=-1)
return boxes
boxes = box_center_to_corner(boxes)
print(boxes)
boxes.shape
tensor([[ 60., 45., 378., 516.],
[400., 112., 655., 493.]])
torch.Size([2, 4])