How to use view as blocks in Scikit-Image?

Viewed 3507

I have a numpy array of shape (12,224,224). This is 12 images of size (244, 244). When I had a single image, this was simple. The image was of size (x,y). For example, x is an image of size (400,400), for which I could use view_as_blocks like this:

from skimage.util import view_as_blocks as vablks
xx = vablks(x, block_shape=(8,8))

This would result in a block of shape (50,50,8,8). Now I would like to know how to apply this when I have a list of images. Either I lose shape, that is my 12 images are combined into one (224,224) block broken down into (28,28,8,8), or I run into a ValueError. Here is the code I tried to use for iterating over the 12 images and viewing the (224,224) shaped images

xx = []
for item_ in x:
    xx.append(blockSplitter(item_))

where x is a list of images.

Here is the error:

ValueError: 'block_shape' is not compatible with 'arr_in'

Overall, I would like to know how to view the images as blocks of 8x8 without losing the images. Help, Please and Thank You.

1 Answers

You have at least two options:

1) Convert the list to an array, as suggested by the commenter above. Then use view_as_blocks with the correct parameters:

from skimage.util import view_as_blocks
images = [np.zeros((50, 50)) for i in range(10)]
images = np.array(images)
all_blocks = view_as_blocks(images, block_shape=(1, 10, 10)).squeeze()

2) Convert each item in the list to a windowed view, and then convert the end result to an array:

from skimage.util import view_as_blocks
images = [np.zeros((50, 50)) for i in range(10)]
image_blocks = [view_as_blocks(image, block_shape=(10, 10)) for image in images]
all_blocks = np.array(image_blocks)
Related