%% Creating specific filters
% The command fspecial will create defined geometric filters.  Some
% accepted types are: average (mean), disk, gaussian, laplacian, log, and
% motion.

clear
close all

% Load in a test image
img = im2double(imread('cameraman.tif'));


filt6 = fspecial('average',3)
filt7 = fspecial('disk',4) % Second argument is the disk radius

% If you ask for a gaussian filter, you have to provide both the size and
% the sigma
filt8 = fspecial('Gaussian',5,0.8)

% We can then apply the filters using the same imfilter command
imgFilt7 = imfilter(img,filt7);
imgFilt8 = imfilter(img,filt8);

figure
imshow([img imgFilt7 imgFilt8],'InitialMagnification','Fit','Border','Tight')

%% Edges
% Finding edges is as easy as using the edge functin
[imgEdge1, thresh] = edge(img,'prewitt');

figure
imshow([img imgEdge1],'InitialMagnification','Fit','Border','Tight')

% We can also set the threshold for edge detection
imgEdge2 = edge(img,'prewitt',thresh/2); % Cut the previous threshold in 1/2

figure
imshow([img imgEdge1 imgEdge2],'InitialMagnification','Fit','Border','Tight')

% We can try the sobel method, too
imgEdge3 = edge(img,'sobel');

figure
imshow([img imgEdge1 imgEdge3],'InitialMagnification','Fit','Border','Tight')

% Or Laplacian of gaussian
imgEdge4 = edge(img,'LoG');

figure
imshow([imgEdge3 imgEdge4],'InitialMagnification','Fit','Border','Tight')

% You can define the threshold and the sigma of the gaussian blur of the
% LoG filter.


%% Sharpening
% To sharpen the image, we use the laplacian to find the edges, and then subtrac
% those from the original image.
clear
close all

img = im2double(imread('cameraman.tif'));

% Create the laplacian filter using fspecial
lapFilt = fspecial('laplacian',.7)
% Apply the filter
imgLap = imfilter(img,lapFilt);

figure
imshow([img 5*imgLap])

% Now we can subtract the laplacian 
imgSharp = max(imsubtract(img,imgLap),0);
imshow([img imgSharp])

% Unsharpening uses, hilariously, the imsharpen command
imgUnsharp = imsharpen(img);

% The imsharpen also allows you to set the k value (amount), the sigma of
% the gaussian blur (radius), and the threshold to be considered an edge
% pixel.
figure
imshow([img imgSharp imgUnsharp])

