#include #include /* useful function for reading in ppm/pgm headers */ void getline(char *buf,FILE *fp) { //read until end of line from fp char *p=buf; char c; while ((c=fgetc(fp))!='\n') { *p=c; p++; } *p='\0';//terminate as string } /* some useful variables */ CvSize imagesize; int sizeX,sizeY; IplImage *ip,*ipout; unsigned char *rawptr; char tstring[1000],inchar; FILE *fp; int maxval; char fname[100]; int main(int argc,char **argv) { int i; /* read in image */ printf("name of input?\n"); scanf("%s",fname); fp = fopen(fname, "rb"); if (!fp) { fprintf(stderr, "Unable to open file `%s'\n", fname); exit(1); } getline(tstring,fp); fprintf(stderr,"scanned %s\n",tstring); if (! ((tstring[0]=='P') && (tstring[1]=='5')) ) { fprintf(stderr,"can only work with P5 versions of ppm images\n"); exit(-1); } /*Check for comment and swallow whole line */ getline(tstring,fp); while (tstring[0]=='#') getline(tstring,fp); //skip comments if (sscanf(tstring, "%d %d", &sizeX, &sizeY) != 2) { fprintf(stderr, "Error loading image `%s'\n", fname); exit(1); } getline(tstring,fp); while (tstring[0]=='#') getline(tstring,fp); //skip comments // throw away the maxval imagesize.width=sizeX; imagesize.height=sizeY; ip= cvCreateImage(imagesize,IPL_DEPTH_8U,1);// 3 channels RGB //default is 4 bytes align ip->roi=0; // no region of interest /* read image in one line at a time */ rawptr= (unsigned char *) ip->imageData; for (i=0;iwidthStep; //increment padded line } fclose(fp); /* process image */ ipout= cvCreateImage(imagesize,IPL_DEPTH_8U,1); cvCanny(ip,ipout,0.1,1.0,5); /* output image */ printf("name of output?\n"); scanf("%s",fname); fp = fopen(fname, "wb"); fprintf(fp,"P5\n%d %d\n255\n",ipout->width,ipout->height); /* write image in out line at a time */ rawptr= (unsigned char *) ipout->imageData; for (i=0;iwidthStep; //increment padded line } fclose(fp); }