【问题标题】:cv2.boundingRect rect format documentationcv2.boundingRect rect 格式文档
【发布时间】:2021-07-19 16:34:22
【问题描述】:

为什么这段代码产生(1, 1, 1, 1)结果,据我所知cv2.boundingRectx,y,w,h格式返回rect,但为什么w,h在这种情况下不为零?

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))

绑定文档中没有太多信息:

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

更新:

在 C++ 中,它似乎返回 return Rect(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1);

【问题讨论】:

    标签: python opencv


    【解决方案1】:

    格式不正确。一种方法是在 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)
    

    返回:

    0 0 2 8
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 2017-07-16
    • 1970-01-01
    • 2021-05-07
    相关资源
    最近更新 更多