gpt4 book ai didi

c++ - OpenCV:如何获取每个轮廓的边界框?

转载 作者:搜寻专家 更新时间:2023-10-31 00:30:58 33 4
gpt4 key购买 nike

** ~ 更新了 ~ **你好,我有一个源文件,我把它转换成下面的图片,我的程序里有一个轮廓

void find_contour(int, void*, Mat _mat)
{
Mat edge_detect_canny;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(_mat, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
Mat drawing = Mat::zeros(_mat.size(), CV_8UC3);
for (int i = 0; i < contours.size(); i++)
{
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
drawContours(drawing, contours, i, color, 1, 8, hierarchy, 1, Point());
}
IMshow(drawing, "draw-Res", 0);
imwrite("c:\\draw.bmp", drawing);
int cs = contours.size();
cout << cs << "contour.size" << endl;
}

效果很好,但我想添加边界框,找到每个轮廓的边界框以选择我的所有对象。我该怎么办?

最佳答案

您可以获得每个轮廓的边界框:

Rect box = boundingRect(contours[i]); 

你可以把它画成:

rectangle(drawing, box, color);

所以只需在 for 循环中添加这两行:

Rect box = boundingRect(contours[i]); 
rectangle(drawing, box, color);

关于c++ - OpenCV:如何获取每个轮廓的边界框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35204553/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com