- remember to give file name with .bmp extension
- it will only work with 8 bit images
Code for simple edge detection using C
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
void main()
{
FILE *imag1, *imag2;
int i=0,byte,q;
char namein[30];
char nameout[30];
printf("enter name of input image with extension \n");
gets(namein);
printf("enter name of output image with extension \n");
gets(nameout);
//..................clone image..................
imag1=fopen(namein,"rb+");
if(imag1==NULL)
printf("cannot open file");
imag2=fopen(nameout,"wb");
while(!feof(imag1))
{
fputc(fgetc(imag1),imag2);
}
//...............................end clone image...................
int pic[400][600];
FILE *fp;
fp=fopen(nameout,"rb+");
//for(i=0;i<54;i++) byte = getc(fp);
fseek( fp,55,SEEK_SET );
for(q=1;q<300;q++)
for(i=1;i<600;i++)
{
// printf("Pixel ->%d \n",getc(fp));
pic[q][i]=getc(fp);
}
fclose(fp);
//................pix alter.................
int temp1;
FILE *fp2;
fp2=fopen(nameout,"rb+");
// for(i=0;i<54;i++) byte = getc(fp);
fseek( fp2,55,SEEK_SET );
// printf("%d",q);
//printf("pic array mid value %d \n",pic[60][500]);
for(q=0;q<250;q++)
for(i=2;i<400;i++)
{
temp1=(-1*pic[i-1][q])+(1*pic[i+1][q]);
temp1=temp1*temp1;
temp1=sqrt(temp1);
printf("mod pixel sqrt res %d \n",temp1);
printf("pic array value %d \n",temp1);
// putc(0,fp2);
putc(temp1,fp2);
// printf("mod pix %d \n",getc(fp2));
}
fclose(fp2);
}
************************* output**********************************
Here is a 8 bit image for you to try
A little for you to understand...........
Description for Edge Detection
Take input from the
user:
1)
We take
the name of the original file in which we want to implement Edge
detection.
2)
Than we give the name of the new image we will
show the detected edges
//……………………………………implementation…………………………………………………………………………
char namein[30];
char nameout[30];
printf("enter name of input image with extension
\n");
gets(namein); // name for input
image (original image)
printf("enter
name of output image with extension \n");
gets(nameout); // name for output image (will be
created at run time)
//……………………………………implementation…………………………………………………………………………
Than we clone our
orignal image (create a copy of our image)
//..................clone image..................
imag1=fopen(namein,"rb+"); //original image
if(imag1==NULL)
printf("cannot open file");
imag2=fopen(nameout,"wb");
// clone image
while(!feof(imag1))
{
fputc(fgetc(imag1),imag2); //copy
image
}
//...............................end clone
image...................
Read the clone image
and store the value of pixels in a 2D array named as pic[][]
int pic[400][600];
FILE *fp;
fp=fopen(nameout,"rb+");
fseek( fp,55,SEEK_SET
); //filtering
out image header
for(q=1;q<300;q++)
for(i=1;i<600;i++)
{
// printf("Pixel ->%d
\n",getc(fp)); //to pint value of
pixels
pic[q][i]=getc(fp); //storing pixel
values in matrix
}
fclose(fp);
Find gradient using
the matrix pic[][] and store its value in a integer type variable named as temp1 and write its value in the clone image
Gradient will be calculated by
convolving a 3 by 1 kernal in image [-1,0,1]
//..............................................pix alter...........................................................
int temp1;
FILE *fp2;
fp2=fopen(nameout,"rb+");
// for(i=0;i<54;i++) byte = getc(fp);
fseek(
fp2,55,SEEK_SET ); //
filtering out image header
//
printf("%d",q);
//printf("pic
array mid value %d \n",pic[60][500]);
for(q=0;q<250;q++) //
implementing [-1,0,1] kernel to find out the integer value of gradient
for(i=2;i<400;i++)
{
temp1=(-1*pic[i-1][q])+(1*pic[i+1][q]);
temp1=temp1*temp1;
temp1=sqrt(temp1);
printf("mod pixel sqrt res %d \n",temp1);
printf("pic array value %d \n",temp1);
putc(temp1,fp2); // wrinting
the value of gradient in clone image
// printf("mod pix %d \n",getc(fp2));
}
fclose(fp2);
//..............................................pix
alter...........................................................
Comments
Post a Comment