Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ParalellProg_lab1.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <cmath>
- #include <algorithm>
- #include <opencv2/core/core.hpp>
- #include <opencv2/highgui/highgui.hpp>
- #include <opencv2/opencv.hpp>
- #include <opencv2/imgproc/imgproc.hpp>
- #include <opencv2/objdetect/objdetect.hpp>
- using namespace std;
- using namespace cv;
- #define ll long long
- const vector<vector<int>> sobel_x = { {-1 , 0 , 1} , {-2 , 0 , 2} , {-1 , 0 , 1} };
- const vector<vector<int>> sobel_y = { {-1 , -2 , -1} , {0 , 0 , 0} , {1 , 2 , 1} };
- void Sobel(string & path) {
- Mat img = imread(path);
- assert(img.data);
- //imshow("Original Image",img);
- //waitKey(0);
- Mat img_gray, image_blur;
- GaussianBlur(img, image_blur, Size(5, 5), 3, 3);
- cvtColor(image_blur, img_gray, CV_RGB2GRAY);
- //imshow("Grayscale Image" , img_gray);
- //waitKey(0);
- int cols = img_gray.cols;
- int rows = img_gray.rows;
- int radius = 1;
- Mat _src;
- copyMakeBorder(img_gray, _src, radius, radius, radius, radius, BORDER_REFLECT101);
- Mat gradient_x = img_gray.clone();
- Mat gradient_y = img_gray.clone();
- Mat gradient_f = img_gray.clone();
- for (int r = radius; r < _src.rows - radius; ++r) {
- for (int c = radius; c < _src.cols - radius; ++c) {
- int sx = 0, sy = 0;
- for (int i = -radius; i <= radius; ++i) {
- for (int j = -radius; j <= radius; ++j) {
- sx += _src.at<uchar>(r + i, c + j) * sobel_x[i + radius][j + radius];
- sy += _src.at<uchar>(r + i, c + j) * sobel_y[i + radius][j + radius];
- }
- }
- gradient_x.at<uchar>(r - radius, c - radius) = sx / 30;
- gradient_y.at<uchar>(r - radius, c - radius) = sy / 30;
- }
- }
- Mat absGrad_x, absGrad_y;
- convertScaleAbs(gradient_x, absGrad_x);
- convertScaleAbs(gradient_y, absGrad_y);
- for (int i = 0; i < gradient_f.rows; i++){
- for (int j = 0; j < gradient_f.cols; j++){
- gradient_f.at<uchar>(i, j) = sqrt(pow(gradient_x.at<uchar>(i, j), 2) + pow(gradient_y.at<uchar>(i, j), 2));
- gradient_f.at<uchar>(i, j) = gradient_f.at<uchar>(i, j) >= 240 ? 100 : 0;
- }
- }
- imwrite("output.jpg", gradient_f);
- // imshow("Sobel", gradient_f);
- // waitKey(0);
- }
- int main(){
- ll before = clock();
- int n = 200;
- for (int i = 0; i < n; ++i) {
- string file = "1.jpg";
- Sobel(file);
- }
- ll after = clock();
- cout << "Total time: " << (double)(after - before) / CLOCKS_PER_SEC << " sec" << endl;
- cout << "Average: " << (double)((after - before) / CLOCKS_PER_SEC) / n << " sec" << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement