【发布时间】:2021-07-06 23:54:54
【问题描述】:
我正在使用 patchify 库来修补大图像:
img = cv2.imread("resized.jpg")
patches_img = patchify(img, (224,224,3), step=224)
print(patches_img.shape)
然后我保存补丁:
for i in range(patches_img.shape[0]):
for j in range(patches_img.shape[1]):
single_patch_img = patches_img[i, j, 0, :, :, :]
if not cv2.imwrite('patches/images/' + 'image_' + '_'+ str(i)+str(j)+'.jpg', single_patch_img):
raise Exception("Could not write the image")
然后,我想对这些补丁中的任何一个进行一些修改,例如绘制边界框,所以当我使用 unpatchify 将补丁合并在一起时,边界框将显示在重建图像上。
进行修改后,我运行以下代码将补丁重新合并在一起:
reconstructed_image = unpatchify(patches_img, img.shape)
cv2.imwrite("unpatched.jpg", reconstructed_image)
但是生成的重建图像与原始图像相同,看不到任何变化。 我认为这是因为 unpatchify 读取了变量 patches_img,该变量仍然存储了原始的、未修改的补丁。
我尝试了以下方法:
patches = 'patches/images/*.jpg'
reconstructed_image = unpatchify(patches, img.shape)
cv2.imwrite("unpatched.jpg", reconstructed_image)
但我得到 AttributeError: 'str' object has no attribute 'shape'
谢谢!
【问题讨论】:
-
您不必为了绘制边界框而保存和加载图像(除非您的绘图在 Python 脚本之外)。能否请您展示一个绘图示例?
-
第二部分结束:unpatchify 的第一个参数应该是 NumPy 数组,如 patch_img,而不是字符串。
-
感谢您的回答@Rotem。本质上,我将使用 patchify 进行对象检测。所以管道如下:首先我修补我想要检测对象的图像,然后,我将这些补丁中的每一个提供给我训练过的模型以进行推理并绘制边界框,然后我将每个推断的补丁与 b.boxes 一起保存然后我想使用 unpatchify 将补丁合并到原始图像中,但 b.boxes 已经存在。
-
很好...下次请在您发布“后续”帖子时发布对原始帖子的引用。 + 请添加 python 标签。
标签: python opencv image-processing