What can I do to make this function run faster? I feel like it takes longer than I'd like and i want some optimization tips.

1 view (last 30 days)
function [Location,x1,x2,y1,y2] = FindWaldo(WheresWaldo1,Waldo1)
%Converting the image of waldo to grayscale
[rowW1,colW1,~] = size(Waldo1);
for r = 1:rowW1
for c = 1:colW1
R(r,c) = Waldo1(r,c,1);
G(r,c) = Waldo1(r,c,2);
B(r,c) = Waldo1(r,c,3);
end
end
dim1 = size(Waldo1,1);
dim2 = size(Waldo1,2);
Waldo1gray = zeros(dim1,dim2,'uint8');
for x = 1:dim1
for y = 1:dim2
Waldo1gray(x,y) = ((R(x,y).*.2989)+(G(x,y)*.587)+(B(x,y).*.114));
end
end
[rowWW1,colWW1,~] = size(WheresWaldo1);
for r = 1:rowWW1
for c = 1:colWW1
R(r,c) = WheresWaldo1(r,c,1);
G(r,c) = WheresWaldo1(r,c,2);
B(r,c) = WheresWaldo1(r,c,3);
end
end
dim1 = size(WheresWaldo1,1);
dim2 = size(WheresWaldo1,2);
WheresWaldo1gray = zeros(dim1,dim2,'uint8');
for x = 1:dim1
for y = 1:dim2
WheresWaldo1gray(x,y) = ((R(x,y).*.2989)+(G(x,y)*.587)+(B(x,y).*.114));
end
end
%Converting the background image to grayscale
[row,col] = size(WheresWaldo1gray);
[row1,col1] = size(Waldo1gray);
Break = 0;
Foundy = zeros(row1-1,col1-1);
Foundx = zeros(row1-1,col1-1);
%Making matricies of waldo's location in the background
for r = 1:row
for c = 1:col
if WheresWaldo1(r,c) == Waldo1(1,1)
for r1 = 1:row1
for c1 = 1:col1
m = length(Foundy);
n = length(Waldo1gray);
if m == n
Break = 2;
break
end
if WheresWaldo1(r+(r1-1),c+(c1-1)) == Waldo1(r1,c1)
Foundy(r1,c1) = r+(r1-1);
Foundx(r1,c1) = c+(c1-1);
continue
else
Break = 1;
break
end
end
if Break == 1
Break = 0;
break
elseif Break == 2
break
end
end
end
if Break == 2
break
end
end
if Break == 2
break
end
end
[y2loc,~] = size(Foundy);
[~,x2loc] = size(Foundx);
%Collecting the location of Waldo for printing
y1 = Foundy(1,1);
y2 = Foundy(y2loc,1);
x1 = Foundx(1,1);
x2 = Foundx(1,x2loc);
%Getting range values for red lines
yrange1 = [y1-7,y1-6,y1-5,y1-4,y1-3,y1-2,y1-1];
%xrange1 = x1:x2;
%yrange2 = y1:y2;
xrange2 = [x1-7,x1-6,x1-5,x1-4,x1-3,x1-2,x1-1];
yrange3 = [y2+1,y2+2,y2+3,y2+4,y2+5,y2+6,y2+7];
%xrange3 = x1:x2;
%yrange4 = y1:y2;
xrange4 = [x2+1,x2+2,x2+3,x2+4,x2+5,x2+6,x2+7];
%Drawing top red line
for r1 = x1:x2
for c1 = yrange1(1):yrange1(7)
WheresWaldo1(c1,r1,1) = 255;
WheresWaldo1(c1,r1,2) = 0;
WheresWaldo1(c1,r1,3) = 0;
end
end
%Drawing left red line
for r2 = xrange2(1):xrange2(7)
for c2 = y1:y2
WheresWaldo1(c2,r2,1) = 255;
WheresWaldo1(c2,r2,2) = 0;
WheresWaldo1(c2,r2,3) = 0;
end
end
%Drawing bottom red line
for r3 = x1:x2
for c3 = yrange3(1):yrange3(7)
WheresWaldo1(c3,r3,1) = 255;
WheresWaldo1(c3,r3,2) = 0;
WheresWaldo1(c3,r3,3) = 0;
end
end
%Drawing right red line
for r4 = xrange4(1):xrange4(7)
for c4 = y1:y2
WheresWaldo1(c4,r4,1) = 255;
WheresWaldo1(c4,r4,2) = 0;
WheresWaldo1(c4,r4,3) = 0;
end
end
%Saving picture with waldo marked
Location = WheresWaldo1;
end

Answers (1)

the cyclist
the cyclist on 10 Nov 2018
A few thoughts:
Broadly, you should learn how to profile your code. This will show you where the slow parts of your code are. Focus on those.
MATLAB is vectorized, so everywhere you have code like this
for r = 1:rowWW1
for c = 1:colWW1
R(r,c) = WheresWaldo1(r,c,1);
G(r,c) = WheresWaldo1(r,c,2);
B(r,c) = WheresWaldo1(r,c,3);
end
end
you could instead have written this
R(1:rowWW1,1:colWW1) = WheresWaldo1(1:rowWW1,1:colWW1,1);
G(1:rowWW1,1:colWW1) = WheresWaldo1(1:rowWW1,1:colWW1,2);
B(1:rowWW1,1:colWW1) = WheresWaldo1(1:rowWW1,1:colWW1,3);
without the for loop.
If you do need to use for loops, you should learn to preallocate the memory for them.
Finally, if you upload a MAT file with a typical input to this function, we could run your code and possibly give more specific advice.

Community Treasure Hunt

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

Start Hunting!