小编Ole*_*kiy的帖子

如何修改patchify制作的补丁?

我正在使用 patchify 库来修补大图像:

img = cv2.imread("resized.jpg")
patches_img = patchify(img, (224,224,3), step=224)
print(patches_img.shape)
Run Code Online (Sandbox Code Playgroud)

然后我保存补丁:

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")
Run Code Online (Sandbox Code Playgroud)

然后,我想对任何这些补丁进行一些修改,例如绘制边界框,因此当我使用unpatchify将补丁合并在一起时,边界框将显示在重建图像上。

进行修改后,我运行以下代码将补丁合并在一起:

reconstructed_image = unpatchify(patches_img, img.shape)
cv2.imwrite("unpatched.jpg", reconstructed_image)
Run Code Online (Sandbox Code Playgroud)

但生成的重建图像与原始图像相同,没有可见的变化。我认为这是因为 unpatchify 读取变量patch_img,该变量仍然存储原始的、未修改的补丁。

我尝试了以下方法:

patches = 'patches/images/*.jpg'
reconstructed_image = unpatchify(patches, img.shape)
cv2.imwrite("unpatched.jpg", reconstructed_image)
Run Code Online (Sandbox Code Playgroud)

但我收到AttributeError: 'str' object has no attribute 'shape'

感谢您!

python opencv image-processing

4
推荐指数
1
解决办法
4335
查看次数

标签 统计

image-processing ×1

opencv ×1

python ×1