MATLAB Answers

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

4 views (last 30 days)
I have used the following codes
clc;
clear all;
close all;
a=imread('1.jpg');
figure
imshow(a);
title('Input Image')
b=fspecial('gaussian',[3 3],0.5);
c=imfilter(a,b,'replicate');
d = rgb2ycbcr(c);
Y=d(:,:,1);
Cb=d(:,:,2);
Cr=d(:,:,3);
figure
imshow(Cr);
title('Gray Image');
figure
imhist(Cr);
title('Histogram');
level = graythresh(Cr);
f= imbinarize(Cr,level);
g=medfilt2(f,[5 5]);
h= edge(g, 'canny', 0.6);
i=bwareaopen(h,100);
j= imfill(i,'holes');
figure
imshow(j);
title('segmented Image');
figure
imshow(a);
title('Shape Analysis');
hold on
[B,L] = bwboundaries(j,'noholes');
for k = 1:length(B)
boundary = B{k};
end
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);
text(boundary(1,2)-35,boundary(1,1)+13,metric_string,'Color','G','FontSize',14,'FontWeight','bold');
end

  0 Comments

Sign in to comment.

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.

  2 Comments

Jothika Charlees
Jothika Charlees on 3 Mar 2018
I have to do median filtering after otsu segmention. Then I have to find contours. I'm not familiar about contours ,I searched code for contours in net and then put this
h= edge(g, 'canny', 0.6);
i=bwareaopen(h,100);
j= imfill(i,'holes');
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:
normxcorr2(fruittemplate,RedImage);

  0 Comments

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!

Translated by