title: Pattern matching – Normalized Correlation
body: <p>as a part of my homework i need to implement this pattern matching.
  the goal is to "Detect as many of the 0's (zeros) as you can in image coins4.tif."
  <br> i was given the NGC function. and i need to use it
  <br> this is my main.m file</p>
  
  <pre><code>Image = readImage('coins4.tif');
  Pattern = readImage('zero.tif');
  showImage(Image);
  showImage(Pattern);
  message = sprintf('Pattern matching Normalized Correlation');
  PatternMatching(Image , Pattern);
  uiwait(msgbox(message,'Done', 'help'));
  close all
  </code></pre>
  
  <p><br>this is my PatternMatching function.</p>
  
  <pre><code>function [ output_args ] = PatternMatching( Image , Pattern )
  % Pattern matching – Normalized Correlation
  % Detect as many of the 0's (zeros) as you can in image coins4.tif.
  % Use the 0 of the 10 coin as pattern.
  % Use NGC_pm and find good threshold. Display original image with? detected regions marked using drawRect.
  
  % NGCpm(im,pattern);
  % drawRect(rectCoors,color);
  % rectCoors = [r0,c0,rsize,csize]  - r0,c0 = top-left corner of rect.
  %             rsize = number of rows, csize = number of cols
  %
  % color = an integer &gt;=1 representing a color in the color wheel
  %                   (curerntly cycles through 8 different colors
  
  showImage(Image);
  hold on
  res = NGCpm(Image, Pattern);
  
   for i = 1:size(res,1)
       for j = 1:size(res,2)
           if res(i,j) &gt; 0.9999
               drawRect([i j size(Pattern,1) size(Pattern,2)], 5)
           end
       end
   end
  
  end
  </code></pre>
  
  <p>this is the Given NGCpm.m file </p>
  
  <pre><code>function res=NGC_PM(im,pattern)
  [n m]=size(pattern);
  [im_row,im_col]=size(im);
  if ~(var(pattern(:))==0)
       res = normxcorr2(pattern, im);
       res=res(n:im_row,m:im_col);   
  else
      res=zeros(size(im)-size(pattern)+1);
  end;
   res = 1-abs(res);  % res = abs(res);
  </code></pre>
  
  <p><br>
  this is the pattern i'm trying to find and the results, i'm getting<br></p>
  
  <p>i'm trying to find as many "Zeros" as possiable using the zero pattern of the coin 10.</p>
  
  <p>i'm tryingto understand if there is something wrong with my algorithm in the PatternMatching function. since the NGCpm function is already given to me,  all i need to do is just loop of the best threshold ,correct? 
  <br> or do i need to blur the image or the pattern? </p>
  
  <p><img src="https://i.stack.imgur.com/ooAKI.jpg" alt="this is the pattern i&#39;m trying to find"></p>
  
  <p><img src="https://i.stack.imgur.com/Y5duK.jpg" alt="this is the image and the results"></p>