function EXsharpenImage(file,type); % A. Salerno [BPHS 4090 F13] % For specified image, a user-defined kernel (see below) is convolved with % the image in the spatial domain. The two default kernels are a blurring % and a sharpening [see also http://en.wikipedia.org/wiki/Kernel_(image_processing) ] % ex. > EXsharpenImage('filename.png','s'); close all % specify kernel to be used if nargin<2 type = 's';% Sharpening is default end if strcmp(type,'s') || strcmp(type,'sharp') ker = [-1 -1 -1 -1 50 -1 -1 -1 -1]; elseif strcmp(type,'b') || strcmp(type,'blur') ker = [1 1 1 1 1 1 1 1 1]; %Adaptation of blurring kernel on wiki elseif strcmp(type,'e') || strcmp(type,'edge') ker = [-1 -1 -1 -1 8 -1 -1 -1 -1]; % edge detection end im = imread(file); % Imports an image if numel(size(im)) == 3 %Only for rgb images im = rgb2gray(im); %converts the image into a grayscale end %Plot original Image in b/w figure subplot(1,2,1) imagesc(im); colormap(gray); colorbar; axis image; title('Original Image'); n = size(im); imp = zeros(n(1)+2,n(2)+2); % imp is the matrix that will be used to process % the data. It is the "extended matrix" used. % The addition of 2 is for the border (i.e. % there is an addition at the beginning and at % the end) imp(2:end-1,2:end-1) = im; % Set the centre of the processing matrix to % be the original matrix - basis for how the % image will be processed %Set the corner values to be the same imp(1,1) = im(1,1); imp(end,1) = im(end,1); imp(1,end) = im(1,end); imp(end,end) = im(end,end); % Set the centred "edge rows and columns" to be the same imp(1,2:end-1) = im(1,:); % Top row imp(end,2:end-1) = im(end,:); %Bottom row imp(2:end-1,1) = im(:,1); %Left Column imp(2:end-1,end) = im(:,end); %Right column % Since the matrix can be scaled in multiple different ways, the top corner % seems the easiest to do. Using the top corner of imp, I will scan across % imp, multiplying the elements in 3x3 matricies, summing them, and then % taking that value, and putting into 'fim' the Final IMage im2 = zeros(n(1),n(2)); for i = 1:n(1) % Rows for j = 1:n(2) %columns var = ker.*imp(i:i+2,j:j+2); im2(i,j) = sum(sum(var)); end end % In order to prevent errors, I will now normalize the matrix to it's % greatest value, set this to be the max of the original matrix ensuring % every value ends up as an integer subplot(1,2,2) imagesc(im2); colorbar; axis image; title('Image with Kernel Applied')