ollie60 0 Newbie Poster

Hi

I am having an issue with inputting data in the correct format in the code below. The code is modified from the original and I am currently trying to figure it out. Originally data was read in from what I am assuming was a binary file using the function below:

void read_data(char *inputfile,float *Data, int length)
{
    printf("Reading the Wrapped Values form Binary File.............>");
    FILE *ifptr;
    ifptr = fopen(inputfile,"rb");
    if(ifptr == NULL) printf("Error opening the file\n");
    fread(Data,sizeof(float),length,ifptr);
    fclose(ifptr);
    printf(" Done.\n");
}

I have eliminated that function and want to input a .bmp file using the CImg library. As can be seen I read in the image, crop it to the region I am interested in and the assign it to WrappedImage. This is where I think the problem lies as the algorithm does not produse the expected results. How can I make sure that the image that is being assigned to WrappedImage is in the format that read_data implies?

int _tmain(int argc, _TCHAR* argv[])
{
    int x0,y0,x1,y1;
    int Rclick = 0, Lclick = 0;

    // read in image, create display then display image in new window
    CImg <float> inImage("test.bmp");
    int image_width = inImage.width();
    int image_height = inImage.height();
    CImgDisplay wrappedDisplay(800,600,"Wrapped Image - Left click for the one corner of region of interest, left click for the other",
        0, false, false);
    inImage.display(wrappedDisplay);

    // select the region to unwrap with left click selecting one corner and right click the other
    while (!wrappedDisplay.is_closed()) {
         if (wrappedDisplay.button()&1) { // Left button clicked.
            x0 = wrappedDisplay.mouse_x()*image_width/800;
            y0 = wrappedDisplay.mouse_y()*image_height/600;
            Lclick=1;
         }
         else if (wrappedDisplay.button()&2) { // Right button clicked.
            x1 = wrappedDisplay.mouse_x()*image_width/800;
            y1 = wrappedDisplay.mouse_y()*image_height/600; 
            Rclick=1;
         }  
         // close the display when the left and right click have been detected
         if (Rclick + Lclick == 2)  
             wrappedDisplay.close();
    }
    // crop the image to the selected region
    inImage.crop(x0,y0,x1,y1,true);
    inImage.save("WrappedImage.bmp");

    float *WrappedImage, *UnwrappedImage;
    int image_size = inImage.width() * inImage.height();
    int No_of_Edges = (inImage.width())*(inImage.height()-1) + (inImage.width()-1)*(inImage.height());

    WrappedImage = inImage;

    //allocate memory
    UnwrappedImage = (float *) calloc(image_size, sizeof(float));
    PIXEL *pixel = (PIXEL *) calloc(image_size, sizeof(PIXEL));
    EDGE *edge = (EDGE *) calloc(No_of_Edges, sizeof(EDGE));

    //initialise the pixels
    initialisePIXELs(WrappedImage, pixel, inImage.width(),inImage.height());
    calculate_reliability(WrappedImage, pixel, inImage.width(),inImage.height());
    horizentalEDGEs(pixel, edge, inImage.width(),inImage.height());
    verticalEDGEs(pixel, edge, inImage.width(),inImage.height());

    //sort the EDGEs depending on their reliability. The PIXELs with higher reliability (small value) first
    quick_sort(edge, No_of_Edges);

    //gather PIXELs into groups
    gatherPIXELs(edge, inImage.width(), inImage.height());

    //unwrap the whole image
    unwrapImage(pixel, inImage.width(),inImage.height());
    //copy the image from PIXEL structure to the wrapped phase array passed to this function
    returnImage(pixel, UnwrappedImage, inImage.width(),inImage.height());

    free(edge);
    free(pixel);

    //check the output image
    CImg <float> outImage(UnwrappedImage, inImage.width(),inImage.height());
    outImage.save("UnwrappedImage.bmp");

    free(UnwrappedImage);

    CImgDisplay unwrappedDisplay(800,600,"Unwrapped Image",0, false, false);
    outImage.display(unwrappedDisplay);

    generate3dSurface(outImage);

    return 0;
}

Any help will be greatly appreciated!
Thanks