HI Professionals,
I am back at it again, this time i am trying to figure out how to code convolution within matlab for 1D data
my data below;
I have variable f & g and I would like to perform convolution (f*g) placing the values into (h)
f = [10,50,60,10,20,40,30];
g =[1/3,1/3,1/3];
I am trying to code this process in matlab so that the (filter "g") slides over (f) and where there is no numbers
I must implement (padding) a zero (0).
it is challenging to understand how to code g sliding over f 1Dimensionally to get values of an object of interest in an image
This is an example image of what I am trying to code, for the variables defined at the top!(Please Ignore The Integers In the Image)
Thank you in advance for assisting me once more with my rediculous questions and experiments
Screenshot 2019-10-18 at 07.45.23.png

 Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 18 Oct 2019

0 votes

h = conv(f,g,'valid')

6 commentaires

Comment by Matpar:
Thanks for acknowleding my question, really means loads! Thank you!
I have this code i was working on and not sure where to go from here! See my code as per my challenges!
But how does it handle the padding? the areas where we assume have no values and are replaced by zero!
What I am try to show is if this g =[1/3,1/3,1/3];
starts at 10 in f of the kernel's sliding process. the middle 1/3 slides over the 10 which computes by adding a zero to the left of 10 for (PADDING) thus it should be 0 , 10, 50 in the first slide of the kernel's calculations?
How am I to coding the padding aspet of this? This is what is confusing me!
I really hope you understood my thought process
thank you once more!
I Don't think this is working!
f=[10,50,60,10,20,40,30];
g=[1/3,1/3,1/3];
[f, g]=size(f);
for i=1:f
for j=1:g
if g<=10
f= 0,10,50;
h = conv(f,g,'valid');
end
end
end
disp(h);
h = padarray(conv(f,g,'valid'),[0,numel(g)-1]);
Matpar
Matpar le 18 Oct 2019
Modifié(e) : Matpar le 18 Oct 2019
This worked and thank you for responding to my ridiculousness but question?
is this the only way this can be represented only with one line of code?
The reason for asking is "I am trying to understand" what each piece of code is doing! Can you help a little further by commenting on this part [0,numel(g)-1]) please?
and how do i apply this to an rgb image?
thank you in advance
Andrei Bobrov
Andrei Bobrov le 18 Oct 2019
" ..i apply this to an rgb image?" - Yes.
padarray - function from Image Processing Toolbox. Please read about padarray.
Matpar
Matpar le 21 Oct 2019
Thanx Andrei,
Hi Andrei,
I thinkI am doing something wrong, a humble request for you to guide me please!!my code!
I have an image but the system keep saying A and B must be vectors and i am not sure how to understand it!
please take a look!
im = imread('bird.jpg');
[r, c]=size(im);
im=zeros(r,c);
g=[1/3,1/3,1/3];
h = padarray(conv(im,g,'valid'),[0,numel(g)-1]);
disp(h);
bird.jpg

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by