function taking in an input image and different kernel size

Posted by drifterOcean19 on Stack Overflow See other posts from Stack Overflow or by drifterOcean19
Published on 2013-10-26T11:41:55Z Indexed on 2013/10/26 15:54 UTC
Read the original article Hit count: 265

Filed under:
|

I have this filtering function that takes an input image, performs convolution using a given kernel, and returns the resulting image. However, I can't seem to work it out how to make it takes different kernel sizes.For example instead of pre-defined 3x3 kernel as below in the code, it could instead take 5x5 or 7x7. and then the user could input the type of kernel/filter they want(Depending on the intended effect). I can't seem to put my head around it. i'm quite new to matlab.

function [newImg] = kernelFunc(imgB)


img=imread(imgB);

figure,imshow(img); 




img2=zeros(size(img)+2);

newImg=zeros(size(img));




for rgb=1:3

        for x=1:size(img,1)

            for y=1:size(img,2)

                img2(x+1,y+1,rgb)=img(x,y,rgb);

            end

        end

end


for rgb=1:3
    for i= 1:size(img2,1)-2

        for j=1:size(img2,2)-2

            window=zeros(9,1);

            inc=1;

            for x=1:3

                for y=1:3

                    window(inc)=img2(i+x-1,j+y-1,rgb);

                    inc=inc+1;

                end

            end

           kernel=[1;2;1;2;4;2;1;2;1]/16;



            med=window.*kernel;

            disp(med);

            med=sum(med);

            med=floor(med);


            newImg(i,j,rgb)=med;



        end

    end

end


newImg=uint8(newImg);

figure,imshow(newImg);

end

© Stack Overflow or respective owner

Related posts about matlab

Related posts about image-processing