boundingRect()函数解释:矩形边框(Bounding Rectangle)是说,用一个最小的矩形,把找到的形状包起来。还有一个带旋转的矩形,面积会更小,效果见下图
文章来源:https://www.toymoban.com/news/detail-736222.html
文章来源地址https://www.toymoban.com/news/detail-736222.html
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
- 输入:是一个轮廓点集合,也就是它的参数,可以通过cv2.findContours获取。
- 返回四个值,分别是x,y,w,h;(x,y)是矩阵左上点的坐标,w,h分别是是矩阵的宽和高。目标中心,为:(x+w//2,y+h//2)
# 读取图像
bgr_img = cv2.imread("./img.jpg")
copy_img = bgr_img.copy()
# bgr图像转化为灰度图像
gray_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2GRAY)
# 灰度图像转化为二值图像
th, binary = cv2.threshold(gray_img, 0, 255, cv2.THRESH_OTSU)
# 轮廓函数返回值为两个,第一个是轮廓contours
contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 绘制轮廓
cv2.drawContours(copy_img, contours, -1, (0, 0, 255), 3)
bounding_boxes = [cv2.boundingRect(cnt) for cnt in contours]
for bbox in bounding_boxes:
'''寻找x,y坐标以及宽度和高度'''
[x , y, w, h] = bbox
cv2.rectangle(bgr_img, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow("name", bgr_img)
cv2.waitKey(0)
到了这里,关于opencv-python中 boundingRect()函数解析的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!