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.

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