如何提取具有负坐标值的图像?
我目前正在尝试提取较大图像内的边界框,如下所示:
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)
但是,当 x1
或 y1
坐标为负数时,我遇到问题。我假设问题始于 bbox 数组,所以想知道是否有解决方法?
我正在尝试从 512 x 512 图像中提取一个固定大小的 32 x 32 框,因此需要“填充”无法以该尺寸提取的图像。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有两种可能的方法:
bbox = img[math.max(y1, 0): ...
x
和y
进行负偏移来创建x1
和y1
。只需使用 np.pad 填充图像并使用x
和y
直接:编辑:您在评论中提到您的图像的形状为
(w, h, 3)
。我假设你有一个灰度图像(二维数组)。在这种情况下,您必须使用更复杂的 np.pad 版本:这将避免填充最终轴(颜色通道)。
You have two possible approaches:
bbox = img[math.max(y1, 0): ...
x
andy
to createx1
andy1
. Just pad the image using np.pad and usex
andy
directly: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 ofnp.pad
:This will avoid padding the final axis (colour channels).