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

2 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2016-1-29
编辑:Stephen23 2016-1-30
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.
Stephen23
Stephen23 2016-2-4
@Neetha Mary: it is considered polite on this forum to accept the answer that best resolves your question.

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2016-1-29
编辑:Stephen23 2016-1-29
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.

更多回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by