How can I convolve my image with small patch of size 16 X 16 of same image? My kernel is the patch of that image

8 views (last 30 days)
Hi all
I am working with convolution now.
I tried to convolve two images but it is taking more time to work with.
Also I want to convolve my image with the kernel which is the small patch of the same image of size 16 X 16.
How can I perform this?
Can anyone help me out?

Accepted Answer

Chris Turnes
Chris Turnes on 4 Aug 2014
What type of convolution are you trying to do? If you want a circular convolution, you should use the FFT to calculate it (see the documentation on fftn and ifftn for more details). For example:
A = phantom(256); % image source
B = randn(16); % "patch" or "kernel"
N = max([size(A); size(B)]); % get the correct FFT size
C = ifftn(fftn(A,N) .* fftn(B,N)); % do the convolution
If you want linear convolution, you can use the conv2 function from the Image Processing Toolbox ( see documentation ):
D = conv2(A,B); % 2-D linear convolution
Since the patch is probably much smaller than the image, this should be the most efficient option.
If you do not have conv2 or if you use a larger patch, you can still use the FFT to do linear convolution, but you will need to increase the FFT size. For the example above, the linear convolution of A and B would be
N = size(A) + size(B) - 1; % linear conv. FFT size
E = ifftn(fftn(A, N) .* fftn(B,N)); % do the convolution
If you are not sure which type of convolution you need, see the article Linear and Circular Convolution. You might also want to look into the imfilter function (see the documentation).

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!