def random_zoom_out(self, img, boxes):
'''
Randomly zoom out the image and adjust the bbox locations.
For bbox (xmin, ymin, xmax, ymax), the zoomed out bbox is:
coef -- zoom out coefficient
((1-coef)*w/2 + coef*xmin, (1-coef)*h/2 + coef*ymin,
(1-coed)*w/2 + coef*xmax, (1-coef)*h/2 + coef*ymax)
Args:
img: (PIL.Image) image.
boxes: (tensor) bbox locations, sized [#obj, 4].
Return:
img: (PIL.Image) randomly zoomed out image.
boxes: (tensor) randomly zoomed out bbox locations, sized [#obj, 4].
'''
coef = random.uniform(0.5, 1)
w = img.width
h = img.height
xmin = (1-coef)*w/2 + coef*boxes[:,0]
xmax = (1-coef)*w/2 + coef*boxes[:,2]
ymin = (1-coef)*h/2 + coef*boxes[:,1]
ymax = (1-coef)*h/2 + coef*boxes[:,3]
boxes[:,0] = xmin
boxes[:,1] = ymin
boxes[:,2] = xmax
boxes[:,3] = ymax
top = int(h/2*(1-coef)/coef)
bottom = int(h/2*(1-coef)/coef)
left = int(w/2*(1-coef)/coef)
right = int(w/2*(1-coef)/coef)
img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_REPLICATE)
img = cv2.resize(img, (w, h))
return img, boxes
评论列表
文章目录