title: Match ROI or shape in several images for analysis
body: <p>I have a lot of images to use. But this question I am going to just use the first and last image. </p>
  
  <p>Image 1 of 83 </p>
  
  <p><img src="https://raw.githubusercontent.com/naeluh/List-of-Python-and-Torch-Scripts-for-openCV/master/odd_pys/i/images/imageresized_0.jpg" alt="image description"></p>
  
  <p>Image 83 of 83 </p>
  
  <p><img src="https://raw.githubusercontent.com/naeluh/List-of-Python-and-Torch-Scripts-for-openCV/master/odd_pys/i/images/imageresized_83.jpg" alt="image description"></p>
  
  <p>I ran Image 1 through this code </p>
  
  <pre><code>import numpy as np
  import cv2
  
  def process(filename, key):
     gwash = cv2.imread(filename)
     gwashBW = cv2.cvtColor(gwash, cv2.COLOR_BGR2GRAY)
  
     ret,thresh1 = cv2.threshold(gwashBW,179,255,cv2.THRESH_BINARY) 
     kernel = np.ones((1,1),np.uint8)
     erosion = cv2.erode(thresh1, kernel,iterations = 1) 
     opening = cv2.morphologyEx(erosion, cv2.MORPH_OPEN, kernel)
     closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel) 
     _, contours ,_ = cv2.findContours(closing,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) 
  
     areas = [cv2.contourArea(c) for c in contours]
     idx = np.argmax(areas)
     cnt = contours[idx]
  
     mask = np.zeros_like(gwash)
     cv2.drawContours(mask, contours, idx, (255,255,255), -1) 
     out = np.zeros_like(gwash) 
     out[mask == 255] = gwash[mask == 255]
  
     cv2.imwrite('img/out{}.jpg'.format(key),out)
     print idx
  </code></pre>
  
  <p><img src="https://raw.githubusercontent.com/naeluh/List-of-Python-and-Torch-Scripts-for-openCV/master/odd_pys/i/img/out0.jpg" alt="image description"></p>
  
  <p>This one result I have gotten better and worst results.</p>
  
  <p>Here is the contour code I used and result </p>
  
  <pre><code>import cv2
      import numpy as np
      import imutils
      from matplotlib import pyplot as plt
  
      def process(filename, key):
          image = cv2.imread(filename)
  
          resized = imutils.resize(image, width=600)
          ratio = image.shape[0] / float(resized.shape[0])
          blurred = cv2.GaussianBlur(resized, (5, 5), 0)
          gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
          lab = cv2.cvtColor(resized, cv2.COLOR_BGR2LAB)
          thresh = cv2.threshold(gray, 180, 255, cv2.THRESH_BINARY)[1]
          imagem = cv2.bitwise_not(thresh)
          th4 = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
          th3 = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2)
          gray_2 = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
          gray_blur = cv2.GaussianBlur(gray, (15, 15), 0)
          thresh_2 = cv2.adaptiveThreshold(gray_blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV, 11, 1)
          kernel = np.ones((1, 1), np.uint8)
          closing = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=500)
          cnts = cv2.findContours(closing.copy(), cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
          cnts2 = cv2.findContours(closing.copy(), cv2.RETR_EXTERNAL ,cv2.CHAIN_APPROX_SIMPLE)
  
  
          cnts = cnts[0] if imutils.is_cv2() else cnts[1]
          cnts2 = cnts2[0] if imutils.is_cv2() else cnts2[1]
  
              # loop over the contours
          for c in cnts:
                # compute the center of the contour
                M = cv2.moments(c)
                if M["m00"] != 0:
                    cX = int(M["m10"] / M["m00"])
                    cY = int(M["m01"] / M["m00"])
  
                else:
                    cX, cY = 0, 0
  
                # multiply the contour (x, y)-coordinates by the resize ratio,
                # then draw the contours and the name of the shape and labeled
                # color on the image
                c = c.astype("float")
                c *= ratio
                c = c.astype("int")
                cX *= ratio
                cY *= ratio
                cv2.drawContours(image, [c], -1, (0, 255, 0), 5)
                cv2.circle(image, (int(cX),int(cY)),5,300,3)  
  
          #r = 100.0 / image.shape[1]
          #dim = (100, int(image.shape[0] *r))
  
          #imageresized = cv2.resize(image,(2048,2048),dim,interpolation = cv2.INTER_AREA)
          cv2.imwrite( 'i/image_{}.jpg'.format(key) ,QR_final )
          print 'image_{}.jpg'.format(key) 
  </code></pre>
  
  <p><img src="https://raw.githubusercontent.com/naeluh/List-of-Python-and-Torch-Scripts-for-openCV/master/odd_pys/i/img/closing0.jpg" alt="image description"></p>
  
  <p>So my question is what would the best approach be to accurately find the canvas shape in all the photos using python ?</p>