Esempio n. 1
0
/** Function for loading TIFF images.

    Function for loading TIFF images. Supports RGB (8 bits) and 
grayscale (8 and 16 bits) images, 2D and 3D. Alternative version with simpler code.

  */
VglImage* vglLoadTiffAlt(char* inFilename)
{  
  TIFF* tif;
  VglImage* img;
  uint16 pageNumber, numberPages, subfileType;

  tif = TIFFOpen(inFilename, "r");
  if (tif == NULL){
    fprintf(stderr, "%s:%s: Error: File %s not found.\n", __FILE__, __FUNCTION__, inFilename);
    return NULL;
  }
  
  int width  = tif_Width(tif);
  int height = tif_Height(tif);
  int is3d = tif_Is3d(tif);
  int layers = tif_Layers(tif);
  int depth  = tif_BytesPerPixel(tif);          // bytes per pixel
  int iplDepth = convertDepthTiffToVgl(depth);  // depth \in {IPL_DEPTH_8U, ...}
  int nChannels = tif_nChannels(tif);           // number of channels

  if (is3d)
  {
    img = vglCreate3dImage(cvSize(width,height), iplDepth, nChannels, layers, 0);
  }
  else
  {
    img = vglCreateImage(cvSize(width,height), iplDepth, nChannels);
  }

  char* imageData = img->getImageData();
  int widthStep = img->getWidthStep();
  char* buffer = (char*) tif_Malloc(tif);

  int pixelsPerLine = img->getWidth()*img->getNChannels();
  int bytesPerLine = pixelsPerLine*depth;
  int i = 0;
  int offset = 0;
  do
  {
    /*
    for(int i = 0; i < height; i++)
    {
      printf("i = %d\n", i);
      TIFFReadScanline(tif, buffer, i);
      memcpy(imageData + offset, buffer, bytesPerLine);
      offset += widthStep;
    }
    */
    TIFFReadRGBAImage(tif, width, height, (uint32*) buffer, 0);
    memcpy(imageData + offset, buffer, widthStep*height);
    offset += widthStep + height;
  }
  while(TIFFReadDirectory(tif));

  TIFFClose(tif); 

  vglSetContext(img, VGL_RAM_CONTEXT);

  return img;
}
Esempio n. 2
0
VglImage* vglColorDeconv(VglImage *imagevgl, double *mInitial, int find3rdColor /*=0*/)
{
  
  double *mNormal = (double *) malloc(9*sizeof(double));
  double *mInverse = (double *) malloc(9*sizeof(double));
  double log255 = log(255.0);
  
  if(find3rdColor != 0)
  {
    normalizeMatrix3x3(mInitial, mNormal);
    invertMatrix3x3(mNormal, mInverse);
  } else
    {
      normalizeMatrix(mInitial, mNormal);
      rightInverseMatrix2x3(mNormal, mInverse);
    }
  convertBGRToRGB(imagevgl);

  //vglPrintImageInfo(imagevgl);

  // Creation of structure for the new image
  VglImage *newimagevgl;
  newimagevgl = vglCreate3dImage(cvSize(imagevgl->shape[0], imagevgl->shape[1]), imagevgl->depth, imagevgl->nChannels, imagevgl->shape[2]);

  // Composition of the new image
  int nPixels = imagevgl->shape[0]*imagevgl->shape[1];
  for(int j = 0; j < nPixels; j++)
  {
    // log transform the RGB data
    unsigned char r = ((unsigned char *)imagevgl->ndarray)[j*3];
    unsigned char g = ((unsigned char *)imagevgl->ndarray)[j*3+1];
    unsigned char b = ((unsigned char *)imagevgl->ndarray)[j*3+2];
    double rLog = -((255.0 * log(((double)r+1)/255.0))/log255);
    double gLog = -((255.0 * log(((double)g+1)/255.0))/log255);
    double bLog = -((255.0 * log(((double)b+1)/255.0))/log255);
   
    for(int i = 0; i < 3; i++)
    { 
      double rScaled = rLog * mInverse[i*3];
      double gScaled = gLog * mInverse[i*3+1];
      double bScaled = bLog * mInverse[i*3+2];
      double output = exp(-((rScaled + gScaled + bScaled) - 255.0) * log255 / 255.0);
      if(output > 255) 
	output = 255;
      ((unsigned char *)newimagevgl->ndarray)[j*3+i] = (unsigned char)output;
    }
  }

  convertRGBToBGR(newimagevgl);
  return newimagevgl; 
}
Esempio n. 3
0
/** Function for loading TIFF images.

    Function for loading TIFF images. Supports RGB (8 bits) and 
grayscale (8 and 16 bits) images, 2D and 3D.

  */
VglImage* vglLoadTiff(char* inFilename)
{  
  TIFF* tif;
  VglImage* img;
  uint16 pageNumber, numberPages, subfileType;

  tif = TIFFOpen(inFilename, "r");
  if (tif == NULL){
    fprintf(stderr, "%s:%s: Error: File %s not found.\n", __FILE__, __FUNCTION__, inFilename);
    return NULL;
  }
  
  int width  = tif_Width(tif);
  int height = tif_Height(tif);
  int is3d = tif_Is3d(tif);
  int layers = tif_Layers(tif);
  int depth  = tif_BytesPerPixel(tif);          // bytes per pixel
  int iplDepth = convertDepthTiffToVgl(depth);  // depth \in {IPL_DEPTH_8U, ...}
  int nChannels = tif_nChannels(tif);           // number of channels

  if (is3d)
  {
    img = vglCreate3dImage(cvSize(width,height), iplDepth, nChannels, layers, 0);
  }
  else
  {
    img = vglCreateImage(cvSize(width,height), iplDepth, nChannels);
  }

  char* imageData = img->getImageData();

  int pixelsPerFrame = img->getWidth()*img->getHeight()*img->nChannels;
  int bytesPerFrame = pixelsPerFrame*depth;
  int j = 0;
  do{
    char* buffer = (char*)tif_ReadData(tif);
    memcpy(imageData+j, buffer, bytesPerFrame);
    j += bytesPerFrame;
  }while(TIFFReadDirectory(tif));

  TIFFClose(tif); 

  vglSetContext(img, VGL_RAM_CONTEXT);

  return img;
}
Esempio n. 4
0
/** Function to save three RGB images, one for each deconvolved color
 */
void vglSaveColorDeconv(VglImage *imagevgl, double *mInitial, char *outFilename, int find3rdColor /*=0*/)
{
  double *mNormal = (double *) malloc(9*sizeof(double));
  
  if(find3rdColor != 0)
    normalizeMatrix3x3(mInitial, mNormal);
  else
    normalizeMatrix(mInitial, mNormal);

  convertBGRToRGB(imagevgl);
  
  VglImage *newimagevgl[3];
  for(int i = 0; i < 3; i++)
    newimagevgl[i] = vglCreate3dImage(cvSize(imagevgl->shape[0], imagevgl->shape[1]), imagevgl->depth, imagevgl->nChannels, imagevgl->shape[2]);

  int nPixels = imagevgl->shape[0]*imagevgl->shape[1];
  for(int j = 0; j < nPixels; j++)
    for(int i = 0; i < 3; i++)
    { 
      unsigned char gray = ((unsigned char *)imagevgl->ndarray)[j*3+i];
      double rLut = 255.0 - ((255.0 - ((double)gray)) * mNormal[i]);
      double gLut = 255.0 - ((255.0 - ((double)gray)) * mNormal[i+3*1]);
      double bLut = 255.0 - ((255.0 - ((double)gray)) * mNormal[i+3*2]);
      ((unsigned char *)newimagevgl[i]->ndarray)[j*3] = ((unsigned char)rLut);
      ((unsigned char *)newimagevgl[i]->ndarray)[j*3+1] = ((unsigned char)gLut);
      ((unsigned char *)newimagevgl[i]->ndarray)[j*3+2] = ((unsigned char)bLut);
    }

  int lStart = 0;
  int lEnd = 0;
  for(int i = 0; i < 3; i++)
  {
    convertRGBToBGR(newimagevgl[i]);
    char outname[1024];
    sprintf(outname, outFilename, i);
    vglSave3dImage(newimagevgl[i], outname, lStart, lEnd);
  }  
}
Esempio n. 5
0
int vglSave4dTiff(char* filename, VglImage* image, int lStart, int lEnd)
{
  vglCheckContext(image, VGL_RAM_CONTEXT);
  if ( (image->nChannels != 1) && (image->nChannels != 3) )
  {
    fprintf(stderr, "%s: %s: Error: image has %d channels but only 1 or 3 channels supported. Use vglImage4to3Channels function before saving\n", __FILE__, __FUNCTION__, image->nChannels);
    return 1;
  }
  char* temp_filename = (char*)malloc(strlen(filename)+256);
  int c = 0;
  for(int i = lStart; i <= lEnd; i++)
  {
    VglImage* temp_image = vglCreate3dImage(cvSize(image->getWidth(), image->getHeight()), image->depth, image->nChannels, image->getLength());
    temp_image->ndarray = (char*)malloc(temp_image->getTotalSizeInBytes());
    memcpy((char*)temp_image->ndarray,((char*)image->ndarray)+c,temp_image->getTotalSizeInBytes());
    sprintf(temp_filename, filename, i);
    vglSaveTiff(temp_filename, temp_image);
    c += temp_image->getTotalSizeInBytes();
    vglReleaseImage(&temp_image);
  }

  return 0;
}