Azzera filtri
Azzera filtri

I want create a convolution filter for RGB images

2 visualizzazioni (ultimi 30 giorni)
Mustafa Uysal
Mustafa Uysal il 19 Mar 2019
Commentato: Guillaume il 19 Mar 2019
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 Commenti
Mustafa Uysal
Mustafa Uysal il 19 Mar 2019
Modificato: Mustafa Uysal il 19 Mar 2019
Yes i want it to work on RGB images.
Guillaume
Guillaume il 19 Mar 2019
"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.

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Image Processing and Computer Vision in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by