-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSobelOperator.cpp
More file actions
125 lines (47 loc) · 1.66 KB
/
SobelOperator.cpp
File metadata and controls
125 lines (47 loc) · 1.66 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main(int argc, char *argv[])
{
if (argc != 3) {
printf("Enter exeFileName imageFileName\n");
}
Mat image = imread(argv[1], IMREAD_COLOR);
if (image.empty()) {
cout << "Error: No Image to load" << endl;
}
Mat gr(image.rows,image.cols,CV_8UC1,Scalar(0));
cvtColor(image,gr,COLOR_BGR2GRAY);
Mat dest = gr.clone();
int gx = 0;
int gy = 0;
int G = 0;
int T = atoi(argv[2]);
for (int i = 1; i < gr.rows - 1; i++) {
for (int j = 1; j < gr.cols - 1; j++) {
gx = 0;
gy = 0;
G = 0;
gx = -(int)gr.at<uchar>(i - 1, j - 1) - 2 * (int)gr.at<uchar>(i - 1, j) - (int)gr.at<uchar>(i - 1, j + 1) + (int)gr.at<uchar>(i + 1, j - 1) + 2 * (int)gr.at<uchar>(i + 1, j) + (int)gr.at<uchar>(i + 1, j + 1);
gy = -(int)gr.at<uchar>(i - 1, j - 1) - 2 * (int)gr.at<uchar>(i, j - 1) - (int)gr.at<uchar>(i + 1, j - 1) + (int)gr.at<uchar>(i - 1, j + 1) + 2 * (int)gr.at<uchar>(i, j + 1) + (int)gr.at<uchar>(i + 1, j + 1);
G = abs(gx) + abs(gy);
if (G >= T) {
dest.at<uchar>(i, j) = 255;
}
else
dest.at<uchar>(i, j) = 0;
}
}
// display histogram
namedWindow("Gray Image", 1);
imshow("Gray Image", gr);
namedWindow("Sobel Operator", 1);
imshow("Sobel Operator", dest);
waitKey();
gr.release();
dest.release();
return 0;
}