title: OpenCV image matching - form photo vs form template
body: <p>I'm trying to detect wether a photo represents a predefined formular template filled with data.</p>
  
  <p>I'm new to image processing and OpenCV but my first attempt is to use FlannBasedMatcher and compare the count of keypoints detected. </p>
  
  <p>Is there a better way to do this?</p>
  
  <p><a href="https://i.imgur.com/qDyW1oG.jpg" rel="nofollow noreferrer" title="filled-form.jpg">filled-form.jpg</a></p>
  
  <p><a href="https://nemo-responsive-image.live.cf.public.springer.com/v1/resize/width/736/1/url/http:/resource-cms.springer.com/springer-cms/rest/v1/img/6550082/v3/4by3" rel="nofollow noreferrer" title="form-template.jpg">form-template.jpg</a></p>
  
  <pre><code>import numpy as np
  import cv2
  from matplotlib import pyplot as plt
  MIN_MATCH_COUNT = 10
  img1 = cv2.imread('filled-form.jpg',0)          # queryImage
  img2 = cv2.imread('template-form.jpg',0) # trainImage
  # Initiate SIFT detector
  sift = cv2.xfeatures2d.SIFT_create()
  # find the keypoints and descriptors with SIFT
  kp1, des1 = sift.detectAndCompute(img1,None)
  kp2, des2 = sift.detectAndCompute(img2,None)
  FLANN_INDEX_KDTREE = 1
  index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
  search_params = dict(checks = 50)
  flann = cv2.FlannBasedMatcher(index_params, search_params)
  matches = flann.knnMatch(des1,des2,k=2)
  # store all the good matches as per Lowe's ratio test.
  good = []
  for m,n in matches:
    if m.distance &lt; 0.7*n.distance:
      good.append(m)
  if len(good)&gt;MIN_MATCH_COUNT:
    print "ALL GOOD!" 
  else:
    print "Not enough matches are found - %d/%d" % (len(good),MIN_MATCH_COUNT)
    matchesMask = None
  </code></pre>