Exemplo n.º 1
0
/**
* Read a binary image from a file where the pixels are stored as 32 bit
* floating point numbers using Sun byte order.  The compiler directive WINDOWS
* is used to control how the byte order is interpreted when storing the pixels
* into the data structure images. Images are a one dimensional array of floats
* that contains each successive image in sequence, where each image has in turn
* been unrolled into a 1 dimensional vector.
*
* @param fname The name of the file from which to read the image.
* @param n The index of this image in the images array.
* @param images The matrix into the image is read. Only the nth column is affected.
*/
void readFile(const char *fname, int n, Matrix images) {
    int i;
    FILE *f;

    char nan_error[100];
    char inf_error[100];
    char line[FILE_LINE_LENGTH];
    char imagetype[FILE_LINE_LENGTH];

    if (debuglevel > 1)
        printf("\nReading file: %s\n", fname);

    /* Check to makesure file is of expected size */
    if (images->row_dim != autoFileLength(fname)) {
        DEBUG_CHECK(autoFileLength(fname) < images->row_dim, "File does not contian enough values");
        printf("Warning: file length is greater than vector length.  Croping file...\n");
    }

    f = fopen( fname, "rb" );
    if ( !f ) { printf("Can't open %s\n", fname); exit(1); }

    /* check to see if image is of type RASTER_ID */
    fgets(line,FILE_LINE_LENGTH,f); /* only read in enough to determine
                                    * if the file is of proper type */
    sscanf(line,"%s",imagetype);
    if(strcmp(imagetype,RASTER_ID) == 0 || strcmp(imagetype,"CSU_RASTER") ==0){
        rewind(f);
        /* read in the first line to move the buffer to the data. */
        fgets(imagetype,FILE_LINE_LENGTH,f);
    }
    else{
        /* There is no header info. Just read the data */
        rewind(f);
    }

    /* Set up error messages for use later */
    sprintf(nan_error, "Not A Number value in file: %s", fname);
    sprintf(inf_error, "Infinite value in file: %s", fname);

    for (i = 0;i < images->row_dim;i++) {
        float flt;
        /* read in the correct byte order for floating point values */
        readFloat(f, &flt);

#ifdef CHECK_VALS
        /* Check values to make sure they are real */
        FINITE(flt);
#endif
        /* Save value to images */
        ME(images, i, n) = (double)flt;
    }
    fclose(f);
}
Exemplo n.º 2
0
/*
This function reads images in to a vector.  That vector is then mean subtracted
and then projected onto an optimal basis (PCA, LDA or LPP). Returned is a matrix
that contains the images after they have been projected onto the subspace.
*/
Matrix
readAndProjectImages (Subspace *s, char *imageNamesFile, char *imageDirectory, int *numImages, ImageList **srt)
{
    int i, j;
    Matrix images, vector, smallVector;
    char name[FILE_LINE_LENGTH];
    ImageList *subject, *replicate;

    DEBUG(1, "Reading training file names from file");

    *srt = getImageNames(imageNamesFile, numImages);

    DEBUG_CHECK(*srt, "Error: header no imagenames found in file image list file");

    /* Automatically determine number of pixels in images    */
    sprintf(name, "%s/%s", imageDirectory, (*srt)->filename);
    DEBUG(1, "Autodetecting number of pixels, i.e. vector length based on the size of image 0.");
    DEBUG_CHECK (autoFileLength(name) == s->numPixels, "Images sizes do not match subspace basis vector size");
    DEBUG_INT(1, "Vector length", s->numPixels);
    DEBUG_CHECK(s->numPixels > 0, "Error positive value required for a Vector Length");

    /*Images stored in the columns of a matrix */
    DEBUG(1, "Allocating image matrix");

    images = makeMatrix(s->basis->col_dim, *numImages);
    vector = makeMatrix(s->numPixels, 1);

    i = 0;
    for (subject = *srt; subject; subject = subject->next_subject) {
        for (replicate = subject; replicate; replicate = replicate->next_replicate) {
            if (debuglevel > 0)
                printf("%s ", replicate->filename);
            sprintf(name, "%s/%s", imageDirectory, replicate->filename);
            replicate->imageIndex = i;
            readFile(name, 0, vector);

            writeProgress("Reading images", i,*numImages);

            smallVector = centerThenProjectImages(s, vector);

            /* Copy the smaller vector into the image matrix*/
            for (j = 0; j < smallVector->row_dim; j++) {
                ME(images, j, i) = ME(smallVector, j, 0);
            }
            freeMatrix(smallVector);
            i++;  /* increament the image index */
        }
        if (debuglevel > 0)
            printf("\n");
    }

    return images;
}
Exemplo n.º 3
0
/**
* This function reads in the image list file.  It then
* takes the image filenames and reads in every file.
*
* @returns A Matrix containing all of the images
*/
Matrix readImages(char *imageNamesFile, char *imageDirectory, int *numPixels, int *numImages, int *numSubjects, ImageList **srt) {
    int i;
    Matrix images;
    ImageList *subject, *replicate;

    DEBUG(1, "Reading training file names from file");

    *srt = getImageNames(imageNamesFile, numImages);

    DEBUG_CHECK(*srt, "Error: header no imagenames found in file image list file");

    /* Automatically determine number of pixels in images    */

    DEBUG(1, "Autodetecting number of pixels, i.e. vector length based on the size of image 0.");
    *numPixels = autoFileLength(makePath(imageDirectory, (*srt)->filename));
    DEBUG_INT(1, "Vector length", * numPixels);
    DEBUG_CHECK(*numPixels > 0, "Error positive value required for a Vector Length");

    /* Images stored in the columns of a matrix */
    DEBUG(1, "Allocating image matrix");
    images = makeMatrix(*numPixels, *numImages);

    i = 0;
    (*numSubjects) = 0;
    for (subject = *srt; subject; subject = subject->next_subject) {
        for (replicate = subject; replicate; replicate = replicate->next_replicate) {
            if (debuglevel > 0)
                printf("%s ", replicate->filename);
            replicate->imageIndex = i;
            readFile(makePath(imageDirectory, replicate->filename), i++, images);
        }
        if (debuglevel > 0)
            printf("\n");
        (*numSubjects)++;
    }

    return images;

}
Exemplo n.º 4
0
    for EACH_SUBJECT (imlist, subject) {
        for EACH_REPLICATE (subject, replicate) {
            subjName = strdup (replicate->filename);
            listAccumulate (&nameList, &subjName, sizeof (char *));
            listAccumulate (&subjList, &subjId,   sizeof (int));
            writeProgress ("Reading subjects list", subjId, 0);
        }
        subjId++;
    }

    nameArray = listToNullTerminatedArray (&nameList, sizeof (char *), NULL);
    subjArray = listToNullTerminatedArray (&subjList, sizeof (int),    NULL);

    /* Allocate storage for source images and difference images */

    numPixels = autoFileLength (makePath (imageDirectory, nameArray[0]));

    sourceImages = makeMatrix (numPixels, nImages);
    intraImages  = makeMatrix (numPixels, reqNIntra);
    extraImages  = makeMatrix (numPixels, reqNExtra);

    DEBUG_CHECK (sourceImages != NULL, "Not enough memory to allocate matrix");
    DEBUG_CHECK (intraImages  != NULL, "Not enough memory to allocate matrix");
    DEBUG_CHECK (extraImages  != NULL, "Not enough memory to allocate matrix");

    /* Load in all the source images */

    for (i = 0; i < nImages; i++) {
        readFile (makePath (imageDirectory, nameArray[i]), i, sourceImages);
        writeProgress ("Loading source images", i, nImages);
    }