INNER CODE UNIT · Python
x2
Anything-of-anything/Anything-3D · AnyObject3D/src/main.py:47
x1, x2 = cx - crop_sz / 2., cx + crop_sz / 2.
y1, y2 = cy - crop_sz / 2., cy + crop_sz / 2.
x1, y1, x2, y2 = list(map(lambda x: int(x), [x1, y1, x2, y2]))
# affine transform to get the object-centric image
bbox = [x1, y1, x2, y2]
output_sz = 512
a = (output_sz-1) / (bbox[2]-bbox[0])
b = (output_sz-1) / (bbox[3]-bbox[1])
c = -a * bbox[0]
d = -b * bbox[1]
mapping = np.array([[a, 0, c],
[0, b, d]]).astype(np.float32)
img_patch = cv2.warpAffine(image, mapping, (output_sz, output_sz), borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0))
mask_patch = cv2.warpAffine(mask, mapping, (output_sz, output_sz), borderMode=cv2.BORDER_CONSTANT, borderValue=0)
img_patch = cv2.cvtColor(img_patch, cv2.COLOR_RGB2BGR)
cv2.imwrite(f'./images/{imgname}_patch.jpg', img_patch)
cv2.imwrite(f'./images/{imgname}_mask.png', mask_patch)