|
|
- #include <iostream>
- #include <cstdlib>
- #include <cmath>
- #include <omp.h>
- #include "Ttiming.h"
- #include <opencv2/opencv.hpp>
-
- using namespace std;
- using namespace cv;
-
- #define RX 5
- #define RY 5
- int main(int argc, char *argv[])
- {
- int i,j,k,l,m,n;
- TTiming tt;
- char *endptr;
-
- int ratio[RX][RY] =
- {
- {1, 4, 7, 4, 1},
- {4, 16, 26, 16, 4},
- {7, 26, 41, 26, 7},
- {4, 16, 26, 16, 4},
- {1, 4, 7, 4, 1}
- };
-
- if (argc < 4)
- {
- cerr << "Usage: " << argv[0] << " <n> <input_image> <output_image>" << endl;
- exit(1);
- }
-
- int threads_num = strtol(argv[1], &endptr, 10);
-
- if (*endptr)
- {
- cerr << "Invalid number of threads format" << endl;
- exit(1);
- }
-
- if (threads_num <= 0)
- {
- cerr << "The number of threads must be positive" << endl;
- exit(1);
- }
-
- int suma_wag = 0;
- for (i=0; i<RX; ++i)
- for (j=0; j<RY; ++j)
- suma_wag += ratio[i][j];
-
- //ustawienie odpowiedniej ilosci watkow
- omp_set_num_threads(threads_num);
-
- Mat img = cv::imread(argv[2]);
- if(!img.data )
- {
- cerr << "File " << argv[2] << " does not exist" << endl;
- exit(1);
-
- }
-
- Mat img_out;
- img_out.create(img.rows, img.cols, img.type());
-
- int sumka_r, sumka_g, sumka_b;
-
- //#pragma omp parallel for default(shared)
-
- tt.Begin();
- //#pragma omp parallel for default(shared)
- #pragma omp parallel for private(i, j, k, l, m, n, sumka_r, sumka_g, sumka_b)
- for (i=0; i<img.rows; ++i)
- {
- for (j=0; j<img.cols; ++j)
- {
- if (i<2 || i>img.rows-3 || j<2 || j>img.cols-3)
- {
- img_out.at<Vec3b>(i, j)[0] = img.at<Vec3b>(i, j)[0];
- img_out.at<Vec3b>(i, j)[1] = img.at<Vec3b>(i, j)[1];
- img_out.at<Vec3b>(i, j)[2] = img.at<Vec3b>(i, j)[2];
- }
- else
- {
- sumka_r = 0;
- sumka_g = 0;
- sumka_b = 0;
- m=i-2;
- for (k=0; k<RX; ++k,++m)
- {
- n=j-2;
- for (l=0; l<RY; ++l,++n)
- {
- sumka_b += ratio[k][l] * img.at<Vec3b>(m, n)[0];
- sumka_g += ratio[k][l] * img.at<Vec3b>(m, n)[1];
- sumka_r += ratio[k][l] * img.at<Vec3b>(m, n)[2];
- }
- }
- img_out.at<Vec3b>(i, j).val[0] = sumka_b / suma_wag;
- img_out.at<Vec3b>(i, j).val[1] = sumka_g / suma_wag;
- img_out.at<Vec3b>(i, j).val[2] = sumka_r / suma_wag;
- }
-
- //cout << img_out.at<Vec3b>(i, j)[0].val[0];
- }
- }
- long elapsed = tt.End();
- cout << "Time: " << elapsed << " ms" << endl;
-
- imwrite(argv[3], img_out);
-
- exit(0);
- }
|