title: 4 point persective transform failure
body: <p>I've been trying to do a 4 point perspective transform in order to start doing some OCR.</p>
  
  <p>Starting with the following image I can detect the number plate</p>
  
  <p><a href="https://i.stack.imgur.com/R0phK.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/R0phK.jpg" alt="enter image description here"></a></p>
  
  <p>and crop it out with the green box being the bounding box and the red dots being the corners of the rectangle I want to square up.</p>
  
  <p><a href="https://i.stack.imgur.com/6LtYq.png" rel="noreferrer"><img src="https://i.stack.imgur.com/6LtYq.png" alt="enter image description here"></a></p>
  
  <p>This is the output of the transform.</p>
  
  <p><a href="https://i.stack.imgur.com/IxqBl.png" rel="noreferrer"><img src="https://i.stack.imgur.com/IxqBl.png" alt="enter image description here"></a></p>
  
  <p>At a first look it seams to have done the transform inside out (taking the parts either side rather than between the points).</p>
  
  <p>I'm using the imutils package to do the transform and working from <a href="http://www.pyimagesearch.com/2017/02/13/recognizing-digits-with-opencv-and-python/" rel="noreferrer">this</a> and <a href="http://www.pyimagesearch.com/2014/08/25/4-point-opencv-getperspective-transform-example/" rel="noreferrer">this</a> as a guide. I'm sure it's something relatively simple I'm missing.</p>
  
  <pre><code>#!/usr/bin/python
  import numpy as np
  import cv2
  import imutils
  from imutils import contours
  from imutils.perspective import four_point_transform
  
  img = cv2.imread("sample7-smaller.jpg")
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  blurred = cv2.bilateralFilter(gray,15,75,75)
  v = np.median(blurred)
  lower = int(max(0, (1.0 - 0.33) * v))
  upper = int(min(255, (1.0 + 0.33) * v))
  edged = cv2.Canny(blurred, lower, upper, 255)
  
  conts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
      cv2.CHAIN_APPROX_SIMPLE)
  conts = conts[0] if imutils.is_cv2() else conts[1]
  conts = sorted(conts, key=cv2.contourArea, reverse=True)
  
  for cnt in conts:
      approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
      if len(approx) == 4:
          x,y,w,h = cv2.boundingRect(cnt)
          cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
          for i in approx:
              cv2.circle(img,(i[0][0], i[0][1]),2,(0,0,255), thickness=4)
          warped = four_point_transform(img, approx.reshape(4,2))
          cv2.imshow("crop",img[y:y+h,x:x+w])
          cv2.imshow("warped", warped)
          cv2.waitKey(0)
  </code></pre>