clear all, clc;

I = imread('q.jpg');
I = rgb2gray(I);
bw = im2bw(I, graythresh(I));

%% gradients

hy = fspecial('sobel');
hx = hy';
Iy = imfilter(double(I), hy, 'replicate');
Ix = imfilter(double(I), hx, 'replicate');
g = sqrt(Ix.^2 + Iy.^2);
figure,imshow(g, []);
l = watershed(g);
figure,imshow(l, []);
wr = l == 0;
figure,imshow(wr)
f = I;
f(wr) = 0;
figure, imshow(f);

g2 = imclose(imopen(g,ones(7,7)), ones(7,7));
figure,imshow(g2, []);
l2 = watershed(g2);
figure, imshow(l2, []);
wr2 = l2 == 0;
figure,imshow(wr2)
f2 = I;
f2(wr2) = 0;
figure, imshow(f2);















