Reputation: 1964
I can apply a filter on an image I
by the following code:
h = fspecial('gaussian',20,4);
blurred = conv2(I,h,'same');
The size of the image I
is the same as of blurred
, I want to write the process of convolution in matrix multiplication form i.e. blurred=H*I(:)
so i tried the following:
H=convmtx2(h,size(I));
blurred=H*I(:);
but the size of blurred
after reshaping will be larger than of I
because convmtx2
return a full convolution matrix, How can i get a convolution matrix that will return only the central part not the full convolution.
Upvotes: 1
Views: 2099
Reputation: 221514
See if this works for you -
h = fspecial('gaussian',20,4);
H=convmtx2(h,size(I));
I_conv = reshape(H*I(:),size(h)+size(I)-1);
s1 = round(size(h,1)/2);
blurred = I_conv(s1+1:s1+size(I,1),s1+1:s1+size(I,2));
Upvotes: 2