image processing - How do I create and apply a Gaussian filter in MATLAB without using fspecial, imfilter or conv2? -


i have following code in matlab:

i=imread(image); h=fspecial('gaussian',si,sigma); i=im2double(i); i=imfilter(i,h,'conv'); figure,imagesc(i),impixelinfo,title('original image after convolving gaussian'),colormap('gray'); 

how can define , apply gaussian filter image without imfilter, fspecial , conv2?

it's unfortunate can't use of built-in methods image processing toolbox task. however, can still you're asking, though bit more difficult. i'm still going use functions ipt you're asking. also, i'm going assume image grayscale. i'll leave if want colour images.


create gaussian mask

what can create grid of 2d spatial co-ordinates using meshgrid same size gaussian filter mask creating. i'm going assume n odd make life easier. allow spatial co-ordinates symmetric around mask.

if recall, 2d gaussian can defined as:

the scaling factor in front of exponential concerned ensuring area underneath gaussian 1. deal normalization in way, generate gaussian coefficients without scaling factor, sum of coefficients in mask , divide every element sum ensure unit area.

assuming want create n x n filter, , given standard deviation sigma, code this, h representing gaussian filter.

%// generate horizontal , vertical co-ordinates, %// origin in middle ind = -floor(n/2) : floor(n/2); [x y] = meshgrid(ind, ind);  %// create gaussian mask h = exp(-(x.^2 + y.^2) / (2*sigma*sigma));  %// normalize total area (sum of weights) 1 h = h / sum(h(:)); 

if check fspecial, odd values of n, you'll see masks match.


filter image

the basics behind filtering image each pixel in input image, take pixel neighbourhood surrounds pixel same size gaussian mask. perform element-by-element multiplication pixel neighbourhood gaussian mask , sum of elements together. resultant sum output pixel @ corresponding spatial location in output image. i'm going use im2col take pixel neighbourhoods , turn them columns. im2col take each of these columns , create matrix each column represents 1 pixel neighbourhood.

what can next take our gaussian mask , convert column vector. next, take column vector, , replicate many columns have result of im2col create... let's call gaussian matrix lack of better term. gaussian matrix, element-by-element multiplication matrix , output of im2col. once this, can sum on of rows each column. best way element-by-element multiplication through bsxfun, , i'll show how use soon.

the result of filtered image, single vector. need reshape vector matrix form col2im our filtered image. however, slight problem approach doesn't filter pixels spatial mask extends beyond dimensions of image. such, you'll need pad border of image zeroes can our filter. can padarray.

therefore, our code this, going variables have defined above:

n = 5; %// define size of gaussian mask sigma = 2; %// define sigma here  %// generate gaussian mask ind = -floor(n/2) : floor(n/2); [x y] = meshgrid(ind, ind); h = exp(-(x.^2 + y.^2) / (2*sigma*sigma)); h = h / sum(h(:));  %// convert filter column vector h = h(:);  %// filter our image = imread(image); = im2double(i); i_pad = padarray(i, [floor(n/2) floor(n/2)]); c = im2col(i_pad, [n n], 'sliding'); c_filter = sum(bsxfun(@times, c, h), 1); out = col2im(c_filter, [n n], size(i_pad), 'sliding'); 

out contains filtered image after applying gaussian filtering mask input image i. example, let's n = 9, sigma = 4. let's use cameraman.tif image that's part of matlab system path. using above parameters, image, input , output image get:

enter image description here

enter image description here


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -