% dip10_1.m         gr12oct16   nov23
%    Edge detection
clear all;
help edge
% note "thinning" postprocessing applied in many cases
return

%%
I = double(imread('eight.tif'))/255;
% I = double(imread('circuit.tif'))/255;
% I = (sin(linspace(0,10,200))/2+.5)' * [zeros(1,100)+.2  ones(1,100)];

% I = I + randn(size(I))*.05;

BW1 = edge(I,'sobel');
BW2 = edge(I,'log');
BW3 = edge(I,'canny');
figure(1), imshow(I);
figure(2), imshow([BW1 BW2 BW3])

        %% some morphological postprocessing
        se = strel('disk',10);
        % se = strel('rect',[20,1]);
        BWcl = imclose(BW3,se);
        figure(3), imshow(BWcl);
        % figure(4), imshow(bwmorph(BWcl,'skel',Inf));

%%  sobel (automatic and manual threshold)
% I = imread('circuit.tif');
I = imread('.\img\alpibn.tif');
[BW1,thr] = edge(I,'sobel');
thr
figure(3);
for thr = .02:.01:.08
    imshow(edge(I,'sobel',thr)); pause();
end

%%  LoG (automatic threshold, manual sigma)
figure(4);
for sigma = .1:1:4.1  %  default=2
    [bw,thr] = (edge(I,'log',[],sigma));
    thr            % decreases for increasing sigma
    imshow(edge(I,'log',[],sigma)); pause();
                   % note 'fat' and curvy objects
end

%% Canny  (automatic thresholds)
figure(5);
for sigma = .1:1:4.1  %  default=2
    [bw,thr] = (edge(I,'canny',[],sigma));
    thr            % *increases* with sigma (*)
    imshow(edge(I,'canny',[],sigma)); pause();
                   % note 'fat' and curvy objects
end
%  (*)
% Matlab's doc: the value for thr is "relative to the maximum % of the gradient"

%%  Canny's DoG filter - impulse and freq. responses
clear all; close all;
r = -10:10;
sigma = 2;      % similar to Sobel filter for sigma<1
% h = exp( -r.^2 ./ (2*sigma^2) );
 h = (-r ./ sigma^2) .* exp( -r.^2 ./ (2*sigma^2) );
subplot(211); stem(r,h)
subplot(212); plot(abs(fft(h,200)))

return

