how to convert an image in to binary bits sequence ??????

a=imread('cameramen.jpg');
b= round(a./256);
but i got binary bits like this
1 0 1 0 0 1 0 0 0 0
0 0 0 1 0 1 0 1 0 0
.
.
.
1 0 0 0 0 0 0 0 0 0
0 1 0 1 0 1 0 1 0 0
.
.
1 0 1 0 1 0
0 0 0 0 0 0
but what i want is: b = (1 0 1 0 1 0 0 0 .....n)

 Respuesta aceptada

Guillaume
Guillaume el 4 de Mzo. de 2015
You question is very unclear due to imprecise language.
Are you intending to binarise the image? That is convert each pixel to a single 0 or 1, as you've done with your b = round(a / 256), which convert all pixels below intensity 128 to 0 and all above 127 to 1. Or as your watermarking tag and question title suggest, convert each pixel to a sequence of bits?
If the latter, the best course is actually not to do it and use matlab's bit wise operators. This will be much faster than converting to a matrix of bits and back.
Example, replace LSB of matrix with random 0 or 1:
orig_image = imread('cameraman.tif'); %demo image shipped with matlab
lsb_replacement = uint8(randi([0 1], size(orig_matrix))); %random replacement
%set lsb to 0 (with AND 254) and replace with OR:
new_image = bitor(bitand(orig_matrix, 254), lsb_replacement);
imshowpair(orig_image, new_image, 'montage')
isequal(bitget(new_image, 1), lsb_replacement) %should return true

10 comentarios

user06
user06 el 4 de Mzo. de 2015
yes i want to convert each pixel to a sequence of bits. and i m not getting ur code... i need to convert the image into binary sequence of bits and then afterwards i need to access each and every bit.
The point of my code was to show you that in all likelyhood you don't need to convert to individual bits. In my example, I've replaced the least significant bit of each pixel of the image by another value, just using bit operations.
If you really want to convert the image to a sequence of bits, use dec2bin and subtract ascii code of '0' to convert to [0 1]. Depending on the output you want:
option 1: a linear vector (column 1 first, then column 2, etc.)
reshape(dec2bin(orig_image, 8)' - '0', 1, [])
option 2: a 2D matrix with same height as the image and 8 times the number of columns:
reshape(dec2bin(orig_image', 8)' - '0', [], size(orig_image, 1))'
option 3: a cell array of binary representation of each pixel, the cell array is the same size as the image:
reshape(num2cell(dec2bin(orig_image, 8) - '0', 2), size(orig_image))
user06
user06 el 5 de Mzo. de 2015
Editada: Guillaume el 5 de Mzo. de 2015
i have one more doubt.. ok by these statements i got the sequence of bits.. but when i tried to apply run Length code, it is not giving the correct output. and my code for run Length is:
function data=runLenghts(messageInBits)
length=size(messageInBits);
data =[];
str_len=0;
k=1;
for i=1:length
if messageInBits(i)==messageInBits(i+1)
str_len=str_len+1;
else
data(k)=messageInBits(i);
k=k+1;
data(k)=str_len;
k=k+1;
end
end
end
is there some mistake in the code or what???
Guillaume
Guillaume el 5 de Mzo. de 2015
Editada: Guillaume el 5 de Mzo. de 2015
From a quick read of the code, an obvious mistake is that you never reset the run length str_length to 0 at the end of each run.
Aside from that, although it does not cause problems in your code, you shouldn't be using length as a variable name as it's the name of a matlab function. And numel would be better than size for getting the length of your message.
And of course, vectorising the code would be better:
function rlencoded = runlengthencoding(message)
transitionindices = find(diff(message)) + 1;
transitionlengths = diff([1 transitionindices numel(message)+1]);
valuelengths = [message([1 transitionindices]); transitionlengths];
rlencoded = valuelengths(:)';
end
Guillaume
Guillaume el 5 de Mzo. de 2015
Editada: Guillaume el 5 de Mzo. de 2015
You could also greatly improve the compression of your RLE by only storing the value of the first bit and then just the run lengths. Since the bit value just flip between 0 and 1 at decoding, you just flip the initial value for each length read.
In my code, the output would then be:
rlencoded = [message(1) transitionlengths]; %and no need to calculate valuelengths
user06
user06 el 5 de Mzo. de 2015
it is not giving the correct o/p...
suppose i have a message like [1 1 1 0 0 0 0 1 1 1 1 0 0 1 1 0]
so i want o/p as[ 1 3 0 4 1 4 0 2 1 2 0 1]
Hum:
>>runlengthencoding([1 1 1 0 0 0 0 1 1 1 1 0 0 1 1 0])
ans =
1 3 0 4 1 4 0 2 1 2 0 1
Works fine here.
Unless you've done the compression improvement that I mention in my 2nd comment, which of course gives a different result.
If you look at your output the odd elements are alternating 0 and 1, so you only need to know the first one. So [1 3 4 4 2 2 1] is more compact and encode exactly the same information.
user06
user06 el 7 de Mzo. de 2015
thank u so much..:)
please help me with how to reconstruct the image?
DGM
DGM el 6 de Ag. de 2023
Editada: DGM el 6 de Ag. de 2023
The users on this thread have been inactive for three years, so they're unlikely to respond. If you want to ask a question, start a new thread. When you do, attach relevant images and code so that other people know what you have and what you want.

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 4 de Mzo. de 2015

Editada:

DGM
el 6 de Ag. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by