title: Watershed segmentation algorithm for segmenting occluded leaves in matlab
body: <p>The main task is to eliminate the complicated background of a leaf and extract the targeted leaf from an occluded leaf image in MATLAB.
  To eliminate the background i have applied K-means clustering algo. Now the main task is to segment the leaf from an occluded leaf using watershed segmentation algorithm.
  I am not able to find the perfect segments for every single leaf. Please help me. I have uploaded the sample images and also watershed segmentation code.</p>
  
  <p><strong>ORIGINAL IMAGE</strong>
  <img src="https://i.stack.imgur.com/pUdmG.jpg" alt="enter image description here"></p>
  
  <p><strong>Image after eliminating background using K-Means clustering algorithm and watershed Segmentation superimposed on original image</strong>
  <img src="https://i.stack.imgur.com/8ZlTK.jpg" alt="enter image description here"></p>
  
  <p>I want the main middle leaf to be a single segment, so that i can extract it.</p>
  
  <p>I have given the watershed segmentation code below</p>
  
  <pre><code>function wateralgo(img)
  
  F=imread(img);
  
  F=im2double(F);
  
  %Converting RGB image to Intensity Image
  r=F(:,:,1);
  g=F(:,:,2);
  b=F(:,:,3);
  I=(r+g+b)/3;
  imshow(I);
  
  %Applying Gradient
  hy = fspecial('sobel');
  hx = hy';
  Iy = imfilter(double(I), hy, 'replicate');
  Ix = imfilter(double(I), hx, 'replicate');
  gradmag = sqrt(Ix.^2 + Iy.^2);
  figure, imshow(gradmag,[]), title('Gradient magnitude (gradmag)');
  
  L = watershed(gradmag);
  Lrgb = label2rgb(L);
  figure, imshow(Lrgb), title('Watershed transform of gradient magnitude (Lrgb)');
  
  se = strel('disk',20);
  Io = imopen(I, se);
  figure, imshow(Io), title('Opening (Io)');
  Ie = imerode(I, se);
  Iobr = imreconstruct(Ie, I);
  figure, imshow(Iobr), title('Opening-by-reconstruction (Iobr)');
  
  Ioc = imclose(Io, se);
  figure, imshow(Ioc), title('Opening-closing (Ioc)');
  
  Iobrd = imdilate(Iobr, se);
  Iobrcbr = imreconstruct(imcomplement(Iobrd), imcomplement(Iobr));
  Iobrcbr = imcomplement(Iobrcbr);
  figure, imshow(Iobrcbr), title('Opening-closing by reconstruction (Iobrcbr)');
  
  fgm = imregionalmin(Iobrcbr);
  figure, imshow(fgm), title('Regional maxima of opening-closing by reconstruction (fgm)');
  
  I2 = I;
  I2(fgm) = 255;
  figure, imshow(I2), title('Regional maxima superimposed on original image (I2)');
  
  se2 = strel(ones(7,7));
  fgm2 = imclose(fgm, se2);
  fgm3 = imerode(fgm2, se2);
  fgm4 = bwareaopen(fgm3, 20);
  I3 = I;
  I3(fgm4) = 255;
  figure, imshow(I3), title('Modified regional maxima superimposed on original image (fgm4)');
  
  bw = im2bw(Iobrcbr, graythresh(Iobrcbr));
  figure, imshow(bw), title('Thresholded opening-closing by reconstruction (bw)');
  
  D = bwdist(bw);
  DL = watershed(D);
  bgm = DL == 0;
  figure, imshow(bgm), title('Watershed ridge lines (bgm)');
  
  gradmag2 = imimposemin(gradmag, bgm | fgm4);
  L = watershed(gradmag2);
  I4 = I;
  I4(imdilate(L == 0, ones(3, 3)) | bgm | fgm4) = 255;
  figure, imshow(I4), title('Markers and object boundaries superimposed on original image (I4)');
  
  Lrgb = label2rgb(L, 'jet', 'w', 'shuffle');
  figure, imshow(Lrgb), title('Colored watershed label matrix (Lrgb)');
  
  figure, imshow(I), hold on
  himage = imshow(Lrgb);
  set(himage, 'AlphaData', 0.3);
  title('Lrgb superimposed transparently on original image');
  end
  </code></pre>