Example #1
0
// This function makes a ppm file from the MSP and displays 'nl' detected landmarks.
//im: the input image 
//lmx and lmy: are pointers to nl size arrays including the (x,y) for the nl landmarks
//ppmfile: the output file name
void mspPPM(SHORTIM im, int *ii, int *jj, int nl, const char *ppmfile)
{
   int colourflag;
//   unsigned char colour[3]={0xFF,0xFF,0x00};
//   unsigned char colour[3]={0x00,0xFF,0x00};
   unsigned char colour[3]={0xFF,0xFF,0xFF};
   unsigned char *imgTemp;
   FILE *fp;

   int kk=im.nz/2;
   
   /*size of the mark*/
   int d=4; 

   float temp;

   imgTemp=(unsigned char *)calloc(im.nv,sizeof(unsigned char));

   int low, high;
   setLowHigh(im.v+kk*im.np, im.nx*im.ny, &low, &high, 1.0);

   for(int i=0;i<im.nv;i++)
   {
      if( im.v[i] >= high ) temp=255.0;
      else temp=im.v[i]*255.0/high;

      imgTemp[i]=(unsigned char)temp;
   }

   fp=fopen(ppmfile,"w");
   if(fp==NULL) file_open_error(ppmfile);

   /*Write the header part of the PPM file*/
   fprintf(fp,"P6\n");
   fprintf(fp,"%d %d\n", im.nx, im.ny);
   fprintf(fp,"255\n");


   for (int j=0;j<im.ny;j++)
   {
      for(int i=0;i<im.nx;i++)
      {
         for(int m=0;m<nl;m++)
         {
//uncomment to save in different colours
//            if(m==3) { colour[0]=255; colour[1]=0; colour[2]=0; } 
//            else if(m==1) { colour[0]=0; colour[1]=255; colour[2]=0; } 
//            else if(m==2) { colour[0]=0; colour[1]=0; colour[2]=255; } 
//            else if(m==0) { colour[0]=0; colour[1]=255; colour[2]=191; } 
//            else if(m==4) { colour[0]=0; colour[1]=128; colour[2]=255; } 
//            else if(m==5) { colour[0]=255; colour[1]=0; colour[2]=255; } 
//            else if(m==6) { colour[0]=255; colour[1]=128; colour[2]=0; } 
//            else if(m==7) { colour[0]=255; colour[1]=255; colour[2]=0; } 
            
            if( (i==ii[m] && jj[m]-d<j && j<jj[m]+d) || (j==jj[m] && ii[m]-d<i && i<ii[m]+d) )
            {
               fwrite(colour,1,3,fp);
               colourflag=1;
            }
         }
         if(colourflag==0) 
         {
            fwrite(imgTemp+im.np*kk+im.nx*j+i,1,1,fp);
            fwrite(imgTemp+im.np*kk+im.nx*j+i,1,1,fp);
            fwrite(imgTemp+im.np*kk+im.nx*j+i,1,1,fp);
         }
         colourflag=0;
      }
   }

   fclose(fp);

  if(opt_png)
  {
    char *pngfilename;
    char *cmnd;
    int L;

    L = strlen(ppmfile);
    pngfilename = (char *)calloc(L,sizeof(char));
    cmnd = (char *)calloc(2*L+128,sizeof(char));  // 128 is plenty :)
    stpcpy(pngfilename, ppmfile);
    pngfilename[L-1]='g';
    pngfilename[L-2]='n';
    pngfilename[L-3]='p';

    sprintf(cmnd,"pnmtopng %s > %s",ppmfile,pngfilename); 
    if(opt_png) system(cmnd);

    free(pngfilename);
    free(cmnd);
  }

  if(opt_ppm==NO) remove(ppmfile);

  delete imgTemp;
}
Example #2
0
//im: the input image 
//nx,ny,nz: size of the image
//lm[3]: coordinate of the landmark (satrting from 0)
//ppmfile: name of the ppmfile as the output
void makePPM(SHORTIM im, int *lm, const char *ppmfile)
{
   unsigned char yellow[3]={0xFF,0xFF,0x00};
   unsigned char *imgTemp;
   FILE *fp;

   int nv=im.nx*im.ny*im.nz;
   int np=im.nx*im.ny;

   int ii=lm[0];
   int jj=lm[1];
   int kk=lm[2];

   float temp;

   imgTemp=(unsigned char *)calloc(nv,sizeof(unsigned char));

   int low, high;
   setLowHigh(im.v, nv, &low, &high);

   for(int i=0;i<nv;i++)
   {
      if( im.v[i] >= high ) temp=255.0;
      else temp=im.v[i]*255.0/high;

      imgTemp[i]=(unsigned char)temp;
   }

   fp=fopen(ppmfile,"w");

   /*Write the header part of the PPM file*/
   fprintf(fp,"P6\n");
   fprintf(fp,"# x=%d, y=%d, z=%d\n", ii, jj, kk);
   fprintf(fp,"%d %d\n", im.nx+im.nz+im.nz, im.ny);
   fprintf(fp,"255\n");

   /*Write the data part of the PPM file*/
   for (int j=0,ik=0;j<im.ny & ik<im.nx;j++,ik++)
   {
      for(int i=0;i<im.nx;i++)
      {
         if(i==ii | j==jj) fwrite(yellow,1,3,fp);
         else {
            fwrite(imgTemp+np*kk+im.nx*j+i,1,1,fp);
            fwrite(imgTemp+np*kk+im.nx*j+i,1,1,fp);
            fwrite(imgTemp+np*kk+im.nx*j+i,1,1,fp);
         }
      }

      for(int k=0;k<im.nz;k++)
      {
         if(j==jj | k==kk) fwrite(yellow,1,3,fp);
         else {
            fwrite(imgTemp+np*k+im.nx*j+ii,1,1,fp);
            fwrite(imgTemp+np*k+im.nx*j+ii,1,1,fp);
            fwrite(imgTemp+np*k+im.nx*j+ii,1,1,fp);
         }
      }

      for(int k=0;k<im.nz;k++)
      {
         if(ik==ii | k==kk) fwrite(yellow,1,3,fp);
         else {
            fwrite(imgTemp+np*k+im.nx*jj+ik,1,1,fp);
            fwrite(imgTemp+np*k+im.nx*jj+ik,1,1,fp);
            fwrite(imgTemp+np*k+im.nx*jj+ik,1,1,fp);
         }
      }
   }

   fclose(fp);

   delete imgTemp;
}
Example #3
0
char *find_foreground_mask(short *im, int nv, int nb, int nclass, int niter, short *thresh)
{
	char *mask;
	double *hist;
	double *mean;
	double *var;
	double *p;
	double *fit;
	short *label;
	FILE *fp;
	int low,high,v;
	int bw;
	int T;
	int nbv;

	mask=(char *)calloc(nv,sizeof(char));

	nbv=0;
	for(int i=0; i<nv; i++) if(im[i]>0) nbv++;

	mean = (double *)calloc(nclass,sizeof(double));
	var = (double *)calloc(nclass,sizeof(double));
	p = (double *)calloc(nclass,sizeof(double));
	fit = (double *)calloc(nb,sizeof(double));
	label = (short *)calloc(nb,sizeof(short));
	if(mean==NULL || var==NULL || p==NULL || fit==NULL || label==NULL)
	{
		printf("\nMemory allocation problem in 'find_foreground_mask'\n");
		return(NULL);
	}

	{
		short *im_mskd;
		int k=0;

		im_mskd = (short *)calloc(nbv,sizeof(short));

		if(im_mskd==NULL) 
		{
			printf("\nMemory allocation problem for 'im_mskd', aborting ...\n");
			exit(0);
		}

		for(int i=0; i<nv; i++) if(im[i]>0) im_mskd[k++]=im[i];

		setLowHigh(im_mskd, nbv, &low, &high, 0.01);
		hist=findHistogram(im_mskd, nbv, nb, low, high, &bw);

		free(im_mskd);
	}

	for(int b=0; b<nb; b++) hist[b]/=nbv;

	EMFIT1d(hist, fit, label, nb, mean, var, p, nclass, niter);

	T=0;
	for(int b=1; b<nb-1; b++)
	if( fit[b-1]>fit[b] && fit[b+1]>fit[b] ) 
	{
		T=b;
		break;
	}
			
	*thresh=0;
	for(int i=0;i<nv;i++) 
	if(im[i]>0)
	{
		v = im[i];

		if(v<=low) v=0;
		else if(v>=high) v=high-low;
		else v-=low;

		v = (int)(v/bw);

		if(v==T && im[i] > *thresh ) *thresh=im[i];
	}

	for(int i=0;i<nv;i++) if(im[i] <= *thresh) mask[i]=0; else mask[i]=1;

	free(mean);
	free(var);
	free(p);
	free(fit);
	free(label);
	free(hist);

	return(mask);
}