title: How do scan an opened book with openCV and Python and take the page that is focused in?
body: <p>I need to scan an opened book, in fact it is an agenda; and apply to it a four point transform to crop it a save it but it only requires 4 points and it sometimes takes 6 or 5 points and draws an incomplete rectangle
  So it draws it and tries to close it... but it fails at applying the 4 point transform
  <a href="https://i.stack.imgur.com/z5iGe.jpg" rel="nofollow noreferrer">Filters Applied</a></p>
  
  <pre><code>import cv2
  import imutils
  
  
  def primer_filtro(archivo):
  image = cv2.imread("images/" + archivo)
  image = imutils.resize(image, height=800)
  
  # Hacer Gris y desenfoque Gaussiano
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  gray = cv2.GaussianBlur(gray, (3, 3), 0)
  
  # Definir bordes
  edged = cv2.Canny(gray, 50, 250)
  # Definir pixeles
  kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
  closed = cv2.morphologyEx(edged, cv2.MORPH_CLOSE, kernel)
  
  # Encontrar contorno
  (cnts, _) = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  
  # Dibujar contorno
  for c in cnts:
      peri = cv2.arcLength(c, True)
      approx = cv2.approxPolyDP(c, 0.02 * peri, True)
      if len(approx) == 6:
          cv2.drawContours(image, [approx], -5, (0, 0, 0), 60)
          cv2.imwrite("images/find_book.jpg", image)
          cv2.imshow("Final", image)
          cv2.waitKey(0)
  </code></pre>
  
  <hr>
  
  <pre><code>import cv2
  from skimage.filters import threshold_adaptive
  from pyimagesearch import imutils
  from pyimagesearch.transform import four_point_transform
  
  
  def segundo_filtro(archivo, original):
  original = cv2.imread("images/" + original)
  original = original.copy()
  while True:
      imagen = archivo
      image = cv2.imread("images/" + imagen)
      try:
          ratio = image.shape[0] / 500.0
          # original = image.copy()
          image = imutils.resize(image, height=800)
          break
      except:
          print "Intentando de nuevo"
  
  # Convertir imagenes en gris
  # gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  gray = image
  cv2.imshow("Immg", gray)
  cv2.waitKey(0)
  
  # Aplicar desenfoque Gaussiano
  gray = cv2.GaussianBlur(gray, (5, 5), 0)
  
  # Bordeado de imagen
  bordeado = cv2.Canny(gray, 50, 450)
  # bordeado = cv2.Canny(gray, 50, 250)
  cv2.imshow("Bordeada", bordeado)
  cv2.waitKey(0)
  
  # Encontrar los contornos tomando en cuenta los mas grandes
  (cnts, _) = cv2.findContours(bordeado.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5]
  # Buscar en el contorno
  for c in cnts:
      peri = cv2.arcLength(c, True)
      approx = cv2.approxPolyDP(c, 0.02 * peri, True)
      # Dibujar Contorno
      if len(approx) == 6:
          approx[4] = approx[0]
          approx[5] = approx[1]
          cv2.drawContours(image, [approx], -5, (0, 0, 0), 5)
      elif len(approx) == 4:
          print "Tiene {0} puntos".format(len(approx))
          cv2.drawContours(image, [approx], -5, (125, 0, 0), 5)
      cv2.imwrite("images/lforehead.jpg", image)
      # print "Este tiene {0} puntos".format(len(approx))
      cv2.imshow("Contorno", image)
      cv2.waitKey(0)
  
  try:
      frentear = four_point_transform(original, approx.reshape(4, 2) * ratio)
      frentear = cv2.cvtColor(frentear, cv2.COLOR_BGR2GRAY)
      frentear = threshold_adaptive(frentear, 251, offset=10)
      frentear = frentear.astype("uint8") * 255
      cv2.imwrite("images/escaneo.jpg", frentear)
      cv2.imshow("Frenteado", imutils.resize(frentear, height=650))
      cv2.waitKey(0)
      cv2.destroyAllWindows()
  except:
      exit()
  </code></pre>