python 使用patchify库创建修补程序时出现问题

agyaoht7  于 2023-01-19  发布在  Python
关注(0)|答案(3)|浏览(247)

我正在使用patchify库创建一个更大的. jpg图像的补丁。我正在使用下面的代码,摘自YT视频:https://www.youtube.com/watch?v=7IL7LKSLb9I&ab_channel=DigitalSreeni
当YT人员读取图像(12个tiff图像)时,他得到的large_image_stack变量大小如下:(12,768,1024),即12个图像,每个图像的尺寸为768x1024。
我有一张3000x4000的jpg图片,我得到的large_image_stack变量的大小是(3000,4000,3)。

import numpy as np
from matplotlib import pyplot as plt
from patchify import patchify
import cv2

large_image_stack = cv2.imread("test.jpg")

for img in range(large_image_stack.shape[0]):
    
    large_image = large_image_stack[img]
    
    patches_img = patchify(large_image, (224,224), step=224)
    
    for i in range(patches_img.shape[0]):
        for j in range(patches_img.shape[1]):
            
            single_patch_img = patches_img[i,j,:,:]
            cv2.imwrite('patches/images/' + 'image_' + str(img)+ '_' + str(i)+str(j)+ '.jpg', single_patch_img)

但我得到了以下错误:

    • 数值错误:window_shape太大**

查看patchify库中的view_as_windows. py,我看到以下内容:

arr_shape = np.array(arr_in.shape)
    window_shape = np.array(window_shape, dtype=arr_shape.dtype)

    if ((arr_shape - window_shape) < 0).any():
        raise ValueError("`window_shape` is too large")

由于我对这些事情很陌生,我无法解决这个错误。
任何帮助都将不胜感激!!

w51jfk4q

w51jfk4q1#

我想出了如何解决这个问题,因为这是一个简单的错误。基本上,我只有一个图像,所以用for循环遍历图像没有意义。
然后,对于图像本身,因为它是BGR,所以必须修改表示面片大小的数组,使其应为(224,224,3)
最后,为了保存补丁,我在我做的另一个问题中使用了@Rotem提供的更正代码。
这是最终结果的样子:

img = cv2.imread("test.jpg")
patches_img = patchify(img, (224,224,3), step=224)

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")
hgqdbh6s

hgqdbh6s2#

正如@OlegRuskiy所说,你需要使用(224,224,3)的窗口,其中3是你的频道数。
他没有提到但在他代码中是正确的是,当这个内核通过你的深度维度时,你会得到一个额外的深度维度。对我来说,它是(16, 24, 1, 256, 256, 3)而不是(16, 24, 256, 256, 3)
所以我使用np.squeeze()删除了1,所以输出为(16, 24, 256, 256, 3)

for file in files:
    img=io.imread(path+file)

    patches = patchify(img,(256,256,3),step=256-32) #32 is the number of desiered overlapping pixels
    print(patches.shape)
    patches = np.squeeze(patches)
    print(patches.shape)
    
    for i in range(patches.shape[0]):
        for j in range(patches.shape[1]):
            
            patch = patches[i,j,:,:,:]
            io.imsave(opath+ file.split('.')[0] + '_r'+ str(i).zfill(2) + '_c' + str(j).zfill(2) + '.jpg', patch)
muk1a3rh

muk1a3rh3#

I got TypeError: `arr_in` must be a numpy ndarray. Please help me this out

 for file_name in os.listdir(data_path):
      source = data_path + file_name
      im = Image.open(source)
      patches = patchify(im,(24,24,3),step=24) 
      print(patches.shape)
      patches = np.squeeze(patches)
      print(patches.shape)
        
      for i in range(patches.shape[0]):
            for j in range(patches.shape[1]):
                
              patch = patches[i,j,:,:,:]
             im.save(data_path+ file_name.split('.')[0] + '_r'+ str(i).zfill(2) + '_c' + str(j).zfill(2) + '.jpg', patch)

相关问题