加速这一段代码#include <thread> #include <mutex> // 用于保护m_vpdEdgePoints和m_vdEdgeGradient的锁 std::mutex g_mutex; void process_edges(const cv::Mat& RoiMat, const std::vector<cv::Point2d>& m_vpdEquinoxPoints, const double m_dMeasureLength, const double m_dMeasureHeight, const double m_dSigma, const int m_nThresholdCircle, const int m_nTranslationCircle, const std::vector<double>& m_vdMeasureAngle, std::vector<cv::Point2d>& m_vpdEdgePoints, std::vector<double>& m_vdEdgeGradient, int start_idx, int end_idx, Extract1DEdgeCircle Extract1DEdgeCircle) { std::vector<Edge1D_Result> edges; for (int i = start_idx; i < end_idx; i++) { edges = Extract1DEdgeCircle.Get1DEdge(RoiMat, m_vpdEquinoxPoints[i], m_dMeasureLength, m_dMeasureHeight,m_vdMeasureAngle[i], m_dSigma, m_nThresholdCircle, m_nTranslationCircle == 1 ? Translation::Poisitive : Translation::Negative, Selection::Strongest); // 使用锁保护m_vpdEdgePoints和m_vdEdgeGradient //std::lock_guard<std::mutex> lock(g_mutex); for (int j = 0; j < edges.size(); j++) { m_vpdEdgePoints.push_back(edges[j].m_pdEdgePoint); m_vdEdgeGradient.push_back(edges[j].m_dGradient); } } } const int num_threads = 10; std::vector<std::thread> threads(num_threads); std::vector<std::vector<cv::Point2d>> edge_points(num_threads); std::vector<std::vector<double>> edge_gradients(num_threads); for (int i = 0; i < num_threads; i++) { int start_idx = i * m / num_threads; int end_idx = (i + 1) * m / num_threads; threads[i] = std::thread(process_edges, std::ref(RoiMat), std::ref(m_vpdEquinoxPoints), m_dMeasureLength, m_dMeasureHeight, m_dSigma, m_nThresholdCircle, m_nTranslationCircle, std::ref(m_vdMeasureAngle), std::ref(edge_points[i]), std::ref(edge_gradients[i]), start_idx, end_idx, Extract1DEdgeCircle); } for (int i = 0; i < num_threads; i++) { threads[i].join(); // 合并结果 m_vpdEdgePoints.insert(m_vpdEdgePoints.end(), edge_points[i].begin(), edge_points[i].end()); m_vdEdgeGradient.insert(m_vdEdgeGradient.end(), edge_gradients[i].begin(), edge_gradients[i].end()); }
时间: 2024-01-21 08:17:19 浏览: 160
C++11 std::shared_ptr总结与使用示例代码详解
可以尝试使用并行 STL 算法来加速代码,例如 `std::for_each()` 或 `std::transform()`。这些算法可以利用多线程来并行化循环。具体来说,你可以将 `process_edges()` 函数中的循环替换为一个并行算法,例如:
```cpp
std::vector<std::vector<Edge1D_Result>> edges(num_threads);
std::for_each(std::execution::par, std::begin(threads), std::end(threads), [&](auto& thread) {
thread.join();
});
std::for_each(std::execution::par, std::begin(threads), std::end(threads), [&](auto& thread) {
const auto i = std::distance(std::begin(threads), std::addressof(thread));
int start_idx = i * m / num_threads;
int end_idx = (i + 1) * m / num_threads;
edges[i] = Extract1DEdgeCircle.Get1DEdge(RoiMat, m_vpdEquinoxPoints, m_dMeasureLength, m_dMeasureHeight, m_vdMeasureAngle, m_dSigma, m_nThresholdCircle, m_nTranslationCircle == 1 ? Translation::Poisitive : Translation::Negative, Selection::Strongest, start_idx, end_idx);
});
for (const auto& thread_edges : edges) {
for (const auto& edge : thread_edges) {
m_vpdEdgePoints.push_back(edge.m_pdEdgePoint);
m_vdEdgeGradient.push_back(edge.m_dGradient);
}
}
```
这里我们使用了 `std::for_each()` 并行地执行所有的线程。然后我们再次使用 `std::for_each()` 并行地执行每个线程中的循环,将结果存储在 `edges` 向量中。最后,我们遍历 `edges` 向量并将所有的边缘点和梯度值插入到输出向量中。
注意,这里使用了 C++17 中的并行 STL 算法,需要在编译时指定 `-std=c++17` 或更高的选项。如果你的编译器不支持 C++17,也可以使用 OpenMP 或其他多线程库来手动实现并行化循环。
阅读全文