Filter löschen
Filter löschen

How to xor the image pixel values with values in matrix form?

7 Ansichten (letzte 30 Tage)
Sri Lakshmi
Sri Lakshmi am 20 Feb. 2018
Kommentiert: Jan am 21 Feb. 2018
I am performing image encryption using diffusion method in that I have to xor the image pixel values with key matrix. The image and the matrix are of size 32*96. I am using color image. I have split the image into R,G,B planes. I have converted the pixel values of each plane into binary values using 'dec2bin' command as I need each binary value in 12 bits and also I have converted the decimal values in the matrix using the same command.
%Image of size 32*96 is read in FR
frr=dec2bin(FR(:,:,1),12); % for red plane
frg=dec2bin(FR(:,:,2),12); % for green plane
frb=dec2bin(FR(:,:,3),12); % for blue plane
q2=dec2bin(g,12); % g is the key matrix
q3=xor(frr,q2);% q2 is xored with red plane pixel value
When I did this I got 0 for all 12 bits in q3. When I tried 'xor(str2num(frr),str2num(q2))' I got the values in 1 bit either as 0 or 1 but I need the answer in 12 bits.
For eg, If frr(1,:)=1010000 and q2(1,:)=10101111 I need the answer as q3(1,:)=00001111 Please help me to resolve this issue. Thanks in advance.

Antworten (1)

Jan
Jan am 20 Feb. 2018
Bearbeitet: Jan am 20 Feb. 2018
You can XOR the values directly instead of converting them to char vectors at first.
frr = bin2dec('01010000');
q2 = bin2dec('10101111');
xor(frr, q2)
Or in your case
q3 = xor(FR(:, :, 1), g)
The indirection over converting the data to the char vector containing '0' and '1' is a waste of time. Note that dec2bin dos not create a binary variable, because Matlab does not have such a type yet.
  2 Kommentare
Sri Lakshmi
Sri Lakshmi am 21 Feb. 2018
Thanks. I got values for q3 but it is not a binary string. I need binary string as I have to convert it into decimal and then display it in the form of color image. I am performing encryption I need to decrypt the same. I don't know how to do it as the resultant image is in black and white. Please help me.
Jan
Jan am 21 Feb. 2018
You do not want to get the decimal values, because you want a binary string to convert it to decimal values? The statement "the resultant image is in black and white" is not clear. It depends on how you display what, if it appears as a color or bw image.
The point of my answer is: Converting the data to binary strings is neither required nor efficient. It makes it harder to apply XOR. Better use XOR directly with the uint8 values - if you have them already. You did not explain, what your input FR is.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by