Remove Shadows from Contour of an Object (Grapes) I want to extract only the grapes from the images. Unfortunately, sometimes I don't get the grapes exactly.

I have a code that doesn't work well in all cases. Sometimes, it does not differentiate between the shadows and the grapes.

The example input image:



The result I'm getting:



And this is my code:

RGB = imread('DSC02807.JPG');
GRAY = rgb2gray(RGB);

threshold = graythresh(GRAY);
originalImage = im2bw(GRAY, threshold);

originalImage = bwareaopen(originalImage,250);

SE = strel('disk',10);
IM2 = imclose(originalImage,SE);

originalImage = IM2;

imshow(originalImage);
This is what your code produces (using colormap(hot) with white boundaries that are obtained from IM2):


I think the shadows are nicely treated, why are you thinking that your code doesn't notice between shadow to grapes?

This is the code used for producing this image

bw=IM2&lt;1;
[B,L] = bwboundaries(bw,'noholes'); % fill any holes, so that regionprops can be used to estimate
imagesc(GRAY); hold on
for k = 1:length(B) 
    boundary = B{k};
    plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 1); hold on
end
colormap(hot(256))