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

42 visualizaciones (últimos 30 días)
Ubaid Ullah
Ubaid Ullah el 25 de Mayo de 2011
Editada: Baghdadi Aya el 16 de Nov. de 2021
How to convert an image into a binary stream of data? Is there any Built in function to do that? Thanks in Advance
  1 comentario
Jan
Jan el 25 de Mayo de 2011
Please describe the input ("an image") and the wanted output ("binary stream") exactly: The dimensions and class are necessary to understand your problem.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 25 de Mayo de 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 comentarios
Walter Roberson
Walter Roberson el 22 de Jun. de 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 el 16 de Nov. de 2021
Editada: Baghdadi Aya el 16 de Nov. de 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 ?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Nonlinear Modeling en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by