如何提取具有负坐标值的图像?

发布于 2025-01-14 17:42:40 字数 552 浏览 0 评论 0原文

我目前正在尝试提取较大图像内的边界框,如下所示:

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)

但是,当 x1y1 坐标为负数时,我遇到问题。我假设问题始于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

源来凯始玺欢你 2025-01-21 17:42:40

您有两种可能的方法:

  • 您可以提取每个补丁,将其裁剪到图像的边缘,然后应用填充。您只需确保 x1 和 y1 为正数: bbox = img[math.max(y1, 0): ...
  • 您可以填充整个图像和偏移坐标。这可能会更好,因为您已经对 xy 进行负偏移来创建 x1y1。只需使用 np.pad 填充图像并使用 xy 直接:
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]

编辑:您在评论中提到您的图像的形状为 (w, h, 3)。我假设你有一个灰度图像(二维数组)。在这种情况下,您必须使用更复杂的 np.pad 版本:

img = np.pad(img, ((patch_direction,patch_direction), (patch_direction,patch_direction), (0,0)))

这将避免填充最终轴(颜色通道)。

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 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文