PyImageSearch / imutils

A series of convenience functions to make basic image processing operations such as translation, rotation, resizing, skeletonization, and displaying Matplotlib images easier with OpenCV and Python.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

order_points

tdcwilliams opened this issue · comments

Hi, I was trying to understand your imutils.perspective.order_points code while going through this tutorial

and I think there is a bug here:
https://github.com/jrosebr1/imutils/blob/df65a77087548b5952431cc8006d66bcca7b1abc/imutils/perspective.py#L22

When I changed this to (bl, tl) = leftMost I got clockwise ordering as expected. The current code gives me anticlockwise ordering.

Regards, Tim

Sometimes you get wrong co-ordinates according to your use-case so maybe it's a use-case specific. Changing the source code may not be a good idea because it works for most of the cases(I think) but not always. Check what I got (Issue#221)

It's better to understand the position points(maybe draw it on a piece of paper) and try to follow the code alongside and see which condition is not working in your case.

Hi @haseeb33,
here is a notebook with some examples where imutils.order_points fails order_points in my notebook (anti-clockwise from start = red point). When the tl,bl = line is changed it works fine (as in order_points_new)

order_points.ipynb.zip

I was getting upside down/sidewise images a lot when using imutils.order_points in the tutorials

Hi @tdcwilliams and @haseeb33!
I guess this function works only for simple polygon - that does not intersect itself and has no holes.

https://en.wikipedia.org/wiki/Simple_polygon

Hi @shkorinova!
I have mentioned a usecase in another issue(#221).
The points mentioned in that issue can be viewed below(and it's a simple polygon) so I think this function still fails at some points.

Screen Shot 2020-09-24 at 10 36 39 AM

@haseeb33 Maybe I'm wrong but I used your example from code snippet (#221) and obtained smth like:
example

The code for plotting is:

img = np.zeros((1800,1800,3), np.uint8)
pts = np.array([(1604,934), (1627,1095), (1693,797), (1693,923)]).reshape((-1,1,2))
cv2.polylines(img,[pts],True,(0,255,255), 5)
plt.figure(figsize=(8,8))
plt.imshow(img)
plt.show()

The order of points in #221 description and code snippet is not similar. In the description you use clockwise order of the simple polygon and in the code snippet the order of points are different.

@shkorinova The function order_points should sort the points and return [tl, tr, br, bl] irrespective of the order of the points in the input list.

So the order_points always returns the same ([[1604., 934.], [1693., 923.], [1693., 797.], [1627., 1095.]])(which is wrong) for all:

pts = np.array([(1604,934), (1627,1095), (1693,797), (1693,923)]) #used in issue #221     
pts = np.array([(1604,934), (1627,1095), (1693,797), (1693,923)]) #pts order by you
pts = np.array([(1693,923), (1693,797), (1604,934), (1627,1095)]) #another example

@shkorinova For simplicity, let me modify my example:

pts = np.array([(1604,934), (1627,1095), (1693,923), (1693,797)]) 
print(perspective.order_points(pts))
pts = np.array([(1693,923), (1693,797), (1604,934), (1627,1095)])
print(perspective.order_points(pts))

Both print statements print(not the correct order):
[[1604. 934.], [1693. 923.], [1693. 797.], [1627. 1095.]]

Whereas the code block (Shows the below attached image for both points):

img = np.zeros((1800,1800,3), np.uint8)
pts = pts.reshape((-1,1,2))
cv2.polylines(img,[pts],True,(0,255,255), 5)
plt.figure(figsize=(8,8))
plt.imshow(img)
plt.show()

Screen Shot 2020-09-28 at 8 32 36 AM