title: Fuzzy C Means for tumor segmentation using Matlab
body: <p>I have a segmented liver. I need to segment the tumor in it. I used the FCM method. It is a 3 level FCM thresholding. When I apply it to the images, I need the tumor region(the region that is darker than the remaining parts) alone to get segmented. But am getting the reverse. All the region surrounding the tumor gets segmented. Kindly help me out. The program has two files, <code>testfcmthresh.m</code> and a function <code>fcmthresh.m</code></p>
  
  <p>The input 'segmented liver(using region growing)' and the FCM output image:</p>
  
  <p><img src="https://i.stack.imgur.com/a2sNx.jpg" alt="input segemented liver">
  <img src="https://i.stack.imgur.com/RKiqJ.jpg" alt="output FCM image">
  <a href="http://www.facebook.com/media/set/?set=a.2482420198768.89088.1800503467&amp;type=1&amp;l=dfe444754f" rel="nofollow noreferrer">from here</a></p>
  
  <p>I tried complementing the image obtained by using <code>imcomplement()</code> But I got the entire background also as white, since the background was originally dark. Kindly help me out. </p>
  
  <p><img src="https://i.stack.imgur.com/vn2BA.jpg" alt="after imclearborder">
  <img src="https://i.stack.imgur.com/4rC9E.jpg" alt="after fcm"></p>
  
  <pre><code>function [bw,level]=fcmthresh(IM,sw)
  %FCMTHRESH Thresholding by 3-class fuzzy c-means clustering
  %  [bw,level]=fcmthresh(IM,sw) outputs the binary image bw and threshold level of
  %  image IM using a 3-class fuzzy c-means clustering. It often works better
  %  than Otsu's methold which outputs larger or smaller threshold on
  %  fluorescence images.
  %  sw is 0 or 1, a switch of cut-off position.
  %  sw=0, cut between the small and middle class
  %  sw=1, cut between the middle and large class
  %
  %  Contributed by Guanglei Xiong (xgl99@mails.tsinghua.edu.cn)
  %  at Tsinghua University, Beijing, China.
  
  % check the parameters
  if (nargin&lt;1)
      error('You must provide an image.');
  elseif (nargin==1)
      sw=0;
  elseif (sw~=0 &amp;&amp; sw~=1)
      error('sw must be 0 or 1.');
  end
  
  data=reshape(IM,[],1);
  [center,member]=fcm(data,3);
  [center,cidx]=sort(center);
  member=member';
  member=member(:,cidx);
  [maxmember,label]=max(member,[],2);
  if sw==0
      level=(max(data(label==1))+min(data(label==2)))/2;
  else
      level=(max(data(label==2))+min(data(label==3)))/2;
  end
  bw=im2bw(IM,level);
  </code></pre>
  
  <hr>
  
  <pre><code>%testfcmthresh.m
  
  clear;clc;
  im=imread('mliver3.jpg');
  fim=mat2gray(im);
  level=graythresh(fim);
  bwfim=im2bw(fim,0.1);
  [bwfim0,level0]=fcmthresh(fim,0);
  [bwfim1,level1]=fcmthresh(fim,1);
  subplot(2,2,1);
  imshow(fim);title('Original');
  subplot(2,2,2);
  imshow(bwfim);title(sprintf('Otsu,level=%f',level));
  subplot(2,2,3);
  imshow(bwfim0);title(sprintf('FCM0,level=%f',level0));
  subplot(2,2,4);
  imshow(bwfim1);title(sprintf('FCM1,level=%f',level1));
  % imwrite(bwfim1,'fliver6.jpg');
  </code></pre>