修改代码,获取距离最近的两个首尾点,且记录这两个点对应的轮廓标号,将这两个点对应的轮廓连接起来,然后跳过这两个首尾点,去寻找下一个距离最近的首尾点,重复上面的步骤,直到遍历完所有轮廓 double minDist = DBL_MAX; Point2f p1, p2; for (int i = 0; i < Contours_points.size(); i++) { for (int j = i + 1; j < Contours_points.size(); j++) { double dist1 = norm(Contours_points[i].front() - Contours_points[j].front()); double dist2 = norm(Contours_points[i].front() - Contours_points[j].back()); double dist3 = norm(Contours_points[i].back() - Contours_points[j].front()); double dist4 = norm(Contours_points[i].back() - Contours_points[j].back()); if (dist1 < minDist) { minDist = dist1; p1 = Contours_points[i].front(); p2 = Contours_points[j].front(); } if (dist2 < minDist) { minDist = dist2; p1 = Contours_points[i].front(); p2 = Contours_points[j].back(); } if (dist3 < minDist) { minDist = dist3; p1 = Contours_points[i].back(); p2 = Contours_points[j].front(); } if (dist4 < minDist) { minDist = dist4; p1 = Contours_points[i].back(); p2 = Contours_points[j].back(); } } }
时间: 2024-01-20 14:03:02 浏览: 60
我了解您的问题。您的代码片段中,您需要获取距离最近的两个首尾点,记录这两个点对应的轮廓标号,将这两个点对应的轮廓连接起来,然后跳过这两个首尾点,去寻找下一个距离最近的首尾点,重复上面的步骤,直到遍历完所有轮廓。在这里,您可以使用以下代码来实现这个功能:
```cpp
double min_dist = DBL_MAX;
int min_start_idx, min_end_idx;
// 遍历所有轮廓,找到距离最近的首尾点
for (int i = 0; i < Contours_points.size(); i++) {
for (int j = i + 1; j < Contours_points.size(); j++) {
double dist1 = norm(Contours_points[i].front() - Contours_points[j].front());
double dist2 = norm(Contours_points[i].front() - Contours_points[j].back());
double dist3 = norm(Contours_points[i].back() - Contours_points[j].front());
double dist4 = norm(Contours_points[i].back() - Contours_points[j].back());
// 如果距离最近,则记录对应的首尾点和轮廓标号
if (dist1 < min_dist) {
min_dist = dist1;
min_start_idx = i;
min_end_idx = j;
}
if (dist2 < min_dist) {
min_dist = dist2;
min_start_idx = i;
min_end_idx = j;
}
if (dist3 < min_dist) {
min_dist = dist3;
min_start_idx = i;
min_end_idx = j;
}
if (dist4 < min_dist) {
min_dist = dist4;
min_start_idx = i;
min_end_idx = j;
}
}
}
// 将距离最近的首尾点对应的轮廓连接起来
vector<Point> merged_contour;
merged_contour.insert(merged_contour.end(), Contours_points[min_start_idx].begin(), Contours_points[min_start_idx].end());
merged_contour.insert(merged_contour.end(), Contours_points[min_end_idx].rbegin(), Contours_points[min_end_idx].rend());
// 将已连接的轮廓从Contours_points中去除
Contours_points.erase(Contours_points.begin() + min_end_idx);
Contours_points.erase(Contours_points.begin() + min_start_idx);
// 重复上述步骤,直到遍历完所有轮廓
while (Contours_points.size() > 0) {
double min_dist = DBL_MAX;
int min_start_idx, min_end_idx;
// 遍历剩下的轮廓,找到距离最近的首尾点
for (int i = 0; i < Contours_points.size(); i++) {
for (int j = i + 1; j < Contours_points.size(); j++) {
double dist1 = norm(Contours_points[i].front() - Contours_points[j].front());
double dist2 = norm(Contours_points[i].front() - Contours_points[j].back());
double dist3 = norm(Contours_points[i].back() - Contours_points[j].front());
double dist4 = norm(Contours_points[i].back() - Contours_points[j].back());
// 如果距离最近,则记录对应的首尾点和轮廓标号
if (dist1 < min_dist) {
min_dist = dist1;
min_start_idx = i;
min_end_idx = j;
}
if (dist2 < min_dist) {
min_dist = dist2;
min_start_idx = i;
min_end_idx = j;
}
if (dist3 < min_dist) {
min_dist = dist3;
min_start_idx = i;
min_end_idx = j;
}
if (dist4 < min_dist) {
min_dist = dist4;
min_start_idx = i;
min_end_idx = j;
}
}
}
// 将距离最近的首尾点对应的轮廓连接起来
vector<Point> next_merged_contour;
next_merged_contour.insert(next_merged_contour.end(), Contours_points[min_start_idx].begin(), Contours_points[min_start_idx].end());
next_merged_contour.insert(next_merged_contour.end(), Contours_points[min_end_idx].rbegin(), Contours_points[min_end_idx].rend());
// 将已连接的轮廓从Contours_points中去除
Contours_points.erase(Contours_points.begin() + min_end_idx);
Contours_points.erase(Contours_points.begin() + min_start_idx);
// 将新连接的轮廓添加到merged_contour中
merged_contour.insert(merged_contour.end(), next_merged_contour.begin(), next_merged_contour.end());
}
// merged_contour为连接后的轮廓
```
请注意,这里的`Contours_points`是所有轮廓的点集,`merged_contour`是连接后的轮廓。
阅读全文