Home > front end >  OpenCV, trying to get maximum and minium of multiple contours
OpenCV, trying to get maximum and minium of multiple contours

Time:05-24

So I am trying to get Max top and lowest points in multiple contours. It works when I want to find a the top and bottompoint for the biggest contour:

c = max(contours, key=cv.contourArea)
extTop = tuple(contours[-1][contours[-1][:, :, 1].argmin()][0])
extBot = tuple(contours[-1][contours[-1][:, :, 1].argmax()][0])

Now I try to get the top and bottom point of all the contours together but it doesnt work:

extTop = tuple(contours[contours[:, :, 1].argmin()][0])
extBot = tuple(contours[contours[:, :, 1].argmax()][0])

it gives:

extTop = tuple(contours[contours[:, :, 1].argmin()][0])

TypeError: tuple indices must be integers or slices, not tuple

Anyone knows a solution? I am prety new to coding so it might be a stupid question, but I have been trying for more than a day now.

Edit: tried this first, also didnt work:

c = max(contours, key=cv.contourArea)
extTop = tuple(contours[-1][contours[-1][:, :, 1].argmin()][0])
extBot = tuple(contours[-1][contours[-1][:, :, 1].argmax()][0])

CodePudding user response:

In order to get the top and bottom point of each contour, you need to iterate through the tuple containing all the contours.

In the following, we pick one contour c from the collection of contours. Draw the top and bottom most point:

for c in contours:
  top_point = tuple(c[c[:,:,1].argmin()][0])
  bottom_point = tuple(c[c[:,:,1].argmax()][0])
  img = cv2.circle(img, top_point, 6, (255, 255, 0), -1)
  img = cv2.circle(img, bottom_point, 6, (0, 255, 255), -1)

CodePudding user response:

Edit: Change this:

extTop = tuple(contours[-1][contours[-1][:, :, 1].argmin()][0])
extBot = tuple(contours[-1][contours[-1][:, :, 1].argmax()][0])
     

to:

extTop = tuple(c[c[:, :, 1].argmin()][0])
extBot = tuple(c[c[:, :, 1].argmax()][0])
  • Related