Error using bitxor Inputs must have the same size.

4 views (last 30 days)
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 :
workspace variables
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.

Accepted Answer

Jan
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
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.
Muhammad Abdulrazek
Muhammad Abdulrazek on 11 Nov 2021
data2image:
%--------------------------------------------------------------------------
%this function is to transfer data type back to image for display
%
%Chen Zhifeng
%2007-05-19
%zhifeng@ecel.ufl.edu
%I left this function as is since I'm clueless and a neophyte in image
%processing. JC 7/16/08
%--------------------------------------------------------------------------
function im = data2image(Data, row_im, col_im, third_im, M);%changed data to Data
% M =4;
data = Data';%changed data' to Data'
l = length(data);
col =ceil( 8/log2(M) ); %in case M = 8, 32, 64, 128, we need ceil
row= l/col;
Tm = zeros(row,col);
for i = 1:col,
Tm(:,i) = num2str(data((i-1)*row+1 : i*row));
end
%temp = char(Tm);
%Tmn = num2str(temp);
V_im = base2dec(Tm,M);
im = zeros(row_im, col_im, third_im);
for i=1:third_im,
for j=1:col_im,
im(:,j,i) = V_im((i-1)*row_im*col_im+(j-1)*row_im +1 : (i-1)*row_im*col_im+j*row_im);
end
end
image2data:
%--------------------------------------------------------------------------
%this function is to transfer image to data suitable for transmission
%
%Chen Zhifeng
%2007-05-19
%zhifeng@ecel.ufl.edu
%%I left this function as is since I'm clueless and a neophyte in image
%processing. JC 7/16/08
%--------------------------------------------------------------------------
function [data, row_im, col_im, third_im] = image2data(im, M)
% im = imread('photo.bmp');
% M =4;
[row_im, col_im, third_im] =size(im);
V_im = zeros(1, row_im*col_im*third_im);
for i=1:third_im,
for j=1:col_im,
V_im((i-1)*row_im*col_im+(j-1)*row_im +1 : (i-1)*row_im*col_im+j*row_im) = im(:,j,i);
end
end
%I use dec2base here, then if M>8, for example M=16, the result string may
%include characters, this is not appropriate to use str2num below. However,
%due to the time limit, I will not add function here to deal with this.
%Actually, this may be done by dec2bin function.
Tm = dec2base(V_im,M);
[row, col] = size(Tm);
% data = [];
% for i = 1:col,
% data = [data; Tm(:,i)];
% end
%
%data=[];
for i =1:col,
data(row*(i-1)+1:row*i)=str2num(Tm(:,i)); %very important to add str2num here
end
%Tm = str2num(Tm_char);
%data = str2num(data);
Images are normal grey scale images , i used rgb by mistake
my problem is : how to get the image data & the cipher of the same dimensions to accomplish the bitxor function ?

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Tags

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!