[filename,filepath]=uigetfile({'*.png', '*.jpg'},'Select and image');
if isequal(filename,0)
disp('User selected Cancel')
else
disp(['User selected ', fullfile(filepath, filename)])
end
originalI = imread(strcat(filepath, filename));
figure
imshow(originalI);
title('Please crop the image');
[xpos, ypos] = ginput(1);
title('Original Image');
width = 756;
height = 756;
xLeft = xpos - width/2;
yBottom = ypos - height/2;
croppedI = imcrop(originalI, [xLeft, yBottom, width, height]);
figure
imshow(croppedI);
title('Cropped Image');
saturatedI = rgb2hsv(croppedI);
saturatedI(:,:,1)=saturatedI(:,:,3)*1.2;
saturatedI(saturatedI > 1)=1;
saturatedI=hsv2rgb(saturatedI);
redChannel = saturatedI(:,:,1);
greenChannel = saturatedI(:,:,2);
BWred = imclearborder(redChannel);
fill = imfill(BWred,'holes');
SE=strel('disk',6)
diskimage=imdilate(fill,SE)
BWgreen = imclearborder(greenChannel);
fill = imfill(BWgreen,'holes');
SE=strel('disk',6)
cupimage=imdilate(fill,SE)
figure
imshow(diskimage);
title('disk image');
figure
imshow(cupimage);
title('cup image');
BW=BWred;
CC = bwconncomp(BW);
numPixels = cellfun(@numel,CC.PixelIdxList);
[biggest,idx] = max(numPixels);
BW(CC.PixelIdxList{idx}) = 0;
filteredForeground=BW;
a = diskimage;
stats = regionprops(double(a),'Centroid','MajorAxisLength','MinorAxisLength');
centers = stats.Centroid;
diameters = mean([stats.MajorAxisLength stats.MinorAxisLength],2);
radii = diameters/2;
disp(radii);
figure,imshow(croppedI);
hold on
viscircles(centers, radii);
hold off