tags:

views:

75

answers:

5

I have a binary image in Matlab, and I need the binary array(0 and 1) in a C program, How can I import it?

I can save the binary image in Matlab as ".mat" but I don't know how to read the mat file in C. Also I thought maybe I can save the binary image in Matlab as ".bin" which is readable in "C" but again I don't know how to save an image in matlab as ".bin".

Any help would be greatly appreciated.

+2  A: 

First of all, check the net using a searching engine. The place to search in first is of course Mathworks, where you can find docs like this. The second step, since it looks complicated to write from scratch a converter, is to search if your need is already someone else need. A link like this could be useful, but there's the requirements of a MATLAB itself (linking with one of its libraries, the MAT-file interface library I suppose). I believe it is however the simpler thing to do.

The next simple solution is to use this with a format we know we can read, If the input array is of class logical, imwrite assumes the data is a binary image and writes it to the file with a bit depth of 1, if the format allows it. BMP, PNG, or TIFF formats accept binary images as input arrays.

BMP images are not hard to be handled by custom routines; for PNG images, you can use png library, and for TIFF, I can't remember, but you can search or choose the other options.

Here other ways of saving data are explained; a plain ascii file could be also easy to be parsed by custom C routines.

ShinTakezou
+2  A: 
>> img=rand(10)>.5
img =
     1     0     1     1     0     0     1     1     0     0
     1     1     0     0     0     1     0     0     1     0
     0     1     1     0     1     1     1     1     1     1
     1     0     1     0     1     0     1     0     1     1
     1     1     1     0     0     0     1     1     1     1
     0     0     1     1     0     0     1     0     0     0
     0     0     1     1     0     1     1     0     1     1
     1     1     0     0     1     0     0     0     1     0
     1     1     1     1     1     1     0     1     0     0
     1     1     0     0     1     0     0     0     1     0

>> dlmwrite('file.dat', img, 'delimiter',' ')
Amro
A: 

If you need to export array data from Matlab and import it into a C program, one easy option (assuming the array isn't gigantic) is to use a plain text file as in intermediate. From Matlab, write the array out as strings to a text file with each element separated by a comma (for example, 1,0,0,1,0,1,1,etc). Save this as a regular ASCII text file. Now, you can open the file in C with fopen, read in a character at a time with fgetc, and reconstruct the array.

For example, the C portion might look like:

FILE* fp;
char  inp[3];
int   array[PLENTY_OF_ROOM], index=0;
fp = fopen("exported_data.txt", "r");
while (fgets(inp, 3, fp) != NULL) {
    if ((inp[0] == '0' || inp[0] == '1') && inp[1] == ',')
        array[index++] = inp[0] - '0';
    else {
        fprintf(stderr, "Error: bad format\n");
        exit(1);
    }
}
bta
The code I posted works for a row vector, but it should be easy enough to adapt for a 2D matrix (just add support for the detection and handling of the newline characters that would appear between rows).
bta
+3  A: 

You can also write your data to a binary file, which will be about twice smaller then text.

img=rand(10)>.5; %# an array from @Amro's answer
fid = fopen('test.bin','w');
fwrite(fid,img,'int8')
fclose(fid);

Then I believe you can read it in C without problems. Remember, MATLAB will write data by columns, not by rows - first 1st column, then 2nd, etc. If you want to read it in C by rows, just transpose the array before writing:

fwrite(fid,img','int8') %# notice ' symbol after img
yuk
A: 

On some platforms (like Windows) you have to specify "binary" when opening the file, otherwise read/written bytes can be interpreted as special characters and may not be read/written to the file as expected:

FILE* fin,fout;
...
fin = fopen("file1.bin", "rb"); // the 'b' indicates "binary"   
fout = fopen("file1.bin", "wb"); // the 'b' indicates "binary"
S.C. Madsen