how can i fill the region inside the leaf
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(2 个)
Walter Roberson
2022-5-28
编辑: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 个评论
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;
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 个评论
Image Analyst
2022-5-28
编辑:Image Analyst
2022-5-28
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!