How to convert an image into a binary stream of data?

36 views (last 30 days)
How to convert an image into a binary stream of data? Is there any Built in function to do that? Thanks in Advance
  1 Comment
Jan
Jan on 25 May 2011
Please describe the input ("an image") and the wanted output ("binary stream") exactly: The dimensions and class are necessary to understand your problem.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 25 May 2011
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 ;-)
  34 Comments
Walter Roberson
Walter Roberson on 22 Jun 2021
Why would you need to put in each image manually ?
movfile = 'rhinos.avi';
obj = videoreader(movfile);
TransmissionState = YourFunctionToInitializeTransmissionState();
while(hasFrame(obj))
B = readFrame(obj);
binary_sequence = reshape((dec2bin(typecast(B,'uint8'), 8) - '0').', 1, []);
stream = binary_sequence;
TransmissionState = YourFunctionToTransmit(TransmissionState, stream);
end
YourFunctionToEndTransmission(TransmissionState);
where you would need to write YourFunctionToInitializeTransmissionState() and YourFunctionToTransmit() and YourFunctionToEndTransmission()
The idea of keeping TransmissionState and updating it as you go, is that you might be transmitting by frame but bitstream might not happen to exactly match a full frame so you might want to have some "left over" to be sent with the next set of data. Or you might be wanting to encode sequence identifiers with the frames; or you might be using a block cypher, or so on. YourFunctionToEndTransmission() would be responsible for flushing out any left-over data in the buffers.
One thing that is not shown here is that the remote end will not know what size the images are unless you force a fixed size (with imresize(B)), so you should probably start by transmitting image size information.
Baghdadi Aya
Baghdadi Aya on 16 Nov 2021
Edited: Baghdadi Aya on 16 Nov 2021
i want to refer from binary sequence to a video sequence, haw can i do that ?
vid = VideoReader('Countdown 5 seconds timer.mp4');%read the video
numberFrames = vid.NumberOfFrames; %read frames in the video
n= numberFrames ;
totBit=0;
for i=1:10:n
image = read(vid,i);%read frames in the video
B = image(:);%convert the binary matrix to vector
%Conversion frames to binary sequence.
binary_sequence = reshape(dec2bin(typecast(B,'uint8'), 8) - '0', 1, []);
totBit=[totBit,binary_sequence];
end
The variable named totBit is a an array corresponding for all the stream of video this stream will pass through a transmit function and received function, in the reception I will got a stream of binary data, that stream I want to reconvert to video, How can I do that ?

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!