PICTURE *grad(PICTURE *pic,int opside) { /* uses integral image (sumimage; edgegrad) for bigger operators and simple fastedgegrad for ops up to side 4 - and frees memory */ PICTURE *summedpic, *edgepic; if (opside>4) { summedpic = sumimage(pic); freepic(pic); edgepic = edgegrad(summedpic,opside); freepic(summedpic); } else { edgepic = fastedgegrad(pic,opside); freepic(pic); } return(edgepic); }
/* Select maximum gradient vector components and free input pic */ PICTURE *gradmax(PICTURE *pic) { PICTURE *picout; FVEC_TYPE *bfi = (FVEC_TYPE *)pic->data; FVEC_TYPE *bfo; int x, s, items, samps; float maxx, maxy, fx, fy; if (((pic->fileid)!= FVEC_ID) || ((pic->samples) < 2)) { (void)fprintf(stderr,"gradmax: wrong type of input pic\n"); exit(1); } samps = pic->samples; items = pic->items; picout = copypic(pic,1,FVEC_ID); /* only one sample per pixel output */ bfo = (FVEC_TYPE *)picout->data; for (x=0; x<items; x++) { maxx = (*bfi).fx; maxy = (*(bfi++)).fy; for (s=1; s<samps; s++) /* global maximum over all the samples */ { fx = (*bfi).fx; fy = (*(bfi++)).fy; if (fabs(fx) > fabs(maxx)) maxx = fx; if (fabs(fy) > fabs(maxy)) maxy = fy; } (*bfo).fx = maxx; (*(bfo++)).fy = maxy; } freepic(pic); return(picout); }
int main(int argc, char *argv[]) { PICTURE *pic,*pic2; int opdim = 4; float lowthresh = 0.05f, highthresh = 0.9f; switch (argc) { case 6: highthresh = (float)atof(argv[5]); argc--; case 5: lowthresh = (float)atof(argv[4]); argc--; case 4: opdim = atoi(argv[3]); argc--; case 3: case 2: case 1: dofiles(argc,argv); fprintf(stderr,"%s: operator side = %d\n", PROGNAME,opdim); break; default: fprintf(stderr, "usage: %s [infile] [outfile] [side] [lowthresh] [highthresh]\n", PROGNAME); exit(1); } if ((pic = readpic(FILEIN)) == NULL) { fprintf(stderr,"%s: failed to read input file\n",PROGNAME); exit(1); } if ((pic->fileid) != IMAGE_ID) { fprintf(stderr,"%s: wrong type of input file\n",PROGNAME); exit(1); } timex(); pic = grad(pic,opdim); timex(); // pic = morphgrads(pic); /* optional thinning */ pic = gradmag(pic); pic2 = ftoim(pic,lowthresh,highthresh,1); freepic(pic); /* ftoim does not free pic */ writepmpic(pic2,FILEOUT); exit(0); }
PICTURE *morphgrads(PICTURE *pic) { PICTURE *picout; FVEC_TYPE *bfi = (FVEC_TYPE *)pic->data; if ((pic->fileid)!= FVEC_ID) { (void)fprintf(stderr,"morphgrads: wrong type of input pic\n"); exit(1); } picout = copypic(pic,pic->samples,FVEC_ID); erodegrads(pic,picout); dilategrads(picout,pic); freepic(picout); return(pic); }
/* uses simple mahabolis grad magnitude (absolute values) and frees input pic */ PICTURE *gradmag(PICTURE *pic) { PICTURE *picout; FVEC_TYPE *bfi = (FVEC_TYPE *)pic->data; FIMAGE_TYPE *bfo,vfx,vfy; int x, sitems; if ((pic->fileid)!= FVEC_ID) { (void)fprintf(stderr,"gradmag: wrong type of input pic\n"); exit(1); } sitems = (pic->samples)*(pic->items); picout = copypic(pic,(pic->samples),FIMAGE_ID); bfo = (float*)picout->data; for (x=0; x<sitems; x++) { vfx = (*bfi).fx; vfy = (*bfi).fy; *(bfo++) = ((vfx < 0.0)?(-vfx):(vfx))+((vfy < 0.0)?(-vfy):(vfy)); bfi++; } freepic(pic); return(picout); }
int main(int argc, char* argv[]) { int parameter=checkparametr (argc,argv); if (parameter!=0){ return -2;} PTGALLERY *ptgallery=malloc(sizeof(PTGALLERY)); if(!ptgallery){ errorHandle(ERROR_NO_MEMORY); return 0; } struct node * actualPic=NULL; initGallery(ptgallery); char c; char bond[5000]; struct node *newpic; while (1){ printf(">"); fgets(bond,5000,stdin); if (strlen(bond)>2){ errorHandle(ERROR_WRONG_CMD); } else { c=bond[0]; int b=0; switch (c){ case 'n':; newpic=malloc(sizeof(struct node)); if(!newpic){ errorHandle(ERROR_NO_MEMORY); break; } newpic->next=NULL; newpic->prev=NULL; b=newpicture(actualPic,newpic,ptgallery); if(!b){ actualPic = newpic; printgal(ptgallery, actualPic); }else{ printgal(ptgallery, actualPic); free(newpic); } break; case 'd': if((ptgallery->first == NULL)||(ptgallery->last == NULL)){ break; } else if((ptgallery->first == ptgallery->last)){ ptgallery->first = NULL; ptgallery->last = NULL; free(actualPic->value); free(actualPic); actualPic=NULL; break; } else { if(!(actualPic->prev)){ ptgallery->first = actualPic->next; actualPic = actualPic->next; freepic(ptgallery->first->prev); ptgallery->first->prev = NULL; } else if(actualPic->next == NULL){ ptgallery->last=actualPic->prev; actualPic = actualPic->prev; freepic(ptgallery->last->next); ptgallery->last->next=NULL; } else { struct node *needed=actualPic; actualPic->prev->next=actualPic->next; actualPic->next->prev=actualPic->prev; actualPic = actualPic->next; free (needed); } } printgal(ptgallery,actualPic); break; case 'r': if(ptgallery->first==actualPic){ actualPic=ptgallery->last; } else { actualPic=actualPic->prev; } printgal(ptgallery, actualPic); break; case 'l': if(ptgallery->last==actualPic){ actualPic=ptgallery->first; } else { actualPic=actualPic->next; } printgal(ptgallery, actualPic); break; case 's': printf ("size: %i\n",howmanypictures(ptgallery)); break; case 'q': actualPic = ptgallery->first; while(actualPic!=NULL){ if(actualPic->next !=NULL){ actualPic = actualPic->next; freepic(actualPic->prev); } else { freepic(actualPic); break; } } free(ptgallery); return 0; case '\n': break; default: errorHandle(ERROR_WRONG_CMD); break; }}} return 0; }