The following program i done are only able to detect half of the edge , can anyone solve this problem for me please ? thanks
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main(void)
{
FILE *fptr1, *fptr2;
int row, col, r, c, i, j, max_gray;
int threshold;
int **pic_mat, **edge_img;
char dummy1[50],dummy2[50]; int Sobel_v[3][3]={1,0,-1,2,0,-2,1,0,-1}; int Sobel_h[3][3]={1,2,1,0,0,0,-1,-2,-1}; fptr1 = fopen("Picture 2.pgm","r");
fptr2 = fopen("test3_edge.pgm","w");
if(fptr1==NULL)
{
printf("Error: can't open file.\n");
getch();
exit(1);
}
fgets(dummy1,50,fptr1);
printf("%s",dummy1);
fgets(dummy2,50,fptr1);
printf("%s",dummy2);
getch();
i = fscanf(fptr1,"%d %d",&col,&row);
printf("%d\n",i); printf("col=%d row=%d\n",col,row);
getch();
i = fscanf(fptr1,"%d",&max_gray);
printf("%d %d\n",i,max_gray);pic_mat = (int**)calloc(row,sizeof(int*));
for(i=0;i<row;i++)
pic_mat[i] = (int*)calloc(col,sizeof(int));
for(i=0;i<row;i++)
for(j=0;j<col;j++)
fscanf(fptr1,"%d ",&pic_mat[i][j]);
edge_img = (int**)calloc(row,sizeof(int*));
for(i=0;i<row;i++)
edge_img[i] = (int*)calloc(col,sizeof(int));
for(j=0;j<col;j++)
{
edge_img[0][j] = pic_mat[0][j];
edge_img[row-1][j] = pic_mat[row-1][j];
}
for(i=1;i<row-1;i++)
{
edge_img[i][0] = pic_mat[i][0];
edge_img[i][col-1] = pic_mat[i][col-1];
}
threshold = 100;
for(r=1;r<row-1;r++)
for(c=1;c<col-1;c++)
{
for(i=-1;i<=1;i++)
for(j=-1;j<=1;j++)
edge_img[r][c] += pic_mat[r+i][c+j]*Sobel_v[i+1][j+1];
if(edge_img[r][c] < threshold)
edge_img[r][c] = 0;
else
edge_img[r][c] = 255;
}
threshold = 100;
for(r=1;r<row-1;r++)
for(c=1;c<col-1;c++)
{
for(i=-1;i<=1;i++)
for(j=-1;j<=1;j++)
edge_img[r][c] += pic_mat[r+i][c+j]*Sobel_h[i+1][j+1];
if(edge_img[r][c] < threshold)
edge_img[r][c] = 0;
else
edge_img[r][c] = 255;
}
fprintf(fptr2,"P2\n");
fprintf(fptr2,"# Sobel Edge Detection\n");
fprintf(fptr2,"%d %d\n",col, row);
fprintf(fptr2,"%d\n",max_gray);
for(i=0;i<row;i++)
{
if(i!=0)
fprintf(fptr2,"\n");
for(j=0;j<col;j++)
fprintf(fptr2,"%d ",edge_img[i][j]);
}
getch();
fclose(fptr1);
fclose(fptr2);
}