Steve on Image Processing with MATLAB

Image processing concepts, algorithms, and MATLAB


Steve on Image Processing with MATLAB has been archived and will not be updated.

Adaptive thresholding for binarization

Despite recent appearances on the blog, I still exist! It's just been a little crazier than usual for the last month or so.

Anyway ... I'm back, and I'm going to try to wrap things up about image binarization. In my 14-Jun-2016 post, I discussed the algorithm underlying imbinarize for the global thresholding case. Today I'm going to talk about the algorithm for the adaptive thresholding case.

Here's an image suffering from an extreme case of nonuniform illumination.

I = imread('printedtext.png');

Here is the binarization using a global threshold.

bw1 = imbinarize(I);
title('Global threshold')

And here is the binarization using an adaptive threshold. Note that we have to tell the function that the foreground pixels (representing text characters) are darker than the background pixels (the white paper).

bw2 = imbinarize(I,'adaptive','ForegroundPolarity','dark');
title('Adaptive threshold')

The algorithm used by |imbinarize(I,'adaptive',...) is sometimes called Bradley's method, for the paper by D. Bradley and G. Roth, "Adaptive Thresholding Using Integral Image," Journal of Graphics Tools, vol. 12, issue 2, pp. 13-21, 2007.

This method uses a large-neighborhood mean filter. If the input image pixel is more than a certain percentage greater than the mean filter, then it is set to white.

To perform large-neighborhood mean filtering (also called box filtering) efficiently, the implementation uses something called an integral image. With this technique, time required to perform mean filtering depends only on the number of image pixels. The time is independent of the neighborhood size. Maybe I'll discuss integral images and box filtering in a future post. In the meantime, you can look at integralImage and imboxfilt.

So how big is the mean filter neighborhood? Well, there's no fixed rule. This is another one of those magic numbers that bedevil image processing. The function imbinarize uses a square neighborhood that is about 1/8 of the smallest image dimension. This is just a heuristic rule that works reasonably well for a variety of images.

The function imbinarize does everything for you in one step. It computes the adaptive threshold image and then applies it to produce a binary output image. If you want the adaptive threshold image itself, or if you want more control over the how the adaptive threshold image is computed, then you can use adaptthresh.

Here is the adaptive threshold image for the printed text example shown above.

T = adaptthresh(I,'ForegroundPolarity','dark');
title('Adaptive threshold image')

When you use adaptthresh, you can control the neighborhood size directly. You can also specify other local background measurement methods, including median filtering and Gaussian filter.

Wrapping Up

With the new set of Image Processing Toolbox interfaces, using imbinarize as your one-step solution for both global and adaptive thresholding. Gain finer control over algorithm details, if you need to, by using the underlying functions otsuthresh and adaptthresh. The older functions, im2bw and graythresh, still exist for compatibility, but we encourage you to use the new functions in your new code.

Published with MATLAB® R2016a

  • print


To leave a comment, please click here to sign in to your MathWorks Account or create a new one.