I am using template matching to find the template image inside my given input image in MATLAB. So it normally forms a rectangle over the area where it finds the template image in my given input image.
But if my input image doesn't contain the template image, it forms rectangle over some random region.
What I want is to crop the image found as a result of template matching (i.e the one found in rectangle), so that later I can compare it with my template image and check how much similar they are.
Here's the code I used:
IReal1 = imread('Real_4.jpg');
check1 = imread('check.jpg') ;
IReal = rgb2gray(IReal1) ;
check = rgb2gray(check1) ;
[Ir Ic] = size(IReal) ;
[Tr Tc] = size(check) ;
R = normxcorr2(check , IReal);
R = imcrop(R , [Tc Tr Ic Ir]);
[r c v] = find(R == (max(max(R)))) ;
RGB = insertShape(IReal, 'rectangle', [c r Tc Tr], 'Linewidth', 3);
imshow(RGB) ;