0

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) ;
Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
Prayag Bhatia
  • 366
  • 3
  • 16

1 Answers1

1

You need to use a minimum threshold for R. E.g., something like this (sorry I don't have time to write this out exactly): idx = find( R > 0.5 ); [r,c] = ind2sub( size(R), idx ); The issue is R will always have a maximum but the match may be a false one as you're discovering. Obviously you'll have to tune the 0.5 for your application (lower values mean less misses but more false positives; higher values give the opposite).

Suhas C
  • 192
  • 1
  • 8