Advertisement
osipyonok

MPI_Sobel_2

Apr 20th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. // ParalellProg_lab1_v2.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. #include <iostream>
  7. #include <cmath>
  8. #include <algorithm>
  9. #include <opencv2/core/core.hpp>
  10. #include <opencv2/highgui/highgui.hpp>
  11. #include <opencv2/opencv.hpp>
  12. #include <opencv2/imgproc/imgproc.hpp>
  13. #include <opencv2/objdetect/objdetect.hpp>
  14. #include <mpi.h>
  15.  
  16. using namespace std;
  17. using namespace cv;
  18.  
  19. #define ll long long
  20.  
  21. const vector<vector<int>> sobel_x = { { -1 , 0 , 1 } ,{ -2 , 0 , 2 } ,{ -1 , 0 , 1 } };
  22. const vector<vector<int>> sobel_y = { { -1 , -2 , -1 } ,{ 0 , 0 , 0 } ,{ 1 , 2 , 1 } };
  23.  
  24. void Sobel(string & path, int argc, char* argv[]) {
  25.     Mat img = imread(path);
  26.  
  27.  
  28.     Mat img_gray, image_blur;
  29.     GaussianBlur(img, image_blur, Size(5, 5), 3, 3);
  30.     cvtColor(image_blur, img_gray, CV_RGB2GRAY);
  31.  
  32.  
  33.     int cols = img_gray.cols;
  34.     int rows = img_gray.rows;
  35.  
  36.  
  37.     int radius = 1;
  38.  
  39.     Mat _src;
  40.     copyMakeBorder(img_gray, _src, radius, radius, radius, radius, BORDER_REFLECT101);
  41.  
  42.     Mat gradient_x = img_gray.clone();
  43.     Mat gradient_y = img_gray.clone();
  44.     Mat gradient_f = img_gray.clone();
  45.  
  46.  
  47.  
  48.     for (int r = radius; r < _src.rows - radius; ++r) {
  49.         for (int c = radius; c < _src.cols - radius; ++c) {
  50.             int sx = 0, sy = 0;
  51.             for (int i = -radius; i <= radius; ++i) {
  52.                 for (int j = -radius; j <= radius; ++j) {
  53.                     sx += _src.at<uchar>(r + i, c + j) * sobel_x[i + radius][j + radius];
  54.                     sy += _src.at<uchar>(r + i, c + j) * sobel_y[i + radius][j + radius];
  55.                 }
  56.             }
  57.             gradient_x.at<uchar>(r - radius, c - radius) = sx / 30;
  58.             gradient_y.at<uchar>(r - radius, c - radius) = sy / 30;
  59.         }
  60.     }
  61.  
  62.     Mat absGrad_x, absGrad_y;
  63.     convertScaleAbs(gradient_x, absGrad_x);
  64.     convertScaleAbs(gradient_y, absGrad_y);
  65.  
  66.     for (int i = 0; i < gradient_f.rows; i++) {
  67.         for (int j = 0; j < gradient_f.cols; j++) {
  68.             gradient_f.at<uchar>(i, j) = sqrt(pow(gradient_x.at<uchar>(i, j), 2) + pow(gradient_y.at<uchar>(i, j), 2));
  69.             gradient_f.at<uchar>(i, j) = gradient_f.at<uchar>(i, j) >= 240 ? 100 : 0;
  70.         }
  71.     }
  72.  
  73.     imwrite(path + "output.jpg", gradient_f);
  74. }
  75.  
  76. vector<string> q;
  77.  
  78. int main(int argc, char* argv[]) {
  79.     ll before = clock();
  80.  
  81.  
  82.     int n;
  83.  
  84.     int worldSize;
  85.     int worldRank;
  86. //  char processorName[MPI_MAX_PROCESSOR_NAME];
  87. //  int nameLength;
  88.  
  89.  
  90.     MPI_Init(&argc, &argv);
  91.     MPI_Comm_size(MPI_COMM_WORLD, &worldSize);
  92.     MPI_Comm_rank(MPI_COMM_WORLD, &worldRank);
  93.     MPI_Status status;
  94. //  MPI_Get_processor_name(processorName, &nameLength);
  95. //  cout << worldSize << " " << worldRank << endl
  96.  
  97.     freopen("files.txt", "r", stdin);
  98.     string str;
  99.     while (getline(cin, str))q.push_back(str);
  100.     n = q.size();
  101.  
  102.  
  103.     for (int i = worldRank; i < n; i += worldSize) {
  104.         //  for (int i = 0; i < n; ++i) {
  105.         string file = q[i];
  106.         Sobel(file, argc, argv);
  107.     }
  108.     if (worldRank == 0) {
  109.         ll after = clock();
  110.  
  111.         cout << "Total time: " << (double)(after - before) / CLOCKS_PER_SEC << " sec" << endl;
  112.         cout << "Average time: " << (double)((after - before) / CLOCKS_PER_SEC) / n << " sec" << endl;
  113.  
  114.         system("pause");
  115.     }
  116.     MPI_Finalize();
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement