Manual vs Automatic Thresholding using Matlab

Admin
0
Image thresholding is one of the most common task in image processing used for segmenting desired object from typical background. At this time, I will show you 2 ways of doing this task.

1. Manual Thresholding
This method can be done simply by applying a threshold value to a given image. For the test image, I will load a standard image provided by MATLAB, rice.png

im = imread('rice.png');
figure(1)
imagesc(im) ; colormap gray

By running the code above, the image below should come to your screen.

Input image : grain.jpg

This image, is uint8 type, so that its pixel value ranging from 0 - 255. For example, if I want to extract the grains, a threshold value named "Th" is used. Here, I want only the pixel exceeding the Th will be categorized as desired pixels (grain pixels). Here is the code :

Th       = 150 ;
imTh     = im> Th ;
figure(2)
imagesc(imTh) ; colormap gray

The imTh is the binary image containing the segmented grains. Look at this results :

Thresholded image : (Th = 150)

Seems like the grains perfectly segmented. Of course, you can change the Th value to get better results. Here is another results using higher Th.

Thresholded image : (Th = 170)

By increasing the Th, fewer pixel is returned since we used the ">" operator.

2. Automatic Thresholding
There another way to perform segmentation, i.e., automatic approach. For this purpose, we can used Otsu's method. One of brief explanation about this method can be found in here. At this moment I only show the way of using it.

The Otsu method help you to find the optimum threshold value used in segmenting your image. Here is the code in MATLAB :

T           = graythresh(im);
imThOtsu    = imbinarize(im,T);
figure(3)
imagesc(imThOtsu) ; colormap gray

The graythresh will calculate optimum threshold value, then this value is used in segmenting desired pixel using imbinarize function. The code above will result in this image :

                                        Thresholded image : Automatic approach

See, this method give you a better result even no Th value given to it.

The last, all of the codes above are only can be used to extract the object into two type of pixel (0 and 1). In real case, multiple pixel classification may be experienced. Do not worry, Otsu's multithreshoding is there for you. Here is the example : 

N           = 2 ;
T           = multithresh(im,N);
imMultiOtsu = imquantize(im,T) ;
figure (4)
imagesc(imMultiOtsu) ; colorbar

The N refers to number of pixel class, the resulted image will contain (N + 1) class after the execution. Here is the results

 Multii-thresholded image : 3 Class

As you can see, there is 3 classes of pixels (blue, green, and yellow). Okay, let me end this by summarizing the codes as follows :

clc
clear
close all

im = imread('rice.png');
figure(1)
imagesc(im) ; colormap gray

Th          = 150 ;
imTh        = im> Th ;
figure(2)
imagesc(imTh) ; colormap gray


figure(3)
T           = graythresh(im);
imThOtsu    = imbinarize(im,T);
imagesc(imThOtsu) ; colormap gray

N           = 2 ;
T           = multithresh(im,N);
imMultiOtsu = imquantize(im,T) ;
figure (4)
imagesc(imMultiOtsu) ; colorbar

Hope you enjoy guys. Just give me a feedback or request a specific MATLAB tutorial.
By the way, you can also visit my video collection on Youtube (here). Almost all of them are related to MATLAB coding. Cheers.










Post a Comment

0Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

Disclaimer : Content provided on this page is for general informational purposes only. We make no representation or warranty of any kind, express or implied, regarding the accuracy, adequacy, validity, reliability, availability or completeness of any information.

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top