Reputation: 143
I got an error when trying to use the boundingRect()
function in opencv. What given is a list of points
lists = []
for match in enumerate(matches):
lists.append(kp2[match.trainIdx].pt)
x,y,w,h = cv2.boundingRect(lists)
TypeError: points is not a numpy array, neither a scalar
P/s: I want to draw a rectangle around the detected object in the image
Any help is appreciate
Edit
The lists before change to np array
[(328.0, 227.0), (372.0, 241.0), (366.0, 229.0)]
and after
[[ 328. 227.]
[ 372. 241.]
[ 366. 229.]]
Upvotes: 3
Views: 5774
Reputation: 75
The list of points needs to be passed as a numpy array. Therefore, first convert it, e.g. with
lists = np.array(lists, dtype=np.float32)
The failed assertion
'error: (-215) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function cv::pointSetBoundingRect'
is triggered if the created numpy array does not have the datatype float32
or int32
. Another reason would be if the passed array does not contain any points.
The parameter dtype=np.float32
in the array creation command ensures that the created array has the datatype float32
. In a 64bit environment, omitting this parameter will lead to the array being created with datatype float64
, which causes the assertion to fail.
After the conversion to a numpy array with datatype float32
, passing the result to cv.boundingRectangle
will work as expected.
Intentionally answering an old question after I was directed here by a search engine.
Upvotes: 2
Reputation: 31
I just had exactly the same issue. Didn't resolve it but here is the code that solved the problem:
points = ([1,1], [3,1], [3,3], [1,4])
xx, yy = zip(*points)
min_x = min(xx); min_y = min(yy); max_x = max(xx); max_y = max(yy)
bbox = [(min_x, min_y), (max_x, min_y), (max_x, max_y), (min_x, max_y)]
Upvotes: 3