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

조회 수: 23 (최근 30일)
Ubaid Ullah
Ubaid Ullah 2011년 5월 25일
편집: Baghdadi Aya 2021년 11월 16일
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에서 Signal Import and Export에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by