Reputation: 516
I'm currently trying to extract an bounding box inside of a larger image as shown here:
img = cv2.imread(args.data_dir + i[1]["image_id"])
x = int(i[1]['xCenter'])
y = int(i[1]['yCenter'])
x1 = int(i[1]['xCenter'] - patch_direction)
y1 = int(i[1]['yCenter'] - patch_direction)
bbox = img[y1:y1+args.patch_size, x1:x1+args.patch_size]
cv2.imwrite(args.fp_dir + f'{(i[1]["image_id"]).rstrip(".png")}_x_{x}_y_{y}.png', bbox)
However, I encounter issues when the x1
or y1
coordinates are negative. I'm assuming the issue begins in the bbox array and so was wondering if there is a workaround?
I'm trying to extract a fixed sized box of 32 x 32 from a 512 x 512 image and so will need to 'pad' images that can't be extracted at that size.
Upvotes: 0
Views: 860
Reputation: 1876
You have two possible approaches:
bbox = img[math.max(y1, 0): ...
x
and y
to create x1
and y1
. Just pad the image using np.pad and use x
and y
directly:img = np.pad(img, (patch_direction,)) # This pads before and after on each axis with the same amount
bbox = img[y:y+args.patch_size, x:x+args.patch_size]
Edit: You mention in the comments that your image has a shape of (w, h, 3)
. I assumed you had a greyscale image (2D array). In that case you have to use the more complex version of np.pad
:
img = np.pad(img, ((patch_direction,patch_direction), (patch_direction,patch_direction), (0,0)))
This will avoid padding the final axis (colour channels).
Upvotes: 1