keshari abeysinghe
keshari abeysinghe

Reputation: 69

How to solve "ValueError: `window_shape` is incompatible with `arr_in.shape` "?

I tried to patch the big .tif images by using patchify library. I followed the https://www.youtube.com/watch?v=7IL7LKSLb9I&ab_channel=DigitalSreeni video and I got this code from this . I got the following error.

Traceback (most recent call last): File "generate.py", line 13, in patches_img = patchify(large_image, (256, 256), step=256) # Step=256 for 256 patches means no overlap File "C:\Users\HP\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\patchify_init_.py", l ine 32, in patchify return view_as_windows(image, patch_size, step) File "C:\Users\HP\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\patchify\view_as_windows .py", line 28, in view_as_windows raise ValueError("window_shape is incompatible with arr_in.shape") ValueError: window_shape is incompatible with arr_in.shape

The Code is below mentioned

import numpy as np
from matplotlib import pyplot as plt
from patchify import patchify
import tifffile as tiff

large_image_stack = tiff.imread('C:\\Users\\HP\\Downloads\\Radar_data\\test\\images\\2018_09_26.tif')
large_mask_stack = tiff.imread('C:\\Users\\HP\\Downloads\\Radar_data\\test\\masks\\2018_12_19_d.tif')

for img in range(large_image_stack.shape[0]):

    large_image = large_image_stack[img]

    patches_img = patchify(large_image, (256, 256), step=256)  # Step=256 for 256 patches means no overlap

    for i in range(patches_img.shape[0]):
        for j in range(patches_img.shape[1]):
            single_patch_img = patches_img[i, j, :, :]
            tiff.imwrite('patches/images/' + 'image_' + str(img) + '_' + str(i) + str(j) + ".tif", single_patch_img)

for msk in range(large_mask_stack.shape[0]):

    large_mask = large_mask_stack[msk]

    patches_mask = patchify(large_mask, (256, 256), step=256)  # Step=256 for 256 patches means no overlap

    for i in range(patches_mask.shape[0]):
        for j in range(patches_mask.shape[1]):
            single_patch_mask = patches_mask[i, j, :, :]
            tiff.imwrite('patches/masks/' + 'mask_' + str(msk) + '_' + str(i) + str(j) + ".tif", single_patch_mask)
            single_patch_mask = single_patch_mask / 255.

I am a new one for this area and hope anyone will help. Thank you.

Upvotes: 1

Views: 3298

Answers (1)

Ahmed
Ahmed

Reputation: 121

This code simply does not work if:

1- the loaded image is an RGB image (with a third dimension=3), so need to consider in the patch size: patches_img = patchify(large_image, (256, 256,3), step=256)

and

single_patch_img=img_patches[i,j,:,:,:]

2- or the original image is not a stack of images (need to remove the first loop)

more insight in here.

Upvotes: 1

Related Questions