Ask Your Question
0

Custom contour extreme points detection?

asked 2017-10-17 02:11:45 -0600

Santhosh1 gravatar image

updated 2017-10-17 02:24:39 -0600

I'm still a beginner so please explain what's happening here. I know its my knowledge gap of python which is the problem here, this could even be asked on StackOverFlow.

I have created a custom contour using

contour_coordinates = np.empty(shape=[0,2])

Adding each point like

contour_coordinates = np.append(contour_coordinates, [[x,y]], axis=0)

Example points added

[[584 298]
 [592 298]
 [598 299]
 [605 301]
 [611 304]
 [616 308]
 [619 312]
 [622 316]]

When I try this command leftmost = tuple(contour_coordinates[:,:,0].argmin()) using this Contour Properties

I'm getting this error

leftmost = tuple(contour_coordinates[:,:,0].argmin())
IndexError: too many indices for array

I know its a python indexing problem but can't seem to be finding out what the issue is as this is how the OpenCV store the detected contour

Contour Stored by OpenCV

[[[ 729 1244]]
 [[ 729 1245]]
 [[ 729 1246]]
 [[ 729 1247]]
 [[ 729 1248]]
 [[ 728 1249]]
 [[ 728 1250]]
 [[ 728 1251]]
 [[ 727 1252]]]

Explain please?....

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-10-17 02:47:42 -0600

berak gravatar image

updated 2017-10-17 04:09:29 -0600

in the opencv tutorial, all the contours found in an image are used..

that is a 3d array, a list of contours, where each contour is a list of points, like this:

[[[ 729 1244]]
 [[ 729 1245]]
 ...        ]]]

your example only has a single contour, let's make it:

arr = np.array([[592, 298], [598, 299], [605, 301], [584, 298],
                [611, 304], [616, 308], [619, 312], [622, 316]])
print(arr.shape)
(8, 2)

now, arr[:,0] will be the "slice" of all x values in your contour, and argmin() will give you the index of the smallest value, so:

leftmost_id = arr[:,0].argmin()
print(leftmost_id, arr[leftmost_id])
3 [584 298]

note, that you can also use minAreaRect to get the "extreme points" of your contour:

ma = cv2.minAreaRect(arr);
bp = cv2.boxPoints(ma)
print (bp)

[[ 622.00006104  316.00003052]
 [ 584.00006104  298.00003052]
 [ 586.68780518  292.32583618]
 [ 624.68780518  310.32583618]]
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-10-17 02:11:45 -0600

Seen: 2,440 times

Last updated: Oct 17 '17