如何修改patchify制作的补丁?

Ole*_*kiy 4 python opencv image-processing

我正在使用 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'

感谢您!

Rot*_*tem 5

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

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

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

笔记:

  • 我将图像文件格式从 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).
Run Code Online (Sandbox Code Playgroud)
  • 使用 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)
Run Code Online (Sandbox Code Playgroud)
  • 读取图像并将其放置在以下位置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
Run Code Online (Sandbox Code Playgroud)
  • 取消补丁
    reconstructed_image = unpatchify(patches, img.shape)
Run Code Online (Sandbox Code Playgroud)

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

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

样本输出(缩小尺寸):
在此输入图像描述