How do you convert a char binary stream of an image into a m
How do you convert a char binary stream of an image into a matrix in mat lab? Is it possible to display those binary numbers as equivalent of black and white colour to form an image?
Solution
There is no special built in function to convert char binary image to a matrix. That\'s partly because \"binary stream of data\" is not well defined: so if it is in 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 in your image Each \"number\" tells you part of the color information. If the image file is in an indexed form you will get a NxM array (for an image of the size NxM). If the image is not indexed, you will get an NxMx3 array, where each of the three NxM arrays corresponds, respectively to the red, green and blue channel.