how use parfor for image encryption

1 view (last 30 days)
omar A.alghafoor
omar A.alghafoor on 26 Jul 2020
Commented: omar A.alghafoor on 31 Jul 2020
Hi .....
I am trying to write this code in parallel but I couldn't do that :
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [layer_image] = Encryption_SBOX(layer_image,SBox)
[rows,cols,~] = size(layer_image);
%upperLeft
STARTblockImage1_rows=1;
STARTblockImage1_cols=1;
ENDblockImage1_rows=floor(rows/2);
ENDblockImage1_cols=floor(cols/2);
%upperRight
% STARTblockImage2_rows=1;
STARTblockImage2_cols=floor(cols/2);
% ENDblockImage2_rows=floor(rows/2);
% ENDblockImage2_cols=cols;
%lowerLeft
STARTblockImage3_rows=floor(rows/2);
% STARTblockImage3_cols=1;
% ENDblockImage3_rows=rows;
% ENDblockImage3_cols=floor(cols/2);
% lowerRight5
STARTblockImage4_rows=floor(rows/2);
STARTblockImage4_cols=floor(cols/2);
% ENDblockImage4_rows=rows;
% ENDblockImage4_cols=cols;
BZ=16; % block size
% flipfiop=0;
for i=STARTblockImage1_rows:BZ:ENDblockImage1_rows0
for j=STARTblockImage1_cols:BZ:ENDblockImage1_cols
iblock=i;
while(iblock<=i+BZ)&&(iblock<=ENDblockImage1_rows)
jblock=j;
while(jblock<=j+BZ)&&(jblock<=ENDblockImage1_cols)
%upperLeft
x = dec2bin(layer_image(iblock,jblock),8);
x = dec2bin(Temp1(i,j),8);
l = x(1, 1 : 4);
r = x(1, 5 : 8);
ld = bin2dec(l); % row
rd = bin2dec(r); % column
layer_image(iblock,jblock)=SBox(ld+1,rd+1);
% layer_image(iblock,jblock)=flipfiop;
upperRight
if ((jblock+STARTblockImage2_cols)<=cols)
x = dec2bin(layer_image(iblock,(jblock+STARTblockImage2_cols)),8);
l = x(1, 1 : 4);
r = x(1, 5 : 8);
ld = bin2dec(l); % row
rd = bin2dec(r); % column
layer_image(iblock,(jblock+STARTblockImage2_cols))=SBox(ld+1,rd+1);
else
disp(jblock+STARTblockImage2_cols);
end
%lowerLeft
x = dec2bin(layer_image(iblock+STARTblockImage3_rows,jblock),8);
l = x(1, 1 : 4);
r = x(1, 5 : 8);
ld = bin2dec(l); % row
rd = bin2dec(r); % column
layer_image(iblock+STARTblockImage3_rows,jblock)=SBox(ld+1,rd+1);
%lowerRight
x = dec2bin(layer_image(iblock+STARTblockImage4_rows,jblock+STARTblockImage4_cols),8);
l = x(1, 1 : 4);
r = x(1, 5 : 8);
ld = bin2dec(l); % row
rd = bin2dec(r); % column
layer_image(iblock+STARTblockImage4_rows,jblock+STARTblockImage4_cols)=SBox(ld+1,rd+1);
jblock=jblock+1;
end
iblock=iblock+1;
end
end
end
  3 Comments
Walter Roberson
Walter Roberson on 26 Jul 2020
Which variable are you trying to parfor over?
omar A.alghafoor
omar A.alghafoor on 27 Jul 2020
Edited: Walter Roberson on 28 Jul 2020
I rewrite above code :
function [layer_image] = Encryption_SBOX(layer_image,SBox)
[rows,cols,~] = size(layer_image);
%upperLeft
STARTblockImage1_rows=1;
STARTblockImage1_cols=1;
ENDblockImage1_rows=floor(rows/2);
ENDblockImage1_cols=floor(cols/2);
%upperRight
STARTblockImage2_rows=1;
STARTblockImage2_cols=floor(cols/2)+1;
ENDblockImage2_rows=floor(rows/2);
ENDblockImage2_cols=cols;
%lowerLeft
STARTblockImage3_rows=floor(rows/2)+1;
STARTblockImage3_cols=1;
ENDblockImage3_rows=rows;
ENDblockImage3_cols=floor(cols/2);
% lowerRight5
STARTblockImage4_rows=floor(rows/2)+1;
STARTblockImage4_cols=floor(cols/2)+1;
ENDblockImage4_rows=rows;
ENDblockImage4_cols=cols;
% block size
% flipfiop=0;
%parparing buffer for rows in 4 blocks
Temp1=layer_image(1,floor(cols/2));
Temp2=layer_image(1,floor(cols/2)+1:cols);
Temp3=layer_image(floor(rows/2)+1,floor(cols/2));
Temp4=layer_image(floor(rows/2)+1,cols);
for i=STARTblockImage1_rows:ENDblockImage1_rows
Temp1=layer_image(i,ENDblockImage1_cols);
Temp2=layer_image(i,STARTblockImage2_cols:ENDblockImage2_cols);
Temp3=layer_image(STARTblockImage3_rows+i,ENDblockImage1_cols);
Temp4=layer_image(STARTblockImage4_rows+i,STARTblockImage1_cols+1:ENDblockImage4_cols);
parfor j=STARTblockImage1_cols:ENDblockImage1_cols
%upperLeft
x = dec2bin(Temp1(i,j),8);
l = x(1, 1 : 4);
r = x(1, 5 : 8);
ld = bin2dec(l); % row
rd = bin2dec(r); % column
Temp1(i,j)=SBox(ld+1,rd+1);
% upperRight
x = dec2bin(Temp2(i,j),8);
l = x(1, 1 : 4);
r = x(1, 5 : 8);
ld = bin2dec(l); % row
rd = bin2dec(r); % column
Temp2(i,j)=SBox(ld+1,rd+1);
% %lowerLeft
x = dec2bin(Temp3(i,j),8);
l = x(1, 1 : 4);
r = x(1, 5 : 8);
ld = bin2dec(l); % row
rd = bin2dec(r); % column
Temp3(i,j)=SBox(ld+1,rd+1);
% % %lowerRight
x = dec2bin(Temp4(i,j),8);
l = x(1, 1 : 4);
r = x(1, 5 : 8);
ld = bin2dec(l); % row
rd = bin2dec(r); % column
Temp4(i,j)=SBox(ld+1,rd+1);
end
layer_image(i,ENDblockImage1_cols)=Temp1;
layer_image(i,STARTblockImage2_cols:ENDblockImage2_cols)=Temp2;
layer_image(STARTblockImage3_rows+i,ENDblockImage1_cols)=Temp3;
layer_image(STARTblockImage4_rows+i,STARTblockImage1_cols+1:ENDblockImage4_cols)=Temp4;
end
end
but there are error :
Error using test_nested_parallel (line 65)
Index in position 2 exceeds array bounds (must not exceed 1).
I think because temp( 1or others)

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 26 Jul 2020
Edited: Walter Roberson on 26 Jul 2020
% layer_image(iblock,jblock)=flipfiop;
upperRight
You do not have any function or variable named upperRight
  3 Comments
Walter Roberson
Walter Roberson on 28 Jul 2020
If you expect an RGB image then why are you using
Temp1=layer_image(1,floor(cols/2));
which would be for accessing a grayscale or colormap image?
Consider
Temp1=layer_image(1,floor(cols/2),:);
However, if you are only passing in one color pane at a time, then
[rows,cols,~] = size(layer_image);
confuses the issue -- it would not technically be wrong, but it would lead the reader to expect that there is a 3rd dimension.
omar A.alghafoor
omar A.alghafoor on 31 Jul 2020
thank you , will try this solution.

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!