Error using bitxor Inputs must have the same size.
4 views (last 30 days)
Show older comments
Hello there,
i have a proplem with bitxor function :
Error using bitxor Inputs must have the same size.
the first input " original " & the second input " cipher " has the following sizes like below :
How can i make original & cipher variable of same size ?
and here is my code :
%Using Logistic chaotic mapping, sequence encryption of grayscale images
original = imread('pp.jpg');
figure(1);
subplot(1,3,1);
imshow(original);
title('original');
[M,N]=size(original);
x=0.1;
u=4;
% %Iterative200Second, achieve full chaotic state
for i=1:200
x=u*x*(1-x);
end
% %Generate a single-dimensional chaotic encryption sequence
A=zeros(1,M*N); %generate1*Mn zero matrix
A(1)=x;
%Generate chaotic sequence
for i=1:M*N-1
A(i+1)=u*A(i)*(1-A(i));
end
%Normalized sequence
B=uint8(255*A); %Convert to 255 type of data
% %Transforming into two-dimensional chaotic encryption sequence
Cipher=reshape(B,M,N); %Reshape changes the shape of the specified matrix, but the number of elements does not change; here B is converted to M line, N columns
Encrypted =bitxor(original ,Cipher); %Tone or operation encryption
figure(1);
subplot(1,3,2);
imshow(Encrypted);
title('Encrypted');
%Decryption
Decrypted=bitxor(Encrypted,Cipher); %Different or operation decryption
figure(1);
subplot(1,3,3);
imshow(Decrypted);
title('Decrypted');
%Draw a histogram of original Image and Encrypted Image
figure(2);
subplot(1,3,1);
imhist(original);
title(' original ');
figure(2);
subplot(1,3,2);
imhist(Encrypted);
title('Encrypted');
figure(2);
subplot(1,3,3);
imhist(Decrypted);
title('Decrypted');
Thanks in advance.
0 Comments
Accepted Answer
Jan
on 11 Nov 2021
Edited: Jan
on 11 Nov 2021
The mistake happens here:
[M,N]=size(original);
The original image is an RGB image, not a grey-scale image as the comments imply, which store its data in [nRows x nColumns x 3] array. With [M, N] = size(img) the N is nColumns*3.
Note:
[M, N] = size(X)
% is not the same as:
M = size(X, 1)
N = size(X, 2)
Better:
nByte = numel(original); % instead of: [M,N]=size(original);
... Modify code to use nByte instead of M*N
Cipher = reshape(B, size(original)); %Reshape changes the shape of the specified matrix, but the number of elements does not change; here B is converted to M line, N columns
Encrypted = bitxor(original ,Cipher);
4 Comments
Jan
on 11 Nov 2021
Edited: Jan
on 11 Nov 2021
Now you are using
Encry1 = bitxor(original1_Data',Cipher1);
The tranpspose operator ' works with matrices only. In the first example your data was an RGB image, which is imported as 3D array. Not the original data are a 2D matrix. Why do you transpose it?
I cannot run your code due to themissing images and the functions "data2image" and "image2data". But you can use the debugger to examine, what's going on: Set a break point in the first line and step through the code. The problem concerns the dimension of the image data and the cipher. Clarify what happens with RGB and grayscale images.
More Answers (0)
See Also
Categories
Find more on Convert Image Type in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!