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.
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!
ReplyDeleteCould you please help me to find a solution for this?
Many thanks,
Rokhsareh.
You may try different values of threshold T (if it merges, reduce T).
DeleteWhen the intensity difference in the object boundary is less than the threshold region growing is tend to merge the two objects...
Hi,i tried executing the above program but encountered an the following error
ReplyDelete"function regionGrowing(name,T)
Error: Function definitions are not permitted in this context."
could you plz help me rectify it?
Regards Pavithra
Could you elaborate how did you execute the program?
DeleteTypically, 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.
How to extend this to a 6 connected neighbourhood?perform region growing on stack of images
ReplyDeleteEdit the neighbours() function to return 6-connected neighbourhood...
Deletethis code is very usefull..is ther any possible ways to select the seed point without clicking..
ReplyDeleteYes, you can. Simply use a variable for seed and get the coordinate from user..
Deleteya.. how can i get that seed point...
DeleteIs there any way to get seed point automatically without knowing the coordinate first? Please I really need the aswer...
DeleteHey 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 ??
ReplyDeleteThis is the problem of 'region growing', not the implementation.
DeleteYou can use region growing only when your object boundary is clearly distinguishable from the background.
i am getting neighbours function as undefined since it is not a command
ReplyDeleteYou should copy both the above functions to your .m file.
Deletecan u plz send me the output image
ReplyDeletei am getting only the input image..
ReplyDeleteHi, I was trying to get run this program but after clicking on the image command window is showing that
ReplyDelete" regionGrowing
Error using regionGrowing (line 17)
Not enough input arguments.
Please do help me to run this code.
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....
ReplyDelete>> regionGrowing(name,t)
??? Undefined function or variable 'name'.
??? Undefined function or method 'neighbours' for input arguments of type 'uint16'.
ReplyDeleteError in ==> regionGrowing at 16
n=neighbours(F(1,1),F(1,2),r,c); % 4 neighbourhood
Please help, any one?
I managed to sort it out.
ReplyDeletethere is an error comihg that is "Not enough input arguments." please let me know that what to do...
ReplyDeleteI tried your code, but I am getting small dot instead of region in the output image.
ReplyDeleteDot is appearing at the position of selected seed area. Please help
This comment has been removed by the author.
ReplyDeletehello I'm a new in matlab but I need to use the region growing in matlab.
ReplyDeleteMay I ask u how to use it?
hep me with a project regarding region growing for a price?
ReplyDeleteDo you have a Region Growing in 3D code.
ReplyDeleteThis comment has been removed by the author.
ReplyDeletehelp Error in ==> jseg at 18
ReplyDeleteif(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
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