Home > OS >  OpenCV - minAreaRect // points is not a numerical tuple
OpenCV - minAreaRect // points is not a numerical tuple

Time:04-25

The error points is not a numerical tuple is being output.

# Converting image to a binary image
# ( black and white only image).
blur = cv2.GaussianBlur(img,(5,5),0)
_, threshold = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY cv2.THRESH_OTSU)

contours, hierarchy = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

print(contours)

minArea = cv2.minAreaRect(contours)

The contours are:

(array([[[747, 305]],

       [[746, 306]],

       [[745, 306]],

       [[744, 307]],

       [[743, 308]],

       [[743, 309]],

       [[744, 310]],

       [[744, 311]],

       [[744, 312]],

       [[744, 313]],

       [[757, 306]],

       [[756, 306]],

       [[755, 306]],

       [[754, 306]],

       [[753, 306]],

       [[752, 305]],

       [[751, 305]],

       [[750, 305]],

       [[749, 305]],

       [[748, 305]]], dtype=int32),)

Overload resolution failed:

  • points is not a numerical tuple
  • Expected Ptr<cv::UMat> for argument 'points'

Is there anything obvious I'm doing wrong?

CodePudding user response:

cv2.findContours returns a list of contours. Each contour is a list of points. Therefore contours returned by it is not a list of points, but rather a list of lists of points.

cv2.minAreaRect on the other hand requires as input a single list of points, and therefore you get an error when you feed it with contours.

You can solve it by flattening the contours into a single list of points, like this:

contours_flat = np.vstack(contours).squeeze()
minArea = cv2.minAreaRect(contours_flat)

Altenatively you can get the minAreaRect for each contour in contours list by using (idx is the 0 based index of the contour):

minArea = cv2.minAreaRect(contours[idx])

You can see here more about contours: Contours, cv::findContours

And about minAreaRect: cv::minAreaRect

  • Related