I'm trying to build an edge detection program using the code below. I have faced a variety of problems though, that I don't know how to solve.
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define xrows 700
#define ycolumns 1244
int Gradient[xrows][ycolumns];
int Image_input[xrows][ycolumns];
int G_x[xrows][ycolumns];
int G_y[xrows][ycolumns];
int main() {
FILE *fw = fopen("sobel_outt.txt", "w");
FILE *fr = fopen("ny.txt", "r");
int x, y, row, column, num;
int i = 0;
int XLENGTH = 700;
int YLENGTH = 1244;
for (row = 0; row < XLENGTH; row++) {
for (column = 0; column < YLENGTH; column++) {
fscanf(fr, "%d " ",", &num);
Image_input[row][column] = num;
}
}
fclose(fr);
for (x = 0; x < XLENGTH; x += 3) {
i++;
for (y = 0; y < YLENGTH; y += 3) {
if ((x == 0) || (x == XLENGTH - 1) || (y == 0) || (y == YLENGTH - 1)) {
G_x[x][y] = G_y[x][y] = Gradient[x][y] = 0;
} else {
G_x[x][y] = Image_input[x + 1][y - 1]
+ 2 * Image_input[x + 1][y]
+ Image_input[x + 1][y + 1]
- Image_input[x - 1][y - 1]
- 2 * Image_input[x - 1][y]
- Image_input[x - 1][y + 1];
G_y[x][y] = Image_input[x - 1][y + 1]
+ 2 * Image_input[x][y + 1]
+ Image_input[x + 1][y + 1]
- Image_input[x - 1][y - 1]
- 2 * Image_input[x][y - 1]
- Image_input[x + 1][y - 1];
Gradient[x][y] = (abs(G_x[x][y]) + abs(G_y[x][y]));
if (Gradient[x][y] > 255) {
Gradient[x][y] = 255;
}
}
fprintf(fw, "%d,\n", Gradient[x][y]);
}
}
printf("i= %d", i);
fclose(fw);
return 0;
}
The program seems to execute fine when run in the devcpp IDE and all of the matrices are declared as global variables. Whenever I declare them inside the main function, the program crashes.
I tried to run the program using Visual Studio, but I faced a couple more problems. I got some error messages stating that fscanf
is ignored and fprintf
is unsafe.
Last but not least I got another error, stating that I used up all of the stack memory available.
Any suggestions would be welcomed .
EDIT: Many of you suggested that I caused a stack overflow. I will try to use the heap memory as an alternative. My second problem still remains though.