From 4229bf35e0dc55399c75ba3c70422f1c1a6b7c76 Mon Sep 17 00:00:00 2001 From: Adrian Rosebrock Date: Mon, 14 Mar 2016 12:13:04 -0400 Subject: [PATCH] Updated `order_points` to make it more robust --- MANIFEST | 2 ++ README.md | 2 +- imutils/perspective.py | 43 +++++++++++++++++++++++------------------- setup.py | 2 +- 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/MANIFEST b/MANIFEST index 8b7a659..0276c4f 100644 --- a/MANIFEST +++ b/MANIFEST @@ -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 diff --git a/README.md b/README.md index c29b321..9ecce03 100644 --- a/README.md +++ b/README.md @@ -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:
$ pip install imutils
diff --git a/imutils/perspective.py b/imutils/perspective.py index f2f1322..48e1890 100644 --- a/imutils/perspective.py +++ b/imutils/perspective.py @@ -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 @@ -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 \ No newline at end of file diff --git a/setup.py b/setup.py index 315f6ee..5712282 100644 --- a/setup.py +++ b/setup.py @@ -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='adrian@pyimagesearch.com',