Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Optimize code for a blob-detector (MATLAB)

  1. Feb 7, 2015 #1
    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 (Text):
    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 (Text):

    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
     
     
  2. jcsd
  3. Feb 7, 2015 #2
    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).
     
  4. Feb 11, 2015 #3

    kreil

    User Avatar
    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 (Text):
    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 (Text):
    profile viewer
    Of course, if you want more info, type
    Code (Text):
    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Optimize code for a blob-detector (MATLAB)
  1. Matlab codes (Replies: 2)

  2. Matlab code (Replies: 0)

  3. Error code in Matlab (Replies: 3)

Loading...