Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

minAreaRect internal Error

I am trying to run the following code but an error arise when I call the minAreaRect but it goes well through boundingRect. I am confused because these two functions take the same input. The image is in this link: img

#!/usr/bin/env python

import numpy as np
import cv2

img = cv2.imread('test.png', cv2.IMREAD_COLOR)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# define range of blue color in HSV
gimp_lower_blue = np.array([210, 60, 60])
gimp_upper_blue = np.array([230, 80, 80])

# https://stackoverflow.com/questions/10948589/choosing-the-correct-upper-and-lower-hsv-boundaries-for-color-detection-withcv
gimp_to_opencv_hsv = np.array([180.0/360, 255.0/100, 255.0/100])
lower_blue = np.multiply(gimp_lower_blue, gimp_to_opencv_hsv)
upper_blue = np.multiply(gimp_upper_blue, gimp_to_opencv_hsv)

# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower_blue, upper_blue)
contours = cv2.findContours(
    mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
x, y, w, h = cv2.boundingRect(contours[0])
cv2.rectangle(img, (x-3, y-3), (x+w+3, y+h+3), (0, 255, 0), 5)
cv2.imshow('image', img)
cv2.waitKey()

rect = cv2.minAreaRect(contours[0])
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(img, [box], 0, (0, 0, 255), 2)
cv2.imshow('image', img)
cv2.waitKey(0)

The first line of the error is: OpenCV Error: Assertion failed (total >= 0 && (depth == CV_32F || depth == CV_32S)) in convexHull, file /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/imgproc/src/convhull.cpp, line 136

minAreaRect internal Error

I am trying to run the following code but an error arise when I call the minAreaRect but it goes well through boundingRect. I am confused because these two functions take the same input. The image is in this link: img

#!/usr/bin/env python

import numpy as np
import cv2

img = cv2.imread('test.png', cv2.IMREAD_COLOR)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# define range of blue color in HSV
gimp_lower_blue = np.array([210, 60, 60])
gimp_upper_blue = np.array([230, 80, 80])

# https://stackoverflow.com/questions/10948589/choosing-the-correct-upper-and-lower-hsv-boundaries-for-color-detection-withcv
gimp_to_opencv_hsv = np.array([180.0/360, 255.0/100, 255.0/100])
lower_blue = np.multiply(gimp_lower_blue, gimp_to_opencv_hsv)
upper_blue = np.multiply(gimp_upper_blue, gimp_to_opencv_hsv)

# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower_blue, upper_blue)
contours = cv2.findContours(
    mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
x, y, w, h = cv2.boundingRect(contours[0])
cv2.rectangle(img, (x-3, y-3), (x+w+3, y+h+3), (0, 255, 0), 5)
cv2.imshow('image', img)
cv2.waitKey()

rect = cv2.minAreaRect(contours[0])
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(img, [box], 0, (0, 0, 255), 2)
cv2.imshow('image', img)
cv2.waitKey(0)

The first line of the error is: OpenCV Error: Assertion failed (total >= 0 && (depth == CV_32F || depth == CV_32S)) in convexHull, file /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/imgproc/src/convhull.cpp, line 136