Marker controlled watershed segmentation

15 vues (au cours des 30 derniers jours)
Radhika Bhagwat
Radhika Bhagwat le 6 Fév 2017
Commenté : Image Analyst le 7 Fév 2017
I am trying to segment images with overlapping objects in Matlab.For that I am using watershed transform.The input image is
In this image I want to seperate the leaf from the small portion of weed.
This is the code I am using
if true
% code
end
I= imread('E:\cotton29.jpg');
Irgb = rgb2gray(I);
hy = fspecial('sobel');
hx = hy';
Iy = imfilter(double(Irgb), hy, 'replicate');
Ix = imfilter(double(Irgb), hx, 'replicate');
Igradmag = sqrt(Ix.^2 + Iy.^2);
figure
imshow(Igradmag,[]), title('Gradient magnitude (gradmag)')
Igrad = Igradmag;
% segmenting the image
[Ibinary]= segmentationbycolor(I);
%%foreground marker
se1 = strel('disk',50);
Ierode = imerode(Ibinary,se1);
figure();imshow(Ierode);
Ibwopen = bwareaopen(Ierode,150);
figure();imshow(Ibwopen);
Itemp = Irgb;
Itemp(Ibwopen)=255;
figure();imshow(Itemp);
%%background marker
D = bwdist(Ibinary);
DL = watershed(D);
bgm = DL == 0;
figure
imshow(bgm), title('Watershed ridge lines (bgm)')
%%impose minima
gradmag2 = imimposemin(Igrad, bgm | Ibwopen);
L = watershed(gradmag2);
%%visualize
I4 = Irgb;
I4(imdilate(L == 0, ones(3, 3)) | bgm | Ibwopen) = 255;
figure
imshow(I4)
The output is
My question is why am I not getting exact boundary?Is something wrong in the code? Does the gradient of image play an important role in this watershed segmentation? Sorry but I am not able to remove the initial if ,end in the code.

Réponses (1)

Image Analyst
Image Analyst le 6 Fév 2017
Modifié(e) : Image Analyst le 7 Fév 2017
What is the "segmentationbycolor" function?
"My question is why am I not getting exact boundary?" <=== because your approach is wrong. The first mistake is to throw away the most valuable feature distinguishing the plants from the dirt, and that is the color. And then you did edge detection for some reason? Why did you do that?
I know all beginners, for some reason, think that edge detection is a necessary part of image analysis, but it is not. Often/usually it is not even needed at all. What you should do is color segmentation based on hue. I have demos for that in my File Exchange http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862, and here in the forum if you'll search for "leaf".
Once you separate the green from the brown you can work on distinguishing the very small weed from the larger leaf. However, if you find an algorithm that does it for this image, it will probably be specific to this image, and probably won't be robust enough to handle any number of leaves of one type with weed leaves of the other slightly different color. I think you need to rethink the entire aim of the project. For example if it's your goal to get an estimate of weeds in a crop on a farm, then you might be able to get a good estimate by finding the bare dirt rows between the plants and then investigating those rows only for the area fraction in them that is green. I think you're getting "lost in the weeds", so to speak, if you try to do it on a very small field of view like this.
  2 commentaires
Radhika Bhagwat
Radhika Bhagwat le 7 Fév 2017
@Image Analyst Thank you for your reply.
I have a set of images like the one given above(They may or may not have overlapping objects) and want to segment the leaf from any overlapping objects (if any) in the image. Once I segment these leaves,I want to do feature extraction for classification of plant species. For segmenting the leaf from any overlapping objects I am using watershed segmentation. Watershed transform works on gradient images. Segmentationbycolor is a function that segments the green part from background. Here is the code for it:
function[Ibinary]= segmentationbycolor(I)
cform = makecform('srgb2lab');
lab_he = applycform(I,cform);
ab = double(lab_he(:,:,2:3));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
nColors = 2;
% repeat the clustering 3 times to avoid local minima
[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
'Replicates',3);
pixel_labels = reshape(cluster_idx,nrows,ncols);
figure();imshow(pixel_labels,[]), title('image labeled by cluster index');
Ibinary= im2bw(pixel_labels,1);
prompt = ' complement if leaf is black. 1 to complement 0 if nocomplement';
val = input(prompt);
if (val==1)
Ibinary = imcomplement(Ibinary);
end
end
Do you suggest any other better approach for this? I will check the link given by you. Thank you once again.
Image Analyst
Image Analyst le 7 Fév 2017
I did suggest other approaches.
Like I said,
  1. I've given code for leaf segmentation before in this forum if you search for it.
  2. Or you can make slight adaptations to my color segmentation demos in my File Exchanges.
  3. Lastly, you can use the Color Thresholder app on the Apps tab of the tool ribbon.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Agriculture dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by