在 OpenCV 中,可以使用 boundingRect 函数来确定一个矩形的坐标。该函数可以接受一个轮廓作为输入,然后返回一个包围该轮廓的最小矩形,也就是 bounding rectangle。\n\n以下是一个示例代码,展示了如何使用 boundingRect 函数来确定一个矩形的坐标:\n\npython\nimport cv2\n\n# 定义一个轮廓\ncontour = [(10, 10), (10, 50), (50, 50), (50, 10)]\n\n# 使用 boundingRect 函数确定矩形的坐标\nx, y, w, h = cv2.boundingRect(contour)\n\n# 打印矩形的坐标\nprint("矩形的坐标:")\nprint("左上角坐标:({}, {})".format(x, y))\nprint("宽度:{}".format(w))\nprint("高度:{}".format(h))\n\n\n在上述示例中,我们定义了一个轮廓 contour,然后使用 boundingRect 函数确定该轮廓的最小矩形。函数返回的结果是一个四元组,分别表示矩形的左上角坐标(x, y)和宽度 w 以及高度 h。最后,我们打印出矩形的坐标。\n\n注意:boundingRect 函数只能用于凸轮廓,如果轮廓是非凸的,可以使用 approxPolyDP 函数对轮廓进行近似处理,然后再使用 boundingRect 函数确定矩形的坐标。

OpenCV 中如何获取矩形坐标:boundingRect 函数详解

原文地址: https://www.cveoy.top/t/topic/psBk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录