-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathcannyEdgeDetection.c
274 lines (241 loc) · 9.38 KB
/
cannyEdgeDetection.c
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define M_PI
#define GAUSSIAN_SIZE 5
#define GAUSSIAN_SIGMA 1.4
#define SOBEL_SIZE 3
#define LOW_THRESHOLD_RATIO 0.1
#define HIGH_THRESHOLD_RATIO 0.3
void toGrayScale(unsigned char* image, int width, int height);
void applyGaussianFilter(unsigned char* image, int width, int height);
void createGaussianKernel(double kernel[][GAUSSIAN_SIZE], int size, double sigma);
void applySobelOperator(unsigned char* image, int width, int height, unsigned char* gradientImage);
void performNonMaxSuppression(unsigned char* gradientImage, int width, int height, unsigned char* suppressedImage);
void applyDoubleThresholding(unsigned char* suppressedImage, int width, int height, unsigned char* edgesImage);
int main() {
// Load input image
FILE* file = fopen("dog.raw", "rb");
if (file == NULL) {
printf("Error opening the input image file.\n");
return 1;
}
int width = 640;
int height = 480;
unsigned char* image = (unsigned char*)malloc(width * height * sizeof(unsigned char));
fread(image, sizeof(unsigned char), width * height, file);
fclose(file);
// Convert the image to grayscale
toGrayScale(image, width, height);
// Apply Gaussian smoothing
applyGaussianFilter(image, width, height);
// Apply Sobel operator to calculate gradients
unsigned char* gradientImage = (unsigned char*)malloc(width * height * sizeof(unsigned char));
applySobelOperator(image, width, height, gradientImage);
// Perform non-maximum suppression
unsigned char* suppressedImage = (unsigned char*)malloc(width * height * sizeof(unsigned char));
performNonMaxSuppression(gradientImage, width, height, suppressedImage);
// Apply double thresholding and hysteresis
unsigned char* edgesImage = (unsigned char*)malloc(width * height * sizeof(unsigned char));
applyDoubleThresholding(suppressedImage, width, height, edgesImage);
// Save the resulting image
file = fopen("canny_edges.raw", "wb");
if (file == NULL) {
printf("Error opening the output image file.\n");
return 1;
}
fwrite(edgesImage, sizeof(unsigned char), width * height, file);
fclose(file);
printf("Canny edges saved to canny_edges.raw\n");
// Free memory
free(image);
free(gradientImage);
free(suppressedImage);
free(edgesImage);
return 0;
}
void toGrayScale(unsigned char* image, int width, int height) {
for (int i = 0; i < width * height; i++) {
unsigned char red = image[3 * i];
unsigned char green = image[3 * i + 1];
unsigned char blue = image[3 * i + 2];
unsigned char gray = (unsigned char)(0.299 * red + 0.587 * green + 0.114 * blue);
image[3 * i] = gray;
image[3 * i + 1] = gray;
image[3 * i + 2] = gray;
}
}
void applyGaussianFilter(unsigned char* image, int width, int height) {
double gaussianKernel[GAUSSIAN_SIZE][GAUSSIAN_SIZE];
createGaussianKernel(gaussianKernel, GAUSSIAN_SIZE, GAUSSIAN_SIGMA);
unsigned char* smoothed = (unsigned char*)malloc(width * height * sizeof(unsigned char));
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
double sum = 0.0;
for (int j = 0; j < GAUSSIAN_SIZE; j++) {
for (int i = 0; i < GAUSSIAN_SIZE; i++) {
int pixelX = x - GAUSSIAN_SIZE / 2 + i;
int pixelY = y - GAUSSIAN_SIZE / 2 + j;
if (pixelX >= 0 && pixelX < width && pixelY >= 0 && pixelY < height) {
int gray = image[pixelY * width + pixelX];
sum += gray * gaussianKernel[i][j];
}
}
}
int smoothedGray = (int)round(sum);
smoothed[y * width + x] = smoothedGray;
}
}
memcpy(image, smoothed, width * height * sizeof(unsigned char));
free(smoothed);
}
void createGaussianKernel(double kernel[][GAUSSIAN_SIZE], int size, double sigma) {
double sum = 0.0;
for (int j = 0; j < size; j++) {
for (int i = 0; i < size; i++) {
int x = i - size / 2;
int y = j - size / 2;
kernel[i][j] = exp(-(x * x + y * y) / (2 * sigma * sigma));
sum += kernel[i][j];
}
}
for (int j = 0; j < size; j++) {
for (int i = 0; i < size; i++) {
kernel[i][j] /= sum;
}
}
}
void applySobelOperator(unsigned char* image, int width, int height, unsigned char* gradientImage) {
int sobelX[SOBEL_SIZE][SOBEL_SIZE] = {
{-1, 0, 1},
{-2, 0, 2},
{-1, 0, 1}
};
int sobelY[SOBEL_SIZE][SOBEL_SIZE] = {
{1, 2, 1},
{0, 0, 0},
{-1, -2, -1}
};
for (int y = 1; y < height - 1; y++) {
for (int x = 1; x < width - 1; x++) {
int gradientX = 0;
int gradientY = 0;
for (int j = -1; j <= 1; j++) {
for (int i = -1; i <= 1; i++) {
int pixelX = x + i;
int pixelY = y + j;
int gray = image[pixelY * width + pixelX];
gradientX += gray * sobelX[i + 1][j + 1];
gradientY += gray * sobelY[i + 1][j + 1];
}
}
int gradientMagnitude = (int)sqrt(gradientX * gradientX + gradientY * gradientY);
gradientImage[y * width + x] = gradientMagnitude;
}
}
}
void performNonMaxSuppression(unsigned char* gradientImage, int width, int height, unsigned char* suppressedImage) {
for (int y = 1; y < height - 1; y++) {
for (int x = 1; x < width - 1; x++) {
int gradientMagnitude = gradientImage[y * width + x];
int angle = (int)(atan2((double)getGradientY(gradientImage, x, y, width), (double)getGradientX(gradientImage, x, y, width)) * 180.0 / M_PI);
angle = (angle + 180) % 180;
int neighbor1 = 0;
int neighbor2 = 0;
if ((angle >= 0 && angle < 22.5) || (angle >= 157.5 && angle < 180)) {
neighbor1 = gradientImage[(y - 1) * width + x];
neighbor2 = gradientImage[(y + 1) * width + x];
}
else if (angle >= 22.5 && angle < 67.5) {
neighbor1 = gradientImage[(y - 1) * width + (x + 1)];
neighbor2 = gradientImage[(y + 1) * width + (x - 1)];
}
else if (angle >= 67.5 && angle < 112.5) {
neighbor1 = gradientImage[y * width + (x + 1)];
neighbor2 = gradientImage[y * width + (x - 1)];
}
else if (angle >= 112.5 && angle < 157.5) {
neighbor1 = gradientImage[(y - 1) * width + (x - 1)];
neighbor2 = gradientImage[(y + 1) * width + (x + 1)];
}
if (gradientMagnitude >= neighbor1 && gradientMagnitude >= neighbor2) {
suppressedImage[y * width + x] = gradientMagnitude;
}
else {
suppressedImage[y * width + x] = 0;
}
}
}
}
void applyDoubleThresholding(unsigned char* suppressedImage, int width, int height, unsigned char* edgesImage) {
int lowThreshold = (int)(LOW_THRESHOLD_RATIO * 255);
int highThreshold = (int)(HIGH_THRESHOLD_RATIO * 255);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int gray = suppressedImage[y * width + x];
if (gray >= highThreshold) {
edgesImage[y * width + x] = 255;
}
else if (gray >= lowThreshold) {
int isStrongNeighbor = 0;
for (int j = -1; j <= 1; j++) {
for (int i = -1; i <= 1; i++) {
if (x + i >= 0 && x + i < width && y + j >= 0 && y + j < height) {
int neighborGray = suppressedImage[(y + j) * width + (x + i)];
if (neighborGray >= highThreshold) {
isStrongNeighbor = 1;
break;
}
}
}
if (isStrongNeighbor) {
break;
}
}
if (isStrongNeighbor) {
edgesImage[y * width + x] = 255;
}
else {
edgesImage[y * width + x] = 0;
}
}
else {
edgesImage[y * width + x] = 0;
}
}
}
}
int getGradientX(unsigned char* image, int x, int y, int width) {
int sobelX[SOBEL_SIZE][SOBEL_SIZE] = {
{-1, 0, 1},
{-2, 0, 2},
{-1, 0, 1}
};
int gx = 0;
for (int j = -1; j <= 1; j++) {
for (int i = -1; i <= 1; i++) {
int pixelX = x + i;
int pixelY = y + j;
int gray = image[pixelY * width + pixelX];
gx += gray * sobelX[i + 1][j + 1];
}
}
return gx;
}
int getGradientY(unsigned char* image, int x, int y, int width) {
int sobelY[SOBEL_SIZE][SOBEL_SIZE] = {
{1, 2, 1},
{0, 0, 0},
{-1, -2, -1}
};
int gy = 0;
for (int j = -1; j <= 1; j++) {
for (int i = -1; i <= 1; i++) {
int pixelX = x + i;
int pixelY = y + j;
int gray = image[pixelY * width + pixelX];
gy += gray * sobelY[i + 1][j + 1];
}
}
return gy;
}