【问题标题】:How to modify patches made by patchify?如何修改patchify制作的补丁?
【发布时间】: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


【解决方案1】:

为了重建图像,我们必须一张一张地读取图像,并将每张图像放置在原始补丁位置。

文件命名存在错误,例如:
i = 1j = 11i = 11j = 1 ('image__111.jpg') 同名。
更好的文件命名:

cv2.imwrite('patches/images/' + 'image_' + '_'+ str(i).zfill(2) + '_' + str(j).zfill(2) + '.png', single_patch_img)

注意:

  • 我将图像文件格式从 JPEG 更改为 PNG,以保持原始图像质量。
    JPEG 是一种无损图像格式,因此每次存储和加载都会降低一些质量。

建议的重构解决方案:

  • 阅读test.jpg 只是为了了解形状(img
    img = cv2.imread("test.jpg")
    img = np.zeros_like(img)  # Fill with zeros for the example (start from an empty image).
  • 使用 patchify 只是为了获取形状(patches
    patches = patchify(img, (224,224,3), step=224)  # We could have also used: patches = np.zeros((14, 18, 1, 224, 224, 3), np.uint8)
  • 读取图像并将它们放置在patches中的原始位置:
    for i in range(patches.shape[0]):
        for j in range(patches.shape[1]):
            single_patch_img = cv2.imread('patches/images/' + 'image_' + '_'+ str(i).zfill(2) + '_' + str(j).zfill(2) + '.png')  # Read a patch image.
            if single_patch_img is None:
                raise Exception("Could not read the image") 
            patches[i, j, 0, :, :, :] = single_patch_img.copy()  # Copy single path image to patches
  • 取消补丁
    reconstructed_image = unpatchify(patches, img.shape)

这是一个完整的代码示例,用于修补、保存修补程序、加载修补程序和取消修补:

import cv2
import numpy as np
from patchify import patchify, unpatchify


img = cv2.imread("test.jpg")
patches_img = patchify(img, (224,224,3), step=224)  # patches_img.shape = (14, 18, 1, 224, 224, 3)

for i in range(patches_img.shape[0]):
    for j in range(patches_img.shape[1]):
        single_patch_img = patches_img[i, j, 0, :, :, :]
        cv2.rectangle(single_patch_img, (30, 30), (224-30, 224-30), (0, 255, 0), 3)  # Draw something (for testing).
        if not cv2.imwrite('patches/images/' + 'image_' + '_'+ str(i).zfill(2) + '_' + str(j).zfill(2) + '.png', single_patch_img):  # Save as PNG, not JPEG for keeping the quality.
            raise Exception("Could not write the image") 

# Store an unpatchified reference for testing
cv2.imwrite("unpatched_ref.jpg", unpatchify(patches_img, img.shape))

# Unpatchify
################################################################################

# Allocate sapces for storing the patches
img = cv2.imread("test.jpg")  # Read test.jpg just for getting the shape
img = np.zeros_like(img)  # Fill with zeros for the example (start from an empty image).

# Use patchify just for getting the size. shape = (14, 18, 1, 224, 224, 3)
# We could have also used: patches = np.zeros((14, 18, 1, 224, 224, 3), np.uint8)
patches = patchify(img, (224,224,3), step=224)

for i in range(patches.shape[0]):
    for j in range(patches.shape[1]):
        single_patch_img = cv2.imread('patches/images/' + 'image_' + '_'+ str(i).zfill(2) + '_' + str(j).zfill(2) + '.png')  # Read a patch image.
        if single_patch_img is None:
            raise Exception("Could not read the image") 
        patches[i, j, 0, :, :, :] = single_patch_img.copy()  # Copy single path image to patches

reconstructed_image = unpatchify(patches, img.shape)

cv2.imwrite("unpatched.jpg", reconstructed_image)

样本输出(缩小尺寸):

【讨论】:

  • 绝对精彩@Rotem。它现在完美运行,非常感谢你,你帮了我很多!是的,感谢有关发布原始帖子和 python 标签的参考的建议!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-07
  • 2012-12-10
  • 2019-01-09
  • 2011-03-22
  • 1970-01-01
  • 2019-11-11
  • 1970-01-01
相关资源
最近更新 更多