I want create a convolution filter for RGB images
7 次查看(过去 30 天)
显示 更早的评论
Hello,
I want create a 2-D convolution filter. I know there are some functions like imfilter etc. but i don't want use them. How can i change the code on the below for images ?
%INPUT MATRIX
A = zeros(5);
A(:) = 1:25;
%KERNEL
avg3 = ones(3)/9;
%PAD THE MATRIX WITH ZEROS
B = padarray(A,[1 1]);
% PRE-ALLOCATE THE MATRIX
Output = zeros([size(A,1) size(A,2)]);
%PERFORM COONVOLUTION
for i = 1:size(B,1)-2
for j = 1:size(B,2)-2
Temp = B(i:i+2,j:j+2).*avg3;
Output(i,j) = sum(Temp(:));
end
end
display(Output);
3 个评论
Guillaume
2019-3-19
"but i don't want use them"
Why not? What is the point of reinventing the wheel? The built-in functions will perform a lot faster and more reliably than what you have written.
What you have written by the way is not a convolution. It works like a convolution because your convolution matrix is symmetrical but it is a correlation.
With regards to your question, what's stopping you from performing the same calculation on all 3 planes, in successsion?
Finally, note that
Output = zeros([size(A,1) size(A,2)]);
is simply
Output = zeros(size(A));
The latter has the advantage of also working straight out of the box for colour images.
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!