加速这一段代码例程#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::vector<std::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 20:17:46 浏览: 137
可以考虑使用OpenMP来并行化这段代码,OpenMP是一个支持多平台的共享内存并行编程API。可以在循环中使用#pragma omp parallel for指令来并行化循环,具体如下:
```
#include <omp.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, Extract1DEdgeCircle Extract1DEdgeCircle) {
std::vector<Edge1D_Result> edges;
#pragma omp parallel for
for (int i = 0; i < m_vpdEquinoxPoints.size(); 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
#pragma omp critical
{
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();
std::vector<cv::Point2d> m_vpdEdgePoints;
std::vector<double> m_vdEdgeGradient;
Extract1DEdgeCircle Extract1DEdgeCircle;
#pragma omp parallel num_threads(10)
{
int tid = omp_get_thread_num();
int start_idx = tid * m / 10;
int end_idx = (tid + 1) * m / 10;
std::vector<cv::Point2d> edge_points;
std::vector<double> edge_gradients;
process_edges(RoiMat, m_vpdEquinoxPoints, m_dMeasureLength, m_dMeasureHeight, m_dSigma, m_nThresholdCircle, m_nTranslationCircle, m_vdMeasureAngle, edge_points, edge_gradients, Extract1DEdgeCircle);
// 合并结果
#pragma omp critical
{
m_vpdEdgePoints.insert(m_vpdEdgePoints.end(), edge_points.begin(), edge_points.end());
m_vdEdgeGradient.insert(m_vdEdgeGradient.end(), edge_gradients.begin(), edge_gradients.end());
}
}
return 0;
}
```
这里使用了#pragma omp parallel num_threads(10)指令来启动10个线程,每个线程处理m_vpdEquinoxPoints向量中的部分点。使用#pragma omp critical指令来保护m_vpdEdgePoints和m_vdEdgeGradient的写入操作,避免多线程竞争。
阅读全文