'How can I extract images with negative coordinate values?
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.
Solution 1:[1]
You have two possible approaches:
- You can extract each patch, cropping it to the edge of the image, then apply padding. You just need to ensure x1 and y1 are positive:
bbox = img[math.max(y1, 0): ... - You can pad the whole image and offset coordinates. This is probably better since you already negatively offset
xandyto createx1andy1. Just pad the image using np.pad and usexandydirectly:
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).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
