Search code examples
matlabimage-processingmatrixmatlab-figuresubmatrix

In MATLAB, how to create all the possible Sub-Matrices from a large Matrix by shifting exactly one row and one column?


Suppose i have a 256*256 matrix(image pixel values).I want to create that Sliding window 1 to Sliding Window n by shifting exactly one row and one column. All the Sliding Window Matrix Should be in the size of 5*5.

I have tried with this following code. By this i am getting the submatrices but not by the shifting of exactly one row and one column.

I = imread('D:\Study Material\project\Finger Print Database\1_2.png');

J = imresize(I, [128 128]);

C=mat2tiles(J,[5,5]);
  • Suppose original matrix has 10 row and 10 column.
  • I'm getting the sub-matrices like (column1-column5 & row1-row5) then (column6-column10 & row6-row10).
  • But I need like this- (column1-column5 & row1-row5)then (column2-column6 & row1-row5)like this first column will be shifted to 10. After that row will be shifted to 10.

Solution

  • You need im2col with the 'sliding' option, followed by reshape:

    blockSize = [5 5];
    C = reshape(im2col(J, blockSize, 'sliding'), blockSize(1), blockSize(2), []);