# Optimize code for a blob-detector (MATLAB)

• MATLAB
The blobdetector consist of the following functions:

gaussImage: Filters a image with a gaussian filter
gaussPyramid: Uses gaussImage to filter the same image several times with different std.
diffOfGaussians: Uses gaussPyramid to compute the difference between consecutive filtered images
findDogMaximas: Finds local maximas in a image, by setting it to minus it also finds minimum
dogDetector: Finds extreme points from all layers of filtered images and stores them in a 3xN vector.

findDogmaximas takes about 15 min to run and dogDetector takes over 1 hour!

findDogmaximas has a vector that changes size for every loop iteration since I don't know how many extreme potins that exists in an Image.

Computer specifications:
Lenovo Laptop
i7-3632QM Processor
CPU 2.2 GHz
8 GB RAM
64-bits operativesystem

Code:

Code:
function maxima = findDogMaximas(dogLower,dogMiddle,dogHigher)

maxima=[];
nrofmaxima=0;
[M ,N]=size(dogMiddle);

count_middle=0;
count_lower=0;
count_higher=0;

for j=2:1:M-1

for k=2:1:N-1

for x=-1:1:1
for z=-1:1:1

if dogMiddle(j+x,k+z)<dogMiddle
count_middle=count_middle+1;
end

if dogLower(j+x,k+z)<dogLower
count_lower=count_lower+1;
end

if dogHigher(j+x,k+z)<dogHigher
count_higher=count_higher+1;
end

end
end

sum=count_middle+count_lower+count_higher;

if sum<26

nrofmaxima=nrofmaxima+1;
maxima(1,nrofmaxima)=j;
maxima(2,nrofmaxima)=k;

end

end
end

end

Code:
function detected = dogDetector(im)

tic
k=2.^(1/3);
stds=zeros(1,9);

for x=1:1:9

stds(1,x)=k^(x-1);

end

stds=1.6.*stds;

DoG=diffOfGaussians(im,stds);
n=length(DoG);
detected=[];

for i=2:1:n-1

dogLower=DoG{i-1};
dogMiddle=DoG{i};
dogHigher=DoG{i+1};

maxima=findDogMaximas(dogLower,dogMiddle,dogHigher);
minima=findDogMaximas(-dogLower,-dogMiddle,-dogHigher);

dog_stdsi=[maxima minima];
dog_stdsi(3,:)=stds(i);
detected=[detected dog_stdsi];

end
toc
end

I wrote the if statemens completely wrong. omg :P

if dogMiddle(j+x,k+z)<dogMiddle
count_middle=count_middle+1;

Im referring to the whole dogMiddle! it should only be one point. That is dogMiddle(j,k).

kreil
Gold Member
I think the first thing you should do here is to analyze the execution of your code with the profiler. All you need to do is type this into MATLAB first:

Code:
profile on

This turns on the code profiler for any subsequent code that you execute.

Next, execute your main function call (dogDetector is the main function here that calls all the others right?) Once MATLAB returns the result (however long that is), type the following into MATLAB to close the profiler and view the results:

Code:
profile viewer

help profile