how can i fill the region inside the leaf

4 次查看（过去 30 天）
Alaa Eldesoky2022-5-28

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.

回答（2 个）

Walter Roberson 2022-5-28

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 个评论显示隐藏 1更早的评论
Walter Roberson 2022-5-28
~bwareafilt(~isleaf, [1 SIZE] )
would fill any holes up to area SIZE

Image Analyst 2022-5-28
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;
% Get blobs in the background up to some max size, like 100 pixels.
maxAcceptableSize = 100;
% We want to add/OR those small blobs into the leaf mask so that
% they will be considered as leaf, not background.
3 个评论显示隐藏 2更早的评论
Alaa Eldesoky 2022-5-28
oh! okay, I get what you mean, I'll try again the HSV thresholding

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by