problem in seeded region growing algorithm

I am trying to implement seed region growing algorithm but its not giving proper output.Please help me solve the error..
here is my code..
clc;clear all;close all;
r=imread('e:\IMAGES\pears.jpg'); % read the image
[rows columns numberOfColorChannels] = size(r);
if numberOfColorChannels > 1
r = rgb2gray(r); % converting image to grayscale
else
r = r; % It's already gray.
end
[row col]=size(r)
g=zeros(row,col);
I=zeros(size(r));
I=r;
S=zeros(size(r));
S(80:82,172:174)=ones(3,3); %initial seed image
figure,imshow(S),title('Initial Seed Image')
T=50; %threshold value
r=double(r);
w = [1 1 1;1 1 1;1 1 1 ];
SI=size(r)
p=size(r);
for x=2:1:p(1)-1
for y=2:1:p(2)-1
a1=[w(1)*S(x-1,y-1) w(2)*S(x-1,y) w(3)*S(x-1,y+1) w(4)*S(x,y-1) w(5)*S(x,y) w(6)*S(x,y+1) w(7)*S(x+1,y-1) w(8)*S(x+1,y) w(9)*S(x+1,y+1)];
AI(x,y)=min(a1);
end
end
SI= padarray(AI,[1 1],'symmetric','post')
SI=uint8(SI)
J=find(SI);
S1=r(J); % seed value obtained from SI
seedvalue=S1;
S=abs(r- seedvalue)<= T; % thresholding the absolute difference between original image & seed value
figure,imshow(S),title('thresholding output')
for p=1:1:row
for q=1:1:col
if(SI(p,q)==1)
for n=q:1:col
if S(p,q)==S(p,n)
g(p,n)=1;
end
end
for n=q:(-1):1
if S(p,q)==S(p,n)
g(p,n)=1;
end
end
for n=p:-1:1
if S(p,q)==S(n,q)
g(n,q)=1;
end
end
for n=p:1:row
if S(p,q)==S(n,q)
g(n,q)=1;
end
end
for n1=p:1:row
for n2=q:1:col
if S(p,q)==S(n1,n2)
g(n1,n2)=1;
end
end
end
for n1=p:1:row
for n2=q:(-1):1
if S(p,q)==S(n1,n2)
g(n1,n2)=1;
end
end
end
for n1=p:-1:1
for n2=q:(-1):1
if S(p,q)==S(n1,n2)
g(n1,n2)=1;
end
end
end
for n1=p:-1:1
for n2=q:1:col
if S(p,q)==S(n1,n2)
g(n1,n2)=1;
end
end
end
end
end
end
g=uint8(g)
SE = ones(1,1);
g =bwlabel(imdilate(g,SE),8);%reconstructing the image
%g=logical(g)
%I(g)=0
%figure,imshow(I),title('Final Output')
Lrgb = label2rgb(g);
figure, imshow(Lrgb)
title('output of region growing process')
figure, imshow(I), hold on
himage = imshow(Lrgb);
set(himage, 'AlphaData', 0.3);
title('final output superimposed on original image')

10 Comments

How does the output you are seeing differ from the output you expect? Is there an error message?
I ran it and it looks like it ran fine to me. All the pears in figure2 that were touching the seed point and touching each other got selected as the same object in subsequent images, which is what it should do. So, what's the problem? You'd get the same thing just calling bwlabel() or bwconncomp() on your binary image.
but the output must show only one pear of which seed point is selected.I dont want other pears to be highlighted.
Well then your binary image is wrong, and that is calculated before you even start the region growing. All those other pears are connected to the seed pear in your binary image.
Anyway, if you have a seed point, you don't need to do all that stuff you did since there is a function in the Image Processing Toolbox that does all that for you in one line. It's called imreconstruct (). But of course, it also requires that you have the pears separated before calling it just like your algorithm does.
i know imreconstruct command gives the expected output but is there any other way of getting correct output without using direct command
You can write your own region growing, or labeling, code as you did. Are you not seeing how, in your binary image, that the vast majority of the pears in your image area connected by white pathways to the pear where you placed the seed? Please let me know if you realize that. And if you do, why you don't think your growing code will reach out to all of those?
I am not getting what you are trying to say
I'm not sure how else to say it. What do you think region growing does? It captures (labels) all pixels that are connected to the seed pixel according to some criteria, like they have the same or similar gray level. You have just a binary image. You have about 3 or 4 white binary blobs, not 2 dozen pears. Look at your binary image - see? Just 3 blobs. That one gigantic blob that takes up almost your whole image? It's right over your seed point so that whole gigantic blob is going to be selected. Why should it get just the pear over the seed? According to your binary image that you are using, that pear is physically connected to the next one, and the next one, and the next one after that. Virtually all of them are connected. I suggest you look at how you came up with that binary image. And I suggest you look at this example of marker controlled watershed segmentation, which luckily for you is done with the pears demo image. http://matlab.wikia.com/wiki/FAQ#After_installation.2C_MATLAB_crashes_or_gives_an_error_message_when_I_try_to_run_MATLAB.
thank you Image Analyst
aarti sawant, can you help me, please give your code to me for the sake of college.

Sign in to comment.

Answers (1)

i have a doubt in setting initial seed points using seed determination for segmentation.

Asked:

on 1 Jan 2014

Commented:

on 29 Mar 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!