Sunday, January 29, 2012

Region Growing


This method gets image and threshold as arugments and gets the mouse click coordinates as the seed to proceed. Here, starting from the seed the intensity values of each pixel is compared with its neighbours and if it is within the threshold, it'll be marked as one.

Matlab Code

function regionGrowing(name,T)
    im=rgb2gray(imread(name));
    im=im2double(im);
    [r,c]=size(im);
    A=zeros(r,c); % segmented mask
    F=[]; % frontier list
    subplot(1,2,1);
    imshow(im);
    title('Original');
    s=uint16(ginput(1));    % get the click coordinates
    s=[s(2),s(1)];      % [row,col]
    A(s(1),s(2))=1;
    F=[F;s];
    while(~isempty(F)) % if frontier is empty
        n=neighbours(F(1,1),F(1,2),r,c);   % 4 neighbourhood
        for i=1:size(n,1)
            if(abs(im(F(1,1),F(1,2))-im(n(i,1),n(i,2)))<T && A(n(i,1),n(i,2))~=1)% less than threshold & not already segmented
                A(n(i,1),n(i,2))=1;
                F=[F;n(i,1),n(i,2)];
            end
        end
        F(1,:)=[];
    end
    subplot(1,2,2);
    imshow(A);
    title(sprintf('Threshold: %0.4f',T));
end

function out=neighbours(s1,s2,r,c)
    out=[];
    if(s2>1),  out=[out;s1,s2-1];   end
    if(s1>1),  out=[out;s1-1,s2];   end
    if(s1<r),  out=[out;s1+1,s2];   end
    if(s2<c),  out=[out;s1,s2+1];   end
end

Output

The segmented region is shown in white.


29 comments:

  1. Hi, thanks, it works very nice. And also, I've tried this with other images like road, lungs as well. There is a problem that I can't solve it! sometimes it merges to the other objects. For instance when it has to just grow in the road, it merges to the trees around the road as well!
    Could you please help me to find a solution for this?
    Many thanks,
    Rokhsareh.

    ReplyDelete
    Replies
    1. You may try different values of threshold T (if it merges, reduce T).
      When the intensity difference in the object boundary is less than the threshold region growing is tend to merge the two objects...

      Delete
  2. Hi,i tried executing the above program but encountered an the following error
    "function regionGrowing(name,T)
    Error: Function definitions are not permitted in this context."
    could you plz help me rectify it?
    Regards Pavithra

    ReplyDelete
    Replies
    1. Could you elaborate how did you execute the program?
      Typically, Create two '.m' files for each functions above with the file name as the function name and call regionGrowing(name,T) in the matlab command line.

      Delete
  3. How to extend this to a 6 connected neighbourhood?perform region growing on stack of images

    ReplyDelete
    Replies
    1. Edit the neighbours() function to return 6-connected neighbourhood...

      Delete
  4. this code is very usefull..is ther any possible ways to select the seed point without clicking..

    ReplyDelete
    Replies
    1. Yes, you can. Simply use a variable for seed and get the coordinate from user..

      Delete
    2. ya.. how can i get that seed point...

      Delete
    3. Is there any way to get seed point automatically without knowing the coordinate first? Please I really need the aswer...

      Delete
  5. Hey Great job btw yar !! I have a problem with this code , if i give the seed point as a minute region say sumthin similar to point it is not detecting properly either an entire black image comes if thershold is lower or just a point somewhere in the white region pops up if thershold is higher .. Is there any way to correct it ??

    ReplyDelete
    Replies
    1. This is the problem of 'region growing', not the implementation.
      You can use region growing only when your object boundary is clearly distinguishable from the background.

      Delete
  6. i am getting neighbours function as undefined since it is not a command

    ReplyDelete
    Replies
    1. You should copy both the above functions to your .m file.

      Delete
  7. can u plz send me the output image

    ReplyDelete
  8. i am getting only the input image..

    ReplyDelete
  9. Hi, I was trying to get run this program but after clicking on the image command window is showing that
    " regionGrowing
    Error using regionGrowing (line 17)
    Not enough input arguments.

    Please do help me to run this code.

    ReplyDelete
  10. May I know how to use this m-file? I create two function m-file called regionGrowing and neighbours. After that I have no idea how to use it....
    >> regionGrowing(name,t)
    ??? Undefined function or variable 'name'.

    ReplyDelete
  11. ??? Undefined function or method 'neighbours' for input arguments of type 'uint16'.

    Error in ==> regionGrowing at 16
    n=neighbours(F(1,1),F(1,2),r,c); % 4 neighbourhood

    Please help, any one?

    ReplyDelete
  12. there is an error comihg that is "Not enough input arguments." please let me know that what to do...

    ReplyDelete
  13. I tried your code, but I am getting small dot instead of region in the output image.
    Dot is appearing at the position of selected seed area. Please help

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. hello I'm a new in matlab but I need to use the region growing in matlab.

    May I ask u how to use it?

    ReplyDelete
  16. hep me with a project regarding region growing for a price?

    ReplyDelete
  17. Do you have a Region Growing in 3D code.

    ReplyDelete
  18. This comment has been removed by the author.

    ReplyDelete
  19. help Error in ==> jseg at 18
    if(abs(im(F(1,1),F(1,2))-im(n(i,1),n(i,2)))<T && A(n(i,1),n(i,2))~=1)% less than threshold & not already segmented

    ReplyDelete
  20. I'm Serenity Autumn, currently living in Texas city, USA. I am a widow at the moment with Four kids and i was stuck in a financial situation in May 2019 and i needed to refinance and pay my bills. I tried seeking loans from various loan firms both private and corporate but never with success, and most banks declined my credit. But as God would have it, I was introduced to a woman of God a private loan lender who gave me a loan of 850,000.00 USD and today am a business owner and my kids are doing well at the moment, if you must contact any firm with reference to securing a loan without collateral , no credit check, no co signer with just 2% interest rate and better repayment plans and schedule, please contact Mrs Mr Bejamin Lee On Email 247officedept@gmail.com   And Whats-App +1-989-394-3740. He doesn't know that am doing this but am so happy now and i decided to let people know more about him and also i want God to bless him more.

    ReplyDelete