加速这一段代码例程#include <thread> #include <mutex> // 用于保护m_vpdEdgePoints和m_vdEdgeGradient的锁 std::mutex g_mutex; void process_edges(const cv::Mat& RoiMat, const std::vectorcv::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::vectorcv::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_guardstd::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); } } } int main() { int m = m_vpdEquinoxPoints.size(); const int num_threads = 10; std::vectorstd::thread threads(num_threads); std::vectorstd::vectorcv::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()); } return 0; }
时间: 2024-01-21 11:18:04 浏览: 61
m5310开发板OneNET Demo例程.rar_M5310 DEMO_ONENET例程_holdtz9_m5310-a连接O
可以尝试使用 OpenMP 或者 TBB 等并行编程库来进行加速。具体实现方式如下:
1. 使用 OpenMP
在循环中添加 `#pragma omp parallel for` 来开启并行化,代码如下:
```cpp
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;
#pragma omp parallel for
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);
}
}
}
int main() {
int m = m_vpdEquinoxPoints.size();
const int num_threads = 10;
std::vector<std::vector<cv::Point2d>> edge_points(num_threads);
std::vector<std::vector<double>> edge_gradients(num_threads);
#pragma omp parallel num_threads(num_threads)
{
int tid = omp_get_thread_num();
int start_idx = tid * m / num_threads;
int end_idx = (tid + 1) * m / num_threads;
process_edges(RoiMat, m_vpdEquinoxPoints, m_dMeasureLength, m_dMeasureHeight, m_dSigma, m_nThresholdCircle, m_nTranslationCircle, m_vdMeasureAngle, edge_points[tid], edge_gradients[tid], start_idx, end_idx, Extract1DEdgeCircle);
}
// 合并结果
for (int i = 0; i < num_threads; i++) {
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());
}
return 0;
}
```
2. 使用 TBB
在循环中使用 `tbb::parallel_for` 来开启并行化,代码如下:
```cpp
#include <tbb/parallel_for.h>
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;
tbb::parallel_for(start_idx, end_idx, [&](int 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);
}
});
}
int main() {
int m = m_vpdEquinoxPoints.size();
const int num_threads = 10;
std::vector<std::vector<cv::Point2d>> edge_points(num_threads);
std::vector<std::vector<double>> edge_gradients(num_threads);
tbb::parallel_for(0, num_threads, [&](int tid) {
int start_idx = tid * m / num_threads;
int end_idx = (tid + 1) * m / num_threads;
process_edges(RoiMat, m_vpdEquinoxPoints, m_dMeasureLength, m_dMeasureHeight, m_dSigma, m_nThresholdCircle, m_nTranslationCircle, m_vdMeasureAngle, edge_points[tid], edge_gradients[tid], start_idx, end_idx, Extract1DEdgeCircle);
});
// 合并结果
for (int i = 0; i < num_threads; i++) {
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());
}
return 0;
}
```
需要注意的是,在使用锁时,应该使用 `std::lock_guard<std::mutex>` 或者 `std::unique_lock<std::mutex>` 来保护临界区域。
阅读全文