ZBLOG

cv::Mat image1; image.copyTo(image1); int sum = 0; int currentRadius = 0; int sum1 = cv::sum(image1)[0]; // 计算灰度图像的像素总和 while (sum < sum1 * ratio / 100) { // 遍历每个像素点 #pragma omp parallel for reduction(+:su...

如果你想每次只增加多出的那几圈半径,可以在循环中判断是否需要增加20(或其他值),而不是固定地每次都增加20。你可以通过计算当前总和与目标总和之间的差异来确定是否需要增加半径。

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

  1. cv::Mat image1;
  2. image.copyTo(image1);
  3. int sum = 0;
  4. int currentRadius = 0;
  5. int sum1 = cv::sum(image1)[0]; // 计算灰度图像的像素总和
  6. while (sum < sum1 * ratio / 100)
  7. {
  8. // 遍历每个像素点
  9. #pragma omp parallel for reduction(+:sum)
  10. for (int i = Center.y - currentRadius; i <= Center.y + currentRadius; ++i)
  11. {
  12. double powIcenterX = pow(i - Center.y, 2);
  13. uchar* rowPtr = image1.ptr<uchar>(i); // 获取当前行指针,减少内存访问次数
  14. for (int j = Center.x - currentRadius; j <= Center.x + currentRadius; ++j)
  15. {
  16. // 确保不会超出图像范围
  17. if (i >= 0 && i < image1.rows && j >= 0 && j < image1.cols)
  18. {
  19. // 计算欧几里得距离
  20. double distance = sqrt(powIcenterX + pow(j - Center.x, 2));
  21. // 如果在半径范围内且该像素未被处理过,则将灰度值累加到总和中
  22. if (distance <= currentRadius && rowPtr[j] != 0)
  23. {
  24. //#pragma omp atomic update
  25. sum += rowPtr[j];
  26. // 将已处理过的像素值设为0,表示已累加过
  27. rowPtr[j] = 0;
  28. }
  29. }
  30. }
  31. }
  32. int nextRadius = currentRadius + 20; // 增加20作为下一个半径
  33. int nextSum = 0;
  34. // 计算下一个半径对应的总和(包括新增的圈)
  35. for (int i = Center.y - nextRadius; i <= Center.y + nextRadius; ++i)
  36. {
  37. double powIcenterX = pow(i - Center.y, 2);
  38. uchar* rowPtr = image1.ptr<uchar>(i); // 获取当前行指针,减少内存访问次数
  39. for (int j = Center.x - nextRadius; j <= Center.x + nextRadius; ++j)
  40. {
  41. // 确保不会超出图像范围
  42. if (i >= 0 && i < image1.rows && j >= 0 && j < image1.cols)
  43. {
  44. // 计算欧几里得距离
  45. double distance = sqrt(powIcenterX + pow(j - Center.x, 2));
  46. // 如果在下一个半径范围内且该像素未被处理过,则将灰度值累加到总和中
  47. if (distance <= nextRadius && rowPtr[j] != 0)
  48. {
  49. nextSum += rowPtr[j];
  50. }
  51. }
  52. }
  53. }
  54. // 判断是否需要增加半径
  55. if (nextSum - sum >= sum1 * ratio / 100)
  56. {
  57. currentRadius = nextRadius;
  58. sum = nextSum;
  59. }
  60. }

这样,每次只会增加多出的那几圈半径。请根据你的实际需求进行调整和测试。


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?