I want to get rid of unwanted blobs in the segmented image (i only need fruits in the image). Please suggest me a solution and attaching the image

1 view (last 30 days)
I have used the following codes
clear all;
close all;
title('Input Image')
b=fspecial('gaussian',[3 3],0.5);
d = rgb2ycbcr(c);
title('Gray Image');
level = graythresh(Cr);
f= imbinarize(Cr,level);
g=medfilt2(f,[5 5]);
h= edge(g, 'canny', 0.6);
j= imfill(i,'holes');
title('segmented Image');
title('Shape Analysis');
hold on
[B,L] = bwboundaries(j,'noholes');
for k = 1:length(B)
boundary = B{k};
stats = regionprops(L,'BoundingBox','Area');
for k = 1:length(B)
boundary = B{k};
thisBB = stats(k).BoundingBox;
rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],...
'EdgeColor','y','LineWidth',2 )
delta_sq = diff(boundary).^2;
perimeter = sum(sqrt(sum(delta_sq,2)));
area = stats(k).Area;
metric = 4*pi*area/perimeter^2;
metric_string = sprintf('%2.2f',metric);

Accepted Answer

Image Analyst
Image Analyst on 3 Mar 2018
Edited: Image Analyst on 3 Mar 2018
I think the whole algorithm is not well thought out. I mean, doing edge detection on the median filtered version of the binary image???? What's all that about??? What you need to do is color segmentation. For example, you can use the Color Thresholder app on the Apps tab of the tool ribbon.
Or you can look at several color segmentation methods in my File Exchange: Click here
To learn how to format your code (so I don't have to fix it again for you), read this link: http://www.mathworks.com/matlabcentral/answers/13205#answer_18099
Please attach 1.jpg if you want more help.
Image Analyst
Image Analyst on 3 Mar 2018
I would not do it that way at all.
Median filtering will smooth the borders. Not sure why that is needed instead of using the more accurate original blobs. It may totally eliminate some small blobs - something that you can do directly with bwareaopen() or bwareafilt(). Using canny edge detection on a binary image is not recommended - you'd be better off using bwperim(), but I don't even recommend that. Calling bwareaopen() on a canny filtered image has the effect of getting rid of blobs with perimeters less than 100 pixels. Again, a roundabout way of doing what should be done more directly with bwpropfilt(). But I have doubts why you are filtering small blobs based on perimeter length rather than area, which is more common.
And of course this is after the biggest mistake of all, which was to use Otsu thresholding, which is only good in certain situations, of which yours is not one of those. Why not? Think about it and eventually you'll realize why.
Good luck though. This is how you learn.

Sign in to comment.

More Answers (1)

Ahmet Cecen
Ahmet Cecen on 3 Mar 2018
Grab the red channel of that image and manually crop a portion of the red fruit as a small template (use "crop") run:

Community Treasure Hunt

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

Start Hunting!

Translated by