Click here to Skip to main content
15,902,447 members
Please Sign up or sign in to vote.
1.57/5 (4 votes)
See more:
The following program i done are only able to detect half of the edge , can anyone solve this problem for me please ? thanks
C
#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]; //dummy string used to skip lines
int Sobel_v[3][3]={1,0,-1,2,0,-2,1,0,-1}; //Sobel operator (vertical)
int Sobel_h[3][3]={1,2,1,0,0,0,-1,-2,-1}; //Sobel operator (horizontal)
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);
}
//Skip 2 lines
fgets(dummy1,50,fptr1);
printf("%s",dummy1);
fgets(dummy2,50,fptr1);
printf("%s",dummy2);
getch();

//Get the col and row
i = fscanf(fptr1,"%d %d",&col,&row);
printf("%d\n",i); //i==2 for correct read-in
printf("col=%d row=%d\n",col,row);
getch();
//Get the max value for gray
i = fscanf(fptr1,"%d",&max_gray);
printf("%d %d\n",i,max_gray);//i==1
//getch();
//Dynamic memory allocation for 2D array (matrix)
pic_mat = (int**)calloc(row,sizeof(int*));
for(i=0;i<row;i++)
pic_mat[i] = (int*)calloc(col,sizeof(int));

//Get the image
for(i=0;i<row;i++)
for(j=0;j<col;j++)
fscanf(fptr1,"%d ",&pic_mat[i][j]);

//Dynamic memory allocation for 2D array (matrix)
edge_img = (int**)calloc(row,sizeof(int*));
for(i=0;i<row;i++)
edge_img[i] = (int*)calloc(col,sizeof(int));
//Set first/last row/col same as image
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];
}
//ignore first row/col and last row/col
//Sobel_v operator and thresholding (threshold value trial and error)
//Try the result of different thresholds
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;
}
//Do the same for Sobel_h
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;
}
//Then combine the two
//output edge to a file
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);
}
Posted
Updated 20-Oct-10 8:53am
v2
Comments
Sandeep Mewara 20-Oct-10 15:43pm    
What?
Sandeep Mewara 20-Oct-10 15:44pm    
Forget it.. just saw the time of question. Don't know why almost an year old question are being digged out and edited! :doh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900