【发布时间】:2021-12-22 22:32:44
【问题描述】:
我尝试阅读文档,但无法理解其中的区别。 当目标是 3x3 正方形时,我期待相同的结果, 但我用 cv::boundingRect() 得到 3x3,用 cv::minAreaRect() 得到 2x2。 我正在使用 OpenCV 4.4。
这是一个示例代码。
char data[25] = {
0, 0, 0, 0, 0,
0, 255, 255, 255, 0,
0, 255, 255, 255, 0,
0, 255, 255, 255, 0,
0, 0, 0, 0, 0
};
cv::Mat image = cv::Mat(5, 5, CV_8U, data);
std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierarchy;
cv::findContours(image, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
cv::Rect boundingRect = cv::boundingRect(contours[0]);
std::cout << "[cv::boundingRect]" << std::endl;
std::cout << "w, h: " << boundingRect.size().width << " x " << boundingRect.size().height << std::endl;
cv::RotatedRect minAreaRect = cv::minAreaRect(contours[0]);
std::cout << "[cv::minAreaRect]" << std::endl;
std::cout << "w, h: " << minAreaRect.size.width << " x " << minAreaRect.size.height << std::endl;
输出是:
[cv::boundingRect]
w, h: 3 x 3
[cv::minAreaRect]
w, h: 2 x 2
提前致谢。
【问题讨论】:
-
请提供重现问题的确切代码...minimal reproducible example
-
@ChristophRackwitz 我添加了示例代码,请看一下。