-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsobel.cpp
153 lines (117 loc) · 4.82 KB
/
sobel.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <cmath>
#include "sobel.h"
void generateSobelImages(cv::Mat &input) {
// Create objects for our output images
cv::Mat ddx;
cv::Mat ddy;
cv::Mat magnitudeOfGradient;
cv::Mat directionOfGradient;
// Initialise the objects
ddx.create(input.size(), input.type());
ddy.create(input.size(), input.type());
magnitudeOfGradient.create(input.size(), input.type());
directionOfGradient.create(input.size(), input.type());
// Set the objects to initial values
for(int imageRowIndex = 0; imageRowIndex < input.rows; imageRowIndex++) {
for(int imageColumnIndex = 0; imageColumnIndex < input.cols; imageColumnIndex++) {
ddx.at<uchar>(imageRowIndex, imageColumnIndex) = (uchar) 0;
ddy.at<uchar>(imageRowIndex, imageColumnIndex) = (uchar) 0;
magnitudeOfGradient.at<uchar>(imageRowIndex, imageColumnIndex) = (uchar) 0;
directionOfGradient.at<uchar>(imageRowIndex, imageColumnIndex) = (uchar) 0;
}
}
// Then calculate the output values
for(int imageRowIndex = 1; imageRowIndex < input.rows - 1; imageRowIndex++) {
for(int imageColumnIndex = 1; imageColumnIndex < input.cols - 1; imageColumnIndex++) {
int ddxAtX = convolveImageAt(input, imageRowIndex, imageColumnIndex, XKERNEL);
// int ddxAtY = getDdyAtIndex();
// int magnitudeOfGradientAtx = getMagnitudeOfGradientAtX();
// int directionOfGradientAtx = getDirectionOfGradientAtX();
ddx.at<uchar>(imageRowIndex, imageColumnIndex) = (uchar) ddxAtX;
}
}
imwrite("ddx.jpg", ddx);
}
int convolveImageAt(cv::Mat &input, int row, int column, const cv::Mat &kernel) {
int sum = 0;
for(int kernelRow = 0; kernelRow < kernel.rows; kernelRow++) {
for(int kernelColumn; kernelColumn < kernel.cols; kernelColumn++) {
int imageRow = row - 1 + kernelRow;
int imageColumn = column - 1 + kernelColumn;
printf("%d\n", row);
int imageval = ( int ) input.at<uchar>( imageRow, imageColumn );
double kernalval = kernel.at<double>( kernelRow, kernelColumn );
sum += imageval * kernalval;
}
}
return sum;
}
/*
void calculateFirstDerivative(cv::Mat &input, cv::Mat &output, const cv::Mat &kernel) {
// Initialise the output using the input
output.create(input.size(), input.type());
for(int imageRowIndex = 0; imageRowIndex < input.rows; imageRowIndex++) {
for(int imageColumnIndex = 0; imageColumnIndex < input.cols; imageColumnIndex++) {
output.at<uchar>(imageRowIndex, imageColumnIndex) = (uchar) 0;
}
}
for(int imageRowIndex = 1; imageRowIndex < input.rows - 1; imageRowIndex++) {
for(int imageColumnIndex = 1; imageColumnIndex < input.cols - 1; imageColumnIndex++) {
int pixelValue = 0;
for(int kernelRowIndex = 0; kernelRowIndex < kernel.rows; kernelRowIndex++) {
for(int kernelColumnIndex = 0; kernelColumnIndex < kernel.cols; kernelColumnIndex++) {
int rowIndex = imageRowIndex + kernelRowIndex - 1;
int columnIndex = imageColumnIndex + kernelColumnIndex - 1;
int imageVal = ( int ) input.at<uchar> (rowIndex, columnIndex);
int kernelVal = ( int ) kernel.at<double>( kernelRowIndex, kernelColumnIndex );
pixelValue = pixelValue + (imageVal*kernelVal);
}
}
output.at<uchar>(imageRowIndex, imageColumnIndex) = (uchar) pixelValue;
}
}
}
void calculateMagnitude(cv::Mat &ddx, cv::Mat &ddy, cv::Mat &output) {
// Use ddx or ddy
output.create(ddx.size(), ddx.type());
// Loop over the image, pixel by pixel
for(int imageRow = 0; imageRow < ddx.rows; imageRow++) {
for(int imageColumn = 0; imageColumn < ddx.cols; imageColumn++) {
int ddxImageVal = ( int ) ddx.at<uchar>( imageRow, imageColumn );
int ddyImageVal = ( int ) ddy.at<uchar>( imageRow, imageColumn );
int gradientAtPixel = abs(ddxImageVal) + abs(ddyImageVal);
gradientAtPixel = gradientAtPixel > 255 ? 255:gradientAtPixel;
output.at<uchar>(imageRow, imageColumn) = (uchar) gradientAtPixel;
}
}
}
void calculateGradientOrientation(cv::Mat &ddx, cv::Mat &ddy, cv::Mat &output) {
// Use ddx or ddy
output.create(ddx.size(), ddx.type());
for(int imageRow = 0; imageRow < ddx.rows; imageRow++) {
for(int imageColumn = 0; imageColumn < ddx.cols; imageColumn++) {
int ddxImageVal = ( int ) ddx.at<uchar>( imageRow, imageColumn );
int ddyImageVal = ( int ) ddy.at<uchar>( imageRow, imageColumn );
int directionAtPixel = 0;
if(ddxImageVal != 0) {
directionAtPixel = atan(ddyImageVal/ddxImageVal);
}
output.at<uchar>(imageRow, imageColumn) = (uchar) directionAtPixel;
}
}
}
void thresholdImage(cv::Mat &input, cv::Mat &output) {
// Use ddx or ddy
output.create(input.size(), input.type());
for(int imageRow = 0; imageRow < input.rows; imageRow++) {
for(int imageColumn = 0; imageColumn < input.cols; imageColumn++) {
int inVal = ( int ) input.at<uchar>( imageRow, imageColumn );
int outval = 0;
if(inVal > 10) {
outval = 255;
}
output.at<uchar>(imageRow, imageColumn) = (uchar) outval;
}
}
}
*/