Advertisement
Guest User

Gauss

a guest
May 24th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. double __timp;
  2. void startTimer()
  3. {
  4. __timp = (double)getTickCount(); // Gฤƒseศ™te timpul curent [ms]
  5. }
  6.  
  7. void stopTimer(const char* text = "")
  8. {
  9. __timp = ((double)getTickCount() - __timp) / getTickFrequency();
  10. printf(text);
  11. printf("Time = %.3f [ms]\n", __timp * 1000);
  12. }
  13.  
  14.  
  15. Mat medianFilterRGB(Mat src, const int size = 1)
  16. {
  17. Mat fin = src.clone();
  18. //src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  19.  
  20.  
  21. int height = src.rows;
  22. int width = src.cols;
  23.  
  24. for(int y = 0; y < height; y++)
  25. for (int x = 0; x < width; x++)
  26. {
  27. vector<int> neighboursR, neighboursG, neighboursB;
  28. for(int dy = -size; dy <= size; dy++)
  29. for (int dx = -size; dx <= size; dx++)
  30. {
  31. int nx = x + dx;
  32. int ny = y + dy;
  33.  
  34. if (nx < 0 || nx >= width) continue;
  35. if (ny < 0 || ny >= height) continue;
  36.  
  37. neighboursR.push_back(src.at<Vec3b>(ny, nx)[2]);
  38. neighboursG.push_back(src.at<Vec3b>(ny, nx)[1]);
  39. neighboursB.push_back(src.at<Vec3b>(ny, nx)[0]);
  40. }
  41. sort(neighboursR.begin(), neighboursR.end());
  42. sort(neighboursG.begin(), neighboursG.end());
  43. sort(neighboursB.begin(), neighboursB.end());
  44.  
  45. fin.at<Vec3b>(y, x) = Vec3b(neighboursB[neighboursB.size() / 2],
  46. neighboursG[neighboursG.size() / 2],
  47. neighboursR[neighboursR.size() / 2]);
  48. }
  49. return fin;
  50. }
  51.  
  52. void filtrariMed()
  53. {
  54. char fname[MAX_PATH];
  55. while (openFileDlg(fname))
  56. {
  57. Mat src;
  58. src = imread(fname, CV_LOAD_IMAGE_COLOR);
  59.  
  60. imshow("Original", src);
  61.  
  62. int height = src.rows;
  63. int width = src.cols;
  64.  
  65. startTimer();
  66. Mat res = medianFilterRGB(src, 2);
  67. stopTimer("Median");
  68.  
  69. imshow("Median", res);
  70. waitKey(10);
  71.  
  72.  
  73. waitKey();
  74. destroyAllWindows();
  75. }
  76. }
  77.  
  78. Mat gaussian2DFilter(Mat src, int size = 6)
  79. {
  80. Mat res = src.clone();
  81.  
  82. double sigma = size/3.0;
  83. double norm = 1 / (2 * PI * sigma * sigma) * exp(-(0) / (2 * sigma * sigma));
  84.  
  85. Mat gaussianFilter = Mat(size, size, CV_16SC1);
  86. for (int y = 0; y < size; y++)
  87. for (int x = 0; x < size; x++)
  88. {
  89. int dx = x - size / 2;
  90. int dy = y - size / 2;
  91. double val = 1 / (2 * PI * sigma * sigma) * exp(-(dx * dx + dy * dy) / (2 * sigma * sigma));
  92. gaussianFilter.at<short>(y, x) = (short)(255*val/norm);
  93. }
  94.  
  95.  
  96. convolve(res, src, gaussianFilter);
  97.  
  98. return res;
  99. }
  100.  
  101. Mat gaussian1D2Filter(Mat src, int size = 6)
  102. {
  103. Mat resp = src.clone();
  104.  
  105. double sigma = size / 3.0;
  106. double norm = 1 / (2 * PI * sigma * sigma) * exp(-(0) / (2 * sigma * sigma));
  107. Mat gaussianFilterx = Mat(1, size, CV_16SC1);
  108.  
  109. for (int x = 0; x < size; x++)
  110. {
  111. int dx = x - size / 2;
  112. int dy = 0;
  113. double val = 1 / (sqrt(2 * PI) * sigma) * exp(-(dx * dx + dy * dy) / (2 * sigma * sigma));
  114. gaussianFilterx.at<short>(0, x) = (short)(255 * val / norm);
  115. }
  116.  
  117.  
  118. convolve(resp, src, gaussianFilterx);
  119.  
  120. Mat res = src.clone();
  121. Mat gaussianFiltery = Mat(size, 1, CV_16SC1);
  122. for (int y = 0; y < size; y++)
  123. {
  124. int dx = 0;
  125. int dy = y - size / 2;
  126. double val = 1 / (sqrt(2 * PI) * sigma * sigma) * exp(-(dx * dx + dy * dy) / (2 * sigma * sigma));
  127. gaussianFiltery.at<short>(y, 0) = (short)(255 * val / norm);
  128. }
  129.  
  130.  
  131. convolve(res, resp, gaussianFiltery);
  132.  
  133. return res;
  134. }
  135.  
  136. void filtrariGauss()
  137. {
  138. char fname[MAX_PATH];
  139. while (openFileDlg(fname))
  140. {
  141. Mat src;
  142. src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
  143.  
  144. imshow("Original", src);
  145.  
  146. int height = src.rows;
  147. int width = src.cols;
  148.  
  149. int size = 7;
  150. //cin >> size;
  151. startTimer();
  152. Mat res2D = gaussian2DFilter(src, size);
  153. stopTimer("2D Gauss");
  154.  
  155. imshow("2D Gauss", res2D);
  156. waitKey(10);
  157.  
  158. startTimer();
  159. Mat res1D = gaussian1D2Filter(src, size);
  160. stopTimer("1D Gauss");
  161. imshow("1D Gauss", res1D);
  162.  
  163. waitKey();
  164. destroyAllWindows();
  165. }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement