# Optimize code for a blob-detector (MATLAB)

• MATLAB
• beyondlight
In summary, the blobdetector consists of several functions including gaussImage, gaussPyramid, diffOfGaussians, findDogMaximas, and dogDetector. These functions are used to filter images with a gaussian filter, compute the difference between consecutive filtered images, find local maximas in an image, and detect extreme points in filtered images. The findDogmaximas function takes about 15 minutes to run, while the dogDetector function takes over 1 hour. The computer used for this code has a Lenovo laptop with an i7-3632QM processor, 2.2 GHz CPU, 8 GB RAM, and a 64-bit operating system. The code may have some errors that are causing it to run slower than expected

#### beyondlight

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).

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

Code:
help profile

You can click through the results to figure out what is bogging your program down. Once you are able to do this, please come back and post about your findings. It will be much easier to ask for help optimizing a snippet of code in a particular function, than to post several functions in their entirety.

## 1. How do I optimize my code for a blob-detector in MATLAB?

To optimize your code for a blob-detector in MATLAB, you can start by using vectorized operations instead of loops whenever possible. This can greatly improve the efficiency of your code. Additionally, try to preallocate arrays and variables to avoid constantly resizing them during the execution of your code. You can also look into using built-in MATLAB functions and algorithms specifically designed for blob detection.

## 2. What are some common mistakes to avoid when optimizing code for a blob-detector?

One common mistake to avoid is using too many nested loops in your code. This can significantly slow down the execution time. Another mistake is not properly preallocating variables and arrays, which can lead to a lot of unnecessary resizing and memory usage. It is also important to avoid using unnecessary or redundant computations.

## 3. How can parallel processing be used to optimize code for a blob-detector?

Parallel processing can be used to optimize code for a blob-detector by distributing the workload across multiple processor cores, thereby reducing the overall execution time. This can be achieved in MATLAB by using the Parallel Computing Toolbox and functions like parfor loops and spmd blocks.

## 4. What are some ways to measure the performance of a blob-detector code in MATLAB?

There are a few ways to measure the performance of a blob-detector code in MATLAB. One way is to use the tic and toc functions to measure the execution time of your code. Another way is to use the MATLAB Profiler, which provides more detailed information on the performance of your code, including the time spent on each function and line of code.

## 5. Can I optimize my code for a blob-detector without sacrificing accuracy?

Yes, it is possible to optimize your code for a blob-detector without sacrificing accuracy. One way to do this is by using more efficient algorithms and functions, as mentioned earlier. It is also important to carefully tune the parameters of your blob-detector to find the right balance between accuracy and performance. You can also consider using different data types and data structures to improve the speed of your code while maintaining accuracy.