Advertisement
osipyonok

ParProg_lab1

Apr 11th, 2017
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. // ParalellProg_lab1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <opencv2/core/core.hpp>
  9. #include <opencv2/highgui/highgui.hpp>
  10. #include <opencv2/opencv.hpp>
  11. #include <opencv2/imgproc/imgproc.hpp>
  12. #include <opencv2/objdetect/objdetect.hpp>
  13.  
  14. using namespace std;
  15. using namespace cv;
  16.  
  17. #define ll long long
  18.  
  19. const vector<vector<int>> sobel_x = { {-1 , 0 , 1} , {-2 , 0 , 2} , {-1 , 0 , 1} };
  20. const vector<vector<int>> sobel_y = { {-1 , -2 , -1} , {0 , 0 , 0} , {1 , 2 , 1} };
  21.  
  22. void Sobel(string & path) {
  23.     Mat img = imread(path);
  24.  
  25.     assert(img.data);
  26.  
  27.     //imshow("Original Image",img);
  28.     //waitKey(0);
  29.  
  30.     Mat img_gray, image_blur;
  31.     GaussianBlur(img, image_blur, Size(5, 5), 3, 3);
  32.     cvtColor(image_blur, img_gray, CV_RGB2GRAY);
  33.  
  34.  
  35.     //imshow("Grayscale Image" , img_gray);
  36.     //waitKey(0);
  37.  
  38.  
  39.     int cols = img_gray.cols;
  40.     int rows = img_gray.rows;
  41.  
  42.  
  43.     int radius = 1;
  44.  
  45.     Mat _src;
  46.     copyMakeBorder(img_gray, _src, radius, radius, radius, radius, BORDER_REFLECT101);
  47.  
  48.     Mat gradient_x = img_gray.clone();
  49.     Mat gradient_y = img_gray.clone();
  50.     Mat gradient_f = img_gray.clone();
  51.  
  52.     for (int r = radius; r < _src.rows - radius; ++r) {
  53.         for (int c = radius; c < _src.cols - radius; ++c) {
  54.             int sx = 0, sy = 0;
  55.             for (int i = -radius; i <= radius; ++i) {
  56.                 for (int j = -radius; j <= radius; ++j) {
  57.                     sx += _src.at<uchar>(r + i, c + j) * sobel_x[i + radius][j + radius];
  58.                     sy += _src.at<uchar>(r + i, c + j) * sobel_y[i + radius][j + radius];
  59.                 }
  60.             }
  61.             gradient_x.at<uchar>(r - radius, c - radius) = sx / 30;
  62.             gradient_y.at<uchar>(r - radius, c - radius) = sy / 30;
  63.         }
  64.     }
  65.  
  66.     Mat absGrad_x, absGrad_y;
  67.     convertScaleAbs(gradient_x, absGrad_x);
  68.     convertScaleAbs(gradient_y, absGrad_y);
  69.  
  70.     for (int i = 0; i < gradient_f.rows; i++){
  71.         for (int j = 0; j < gradient_f.cols; j++){
  72.             gradient_f.at<uchar>(i, j) = sqrt(pow(gradient_x.at<uchar>(i, j), 2) + pow(gradient_y.at<uchar>(i, j), 2));
  73.             gradient_f.at<uchar>(i, j) = gradient_f.at<uchar>(i, j) >= 240 ? 100 : 0;
  74.         }
  75.     }
  76.  
  77.     imwrite("output.jpg", gradient_f);
  78.  
  79. //  imshow("Sobel", gradient_f);
  80. //  waitKey(0);
  81. }
  82.  
  83. int main(){
  84.     ll before = clock();
  85.  
  86.     int n = 200;
  87.  
  88.     for (int i = 0; i < n; ++i) {
  89.         string file = "1.jpg";
  90.         Sobel(file);
  91.     }
  92.  
  93.     ll after = clock();
  94.  
  95.     cout << "Total time: " << (double)(after - before) / CLOCKS_PER_SEC << " sec" << endl;
  96.     cout << "Average: " << (double)((after - before) / CLOCKS_PER_SEC) / n << " sec" << endl;
  97.  
  98.     system("pause");
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement