how to reduce the running time of the code?

6 views (last 30 days)
Hello, The following code takes around 25 minutes to run...it is based on image processing...i need to reduce the computation time of this code...this includes only the encryption and i still need to perform the decryption,....
clc
clear all
close all
A=imread('C:\Desktop\lena.png');
disp(A);
imshow(A);
for i=1:1:256
for j=1:1:256
B{i,j,1} = dec2bin(A(i,j),8); %%conversion of pixel values to 8 bit binary values from original image
end
end
disp(B);
M=randint(256,256,[0,256]); %%key image generation
disp(M);
for k=1:1:256
for l=1:1:256
N{k,l,1} = dec2bin(M(k,l),8);
end
end
disp(N);
figure,imshow(uint8(M));
%%DNA maping of binary values of original image
codebook1 = containers.Map({'00','11','10','01'},{'G','C','T','A'});
outputCell = cellfun(@(x) values(codebook1, {x(1:2),x(3:4),x(5:6),x(7:8)}),B, 'uni', 0);
C = cellfun(@cell2mat, outputCell, 'uni', 0);
disp(C);
ranopCell = cellfun(@(x) values(codebook1, {x(1:2),x(3:4),x(5:6),x(7:8)}),N, 'uni', 0);
O = cellfun(@cell2mat, ranopCell, 'uni', 0);
disp(O);
fun = @(t,v) chen1(t,v,a,b,c,d,k);
[t, v] = ode45(fun, [0 1.54], v0);
x = v(:,1);
x(257)=[];
y = v(:,2);
y(257)=[];
disp(x);
disp(y);
figure,plot(x,y);
%%sorting
[lx,fx]=sort(x);
[ly,fy]=sort(y);
S=fx;
T=(fy)';
%%matrix generated for scrambling
[Tgrid, Sgrid] = meshgrid( T, S );
Z = arrayfun( @(S,T) [S T], Sgrid, Tgrid, 'UniformOutput', false );
%%scrambled matrix
D=cellfun(@(x) C{x(1),x(2)},Z,'un',0);
disp(D);
P=cellfun(@(x) O{x(1),x(2)},Z,'un',0);
disp(P);
%%XOR operation
ind = reshape(1:65536, 256, 256);
E = arrayfun(@(x) letterXOR(D{x},P{x}), ind, 'uni', 0);
%%decoding using rule 4
codebook2 = containers.Map({'C','G','T','A'},{'00','11','10','01'}); %// Lookup-map values to key
outputCell1 = cellfun(@(x) values(codebook2, {x(1),x(2),x(3),x(4)}),E, 'uni', 0);
F = cellfun(@cell2mat, outputCell1, 'uni', 0);
disp(F);
G=cellfun(@bin2dec,F);
disp(G)
figure,imshow(uint8(G));
thanks in advance..

Accepted Answer

Geoff Hayes
Geoff Hayes on 3 Sep 2014
Abirami - you should profile your code to see where the bottlenecks are. Visit the link profile for details on how to use this tool which will identify where your code spends the most time.
A couple of thoughts - I noticed that you have a two loops where you build the binary equivalent of the 256x256 image and the encryption key. Both of these loops assign the binary string to a cell array. It is good practice to allocate memory to the arrays before using them so that MATLAB does not have to resize them on each iteration (see pre-allocate for details). You could do something like
% pre-allocate memory for the 256x256 cell array
B = cell(256,256);
for i=1:1:256
for j=1:1:256
B{i,j} = dec2bin(A(i,j),8); %%conversion of pixel values to 8 bit binary
end
end
This may help, but maybe not as much as you would like. I think that the code is (initially) spending some time in the conversion of all 256*256=65536 (grayscale) pixels into their 8-bit string equivalents using dec2bin. I think that you can avoid all of these conversions (for the image and key) by just creating a map of all 256 integers to their 4-character DNA string. From
codebook1 = containers.Map({'00','11','10','01'},{'G','C','T','A'});
we can create a local map object that maps the values of 0-3 to each of the above characters
map = ['G';'A';'T';'C'];
Here we map '00' to index 1 which is 'G', '01' to index 2 which is 'A', etc. We can then build a 256x4 codebook that maps all possible integers from 0 to 255 (in indices 1 to 256) to the 4-character DNA codes as
codebook1a = char(zeros(256,4));
for k=0:255
x = dec2bin(k,8);
codebook1a(k+1,:) = [map(bin2dec(x(1:2))+1) map(bin2dec(x(3:4))+1) ...
map(bin2dec(x(5:6))+1) map(bin2dec(x(7:8))+1)];
end
Then create C which will be the 256x256 image converted from its 8-bit binary values to the 4-character DNA codes
C = arrayfun(@(x)codebook1a(x+1,:),A,'uni',0);
We add the one to account for the mapping of 0-255 to 1-256. This would replace the need to do any conversion from the 8-bit unsigned integer to a 8-character string to the 4-character DNA code.
We would do the same for the key as
O = arrayfun(@(x)codebook1a(x+1,:),M,'uni',0);
The above assumes that your input image is grayscale and each pixel is a 8-bit unsigned integer (which I think is a safe assumption given how the code has been constructed).
The above may only reduce the running time by a few seconds (with tic/toc I observed a 15 second improvement from the start of the program to the line where O is initialized).
After that comes the use of a function named chen1. This must be a custom function, so it is unclear what it does so perhaps the profiling will reveal something useful.
Definitely comment out the disp calls as they don't really add anything relevant - they just display the contents of the matrices in the Command Window. Consider using code similar to above to work the other way in the conversion of the DNA codes to the 8-bit unsigned integers. The function letterXOR maybe something else that you want to post as it must be another custom function that could be optimized.

More Answers (2)

Guillaume
Guillaume on 3 Sep 2014
The method you're using to convert your integer matrices to DNA sequences is very inefficient (2 x for loops, wrong base, map lookups). I gave you a while back an extremely efficient method that does not involve any loop. See my answer to your previous question convert binary to charaacter. I doubt you could find a more efficient way of performing that conversion.
Similarly, I gave you an extremely efficient way to perform the xor operation in your other question how to perform bitwise XOR operation and scramble two matrices
As I've mentioned before, converting to binary is the wrong thing to do and unnecessarily complicate your code. You're actually operating in base 4, where G=0, C=1, T=2, A=3. If you use base 4, you're then using single digits, no more x(3:4).
Conclusion:
- precompute all the conversions,
- use base 4,
- use the profiler to see where the bottlenecks are.
  1 Comment
Abirami
Abirami on 4 Sep 2014
sir i tried ur code...it has been the most efficient one....and the computation time has considerably reduced..

Sign in to comment.


Supreeth Murugesh
Supreeth Murugesh on 29 Aug 2017
Which is the algorithm used in the above code ?

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!