Reputation: 21622
Why this code produce (1, 1, 1, 1)
result, as I understand cv2.boundingRect
return rect in format x,y,w,h
, but why w,h
is not zero in this case?
import cv2
import numpy as np
cv2.__version__
'4.4.0'
np.__version__
'1.19.2'
a = np.ones((8,2))
x, y, w, h = cv2.boundingRect(a.astype(np.float32))
There is no much information in the bindings docs:
def boundingRect(array): # real signature unknown; restored from __doc__
"""
boundingRect(array) -> retval
. @brief Calculates the up-right bounding rectangle of a point set or non-zero pixels of gray-scale image.
.
. The function calculates and returns the minimal up-right bounding rectangle for the specified point set or
. non-zero pixels of gray-scale image.
.
. @param array Input gray-scale image or 2D point set, stored in std::vector or Mat.
"""
pass
Update:
Seems in C++ it returned like return Rect(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1);
Upvotes: 1
Views: 1960
Reputation: 53081
Not in the correct format. One way is to compute the points in Python/OpenCV.
import cv2
import numpy as np
a = np.ones((8,2))
points = np.column_stack(np.where(a.transpose() > 0))
x, y, w, h = cv2.boundingRect(points)
print(x,y,w,h)
Returns:
0 0 2 8
Upvotes: 3