how to convert an image into bits???

how we can convert an image into bits ?? and that bits back to image?

2 Comments

A relatively simple solution (since what you want is quite definitely not obvious) is to scan it, then print the scanned image.
actually i want to insert the image bits into different image. so i need to convert the image into binary bits..

Sign in to comment.

Answers (5)

It is not clear to me exactly what is your goal? Are you trying to convert image pixels into binary bits? If so, you can use dec2bin for that, and bin2dec to get your image back.
I_in = your image...
% encoding image into array of bits
B = dec2bin(I_in);
C = reshape(B',1,numel(B)); % converted into bits
% decoding image from bits
D = reshape(C,size(B,2),size(B,1));
I_out = reshape(bin2dec(D'),size(I_in))

16 Comments

user06
user06 on 25 Feb 2015
Edited: user06 on 25 Feb 2015
this code is not giving the output image as the input image. input and output images i m attaching. and yes i m trying to convert image pixels into binary bits.
I_out in my code is double type. So, when plotting as an image using imshow, convert that first into uint8, and that will give you a perfect reconstruction. Hope this helps.
I_in = imread('input_image.jpg'); % image that you attached
% encoding image into array of bits
B = dec2bin(I_in);
C = reshape(B',1,numel(B)); % converted into bits
% decoding image from bits
D = reshape(C,size(B,2),size(B,1));
I_out = reshape(bin2dec(D'),size(I_in));
subplot(121), imshow(I_in), title('Input Image')
subplot(122), imshow(uint8(I_out)), title('Reconstracted Image')
I completely fail to see the utility of converting an image into a string of '0' and '1' characters. What useful processing can you do afterward, that wouldn't be obtained much faster by not bothering with the string conversion?
If you want to perform bit operations, there are matlab functions that operate on bits without any transformation of the original data.
actually i m working on watermarking.and i need to process each and every pixel of image. and according to the pixel value (whether 0 or 1) i need to take appropriate action.
A simple search through the forum for steganography would have shown you that this question is asked and answered at least once a week.
You can recover the image adding this at the end of the code, also I found very useful converting an image to bits (I'm working on a simulation of a convolutional channel encoding and Viterbi decoding) an this helped me a lot thanks ;)
imwrite(uint8(I_out),'path of your new image')
in the decoding i get just get a white image as out put
Then you did something wrong, either on computing the image, or displaying it. That's all I can say with what you've given us.
B: Cannot display variables with more than 524288 elements.
C: is displaying the binary stream
D: Cannot display variables with more than 524288 elements.
I: is working
I_out: working
Dear Shoaibur Rahman, Ur code is usefull for me.
My purpose is search the Binary-pattern into binary stream of color image, then I want to implement the concept of Steganography for my research.
Please please provide me the code to search the binary-pattern into file of binary-stream of Colored image,
Please help me, how to solve given problem
B: Cannot display variables with more than 524288 elements.
D: Cannot display variables with more than 524288 elements.
Sanjeev Tyagi
Sanjeev Tyagi on 14 Mar 2017
Edited: Walter Roberson on 14 Mar 2017
In MATLAB variables values are not displayed of following variables
B: Cannot display variables with more than 524288 elements.
C: is displaying the binary stream
D: Cannot display variables with more than 524288 elements
so Binary stream could not be displayed, please help me
524288 elements is 512 x 1024. If you were to display more than that many elements at the same time, you would be devoting an average of less than 2 pixels each to them. At that point you should be considering using imagesc() to display the array.
You cannot get around the 524288 limit; it is built-in to MATLAB. If you need to use the variable browser to look at part of the array, then extract a portion of the array that is no more than 512 x 1024 (or 1024 x 512) and assign it to a variable and view that variable.
Or just disp() the entire array.
how to convert the image into a bit sequence according to the size of the image?
the image has a size of 7x7 pixels, so i want to convert the image into bit like this. How can i do it, sir?
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 1 1 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 1 0 1 0 0
0 0 1 0 1 0 0
filename = 'flamingos.jpg';
img = imread(filename);
if ndims(img) > 2
img = rgb2gray(img);
end
img = imresize(img, [7 7]);
%img is now 7 x 7 grayscale
BW = imbinarize(img);
as_bits = double(BW)
as_bits = 7×7
0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 1 1 1 0 0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 1 1 1 1 1 1 0 0 1 1 1 1 1 1
@Walter Roberson the image has a size of 1920x2560 , i want to convert the image into bit , How can i do , thanks you
https://www.mathworks.com/matlabcentral/answers/180075-how-to-convert-an-image-into-bits#answer_169133 shows how.

Sign in to comment.

Image Analyst
Image Analyst on 24 Feb 2015
It's already in bits, as are all numbers in computers.

7 Comments

then how to extract it?
We don't know what you want. The image is already a variable in your program that you got from imread() presumably. Do you want to turn a gray level into a 1's and 0's string of 8 bits for each number? If so, why? But if so, you can use code given by Shoaibur. If not then you need to explain what you want to do? Are you using local binary patterns for image processing or what? You seem to be a person of very very few words, and this does not help us to answer your question.
actually i want to insert the image bits into different image. so i need to convert the image into binary bits..
See my attached demo for copying and pasting an image or portion of an image that you specify into a bigger image.
Please describe what you want. No one can figure it out. I just gave you code to do copying and pasting. Is that what "insert the image bits into different image" means? Who knows? Only you. Or perhaps you mean watermarking or steganography, though you didn't add any tag for those concepts so I have my doubts. But your imprecise language leaves them as possibilities. Will you clear up exactly what you want? I have a demo for LSB Steganography if that's what you want, but I have hundreds of demos and I can't post them all, so let me know if you're working on steganography.
actually i m working on watermarking.and i need to process each and every pixel of image. and according to the pixel value (whether 0 or 1) i need to take appropriate action.
"actually i m working on watermarking"
See my atttached demo on Least significant bit watermarking.

Sign in to comment.

Image Analyst
Image Analyst on 25 Nov 2021
If you want to view bitplanes 0 through 7, as binary images, see my attached demo.
Hello there! I hope you can help me with this question... I'm simulating a Digital Comm System, so i want to transmit an image and for that i converted that image into a bitstream, i used "imbinarize" function and reshape the matrix into a row vector for the "transmission" which function should i use to make the opposite process for the recosntruction of the image? Thanks!
Walter Roberson
Walter Roberson on 18 Jan 2022
reshape() to reconstruct the image. The remote side will need to know the image size, so either the size will need to be fixed or else you will need to transmit size information.
Note that your process will end up with a black and white image. If you want grey or color then you will need to use something different than imbinarize

Asked:

on 24 Feb 2015

Answered:

on 18 Jan 2022

Community Treasure Hunt

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

Start Hunting!