how can I reduce the execution time of the given code?
Show older comments
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
Stephen23
on 27 Jan 2016
@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.
Walter Roberson
on 27 Jan 2016
Stephen23
on 28 Jan 2016
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.
Stephen23
on 4 Feb 2016
@Neetha Mary: it is considered polite on this forum to accept the answer that best resolves your question.
Accepted Answer
More Answers (1)
Stalin Samuel
on 27 Jan 2016
Edited: Stalin Samuel
on 27 Jan 2016
2 votes
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
Neetha Mary
on 28 Jan 2016
Walter Roberson
on 28 Jan 2016
Is there a question in that statement about 124.354 seconds?
Categories
Find more on Image Arithmetic 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!