I want create a convolution filter for RGB images

3 次查看(过去 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
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 个)

类别

Help CenterFile Exchange 中查找有关 Image Processing and Computer Vision 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by