0

I am trying to break down an image into smaller images (like a grid), process those blocks and join them back up for display, using the reshape function.

I am currently doing this by means of a nested for loop as shown in the code. However, as built-in functions like reshape()/np.reshape() are faster compared to for-loops, I want to achieve the same result by using the reshape() function. I am dealing with a 3-channel BGR image and would like several smaller 3-channel BGR images, that are pieces of the grid that splits the big one.

import numpy as np

roi_ht = 2
roi_wd = 2

parent_array = np.array([[1,10,2,20],
                        [100,1000,200,2000],
                        [3,30,4,40],
                        [300,3000,400,4000]])

smaller_images = [parent_array[x*roi_ht:(x+1)*roi_ht, y*roi_wd:(y+1)*roi_wd] for x in range(0,2)  for y in range(0,2)]

This is the desired result to be achieved using 'reshape' or any other function built-in to python/numpy (smaller images) :

[array([[   1,   10], [ 100, 1000]]), 
 array([[   2,   20], [ 200, 2000]]), 
 array([[   3,   30], [ 300, 3000]]), 
 array([[   4,   40], [ 400, 4000]])]

Vasu Deo.S
  • 1,820
  • 1
  • 11
  • 23

0 Answers0