Filter löschen
Filter löschen

I want create a convolution filter for RGB images

2 Ansichten (letzte 30 Tage)
Mustafa Uysal
Mustafa Uysal am 19 Mär. 2019
Kommentiert: Guillaume am 19 Mär. 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 Kommentare
Mustafa Uysal
Mustafa Uysal am 19 Mär. 2019
Bearbeitet: Mustafa Uysal am 19 Mär. 2019
Yes i want it to work on RGB images.
Guillaume
Guillaume am 19 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Image Processing and Computer Vision finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by