views:

92

answers:

1

Kindly tell me why the original image is not coming with this code. The resulting image receive is yellowish in color,instead of being similar to the image Img_new

Img=imread(‘lena_color.tif’);
Img_new=rgb2gray(img);
Send=zeroes(size(Img_new);
Receive= zeroes(size(Img_new);
Mask= rand(size(Img_new);
for i=1 :256
for j=1:256
Send(i,j)=xor( Img_new(i,j),mask(i,j));
End
End

image(send);
imshow(send);


for i=1 :256
for j=1:256
receive(i,j)=xor( send(i,j),mask(i,j));
End
End

image(receive);
imshow(receive);

plz help

+5  A: 

There are several problems in your code.

  1. MATLAB is case sensitive so end and End is not the same. Same goes for receive, and send.
  2. MATLAB has a lot of matrix-based operations so please use for loops as a last resort since most of these operations can be executed by MATLAB's optimized routines for matrices.
  3. MATLAB's xor returns the logical xor so when it sees two values (or matrices of values) it doesn't matter if it's 234 xor 123 or 12 xor 23 since it is equivalent to 1 xor 1 and 1 xor 1. You're looking for bitxor which does the bitwise xor on each element of the matrix and I've used it in my code below. This is the only way you can retrieve the information with the pixel == xor(xor(pixel,key),key) operation (assuming that's what you want to do).

  4. rand returns a real value from 0 - 1 ; therefore, to do a successful bitwise xor, you need numbers from 0 - 255. Hence, in my code, you'll see that mask has random values from 0-255.

Note: I've used peppers.png since it's available in MATLAB. Replace it with lena_color.tif.

%%# Load and convert the image to gray
img = imread('peppers.png');
img_new = rgb2gray(img);

%%# Get the mask matrix
mask = uint8(rand(size(img_new))*256);

%%# Get the send and receive matrix
send   = bitxor(img_new,mask);
receive = bitxor(send,mask);

%%# Check and display
figure;imshow(send,[0 255]);
figure;imshow(receive,[0 255]);

Update:

%%# Get mask and img somehow (imread, etc.)
img = double(img);
mask_rgb = double(repmat(mask,[1 1 3]));
bitxor(img,mask);

If instead, you choose to make everything uint8 instead of double, then I urge you to check if you are losing data anywhere. img is uint8 so there is no loss, but if any of the values of mask is greater than 255 then making it double will lead to a loss in data.

Jacob
Thanx..this is what i wanted.i figured it out.
gavishna
@gavishna, if you're happy with this answer, it would be good if you would accept it.
Geoff
One small suggestion... you can use [RANDI](http://www.mathworks.com/access/helpdesk/help/techdoc/ref/randi.html) to generate the mask like so: `mask = randi([0 255],size(img_new),'uint8');`
gnovice
@gnovice: Thanks! I should probably use it more often.
Jacob
There is another problem i am facing due to bitxor.The code is as underimg=imread('lena_color.tif'); rawData=dlmread('mask.txt'); mask=reshape(rawData(1:262144),512,512); % Till now its ok send=bitxor(img,mask); %This gives error of Error using ==> bitxor Inputs must be unsigned integers of the same class or scalar doubles. figure,subplot(1,2,1); imshow(send,[0 255]); receive=bitxor(send,mask); imshow(receive,[0 255]);
gavishna
i did double(img);double(mask); but error did not go 2. the same code works for random nos generated using rand and converted into uint8. So i tried to convert mask into uint8(mask(size(img)*256); But to no avail.problem still persists. 3. size(img) = 512 512 3 size(mask)= 512 512 mask(size(img));= 34 34 9 is it not necessary for mask array to be 512 512 3 dimension? Also how to mitigate this error?thanx a ton in advance
gavishna
Before you execute `send = bitxor(img,mask)`, type `whos img mask`. This will tell you the dimensions and the class of these variables. They should be the same. Also, if it's class `double`, that's OK, **if** it only has integers (which is the right thing since you're dealing with images).
Jacob
@Jacob. Mask has only integers.whos img gives img 512x512x3 bytes=786432 clas uint8 arraywhos mask gives 512x512 Bytes 2097152 double array.Now what to do.Kindly help.
gavishna
So you want to use the same mask for all 3 components of the image?
Jacob
Also, the `mask` and `img` have to be the same type. So if you want both to be double, and assuming you want to use the same mask for the 3 components of `img`, we copy the mask along the third dimension. I've updated this in the answer.
Jacob
Thank u Jacob very much.It worked.The conversion of both img and mask to double data type works but converting to uint8 yield an error that it must be scalar or double.Anyways,thanx a ton for your patience and time.
gavishna
Good to know and good luck with the rest!
Jacob