how can I reduce the execution time of the given code?

i have the following code which takes a 311 kb JPEG image as input. It takes more than half hour to execute this code.
clc;
clear all;
close all;
workspace;
image= imread('1.jpg');
% extracting green channel
t2 = image(:,:,2);
[row, col]=size(t2);
k=(row-16+1)*(col-16+1);
i1 = 1
% feature extraction
for i=1:row-15
for j=1:col-15
X(1,i1) = i
X(2,i1) = j
AC = t2(i:i+15,j:j+15)
f(1) = mean2(AC)
B = mat2cell(AC, [8 8],[8 8])
s1 = mean2(B{1,1})
s2 = mean2(B{1,2})
s3 = mean2(B{2,1})
s4 = mean2(B{2,2})
f(2) = (s1/(4 * f(1) + 0.01))
f(3) = (s2/(4 * f(1) + 0.01))
f(4) = (s3/(4 * f(1) + 0.01))
f(5) = (s4/(4 * f(1) + 0.01))
f(6) = s1 - f(1)
f(7) = s2 - f(1)
f(8) = s3 - f(1)
f(9) = s4 - f(1)
m1 = max([f(6),f(7),f(8),f(9)])
m2 = min([f(6),f(7),f(8),f(9)])
X(3,i1) = floor(f(1))
X(4,i1) = floor(255 * f(2))
X(5,i1) = floor(255 * f(3))
X(6,i1) = floor(255 * f(3))
X(7,i1) = floor(255 * f(5))
X(8,i1) = floor(255 * ((f(6) - m2)/(m1 - m2 + 0.01)))
X(9,i1) = floor(255 * ((f(7) - m2)/(m1 - m2 + 0.01)))
X(10,i1) = floor(255 * ((f(8) - m2)/(m1 - m2 + 0.01)))
X(11,i1) = floor(255 * ((f(9) - m2)/(m1 - m2 + 0.01)))
i1 = i1 + 1
end
end
Y = X;
for i1 = 11:-1:3
m1 = Y;
f = Y(i1,:);
m2 = zeros(1,256);
for j = 1:k
m2(f(j)+1) = m2(f(j)+1) + 1;
end
% Convert to cumulative values
for i = 2:256
m2(i) = m2(i) + m2(i - 1);
end
% Sort the array
for j = k:-1:1
Y(:,m2(f(j)+1))= m1(:,j);
m2(f(j)+1) = m2(f(j)+1) - 1;
end
end
P(1,:) = Y(1,:);
P(2,:) = Y(2,:);
for j=1:k-1
P(3,j) = sqrt(((P(1,j+1) - P(1,j))*(P(1,j+1) - P(1,j)))+((P(2,j+1) - P(2,j))*(P(2,j+1)-P(2,j))));
P(3,j) = floor(P(3,j));
end
AC =sort(P(3,:));
how can i reduce execution time.

5 Comments

@Neetha Mary: please do not put empty lines in your code in some attempt to display the code correctly. I just formatted your code correctly for you, it really is very simple:
  • paste your code text.
  • select all of the code text
  • click the {} Code button above the textbox.
DO NOT add empty lines to try and make it look like code. This does not work.
If you use a smaller image and profile the code, where does it spend the most time?
After adding semicolons to all of the lines I ran the code with a small image and found that this line takes 40% of processing time:
B = mat2cell(AC, [8 8],[8 8]);
The cell array B is used on the following four lines, like this:
s1 = mean2(B{1,1})
Removing mat2cell and using basic matrix indexing would remove this bottle-neck.
I know what mat2cell is doing, I just pointed out that is not necessary. See my answer to know how you can remove the slowest operation from your code.
@Neetha Mary: it is considered polite on this forum to accept the answer that best resolves your question.

Sign in to comment.

 Accepted Answer

After adding semicolons to all of the lines I ran the code with a small image and using the profiler, and found that this one line takes 40% of the total processing time:
B = mat2cell(AC, [8 8],[8 8]);
The cell array B is used on the following four lines, like this:
s1 = mean2(B{1,1})
Removing mat2cell and using basic matrix indexing would remove this bottle-neck. You can access those blocks directly using indexing, without using mat2cell, which will be twice as fast (the test script is attached below):
Elapsed time is 12.305447 seconds.
Elapsed time is 6.675523 seconds.
By removing the mat2cell I halved the time for that operation. You should replace all of these lines:
AC = t2(i:i+15,j:j+15)
f(1) = mean2(AC)
B = mat2cell(AC, [8 8],[8 8])
s1 = mean2(B{1,1})
s2 = mean2(B{1,2})
s3 = mean2(B{2,1})
s4 = mean2(B{2,2})
with four lines like this:
s1 = mean2(t2(i+0:i+7,j+0:j+7))
s2 = mean2(t2(i+0:i+7,j+8:j+15))
etc
and one like this:
f(1) = mean2(t2(i:i+15,j:j+15))
Doing this will speed up the slowest operation in the whole code. Then you can use the profiler to check if there are other lines that can be sped up.

More Answers (1)

Stalin Samuel
Stalin Samuel on 27 Jan 2016
Edited: Stalin Samuel on 27 Jan 2016
by adding ';' at end of the each command in the feature extraction part you can reduce the execution time to less than 5 minutes

2 Comments

but it takes 124.354 seconds after adding ';'
Is there a question in that statement about 124.354 seconds?

Sign in to comment.

Asked:

on 27 Jan 2016

Commented:

on 4 Feb 2016

Community Treasure Hunt

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

Start Hunting!