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
Stephen23
2016년 1월 27일
@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
2016년 1월 27일
Stephen23
2016년 1월 28일
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
2016년 2월 4일
@Neetha Mary: it is considered polite on this forum to accept the answer that best resolves your question.
채택된 답변
추가 답변 (1개)
Stalin Samuel
2016년 1월 27일
편집: Stalin Samuel
2016년 1월 27일
2 개 추천
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
Neetha Mary
2016년 1월 28일
Walter Roberson
2016년 1월 28일
Is there a question in that statement about 124.354 seconds?
카테고리
도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!