在排序后,将坐标存储在vector结构体中并输出
时间: 2024-03-20 11:43:28 浏览: 28
好的,我来补充一下代码。在排序后,我们可以将每个连通域的左上角坐标、宽度、高度和质心坐标存储在一个自定义的结构体中,并将这些结构体存储在一个vector中。代码如下:
```
// 定义结构体
struct ComponentInfo {
cv::Point2d centroid;
cv::Rect boundingBox;
};
// 按照质心x坐标排序
std::vector<ComponentInfo> components(centroids.size());
std::vector<int> order(centroids.size());
std::iota(order.begin(), order.end(), 0);
std::sort(order.begin(), order.end(), [&](int i, int j) {
return centroids[i].x < centroids[j].x;
});
// 存储坐标信息
for (int i = 0; i < order.size(); ++i) {
int j = order[i];
components[i].centroid = centroids[j];
components[i].boundingBox = cv::Rect(stats(j, cv::CC_STAT_LEFT), stats(j, cv::CC_STAT_TOP),
stats(j, cv::CC_STAT_WIDTH), stats(j, cv::CC_STAT_HEIGHT));
}
// 输出坐标信息
for (int i = 0; i < components.size(); ++i) {
std::cout << "连通域" << i << ":";
std::cout << "左上角坐标(" << components[i].boundingBox.x << "," << components[i].boundingBox.y << ")";
std::cout << "宽度:" << components[i].boundingBox.width;
std::cout << "高度:" << components[i].boundingBox.height;
std::cout << "质心坐标(" << components[i].centroid.x << "," << components[i].centroid.y << ")" << std::endl;
}
```
这样就可以输出按照质心x坐标排序后的连通域的坐标信息了。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)