- paste your code text.
- select all of the code text
- click the {} Code button above the textbox.
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
2016년 2월 4일
@Neetha Mary: it is considered polite on this forum to accept the answer that best resolves your question.
채택된 답변
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.
댓글 수: 0
추가 답변 (1개)
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
댓글 수: 2
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!