ImgDenoise.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "ImgDenoise.h"
  2. #define FILTER_PI 3.14159
  3. #pragma once
  4. /*====================================================================
  5. 函数名 :SurfaceFilter
  6. 功能 :降噪-模糊滤波
  7. 输入参数说明:
  8. srcImgData:输入数据
  9. dstImgData:输出数据
  10. imgwidth:图像宽
  11. imgheight:图像高
  12. channels:图像通道数
  13. radius:滤波半径
  14. threshold:阈值
  15. ====================================================================*/
  16. #define MIN2(a, b) ((a) < (b) ? (a) : (b))
  17. #define MAX2(a, b) ((a) > (b) ? (a) : (b))
  18. #define CLIP3(x, a, b) MIN2(MAX2(a,x), b)
  19. extern "C" __declspec(dllexport) bool UltrasoundImageFilter(const char* srcImgData, char* dstImgData, int imgwidth, int imgheight, int channels, int radius, int threshold)
  20. {
  21. try
  22. {
  23. // 检查通道数
  24. if (channels != 3 && channels != 4)
  25. {
  26. return false;
  27. }
  28. // 检查图像尺寸
  29. if (imgwidth <= 0 || imgheight <= 0)
  30. {
  31. return false;
  32. }
  33. Mat src, dst;
  34. if (channels == 4)
  35. {
  36. src.create(imgheight, imgwidth, CV_8UC4);
  37. memcpy(src.data, srcImgData, imgwidth * imgheight * channels);
  38. dst = src.clone();
  39. float sumr = 0, sumrw = 0, sumg = 0, sumgw = 0, sumb = 0, sumbw = 0, k = 0;
  40. for (int j = 0; j < src.rows; j++)
  41. {
  42. for (int i = 0; i < src.cols; i++)
  43. {
  44. sumr = sumrw = sumg = sumgw = sumb = sumbw = 0;
  45. for (int n = -radius; n <= radius; n++)
  46. {
  47. for (int m = -radius; m <= radius; m++)
  48. {
  49. int x = CLIP3(i + m, 0, imgwidth - 1);
  50. int y = CLIP3(j + n, 0, imgheight - 1);
  51. k = 1.0f - abs(src.at<Vec4b>(y, x)[0] - src.at<Vec4b>(j, i)[0]) / (2.5f * threshold);
  52. if (k <= 0)
  53. k = 0.01;
  54. sumb += k * src.at<Vec4b>(y, x)[0];
  55. sumbw += k;
  56. k = 1.0f - abs(src.at<Vec4b>(y, x)[1] - src.at<Vec4b>(j, i)[1]) / (2.5f * threshold);
  57. if (k <= 0)
  58. k = 0.01;
  59. sumg += k * src.at<Vec4b>(y, x)[1];
  60. sumgw += k;
  61. k = 1.0f - abs(src.at<Vec4b>(y, x)[2] - src.at<Vec4b>(j, i)[2]) / (2.5f * threshold);
  62. if (k <= 0)
  63. k = 0.01;
  64. sumr += k * src.at<Vec4b>(y, x)[2];
  65. sumrw += k;
  66. }
  67. }
  68. dst.at<Vec4b>(j, i)[0] = sumbw == 0 ? src.at<Vec4b>(j, i)[0] : CLIP3(sumb / sumbw, 0, 255);
  69. dst.at<Vec4b>(j, i)[1] = sumgw == 0 ? src.at<Vec4b>(j, i)[1] : CLIP3(sumg / sumgw, 0, 255);
  70. dst.at<Vec4b>(j, i)[2] = sumrw == 0 ? src.at<Vec4b>(j, i)[2] : CLIP3(sumr / sumrw, 0, 255);
  71. }
  72. }
  73. }
  74. else
  75. {
  76. src.create(imgheight, imgwidth, CV_8UC3);
  77. memcpy(src.data, srcImgData, imgwidth * imgheight * channels);
  78. dst = src.clone();
  79. float sumr = 0, sumrw = 0, sumg = 0, sumgw = 0, sumb = 0, sumbw = 0, k = 0;
  80. for (int j = 0; j < src.rows; j++)
  81. {
  82. for (int i = 0; i < src.cols; i++)
  83. {
  84. sumr = sumrw = sumg = sumgw = sumb = sumbw = 0;
  85. for (int n = -radius; n <= radius; n++)
  86. {
  87. for (int m = -radius; m <= radius; m++)
  88. {
  89. int x = CLIP3(i + m, 0, imgwidth - 1);
  90. int y = CLIP3(j + n, 0, imgheight - 1);
  91. k = 1.0f - abs(src.at<Vec3b>(y, x)[0] - src.at<Vec3b>(j, i)[0]) / (2.5f * threshold);
  92. if (k <= 0)
  93. k = 0.01;
  94. sumb += k * src.at<Vec3b>(y, x)[0];
  95. sumbw += k;
  96. k = 1.0f - abs(src.at<Vec3b>(y, x)[1] - src.at<Vec3b>(j, i)[1]) / (2.5f * threshold);
  97. if (k <= 0)
  98. k = 0.01;
  99. sumg += k * src.at<Vec3b>(y, x)[1];
  100. sumgw += k;
  101. k = 1.0f - abs(src.at<Vec3b>(y, x)[2] - src.at<Vec3b>(j, i)[2]) / (2.5f * threshold);
  102. if (k <= 0)
  103. k = 0.01;
  104. sumr += k * src.at<Vec3b>(y, x)[2];
  105. sumrw += k;
  106. }
  107. }
  108. dst.at<Vec3b>(j, i)[0] = sumbw == 0 ? src.at<Vec3b>(j, i)[0] : CLIP3(sumb / sumbw, 0, 255);
  109. dst.at<Vec3b>(j, i)[1] = sumgw == 0 ? src.at<Vec3b>(j, i)[1] : CLIP3(sumg / sumgw, 0, 255);
  110. dst.at<Vec3b>(j, i)[2] = sumrw == 0 ? src.at<Vec3b>(j, i)[2] : CLIP3(sumr / sumrw, 0, 255);
  111. }
  112. }
  113. }
  114. memcpy(dstImgData, dst.data, imgwidth * imgheight * channels);
  115. return true;
  116. }
  117. catch (...)
  118. {
  119. return false;
  120. }
  121. }