title: Applying SAD on image using matlab
body: <p>Hello so I have an original image and I cropped a part of that image(template) and  wrote a code with SAD algorithim to detect on the original image the part i cropped (template) where it exists and draw a rectangle on it. </p>
  
  <p>The code doesnt have any errors , but the rectangle draw doesnt match the template , so I guess the 'output ' variable is the problem can you please help me</p>
  
  <pre><code>I2=imread('img.PNG');
  I2=rgb2gray(I2);
  [r,c]= size(I2);
  
  %padding the image
  %padding
  B = padarray(I2,[24 24],'replicate' ,'both');
  
  
  %imshow(B);
  
  %creating template
  temp=imread('crop_img.PNG');
  temp= rgb2gray(temp);
  %imshow(temp)
  size(temp)
  
  
  
  output = zeros (size(I2));
  K = size(temp)
  x=1;
  y=1;
  for i = 25 :  r-24
  
      for j = 25: c-24
  
          %dif = temp -I2(i:i+47,j:j+47) ;
          K = imabsdiff(temp,B(i-24:i+24,j-24:j+24));
  
                output(i-24, j-24) = sum (K(:));
  
  
  
  
      end
  
  end
  
  %gettting min value in output
  min_value = output(1,1)
  for i=1 : r
      for j=1 :c
          if(output(i,j)&lt;min_value)
             min_value=output(i,j);
             row=i;
             col=j;
          end    
      end
  end
  row
  col
  output(1,465)
  output(6,200)
  
  %draw rectangle on matching area
  %Create the shape inserter object.
  
  shapeInserter = vision.ShapeInserter;
  
  
  
  %Define the rectangle dimensions as [x y width height].
  
  rectangle = int32([row col 24 24]);
  
  %Draw the rectangle and display the result.
  
  J = step(shapeInserter, I2, rectangle);
  imshow(J); 
  </code></pre>