I want create a convolution filter for RGB images

조회 수: 7 (최근 30일)
Mustafa Uysal
Mustafa Uysal 2019년 3월 19일
댓글: Guillaume 2019년 3월 19일
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
Mustafa Uysal
Mustafa Uysal 2019년 3월 19일
편집: Mustafa Uysal 2019년 3월 19일
Yes i want it to work on RGB images.
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개)

Community Treasure Hunt

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

Start Hunting!

Translated by