-
Notifications
You must be signed in to change notification settings - Fork 0
/
7_Sobel_Operator.cpp
48 lines (34 loc) · 1.24 KB
/
7_Sobel_Operator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//Edge detection - Sobel operator
#include "opencv2/opencv.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/core.hpp"
using namespace cv;
using namespace std;
int main(int argc, char* argv[]) {
Mat image = imread(argv[1], IMREAD_GRAYSCALE);
if (!image.data) {
cout << "Could not find the image! " << endl;
return -1;
}
Mat output = image.clone();
int T = atoi(argv[2]);
int gx, gy, G;
//----------------------------Sobel operator----------------------------------------
for (int i = 1; i < image.rows - 1; i++) {
for (int j = 1; j < image.cols - 1; j++) {
gx = -1 * image.at<uchar>(i - 1, j - 1) - 2 * image.at<uchar>(i - 1, j) - 1 * image.at<uchar>(i - 1, j + 1) + 1 * image.at<uchar>(i+1, j+1) + 2 * image.at<uchar>(i + 1, j) + 1 * image.at<uchar>(i + 1, j + 1);
gy = -1 * image.at<uchar>(i - 1, j - 1) + 1 * image.at<uchar>(i-1, j + 1) - 2 * image.at<uchar>(i , j - 1) + 2 * image.at<uchar>(i, j+1) - 1 * image.at<uchar>(i+1, j - 1) + 1 * image.at<uchar>(i + 1, j + 1);
G = abs(gx) + abs(gy);
if (G > T)
output.at<uchar>(i, j) = 255;
else
output.at<uchar>(i, j) = 0;
}
}
namedWindow("Image");
imshow("Image", image);
namedWindow("Output Image");
imshow("Output Image", output);
waitKey(0);
return 0;
}