title: OpenCV template matching or feature detection to correctly categorize types of faxes?
body: <p>Sorry, this is my first time posting a question.</p>
  
  <p>I need a process to identify images within other images to be able to categorize images (ex. sorting faxes based on a company symbol). </p>
  
  <p>I have tried openCV template matching but I doesn't seem to be that accurate and if I try to use it by looping through multiple images I cant seem to find a threshold of accuracy that would tell me there is a correct match.</p>
  
  <p>I have also tried openCV feature matching with ORB but it seems that my images might not be complex enough so the features get matched all over the page. </p>
  
  <p>For template matching I follow the basic code from the documentation:</p>
  
  <pre><code># load the image image, convert it to grayscale, and detect edges
  template = cv2.imread('PATH TO TEMPLATE IMAGE')
  template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
  template = cv2.Canny(template, 50, 200)
  (tH, tW) = template.shape[:2]
  cv2.imshow("Template", template)
  
  match_list = []
  # loop over the images to find the template in
  for imagePath in glob.glob('PATH TO DIRECTORY OF IMAGES'):
      # load the image, convert it to grayscale, and initialize the
      # bookkeeping variable to keep track of the matched region
      image = cv2.imread(imagePath)
      gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
      found = None
  
      # loop over the scales of the image
      for scale in np.linspace(0.2, 1.0, 20)[::-1]:
          # resize the image according to the scale, and keep track
          # of the ratio of the resizing
          resized = imutils.resize(gray, width = int(gray.shape[1] * scale))
          r = gray.shape[1] / float(resized.shape[1])
  
          # if the resized image is smaller than the template, then break
          # from the loop
          if resized.shape[0] &lt; tH or resized.shape[1] &lt; tW:
              break
  
          # detect edges in the resized, grayscale image and apply template
          # matching to find the template in the image
          edged = cv2.Canny(resized, 50, 200)
          result = cv2.matchTemplate(edged, template, cv2.TM_CCOEFF_NORMED)
          (minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(result)
      match_list.append((maxVal, imagePath))
  
  print(match_list)
  </code></pre>
  
  <p>My questions are:</p>
  
  <ol>
  <li><p>Am I on the right track as far as which openCV process to use?</p></li>
  <li><p>If I use template matching how can I use it to categorize images based on and exact or close to exact match?</p></li>
  </ol>
  
  <p>Thanks!</p>
  
  <p>Part 2: This is the template matching code Ive gone with.</p>
  
  <pre><code>template = cv2.imread('/Users/ME/Documents/so_temp.tiff',0)
  w, h = template.shape[::-1]
  
  match_values = []
  for imagePath in glob.glob('/Users/ME/Documents' + "/*.tiff"):
      # load the image, convert it to grayscale, and initialize the
      # bookkeeping variable to keep track of the matched region
      img = cv2.imread(imagePath)
      img2 = img.copy()
      gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  
      methods = ['cv2.TM_CCORR_NORMED']
  
      for meth in methods:
          img = img2.copy()
          method = eval(meth)
  
          # Apply template Matching
          res = cv2.matchTemplate(gray,template,method)
          min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
  
          match_values.append((min_val, max_val, imagePath))
          print (min_val, max_val, imagePath)
  
          # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
          if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
              top_left = min_loc
          else:
              top_left = max_loc
          bottom_right = (top_left[0] + w, top_left[1] + h)
  
          cv2.rectangle(img,top_left, bottom_right, 255, 2)
  
          plt.subplot(121),plt.imshow(res,cmap = 'gray')
          plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
          plt.subplot(122),plt.imshow(img,cmap = 'gray')
          plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
          plt.suptitle(meth)
  
          plt.show()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/hILCP.png" rel="nofollow noreferrer">This is my result</a></p>
  
  <p>But keep in mind that Im doing this over many faxes and the template matching returns too many false positives, so I need suggestions on what I can do to, either through training or image processing, to return the one correct fax that matches the template.</p>
  
  <p>There is also the problem of identifying the fax if the fax image is upside down, which can be done through feature detection, but I cant seem to get working through template matching. </p>
  
  <p>I hope I made this clearer, thank you.</p>