how can i fill the region inside the leaf

2 views (last 30 days)
I was using the k-mean segmentation with a* space in L*a*b* color space for removing the background image but unfortunatly some of the diseased part was equal to center of the bakground, i want to add these small diseased spot areas to the foreground, can anyone help me with what should i do to solve it.

Answers (2)

Walter Roberson
Walter Roberson on 28 May 2022
Edited: Walter Roberson on 28 May 2022
rgb = reshape(YourImage, [], 3);
idx = kmeans(rgb, NumberOfClusters) ;
labeled = reshape(idx, size(YourImage(:, :, 1)));
isleaf = labeled == ClusterNumberForLeaf;
filledleaf = bwfill(isleaf, 'holes');
You need to figure out how to decide which cluster number is the leaf; it will not be the same every time.
  2 Comments
Alaa Eldesoky
Alaa Eldesoky on 28 May 2022
Edited: Alaa Eldesoky on 28 May 2022
filledleaf = bwfill(isleaf, 'holes');
it doesn't give agood result too
the k-mean with a* space code run with me with all images and give a good results in segmentation, only just in the images that have a dead cell issue in the leaf so it become equal to the value of the soil and i know the programe have to detect the disease before this happening, but i was thinking just if it happening, how could i segment it.
I'm thinking in removing the pixel areas that is quite small from the backgroung image and add it to the foreground image but i don't know the right way to do that.
Walter Roberson
Walter Roberson on 28 May 2022
~bwareafilt(~isleaf, [1 SIZE] )
would fill any holes up to area SIZE

Sign in to comment.


Image Analyst
Image Analyst on 28 May 2022
kmeans() is not usually a good way for color segmentation (my demo is attached) regardless if you're doing it in RGB or any other color space. Why not? Well, for one illustration, imagine you want the area fraction of leaf in the field of view with any amount from 0% to 100%. Now imagine you're looking at an image with virtually no leaf in there -- just dirt. If you tell kmeans to find 2 clusters, it will find two clusters that will be different shades of dirt. None of them will be green leaf color. So now, after segmentation by kmeans you're still going to have to examine the clusters to see which one, if any, are green. What if you have a small amount of green, like 5% and lots of dirt? It still might find clusters that are the two dominant shades of brown and you'd totally miss the green leaf pixels. It's better to use fixed thresholds.
You'd be better off with a segmentation that thresholded on the hue channel using fixed threshold of hues known to be green. Use the Color Thresholder App on the Apps tab of the tool ribbon to threshold in HSV color space. Then find small blobs in the background mask with bwareaopen or bwareafilt. Then OR the small blobs mask in with the leaf mask to fill in the small holes only.
hsvImage = rgb2hsv(rgbImage);
hImage = hsvImage(:, :, 1);
leafMask = hImage > hue1 & hImage < hue2;
backgroundMask = ~leafMask;
% Get blobs in the background up to some max size, like 100 pixels.
maxAcceptableSize = 100;
smallBlobs = bwareafilt(backgroundMask, [1, maxAcceptableSize]);
% We want to add/OR those small blobs into the leaf mask so that
% they will be considered as leaf, not background.
leafMask = leafMask | smallBlobs;
  3 Comments
Image Analyst
Image Analyst on 28 May 2022
Edited: Image Analyst on 28 May 2022
Yeah, it's bad. Hopefully now you'll understand why I said what I said - kmeans is no good for color segmentation and you should use fixed thresholds. Not sure what went wrong when you tried the Color Thresholder.
An alternative is to use discriminant classification. I'm attaching a demo.
Alaa Eldesoky
Alaa Eldesoky on 28 May 2022
oh! okay, I get what you mean, I'll try again the HSV thresholding

Sign in to comment.

Categories

Find more on Agriculture in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!