close all
clc

imshow(I);
hroi = imellipse();
wait(hroi);

mask = hroi.createMask();
figure
imshow(mask)

Iroi = I;
Iroi(imcomplement(mask)) = 180;
figure
imshow(Iroi)

[px, py] = getpts();
px = round(px);
py = round(py);

BW = imcomplement(imbinarize(Iroi));
figure
imshow(BW)

s = strel('disk', 5);
BW = imdilate(BW, s);
BW = imerode(BW, s);
BW = imerode(BW, s);
BW = imdilate(BW, s);

figure
imshow(BW)

BWg = uint8(BW*255);
hold on
for i = 1:3
    singleMask = grayconnected(BWg, py(i), px(i));
    visboundaries(singleMask, 'LineWidth', 1)
    waitforbuttonpress()
end




% I = Igray;
% BW = imbinarize(medfilt2(I, [5, 5]));
% BW = bwareaopen(BW, 120);
% BW = imcomplement(BW);
% BW = bwareaopen(BW, 120);
% 
% imshow(BW)
% hold on;
% boundaries = bwboundaries(BW);
% numberOfBoundaries = size(boundaries, 1);
% for k = 1 : numberOfBoundaries
% 	thisBoundary = boundaries{k};
% 	a = polyarea(thisBoundary(:,1), thisBoundary(:,2));
%     if a > 1000
%         plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2);
%     end
% end
% hold off;

% imshow(img);
% hold on
% title('Select foreground sample (rect)')
% bbox1 = round(getrect());
% BW1 = false(size(img,1),size(img,2));
% BW1(bbox1(2):bbox1(2)+bbox1(4),bbox1(1):bbox1(1)+bbox1(3)) = true;
% visboundaries(BW1,'Color','r');
% 
% title('Select background sample (rect)')
% bbox2 = round(getrect());
% BW2 = false(size(img,1),size(img,2));
% BW2(bbox2(2):bbox2(2)+bbox2(4),bbox2(1):bbox2(1)+bbox2(3)) = true;
% visboundaries(BW2,'Color','b');
% 
% waitforbuttonpress();
% 
% [L,P] = imseggeodesic(img,BW1,BW2);
% imshow(label2rgb(L),'InitialMagnification', 50)
% title('Segmented image')

% I = medfilt2(Igray, [5, 5]);
% I = Igray;
% 
% figure
% imshow(I)
% 
% hy = fspecial('sobel');
% hx = hy';
% Iy = imfilter(double(I), hy, 'replicate');
% Ix = imfilter(double(I), hx, 'replicate');
% gradmag = sqrt(Ix.^2 + Iy.^2);
% figure
% imshow(gradmag,[])
% 
% L = watershed(gradmag);
% Lrgb = label2rgb(L);
% figure, imshow(Lrgb), title('Watershed transform of gradient magnitude (Lrgb)')
% 
% % se = strel('disk', 5);
% % Ie = imerode(I, se);
% % Iobr = imreconstruct(Ie, I);
% % figure
% % imshow(Iobr), title('Opening-by-reconstruction (Iobr)')
% % 
% % Iobrd = imdilate(Iobr, se);
% % Iobrcbr = imreconstruct(imcomplement(Iobrd), imcomplement(Iobr));
% % Iobrcbr = imcomplement(Iobrcbr);
% % figure
% % imshow(Iobrcbr), title('Opening-closing by reconstruction (Iobrcbr)')
% 
% se = strel('disk', 7);
% 
% Id = imdilate(I, se);
% Icbr = imreconstruct(imcomplement(Id), imcomplement(I));
% Icbr = imcomplement(Icbr);
% figure
% imshow(Icbr), title('Closing by reconstruction (Icbr)')
% 
% Ie = imerode(Icbr, se);
% Iobrcbr = imreconstruct(Ie, Icbr);
% figure
% imshow(Iobrcbr), title('Closing-opening by reconstruction (Icbrobr)')
% 
% fgm = imregionalmax(Iobrcbr);
% figure
% imshow(fgm), title('Regional maxima of closing-opening by reconstruction (fgm)')
% 
% hy = fspecial('sobel');
% hx = hy';
% Iy = imfilter(double(Iobrcbr), hy, 'replicate');
% Ix = imfilter(double(Iobrcbr), hx, 'replicate');
% gradmag = sqrt(Ix.^2 + Iy.^2);
% figure
% imshow(gradmag,[])
% 
% L = watershed(gradmag);
% Lrgb = label2rgb(L);
% figure, imshow(Lrgb), title('Watershed transform of gradient magnitude (Lrgb)')

% I = imresize(Igray, 0.2);
% figure, imshow(I), hold all
% [poly, mask] = regionGrowing(I, [], 60, Inf, true, false, false); % click somewhere inside the lungs
% plot(poly(:,1), poly(:,2), 'LineWidth', 2)
% 
% figure
% imshow(mask)

% I = im2double(medfilt2(Igray, [5, 5]));
% figure, imshow(I)
% [boundary, mask] = regionGrowing(I, [], 0.25, Inf, false, false, false);
% figure
% imshow(mask)

% I = Igray;
% h = imshow(I);
% 
% title('Select foreground')
% p = ginput(1);
% row = round(axes2pix(size(I, 2), get(h, 'XData'), p(2)));
% col = round(axes2pix(size(I, 1), get(h, 'YData'), p(1)));
% indFore = sub2ind(size(I), row, col); 
% 
% title('Select background')
% p = ginput(1);
% row = round(axes2pix(size(I, 2), get(h, 'XData'), p(2)));
% col = round(axes2pix(size(I, 1), get(h, 'YData'), p(1)));
% indBack = sub2ind(size(I), row, col); 
% 
% L = superpixels(I,2749);
% BW = lazysnapping(I,L,indFore,indBack);
% imshow(BW)

% nhood = true(9);
% S = rangefilt(I,ones(5));
% BW = imcomplement(imbinarize(img));
% s = strel('disk', 6);
% BWd = imdilate(BW, s);
% imshow(BWd)
% BWe = imerode(BWd, s);
% 
% figure
% imshow(BWe)
% 
% BWe = imerode(BWe, s);
% BWd = imdilate(BWe, s);
% 
% figure 
% imshow(BWd)

