Optimize code for a blob-detector (MATLAB)

  • Context: MATLAB 
  • Thread starter Thread starter beyondlight
  • Start date Start date
  • Tags Tags
    Code Matlab
Click For Summary
SUMMARY

The discussion focuses on optimizing a blob detector implemented in MATLAB, specifically addressing performance issues with the functions findDogMaximas and dogDetector. The findDogMaximas function takes approximately 15 minutes to execute, while dogDetector exceeds one hour due to inefficient code structure and incorrect conditional statements. The use of MATLAB's built-in profile tool is recommended for identifying bottlenecks in the code execution.

PREREQUISITES
  • Understanding of MATLAB programming and syntax
  • Familiarity with image processing concepts, particularly Gaussian filters
  • Knowledge of performance profiling in MATLAB using the profile tool
  • Basic understanding of local maxima and minima detection algorithms
NEXT STEPS
  • Learn how to use MATLAB's profile tool for performance analysis
  • Research optimization techniques for nested loops in MATLAB
  • Explore efficient data structures for dynamic array management in MATLAB
  • Investigate alternative algorithms for blob detection in image processing
USEFUL FOR

Developers and researchers in image processing, MATLAB programmers looking to optimize performance, and anyone interested in enhancing blob detection algorithms.

beyondlight
Messages
64
Reaction score
0
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
 
Physics news on Phys.org
My bad!

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

Of course, if you want more info, type
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.
 

Similar threads

Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 41 ·
2
Replies
41
Views
10K
  • · Replies 3 ·
Replies
3
Views
3K