MATLAB: How to convert an image into a binary stream of data

no_details

How to convert an image into a binary stream of data? Is there any Built in function to do that? Thanks in Advance

Best Answer

No, there is no built in function to do it. That's partly because "binary stream of data" is not well defined: do you mean an array of 0's and 1's, or do you mean an array of the characters '0' and '1' ?
If you are thinking in terms of sending a stream of bits across a serial port or wifi connection or ethernet connection, then you need to be aware that fwrite() to devices does not support the 'bitN' specification that is supported for files. You also need to be aware that none of those three media support raw bit transport, and that it is not easy to find interface devices that allow you to specify the exact stream of bits for any of those three media.
Anyhow, if you have a numeric array X, you can convert it to an equivalent array of 0's and 1's as follows:
reshape((dec2bin(typecast(X(:),'uint8'),8)-'0').',1,[])
This technique as written will not work for arrays of char or objects or structs or cell arrays -- only numeric arrays.
Converting the stream of bits back to an image is left as an exercise to the reader ;-)