function Iout = hw2filters(type,filename)
%function to apply selected filters to an image and animate
try
I = imadjust(imread(filename));
catch
error('--> cannot read file <--');
end
figure;
subplot 122
%make image double for subsequent filtering
I=double (I)
% imshow(I,[0 255]); % the range must be specificed for double images
%note: I'm displaying again becasue of a bug in Matlab ...
subplot 121
imshow(I, [0 255]);
switch lower(type)
case {'thresh'}
disp('Thresholding');
Iout=thresh(I);
case {'median'}
disp('Median Filter');
Iout=median(I,20);
case {'blur_gauss'}
disp('Blurring Gaussian');
Iout=blur_gauss(I,20);
case {'log'}
disp('Blurring Laplacian of Gaussian');
Iout=log(I,11,2);
case {'blur_laplace'}
disp('Blurring Laplacian');
Iout=blur_laplace(I,1);
case {'blur_ave'}
disp('Bluring Using Average ');
Iout=blur_ave(I,11);
case {'unsharp'}
disp('Unsharpening');
Iout=unsharp(I,3,2);
case {'blur_disk'}
disp('Blurring Disk Filter');
Iout=blur_disk(I,10);
case {'hor_prewitt_edge'}
disp('Prewitt Horizontal Edge');
Iout=hor_prewitt_edge(I);
case {'ver_prewitt_edge'}
disp('Prewitt Vertical Edge');
Iout=ver_prewitt_edge(I);
case {'hor_sobel_edge'}
disp('Sobel Horizontal Edge');
Iout=ver_sobel_edge(I);
case {'ver_sobel_edge'}
disp('Sobel Vertical Edge');
Iout=ver_sobel_edge(I);
case {'canny_edge'}
disp('Canny Edge Detection');
Iout=canny_edge(I);
case {'motion'}
disp('Motion');
Iout=motion(I,20,10);
otherwise
disp('Unknown method.');
end
return
function Iout = thresh(I)
i=0;
subplot(1,2,2); hold on;
for thresh=1:6:254
i=i+1;
Iout=I < thresh;
imshow(Iout);
pause(0.1);
drawnow;
swp(i) = sum(sum(Iout))/(256*256);
%fprintf('-> thresh = %u, swp = %f
',thresh, swp(i));
end
hold off
figure, plot(swp)
return
function Iout = median(I,mat_max)
% this function requires the mat_max variable which is the size of
% matrix(row and column same dimension
subplot(1,2,2); hold on;
for mat_size=1:mat_max
Iout=medfilt2(I,[mat_size,mat_size]);
imshow(Iout,[0 255]);
title(strcat('Size nxn Matrix=',num2str(mat_size)));
pause(0.1);
drawnow;
end
hold off
return
function Iout = blur_gauss(I,sigma_max)
% this function is a rotationally symmetric gaussian low pass filter
% HSIZE variable which is vector specifying size row and column
% SIGMA standard deviation and must be positive
subplot(1,2,2); hold on;
for sigma=1:0.5:sigma_max
Iout=imgaussfilt(I,sigma);
imshow(Iout,[0 255]);
title(strcat('Sigma=',num2str(sigma)));
pause(0.8);
drawnow;
end
return
function Iout =log(I,size_max,sigma_max)
% this function requires the 'sigma' and 'size' variables
% you must create a loop for the sigma variable
subplot(1,2,2); hold on;
for sigma=0.1:0.1:sigma_max
for size=1:size_max
H=fspecial('log',size, sigma);
Iout=imfilter(I,H,'replicate');
imshow(Iout,[0 255]);
title(strcat('Sigma=',num2str(sigma),' Size=',num2str(size)));
pause(0.1);
drawnow;
end
end
return
function Iout = blur_laplace(I,alpha_max)
% 3x3 filter this function requires the 'alpha' variable which is the
% shape of the laplacian and is in the range 0-1
subplot(1,2,2); hold on;
for alpha=0.01:0.05:alpha_max
H=fspecial('laplacian',alpha);
Iout=imfilter(I,H,'replicate');
imshow(Iout);
title(strcat('Alpha=',num2str(alpha)));
pause(0.2);
drawnow;
end
return
function Iout = unsharp(I,radius_max,amount_max)
% filter used as part of unsharp masking.
% Radius - Specifies the standard deviation of the Gaussian lowpass
% filter used as part of unsharp masking. This value controls the size
% of the region around the edge pixels that is affected by sharpening
% A large value sharpens wider regions around the edges, whereas a small
% value sharpens narrower regions around edges. Default value is 1.
% Amount - Specifies the strength of the sharpening effect. A higher
% value leads to larger increase in the contrast of the sharpened pixels.
% Typical values for this parameter are within the range [0 2], although
% values greater than 2 are allowed. Very large values for this parameter
% may create undesirable effects in the output image. Default value is 0.8.
subplot(1,2,2); hold on;
i=0;
for value=0.1:0.2:amount_max
for radius=0.1:0.2:radius_max
Iout=imsharpen(I,'Radius',radius,'Amount',value);
imshow(Iout,[0 255]);
title(strcat('Radius=',num2str(radius),' Value=',num2str(value)));
pause(0.1);
drawnow;
end
end;
return
function Iout = blur_disk(I,max_radius)
% simple bluring using a circular averaging filter (pillbox)
% within the square matrix of side 2*RADIUS+1.
% The default RADIUS is 5.
subplot(1,2,2); hold on;
for radius=1:0.1:max_radius
H=fspecial('disk',radius);
Iout=imfilter(I,H,'replicate');
imshow(Iout,[0 255]);
title(strcat('Radius=',num2str(radius)));
pause(0.1);
drawnow;
end;
return
function Iout = ver_sobel_edge(I)
% simple horizontal edge emphasizing display
% returns 3-by-3 filter that emphasizes
% horizontal edges utilizing the smoothing effect by approximating a
% vertical gradient. If you need to emphasize vertical edges, transpose
% the filter H: H'. [1 2 1;0 0 0;-1 -2 -1].
subplot(1,2,2); hold on;
H=fspecial('sobel');
Iout=imfilter(I,H','replicate');
title('Sobel Vertical Edge Emphasizing');
imshow(Iout,[0 255]);
pause(0.3);
drawnow;
return
function Iout = hor_sobel_edge(I)
% See vertical Sobel
subplot(1,2,2); hold on;
H=fspecial('sobel');
Iout=imfilter(I,H,'replicate');
title('Sobel Horizontal Edge Emphasizing');
imshow(Iout,[0 255]);
pause(0.3);
drawnow;
return
function Iout = hor_prewitt_edge(I)
% simple horizontal edge emphasizing display
% returns 3-by-3 filter that emphasizes
% horizontal edges by approximating a vertical gradient. If you need to
% emphasize vertical edges, transpose the filter H: H'.
% [1 1 1;0 0 0;-1 -1 -1].
subplot(1,2,2); hold on;
H=fspecial('prewitt');
Iout=imfilter(I,H,'replicate');
title('Prewitt Horizontal Edge Emphasizing');
imshow(Iout,[0 255]);
pause(0.3);
drawnow;
return
function Iout = ver_prewitt_edge(I)
% simple vertical edge emphasizing display see ho edgerizontal Prewitt
subplot(1,2,2); hold on;
H=fspecial('prewitt');
Iout=imfilter(I,H','replicate');
title('Prewitt Vertical Edge Emphasizing');
imshow(Iout,[0 255]);
pause(0.3);
drawnow;
return
function Iout = canny_edge(I)
% Canny edge emphasizing display
subplot(1,2,2); hold on;
Iout = edge(I,'canny');
imshow(Iout);
pause(0.1)
drawnow
return
function Iout = motion(I,len_max,theta_max)
% simple bluring using a circular averaging filter (pillbox)
subplot(1,2,2); hold on;
for len=1:len_max
for theta=0:5:theta_max
H=fspecial('motion',len,theta);
Iout=imfilter(I,H,'replicate');
imshow(Iout,[0 255]);
title(strcat('Len=',num2str(len),' Theta=',num2str(theta)));
pause(0.1);
drawnow;
end
end;
return
1. Modify the hw2filters.m function library (yours or the posted solution version) per the following
instructions:
a. Create a new function exam and add case {'exam'} to the switch statement to invoke
the function using the calling sequence: l=hw2filters('exam', 'hwtest1.jpg'); (8 pts)
b. Within the exam function, add a imsharpen function call with 'Amount'-2 and
'Radius'=3 to sharpen the original image (8 pts)
c.
Within the exam function, add a averaging filter with 'HSize'=5 to blur the original
image (8 pts)
d.
On a single figure, (Figure 1) plot the original image, the sharpened image and the
blurred image using 3 plots arranged in a 1x3 matrix (1 row, 3 columns). Include a
descriptive title for each plot (8 pts)
e. On a single figure (Figure 2) plot the intensity values of image row 200 versus the
column (x) positions for the original, sharpened and blurred images on a single plot
Include x and y-axis labels and a descriptive title (8 pts)
f. Explain what you observe between the original intensity values at any position and the
sharpened and blurred intensities at these same positions. How does this correlate to
what you would expect from the sharpening and blurring filters used (10 pts)
g. Bonus: Run the modified program for the image: vessels.jpg (see "Assorted Files"
content area) and add any additional observations (20 pts)