Home > Matlab, Vision > GrowCut Segmentation In Matlab

GrowCut Segmentation In Matlab

I came across a cute segmentation idea called “Grow Cut” [pdf]. This paper by Vladimir Vezhnevets and Vadim Konouchine presents a very simple idea that has very nice results. I always feel that the simplest ideas are the best! Below I give a brief description of the algorithm and link to the Matlab/C/mex code.

GrowCut Region Growing Algorithm

This algorithm is presented as an alternative to graph-cuts. The operation is very simple, and can be thought of with a biological metaphor: Imagine each image pixel is a “cell” of a certain type. These cells can be foreground, background, undefined, or others. As the algorithm proceeds, these cells compete to dominate the image domain. The ability of the cells to spread is related to the image pixel intensity.

The authors give some pseudocode that very concisely describes the algorithm.


//for every cell p
for all p in image
  //copy previous state
  labels_new = labels;
  strength_new = strength;
  // all neighbors q of p attack
  for all q neighbors
    if(attack_force*strength(q)>strength_new(p))
      labels_new(p) = labels(q)
      strength(p) = strength_new(q)
    end if
  end for
end for

Segmentation Results

Once implemented, this is a nice way to get segmentations. It is quite fast, and the initialization is very intuitive. Consider this picture of a lotus flower:

growcut image

I made an initialization by clicking 20 points in the flower and 30 points outside. I then made a “label map” where unlabeled pixels are 0 (gray), foreground pixels are 1 (white) and background pixels are -1 (black).

growcut seeds

Based on this simple initialization, we obtain a very decent segmentation:

growcut output

As you can see, it isn’t perfect, but it is quite good. Its possible to interactively refine the seed points to improve the segmentation, but I didn’t do that here.

Matlab Code Downloads

I implemented this code in Matlab (using mex files due to the extensive use of for loops). You can download this below with compiled binaries for mac, linux, and windows. Unzip the file and run >>growcut_test for a demo.

UPDATE: I’ve fixed some bugs thanks to reader, Lin. The code works much better now!

Source & Compiled Binaries (96k) [zip]
“GrowCut” Paper [pdf]

Please let me know if you find this useful, and if you make improvements! Also, check out these related segmentation posts:

Related Segmentation Posts

  1. tariq
    November 5th, 2009 at 03:07 | #1

    sir,
    i have a question about growcut segmentation that you use lotus image for segmntation,find result
    but we use that algotrithm in another image m,we will not get proper result,
    sir any program setting for that we run on different image
    plz send reply as early as possibl

  2. November 9th, 2009 at 03:02 | #2

    @tariq With grow-cut you must be careful with your initializations. If at first your segmentation is incorrect, try adding some additional initialization points both inside and outside your object. As shown in the post, the ‘inside’ initialization points should have a value of 1 and the ‘outside’ initialization points should have a value of 0.

  3. Jhon
    December 16th, 2009 at 16:38 | #3

    Dear sir,
    From where do you get maxC? How do you know the coordinate of the seeds?

  4. December 18th, 2009 at 09:41 | #4

    @Jhon maxC is (256)^2 which is the maximum value of the cost function. To get the coordinate of the seeds, you simply search the “seed image.”

  5. Anonymous
    January 20th, 2010 at 03:58 | #5

    I will learn it .

  6. January 29th, 2010 at 14:45 | #6

    Hi Shawn,

    Just a quick comment regarding maxC… if it is indeed the maximum value of the cost function, if you’re using the L2-norm in RGB space, shouldn’t the max be sqrt(3*(255^2))? I’m not sure where you got the 441 figure from… as 256^2 is 65536, unless you considered a perceptually uniform colour space, but judging from your code, it is RGB data.

    In any case, that was just my two cents. Your code is actually quite good, and I actually stumbled upon this posting by accident. I was looking for something else all together, and found this GrowCut entry you made. I’m considering using it for another application I’m developing.

    Thanks,
    - Ray.

  7. January 29th, 2010 at 14:47 | #7

    Hi Shawn,

    Never mind about that previous post… I just didn’t bother to crunch in the number myself. Your 441 figure is in fact sqrt(3*255^2)… thanks!

    @Jhon, the 441.673 figure is the greatest possible L2 norm between the darkest and brightest colours… in this case, it’s black and white, so sqrt((255-0)^2 + (255-0^2 + (255-0)^2) = sqrt(3*(255^2)) = 441.673.

    Hope this helps,
    - Ray.

  8. January 31st, 2010 at 12:33 | #8

    @Ray Right-on. Thanks for answering some of these other questions, too!

  9. jeff
    April 7th, 2010 at 05:35 | #9

    Hi Shawn, i am interestied in it.
    but i have some questions,like what is the “strength” ?
    how do i get “strength” in this method ?

    Thanks!!

  10. zhang juan
    April 21st, 2010 at 08:27 | #10

    I will learn it . thanks!

  11. bbb
    April 23rd, 2010 at 14:22 | #11

    hello shawn,
    i am very beginner at image processing. I am trying to understand your code, but to use it for other pictures, i should have another label maps. How could you assign a label map just by clicking? I didn’t understand…
    thanks a lot!

  12. azilawati
    April 28th, 2010 at 02:33 | #12

    hello Mr.Shawn

    I tried to construct labels picture, but I found it very difficult to built labels picture for each picture I am going to segment. could you please provide me by the code to construct the labels picture just by clicking?
    thanks alot.

  13. April 28th, 2010 at 23:02 | #13

    Hi Shawn

    I already got. thanks.

  14. April 29th, 2010 at 14:44 | #14

    @azilawati
    Glad you figured it out. I’ve found that the easiest way to create label maps like that is with the command
    >>ginput
    This lets you capture (x,y) coordinates using the mouse

  15. Anonymous
    November 29th, 2010 at 05:12 | #15

    Hi Shawn, I tried the algorithm and find good results for some simple images. But when applied to image with fuzzy or semi-transparent edge, the semi-transparent part was lost. How to improve this?

  16. Jay
    December 25th, 2010 at 12:07 | #16

    Hi Shawn..im doing project in Grow cut segmentation..But i need matlab code for refine the seed points to improve the segmentation..Can you guide me??

  17. Moorthi
    March 8th, 2011 at 07:58 | #17

    Hello sir, can u send me some more example images for my references…… please sir…….

  18. amit
    July 27th, 2011 at 05:33 | #18

    Shawn,for gray scale image i selected maxC=360.62 as per your formula. Is it right?

  19. amit
    July 29th, 2011 at 05:27 | #19

    @Jay
    type following code to create label image.
    Here nn=6 which means you can choose first 6 piexel as foreground piexel and next 6 piexel as background piexel. You can choose more piexel by changing nn value.

    img=imread(‘brain.png’)
    imshow(img);
    nn=6;
    [y,x]=ginput(nn);
    hold on;
    x=round(x);
    y=round(y);
    disp(‘Select background piexel’);
    [yb,xb]=ginput(nn);
    xb=round(xb);
    yb=round(yb);
    label1=zeros(m,n);
    for i=1:nn
    a1=x(i);
    b1=y(i);a2=xb(i);
    b2=yb(i);
    label1(a2,b2)=-1;
    label1(a1,b1)=1;
    end
    labels=double(label1);

  20. toffy
    September 4th, 2011 at 13:36 | #20

    sir,
    is it possible to extract a particular cartoon character from pictures and save it without background information

  21. abdelmaksoud
    November 22nd, 2011 at 06:57 | #21

    good morning sir

    in the GROWCUT algorithm, assert function is not present in the zip file.

    can you send me the assert function for testing the algorithm please.

  22. December 12th, 2011 at 08:57 | #22

    assert shouldn’t be needed to run the algorithm – you can just comment that out.

  23. January 6th, 2012 at 01:21 | #23

    SIR,
    Can you give me some suggestion how to segment overlapping cells using matlab

  24. nag
    January 13th, 2012 at 02:20 | #24

    Is this the same algorithm as ‘Seeded Region Growing Segmentation’?

  25. kiran
    January 27th, 2012 at 08:08 | #25

    can growcut algorithm be used for nuclei segmentation of medical images. can this algorithm segment multiple objects or cells in a single image. can this algorithm handle overlapping of nuclei or cells.

  26. February 6th, 2012 at 11:00 | #26

    I think you could easily modify the code to have more than two competing regions. Check the original paper – I believe the authors discuss this.

    Overlapping cells would be a challenge for this algorithm. For that you may consider shape-constrained active contours.

  27. February 6th, 2012 at 11:02 | #27

    There are many algorithms that use seeded region-growing. This one in particular plays off the idea of cellular autonomy. Not sure if it’s the same as what you’re calling, “Seeded Region Growing Segmentation.”

  28. Sarah
    March 6th, 2012 at 05:36 | #28

    Hi Shawn
    I think that your algorithm is just what I need to proceed in my Bachelor Thesis, but I cannot get the function to work on my Mac. When I try to run the test-script I get following error message:

    ??? Undefined function or method ‘growcutmex’ for input arguments of type ‘double’.

    Error in ==> growcut at 29
    [l s] = growcutmex(img,labels);

    Error in ==> test at 8
    [labels_out, strengths] = growcut(img,labels);

    It appears that it cannot find growcutmex even though it is in the directory. Do you know if anyone else have had this problem when running the script on a Mac and do you have any idea to what I could do to make it work?

    Thanks, Sarah

  29. amit
  30. amit
    March 8th, 2012 at 01:26 | #30

    Do u have C++ compiler like visual studio on your system?@Sarah

  31. Sarah
    March 9th, 2012 at 04:58 | #31

    @ amit
    I think that I have already downloaded all the files, but for some reason the mex file does not work. I have had this problem with another mex file and in that cases it was because I needed the *.mexmaci64 file and not just the *.mexmaci file.
    I do not think that I have C++ compiler, but I must admit that I do not know for sure.

  32. Sarada
    March 19th, 2012 at 07:23 | #32

    Can I get only the MATLAB CODEs (not the compiled ones) of your growcut method?

    –Rgds

  33. Regards
    March 19th, 2012 at 07:23 | #33

    Sarada :
    Can I get only the MATLAB CODEs (not the compiled ones) of your growcut method?
    –Rgds

  34. March 20th, 2012 at 12:40 | #34

    @Sandra the code is implemented in mex (meaning that the codes are a combination of C++ and MATLAB). If you implement GrowCut in pure-MATLAB it will be very slow because it’s highly-dependent on for loops.

    All of the codes I used are provided above for download.

  35. rahul
    March 28th, 2012 at 23:32 | #35

    sir, will you please give me the steps to create the label map for different images..

  36. sumanth
    April 7th, 2012 at 02:16 | #36

    hello sir,
    can i know how to apply measures like local consistency error,global consistency error or rand index,variation of information etc,…which are used for measuring performance….
    kindly mail me at sumanthshaan@yahoo.com

  37. ruthb
    April 13th, 2012 at 17:14 | #37

    Shawn, I’ve downloaded your code and the paper you have linked to just to learn and play with the code so I can understand the theory.

    What’s the difference between your grow-cut algorithm and region-growing algorithm available in Matlab file exchange?

    http://www.mathworks.com/matlabcentral/fileexchange/19084-region-growing/content/regiongrowing.m

    Sorry, if my question seem stupid as I’m totally new to image processing & vision. I’ll learn your code and possibly try and port it into Java.

  38. April 16th, 2012 at 09:13 | #38

    Hi – Not sure how the Matlab codes work, but the GrowCut algorithm uses seeding in a unique way that (in my experience) produces better results than the build-in Matlab function. It also provides the opportunity to build in new “similarity” measures (beyond color similarity). For instance you could extend this method to compare tensors, textures, etc. Hope that’s helpful.

  39. Knotto
    May 7th, 2012 at 20:18 | #39

    Hello Shawn , i am a beginner in Image Processing .Would you please explain me what line number and paragraph of code in “glowcutmex.cpp” should be edit and what the purpose of each editing , if i need to take glow-cut segmentation with another image

    Pardon for my poor english and thank you for your kind.
    Knotto.

  1. No trackbacks yet.