# Adaptive Thresholding for liver segmentation using Matlab

I need to segment liver from an abdominal CT image using Adaptive Thresholding. But I get the entire foreground separated from the background alone. I need only the liver part of the foreground separated. Check out the pdf file in http://www.ijcaonline.org/casct/number1/SPE34T.pdf I need an output similar to that shown in Figure 6.

I attach my coding here. Kindly help me out.

%testadaptivethresh.m
clear;close all;
im1=imread('nfliver2.jpg');
bwim1=adaptivethreshold(im1,11,0.03,0);
figure,imshow(im1);
figure,imshow(bwim1);
imwrite(bwim1,'at2.jpg');


function bw=adaptivethreshold(IM,ws,C,tm)
%ADAPTIVETHRESHOLD An adaptive thresholding algorithm that seperates the
%foreground from the background with nonuniform illumination.
%  bw=adaptivethreshold(IM,ws,C) outputs a binary image bw with the local
%   threshold mean-C or median-C to the image IM.
%  ws is the local window size.
%  tm is 0 or 1, a switch between mean and median. tm=0 mean(default); tm=1 median.
%
%  Contributed by Guanglei Xiong (xgl99@mails.tsinghua.edu.cn)
%  at Tsinghua University, Beijing, China.
%
%  For more information, please see
%  http://homepages.inf.ed.ac.uk/rbf/HIPR2/adpthrsh.htm

if (nargin<3)
error('You must provide the image IM, the window size ws, and C.');
elseif (nargin==3)
tm=0;
elseif (tm~=0 && tm~=1)
error('tm must be 0 or 1.');
end

IM=mat2gray(IM);

if tm==0
mIM=imfilter(IM,fspecial('average',ws),'replicate');
else
mIM=medfilt2(IM,[ws ws]);
end
sIM=mIM-IM-C;
bw=im2bw(sIM,0);
bw=imcomplement(bw);


My modified code for testadaptivethresh.m

clear;
im=imread('nfliver7.gif');
figure,imshow(im)
bwim1=adaptivethreshold(im,300,-0.15,0);
bw=bwareaopen(bwim1,3000);
se=strel('diamond',4);
er=imerode(bw,se);
bw1=bwareaopen(er,3000);
er1=imerode(bw1,se);
bw2=bwareaopen(er1,1000);
fi=imfill(bw2,'holes');
figure,imshow(fi)

op=uint8(fi);
seg=im.*op;
figure,imshow(seg)
imwrite(seg,'sliver7.jpg');

-
is it necessary to use adaptive thresholding? –  vini Mar 9 '12 at 3:15
blogs.mathworks.com/steve/2006/06/02/cell-segmentation found this u could try it out –  vini Mar 11 '12 at 15:21
Yes, I need to use adaptive thresholding only. If not, can you suggest me any other good segmentation method(other than region growing and FCM)? –  Gomathi Mar 12 '12 at 15:57
www4.comp.polyu.edu.hk/~cslzhang/code.htm i found this u can look up the code for K. Zhang, H. Song and L. Zhang, “Active Contours Driven by Local Image Fitting Energy,” Pattern recognition, vol. 43, issue 4, pp. 1199-1206, April 2010. It worked well enough for this image –  vini Mar 12 '12 at 17:32
Thank you so much. I got the output. I changed the parameter values and did morphological operations. Thank you everyone. –  Gomathi Mar 13 '12 at 11:06
show 4 more comments

## migrated from stackoverflow.comMar 7 '12 at 20:32

This question came from our site for professional and enthusiast programmers.

## 1 Answer

I saw the link of the paper (of SS Kumar) you referred and the link where you got the code (HIPR) are two different algorithms - even though both sounds like Adaptive Thresholding

First i would like to tell you the difference.

In the HIPR method, a general assumption is essentially 2 class level image - i.e. foreground and background. In any 2 class thresholding, one expects 2 peaks or regions within the image histogram, specifically background vs. forground, text vs. white paper. If you somehow found an optimal valley point in the histogram - you get cleanest division. Here is how the histogram may look like :

However, this valley point may be slightly shifting around the locally. There are good examples of lighting variations given there. Hence, this optimal valley point exists everywhere but slightly varies spatially, hence a universal threshold would fail. Hence, the valley point (threshold) is computed on every local region.

SS Kumar's paper's method and more specifically the class of images you are dealing with, is multi-class (multiple objects each with different intensity band and spread). In this cases, histograms are multi-modal, i.e. it has many peaks and valleys and presumably each peak correspond to different object, yet it may be even more complex.

The histogram might just look like this: (this is the same image as in paper).

In this case, the above 2 class approach will simply fail because there is no one good valley. Which is why your first image you posted looks like black/white dots all around.

The meaning of Adaptive Thresholding here, implies that you need to identify the correct peak and the band of gray scale which covers most intensities of the lever and other objects are in stark contrasts which allows

What should you do?

First of, if it is compulsory to use Adaptive thresholding, find the histogram and see what intensity range and then for a threshold of left or to the right are the intensity boundaries which pixels should be discarded.

Alternatively you can use Region Growing or split and merge algorithm. Refer to this question for some information: What segmentation methods can be used for simple images?

-
Thank you so much. That was a very informative answer sir. –  Gomathi Mar 19 '12 at 13:59
add comment