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

42 次查看(过去 30 天)
How to convert an image into a binary stream of data? Is there any Built in function to do that? Thanks in Advance
  1 个评论
Jan
Jan 2011-5-25
Please describe the input ("an image") and the wanted output ("binary stream") exactly: The dimensions and class are necessary to understand your problem.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2011-5-25
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 个评论
Walter Roberson
Walter Roberson 2021-6-22
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 2021-11-16
编辑:Baghdadi Aya 2021-11-16
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 ?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Nonlinear Modeling 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by