Saturday, November 20, 2010

Image Processing Using Matlab, Matrices Problem

Programmer Question

Hey everyone,



I'm currently working with Matlab to do some image processing. I've been set a task to basically recreate the convolution function for applying filters. I managed to get the code working okay and everything seemed to be fine.



The next part was for me to do the following..



Write your own m-function for unsharp masking of a given image to produce a new output image.



Your function should apply the following steps:



� apply smoothing to produce a blurred version of the original image,
� subtract the blurred image from the original image to produce an edge image,
� add the edge image to the original image to produce a sharpened image.



Again I've got code mocked up to do this but I run into a few problems. When carrying out the convolution my image is cropped down by 1 pixel, this means when I go to carry out the subtraction for the unsharpening the images are not the same size and the subtraction cannot take place.



To overcome this I want to create a blank matrix in the convolution function that is the same size as the image being inputted, the new image will then go on top of this matrix so in affect the new image has a 1 pixel border around it to make it to its original size. When I try and implement this all I get as an output is the blank matrix I just created, does anyone know why this is happening and if so would you be able to help me fix it?



My code is as follows..



Convolution...



function [ imgout ] = convolution( img, filter )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here

[height, width] = size(img); % height, width: number of im rows, etc.
[filter_height, filter_width] = size(filter);

for height_bound = 1:height - filter_height + 1; % loop over output elements

for width_bound = 1:width - filter_width + 1;

imgout = zeros(height_bound, width_bound); %makes an empty matrix the correct size of image

sum = 0;

for fh = 1:filter_height % loop over mask elements

for fw = 1:filter_width

sum = sum + img(height_bound - fh + filter_height, width_bound - fw + filter_width) * filter(fh, fw);

end
end

imgout(height_bound, width_bound) = sum; % store result

end
end

imshow(imgout)

end


Unsharpen



function sharpen_image = img_sharpen(img)

blur_image = medfilt2(img);

convolution(img, filter);

edge_image = img - blur_image;

sharpen_image = img + edge_image;

end


Any help is greatly appreciated thanks!



Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails