You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.3 KiB

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <omp.h>
  5. #include "Ttiming.h"
  6. #include <opencv2/opencv.hpp>
  7. using namespace std;
  8. using namespace cv;
  9. #define RX 5
  10. #define RY 5
  11. int main(int argc, char *argv[])
  12. {
  13. int i,j,k,l,m,n;
  14. TTiming tt;
  15. char *endptr;
  16. int ratio[RX][RY] =
  17. {
  18. {1, 4, 7, 4, 1},
  19. {4, 16, 26, 16, 4},
  20. {7, 26, 41, 26, 7},
  21. {4, 16, 26, 16, 4},
  22. {1, 4, 7, 4, 1}
  23. };
  24. if (argc < 4)
  25. {
  26. cerr << "Usage: " << argv[0] << " <n> <input_image> <output_image>" << endl;
  27. exit(1);
  28. }
  29. int threads_num = strtol(argv[1], &endptr, 10);
  30. if (*endptr)
  31. {
  32. cerr << "Invalid number of threads format" << endl;
  33. exit(1);
  34. }
  35. if (threads_num <= 0)
  36. {
  37. cerr << "The number of threads must be positive" << endl;
  38. exit(1);
  39. }
  40. int suma_wag = 0;
  41. for (i=0; i<RX; ++i)
  42. for (j=0; j<RY; ++j)
  43. suma_wag += ratio[i][j];
  44. //ustawienie odpowiedniej ilosci watkow
  45. omp_set_num_threads(threads_num);
  46. Mat img = cv::imread(argv[2]);
  47. if(!img.data )
  48. {
  49. cerr << "File " << argv[2] << " does not exist" << endl;
  50. exit(1);
  51. }
  52. Mat img_out;
  53. img_out.create(img.rows, img.cols, img.type());
  54. int sumka_r, sumka_g, sumka_b;
  55. //#pragma omp parallel for default(shared)
  56. tt.Begin();
  57. //#pragma omp parallel for default(shared)
  58. #pragma omp parallel for private(i, j, k, l, m, n, sumka_r, sumka_g, sumka_b)
  59. for (i=0; i<img.rows; ++i)
  60. {
  61. for (j=0; j<img.cols; ++j)
  62. {
  63. if (i<2 || i>img.rows-3 || j<2 || j>img.cols-3)
  64. {
  65. img_out.at<Vec3b>(i, j)[0] = img.at<Vec3b>(i, j)[0];
  66. img_out.at<Vec3b>(i, j)[1] = img.at<Vec3b>(i, j)[1];
  67. img_out.at<Vec3b>(i, j)[2] = img.at<Vec3b>(i, j)[2];
  68. }
  69. else
  70. {
  71. sumka_r = 0;
  72. sumka_g = 0;
  73. sumka_b = 0;
  74. m=i-2;
  75. for (k=0; k<RX; ++k,++m)
  76. {
  77. n=j-2;
  78. for (l=0; l<RY; ++l,++n)
  79. {
  80. sumka_b += ratio[k][l] * img.at<Vec3b>(m, n)[0];
  81. sumka_g += ratio[k][l] * img.at<Vec3b>(m, n)[1];
  82. sumka_r += ratio[k][l] * img.at<Vec3b>(m, n)[2];
  83. }
  84. }
  85. img_out.at<Vec3b>(i, j).val[0] = sumka_b / suma_wag;
  86. img_out.at<Vec3b>(i, j).val[1] = sumka_g / suma_wag;
  87. img_out.at<Vec3b>(i, j).val[2] = sumka_r / suma_wag;
  88. }
  89. //cout << img_out.at<Vec3b>(i, j)[0].val[0];
  90. }
  91. }
  92. long elapsed = tt.End();
  93. cout << "Time: " << elapsed << " ms" << endl;
  94. imwrite(argv[3], img_out);
  95. exit(0);
  96. }