C++ OpenCV 的 cv2.boundingRect() 函数是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42453605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:29:53  来源:igfitidea点击:

How does cv2.boundingRect() function of OpenCV work?

c++python-2.7opencvopencv3.0opencv-contour

提问by vashish doolooa

I need the explanation on boundingRect of OpenCV. I have implemented it, it works great. Is there any reference where this function is fully explained please?

我需要关于 OpenCV 的 boundingRect 的解释。我已经实施了,效果很好。请问这个功能有什么参考资料吗?

回答by Jeru Luke

The cv2.boundingRect()function of OpenCV is used to draw an approximate rectangle around the binary image. This function is used mainly to highlight the region of interest after obtaining contours from an image.

cv2.boundingRect()OpenCV的函数用于在二值图像周围绘制一个近似矩形。此功能主要用于在从图像中获取轮廓后突出显示感兴趣的区域。

As per THE DOCUMENTAIONthere are two types of bounding rectangles:

根据文档,有两种类型的边界矩形:

  1. Straight Bounding Rectangle
  1. 直边界矩形

Here a simple rectangle is drawn around the contour (ROI). As you can see in the documentation, a green rectangle is drawn around the ROI. Corresponding rectangle coordinates are obtained such that a rectangle completely encloses the contour.

这里在轮廓 (ROI) 周围绘制了一个简单的矩形。正如您在文档中看到的,在 ROI 周围绘制了一个绿色矩形。获得相应的矩形坐标,使得矩形完全包围轮廓。

  1. Rotated Rectangle
    • In this case cv2.minAreaRect()function is used to highlight the minimum rectangular area enclosing a contour.
    • cv2.boxPoints()obtains the 4 corner points of the obtained rectangle.
    • np.int0()is done to convert the corrdinates from floatto integerformat.
    • These points are then used to draw the rectangle. This is depicted by the red rectangle in the documentation.
  1. 旋转矩形
    • 在这种情况下,cv2.minAreaRect()函数用于突出包围轮廓的最小矩形区域。
    • cv2.boxPoints()得到得到的矩形的4个角点。
    • np.int0()完成将 corrdinas 从 转换floatinteger格式。
    • 然后使用这些点绘制矩形。这由文档中的红色矩形表示。