ZBLOG

bool GuangShuDetectDll::BeamDiameter_energy(cv::Point2f Center, cv::Mat image, double ratio, double& beamdiameter_energy) { //正常 int sum = 0; int currentRadius = 0; int sum1 = cv::sum(image)[0]; // 计算灰度图像的像素总和 while (sum < sum...

在您的代码中,#pragma omp parallel for reduction(+:sum)的使用位置不正确。根据OpenMP的规范,它应该放在for循环之前,并且需要确保该变量(在这里是sum)是在适当的范围内定义并初始化。

下面是一些可能导致编译错误或运行时错误的常见原因及其解决方法:

  1. 包含头文件:确保你已经包含了OpenMP库所需的头文件。在代码顶部添加:

    1. #include <omp.h>
  2. 启用OpenMP:确保你的编译器启用了OpenMP。例如,在GCC中,你需要使用-fopenmp选项来编译:

    1. g++ -fopenmp your_code.cpp -o your_program
  3. 确定数据共享和私有性:要确保所有并行线程都可以访问和更新相应的数据。您可以通过将 currentRadius 声明为 private 来处理,并且 sum1 应该是一个只读值。

  4. 像素边界检查:请注意,虽然您注释掉了边界检查,但实际上,如果你没有进行有效的范围检查,会导致潜在的内存访问违规。这些检查应该保持激活。

以下是修正后的代码片段:

  1. #include <opencv2/opencv.hpp>
  2. #include <omp.h>
  3. bool GuangShuDetectDll::BeamDiameter_energy(cv::Point2f Center, cv::Mat image, double ratio, double& beamdiameter_energy)
  4. {
  5. //正常
  6. int sum = 0;
  7. int currentRadius = 0;
  8. int sum1 = cv::sum(image)[0]; // 计算灰度图像的像素总和
  9. while (sum < sum1 * ratio / 100)
  10. {
  11. // 遍历每个像素点
  12. sum = 0;
  13. #pragma omp parallel for reduction(+:sum)
  14. for (int i = Center.y - currentRadius; i <= Center.y + currentRadius; ++i)
  15. {
  16. if (i < 0 || i >= image.rows) continue; // 边界检查
  17. uchar* rowPtr = image.ptr<uchar>(i); // 获取当前行指针,减少内存访问次数
  18. double powIcenterX = pow(i - Center.y, 2);
  19. for (int j = Center.x - currentRadius; j <= Center.x + currentRadius; ++j)
  20. {
  21. if (j < 0 || j >= image.cols) continue; // 边界检查
  22. double distance = sqrt(powIcenterX + pow(j - Center.x, 2));
  23. if (distance <= currentRadius)
  24. {
  25. sum += rowPtr[j];
  26. }
  27. }
  28. }
  29. currentRadius += 6; // 将半径增加改为每次加6
  30. if (Center.y - currentRadius < 0 || Center.y + currentRadius >= image.rows ||
  31. Center.x - currentRadius < 0 || Center.x + currentRadius >= image.cols)
  32. {
  33. // 超出图像范围,结束循环
  34. break;
  35. }
  36. }
  37. beamdiameter_energy = (currentRadius - 6 / 2) * 2;
  38. return true;
  39. }

注意上面的边界条件逻辑,以及如何在parallel块中避免超出图像范围。这样做可以避免潜在的问题。如果您仍然遇到问题,请提供具体的错误信息,以便进一步帮助您调试。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=20044

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?