How to convert image (bmp,jpg....) to .bin file ?

I have a colour image and I want convert it to binary such as .bin (without losing color). In C code, I can easly use 'fopen' but it can't work in matlab. Anybody help me please.
Sorry my E is not good.

1 Commento

dao
dao il 16 Ott 2014
Modificato: dao il 16 Ott 2014
Thank Geoff Hayes for your help, But how can I read this bin ? I use
fid = fopen('myBin.bin', 'r');
R = fread(r,[626 1124 3],'uint8');
imshow(R)
but fread function doesn't receive Three-dimensional matrix. So how can I convert this .bin file to image ?

Accedi per commentare.

 Risposta accettata

dao - if you have read the image from file (as a bmp or jpg) using imread, then you can use fopen and fwrite to write the data to a binary file. For example,
% read the image from file
myImage = imread('someImage.jpg');
% open a file to write to
fid = fopen('myBin.bin','w+');
if fid>0
% write the data to file
fwrite(fid,myImage,'uint8');
% close the file
fclose(fid);
end
In the above, we read in some image and open a file for writing (using w+ to indicate that we wish to open or create a new file for writing). It is assumed that the data type for the image is uint8 so we use that in our precision field of the fwrite function.

4 Commenti

Thank you for your help, But how can I read this bin ? I use
fid = fopen('myBin.bin', 'r');
R = fread(r,[626 1124 3],'uint8');
imshow(R)
but fread function doesn't receive Three-dimensional matrix. So how can I convert this .bin file to image ?
Geoff Hayes
Geoff Hayes il 16 Ott 2014
Modificato: Geoff Hayes il 16 Ott 2014
Try reading it as
R = fread(fid,[626 1124*3],'uint8=>uint8')
Note the multiplication of 1124*3 so that we are only passing two dimensions (with the number of elements still being equal to that with three dimensions). Note also the precision (assumes that we wrote to file as uint8) and the fid (not the r that you have used).
Obviously, R is of the wrong dimension, so just reshape it
R=reshape(R,[626 1124 3]);
to get the original dimensions of the image.
Thank for your help. Can you explain more to me about 'uint8=>uint8' ? I still don't get it
From fread precision, the left hand side of the => is the source, and the right hand side is the output. So 'uint8=>uint8' means that for every 8 bit unsigned integer that we read in, we save this value as an 8-bit unsigned integer in our R (output) matrix. I did this because without it, R would be a matrix of data type double, since the default precision is 'uint8=>double'.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by