接着写这段代码取出结构体中最近的坐标点=struct ObjectInfo { std::string className; int xCoord; int yCoord;};std::vector<ObjectInfo> objects;for (size_t j = 0; j < res.size(); j++) { cv::Rect r = get_rect(img, res[j].bbox); cv::rectangle(img, r, cv::Scalar(0x27, 0xC1, 0x36), 2); cv::putText(img, std::to_string(r.x) + "," + std::to_string(r.y), cv::Point(r.x, r.y - 15), cv::FONT_HERSHEY_PLAIN, 1.2, cv::Scalar(0xFF, 0xFF, 0xFF), 2); ObjectInfo obj; obj.className = res[j].class_name; obj.xCoord = r.x; obj.yCoord = r.y; objects.push_back(obj);}
时间: 2024-03-05 17:48:02 浏览: 55
好的,下面是代码实现:
```c++
struct ObjectInfo {
std::string className;
int xCoord;
int yCoord;
};
std::vector<ObjectInfo> objects;
// 定义目标点坐标(假设为(100, 100))
int target_x = 100;
int target_y = 100;
// 定义最近的坐标点变量
ObjectInfo nearest_obj;
int min_distance = std::numeric_limits<int>::max();
for (size_t i = 0; i < objects.size(); i++) {
ObjectInfo obj = objects[i];
int distance = sqrt(pow(obj.xCoord - target_x, 2) + pow(obj.yCoord - target_y, 2));
if (distance < min_distance) {
nearest_obj = obj;
min_distance = distance;
}
}
// 最近的坐标点为 nearest_obj
```
上面的代码中,我们定义了一个目标点坐标和一个最近的坐标点变量,然后遍历了vector容器中所有的ObjectInfo结构体,计算每个结构体的坐标与目标点的距离,找到距离最近的那个结构体,并将其坐标信息赋值给最近的坐标点变量nearest_obj。最后,nearest_obj就是我们要找的最近的坐标点。
阅读全文