如何修改patchify制作的补丁?
How to modify patches made by patchify?
我正在使用 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'
谢谢!
为了重建图像,我们必须一张一张地读取图像,并将每张图像放置在原始补丁位置。
文件命名存在错误,例如:
i = 1
和 j = 11
与 i = 11
和 j = 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)
示例输出(缩小尺寸):
我正在使用 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'
谢谢!
为了重建图像,我们必须一张一张地读取图像,并将每张图像放置在原始补丁位置。
文件命名存在错误,例如:
i = 1
和 j = 11
与 i = 11
和 j = 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)
示例输出(缩小尺寸):