Skip to content

Commit

Permalink
Updated order_points to make it more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
jrosebr1 committed Mar 14, 2016
1 parent faef943 commit 4229bf3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
2 changes: 2 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ imutils/meta.py
imutils/object_detection.py
imutils/paths.py
imutils/perspective.py
imutils/io/__init__.py
imutils/io/tempfile.py
imutils/video/__init__.py
imutils/video/fps.py
imutils/video/pivideostream.py
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ For more information, along with a detailed code review check out the following
- [http://www.pyimagesearch.com/2015/08/10/checking-your-opencv-version-using-python/](http://www.pyimagesearch.com/2015/08/10/checking-your-opencv-version-using-python/)

## Installation
Provided you already have NumPy, Matplotlib, and OpenCV already installed, the `imutils` package is completely `pip`-installable:
Provided you already have NumPy, SciPy, Matplotlib, and OpenCV already installed, the `imutils` package is completely `pip`-installable:

<pre>$ pip install imutils</pre>

Expand Down
43 changes: 24 additions & 19 deletions imutils/perspective.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,36 @@
# website: http://www.pyimagesearch.com

# import the necessary packages
from scipy.spatial import distance as dist
import numpy as np
import cv2

def order_points(pts):
# initialize a list of coordinates that will be ordered
# such that the first entry in the list is the top-left,
# the second entry is the top-right, the third is the
# bottom-right, and the fourth is the bottom-left
rect = np.zeros((4, 2), dtype="float32")
# sort the points based on their x-coordinates
xSorted = pts[np.argsort(pts[:, 0]), :]

# the top-left point will have the smallest sum, whereas
# the bottom-right point will have the largest sum
s = pts.sum(axis=1)
rect[0] = pts[np.argmin(s)]
rect[2] = pts[np.argmax(s)]
# grab the left-most and right-most points from the sorted
# x-roodinate points
leftMost = xSorted[:2, :]
rightMost = xSorted[2:, :]

# now, compute the difference between the points, the
# top-right point will have the smallest difference,
# whereas the bottom-left will have the largest difference
diff = np.diff(pts, axis=1)
rect[1] = pts[np.argmin(diff)]
rect[3] = pts[np.argmax(diff)]
# now, sort the left-most coordinates according to their
# y-coordinates so we can grab the top-left and bottom-left
# points, respectively
leftMost = leftMost[np.argsort(leftMost[:, 1]), :]
(tl, bl) = leftMost

# return the ordered coordinates
return rect
# now that we have the top-left coordinate, use it as an
# anchor to calculate the Euclidean distance between the
# top-left and right-most points; by the Pythagorean
# theorem, the point with the largest distance will be
# our bottom-right point
D = dist.cdist(tl[np.newaxis], rightMost, "euclidean")[0]
(br, tr) = rightMost[np.argsort(D)[::-1], :]

# return the coordinates in top-left, top-right,
# bottom-right, and bottom-left order
return np.array([tl, tr, br, bl], dtype="float32")

def four_point_transform(image, pts):
# obtain a consistent order of the points and unpack them
Expand Down Expand Up @@ -64,4 +69,4 @@ def four_point_transform(image, pts):
warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))

# return the warped image
return warped
return warped
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup(
name='imutils',
packages=['imutils', 'imutils.video', 'imutils.io'],
version='0.3.5',
version='0.3.6',
description='A series of convenience functions to make basic image processing functions such as translation, rotation, resizing, skeletonization, displaying Matplotlib images, sorting contours, detecting edges, and much more easier with OpenCV and both Python 2.7 and Python 3.',
author='Adrian Rosebrock',
author_email='[email protected]',
Expand Down

0 comments on commit 4229bf3

Please sign in to comment.