Automatic Threshold Calculation and Blob Detection

Thresholding of images is a very important idea in image processing. It has applications in wide variety of fields such as segmentation, motion detection , blob detection etc.

Thresholds are typically applied to grayscale images. Threshold selection involves choosing a gray value ‘t’ such that all gray levels greater than ‘t’ are mapped into the “object” label while all other gray levels are mapped into the “background” label  – M.SHNEIER

 What we’ll be discussing is a method to find thresholds ‘t’ automatically given an input image to classify 2 different labels in the image – an object and a background.  Hence the output image can be represented as a binary image of 0 and 1 (black and white) to differentiate between the 2 labels.

The method/algorithm used here is an adaptation of the algorithm presented by M.Shneier in the paper “Using Pyramids to Define Local Thresholds for Blob Detection”-May 1983.


1. Pyramid Down :- Reducing the size of the image by half in both the direction such that the resulting image is one-fourth the area of the current image. This can be performed using different user-defined mechanisms eg. Performing median, average or max operator using a 2×2 , 5×5 kernel or built-in functions provided in Matlab (impyramid) or OpenCV (PyrDown).

2. Spot Detection :- Spots are points of high intensity value peaks surrounded by a valley of low intensity value. We can apply a Laplace filter (edge detector) and then search for good Spots.


1. Read the current pyramid level( the picture if it is the first iteration).

2. Build a new pyramid level using impyramid or PyrDown.

3. Apply a Spot Detector to the new level.

4. Find Good Spots(Spots that are isolated. No Spots around them or else that would mean that there are many spots close by each other that are most probably indicating to the same object. These will get eliminated if we go a level higher in the pyramid ie. They will merge into one spot itself) ie. If there are too many good spots , go to step 1.

5. For each good spot do:-

a) calculate a threshold

b) apply the threshold.


Assuming the above algorithm has given us a Spot Image that highlights only good spots , our next step is to use these spots to calculate a threshold value that can be used to apply the original image. Now this can be done for 2 purposes :-

a) Either apply the same threshold to the entire image by calculating a threshold value from the maximum peak in spot image.

b) OR , use each of the spots found to create different local threshold levels and apply them to the image locally.

Either way the method to calculate the threshold value for one spot or all the spot remains the same

  •  Find the region in the original image that correspond to the spot in the current level image.
  •  Make an estimated threshold value by taking the average of all the pixels in the region of interest. ‘T_o’
  •  Estimate a value of the background. This was done by just taking the mean of the entire picture itself. ‘T_b’
  •  Now, plot a histogram of the original image. Move from ‘T_o’ toward ‘T_b’ and the gray level that has the deepest valley is our final threshold value ‘T_f’.
  •  Apply ‘T_f’ locally or completely.




For the Matlab Code for this algorithm , visit this post


About ranjanritesh

I am currently working as a Software Engineer for Cisco Systems India in Bangalore. I’ve been working for Cisco since August 2013. I am a BITS Goa Alumni. I earned my Bachelors in Electronics and Instrumentation in August 2013 from there. My interests include Image Processing, Systems Programming and hacking on the Linux Kernel. I also like tinker a bit with Arduino and other microcontrollers.
This entry was posted in Blob Detection, Matlab and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s