Click here to Skip to main content
15,922,145 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm using c++ and OpenCV in a project and I need to write an image to file, but because I don't want to lose information from a 32bit single-channel image to an 8bit image I tried writing the values to a .txt and then read them.
For this I was using:

C#
IplImage *L = cvCreateImage(cvSize(width, height), 32, 1);

// ...
// some code building the image

FILE *Ltxt = fopen("L.txt", "w");

for (int h = 0; h < height; h++) {
    for (int w = 0; w < width; w++) {
        fprintf(Ltxt, "%g ", ((float*)(L->imageData + h * L->widthStep))[w]);
    }
}
fclose(Ltxt);


and then I read it elsewhere:

C#
IplImage *L = cvCreateImage(cvSize(width, height), 32, 1);
FILE *Ltxt = fopen("L.txt", "r");

float tf;
for (int h = 0; h < height; h++) {
    for (int w = 0; w < width; w++) {
        fscanf(Ltxt, "%g", &tf);
        ((float*)(L->imageData + h * L->widthStep))[w] = tf;
    }
}

fclose(Ltxt);


I checked the values that I'm getting when I read and write the file and they are the same, but the images do not match.
I also tried using ofstream and ifstream and but the same thing happened.

Does anybody know what I'm doing wrong or another way to preserve the 32bit data of the image?
I'm really stuck here. Thanks.
Posted
Updated 14-Dec-10 13:59pm
v4

What are you using float?? Just us an unsigned 32-bit Integer and you won't lose anything.
 
Share this answer
 
Comments
Sandeep Mewara 15-Dec-10 0:54am    
OP has posted a long comment to your answer as an answer.
Thanks Dave for your reply, but either way I found out a more friendly way to do this that actually works:

To save:

MIDL
CvFileStorage* Lfs = cvOpenFileStorage("L.yml", 0, CV_STORAGE_WRITE);
cvWrite(Lfs, "L", L);
cvReleaseFileStorage(&Lfs);



To read:

MIDL
CvFileStorage* Lfs = cvOpenFileStorage("L.yml", 0, CV_STORAGE_READ);
IplImage *L = (IplImage*) cvReadByName(Lfs, 0, "L");
cvReleaseFileStorage(&Lfs);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900