How to get all matrix values in 3X4 sub-matrix from an M x N matrix(Template Matching)

3 views (last 30 days)
Hi, I an fairly new to Matlab. I am doing template matching. What I want is to loop through the given image grascale matrix and get 3x4 matrix from that using a loop.
I have tried using nested for loops on rows and columns but handling that is becoming difficult as I also have to set some kind of outerbounds etc. I have also tried getting the 3x4 image using colon separator, but still Im stuck at how should my loop work to entertain all the values in the original image matrix. Any kind of help is appreciated. Thanks!

Answers (2)

KALYAN ACHARJYA
KALYAN ACHARJYA on 30 Nov 2020
Edited: KALYAN ACHARJYA on 30 Nov 2020
You can do that multiple ways:
im_blocks=mat2cell(grayImage,M/3*ones(1,3),N/4*ones(1,4));
Please ensure that, MxN are perfectly divisible by 3 and 4 respectively. One you can compare individual cell elememt with template matching. Once done do reverse complete cell to matrix (cell2mat)
Other ways you can see the following link

sushanth govinahallisathyanarayana
You could use im2col, with the 'sliding' option. It will split the image columnwise into patches. Then you can take the inner product of the template with the patch array and reshape it. It is equivalent to convolving.
However, if your patchsize is of mxn, and your image is of size MxN, it will split into (M+m-1)x(N+n-1) patches.
You can reform the correlation surface by reshaping the middle row of the patch array.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!