Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

int 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 = arr[:,0].argmin()
print(leftmost, arr[leftmost])
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]]

int 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 leftmost_id = arr[:,0].argmin()
print(leftmost, arr[leftmost])
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]]

int 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]]