Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Median to # of frames

Subject: Median to # of frames

From: yossi kfir

Date: 14 Nov, 2010 17:05:05

Message: 1 of 8

hello
i want to calculate the Median for each pixel on a number of frames
i mean, for each pixel in the image i want the median of him from all the frames
i wrote the following code but its very slow

// data is a 4-d object containing all the frames
y1=zeros(480,640);
for i=1:480
    for j=1:640
     y1(i,j)=median(data(i,j,10:30)); // taking only 10-30 frames to the median
    end
end

so, can enyone advice me a faster code to do that?
thx yossi

Subject: Median to # of frames

From: David Young

Date: 14 Nov, 2010 17:36:04

Message: 2 of 8

"yossi kfir" <yossikfir@walla.com> wrote in message <ibp4s1$dn3$1@fred.mathworks.com>...
> hello
> i want to calculate the Median for each pixel on a number of frames
> i mean, for each pixel in the image i want the median of him from all the frames
> i wrote the following code but its very slow
>
> // data is a 4-d object containing all the frames
> y1=zeros(480,640);
> for i=1:480
> for j=1:640
> y1(i,j)=median(data(i,j,10:30)); // taking only 10-30 frames to the median
> end
> end
>
> so, can enyone advice me a faster code to do that?
> thx yossi

From your code, data is 3D, not 4D.

Anyway, you could try

    y = squeeze(median(shiftdim(data(:,:,10:30), 2)));

Subject: Median to # of frames

From: yossi kfir

Date: 14 Nov, 2010 18:12:03

Message: 3 of 8

"David Young" <d.s.young.notthisbit@sussex.ac.uk> wrote in message <ibp6m3$9b9$1@fred.mathworks.com>...
> "yossi kfir" <yossikfir@walla.com> wrote in message <ibp4s1$dn3$1@fred.mathworks.com>...
> > hello
> > i want to calculate the Median for each pixel on a number of frames
> > i mean, for each pixel in the image i want the median of him from all the frames
> > i wrote the following code but its very slow
> >
> > // data is a 4-d object containing all the frames
> > y1=zeros(480,640);
> > for i=1:480
> > for j=1:640
> > y1(i,j)=median(data(i,j,10:30)); // taking only 10-30 frames to the median
> > end
> > end
> >
> > so, can enyone advice me a faster code to do that?
> > thx yossi
>
> From your code, data is 3D, not 4D.
>
> Anyway, you could try
>
> y = squeeze(median(shiftdim(data(:,:,10:30), 2)));


thank you David
i tried to run your command and it work very fast
can u explain it to me why i get diffrent result from my code?
thx again yossi

Subject: Median to # of frames

From: David Young

Date: 14 Nov, 2010 20:05:05

Message: 4 of 8

Hi Yossi,

Glad it helped.

> can u explain it to me why i get diffrent result from my code?

Do you mean the numerical results are different, or are you asking about the speed difference?

David

Subject: Median to # of frames

From: yossi kfir

Date: 14 Nov, 2010 20:23:03

Message: 5 of 8

"David Young" <d.s.young.notthisbit@sussex.ac.uk> wrote in message <ibpfdh$mae$1@fred.mathworks.com>...
> Hi Yossi,
>
> Glad it helped.
>
> > can u explain it to me why i get diffrent result from my code?
>
> Do you mean the numerical results are different, or are you asking about the speed difference?
>
> David
hello David
the numerical results are diffrent, and also ill be happy if u can explain the speed result

Subject: Median to # of frames

From: David Young

Date: 15 Nov, 2010 15:17:04

Message: 6 of 8

"yossi kfir" <yossikfir@walla.com> wrote in message <ibpgf7$so1$1@fred.mathworks.com>...
> "David Young" <d.s.young.notthisbit@sussex.ac.uk> wrote in message <ibpfdh$mae$1@fred.mathworks.com>...
> > Hi Yossi,
> >
> > Glad it helped.
> >
> > > can u explain it to me why i get diffrent result from my code?
> >
> > Do you mean the numerical results are different, or are you asking about the speed difference?
> >
> > David
> hello David
> the numerical results are diffrent, and also ill be happy if u can explain the speed result

I'm not sure why the numerical results are different - but maybe I didn't understand your example properly. Can you post some code showing the difference? The results should be identical.

The speed difference is because if you call Matlab functions that process a whole array, the loops are executed more efficiently - the looping code is written in C, and things like checking for illegal array accesses can be done once rather than on every iteration. If you write the loops explicitly in Matlab, there is more overhead.

Subject: Median to # of frames

From: Sean de

Date: 15 Nov, 2010 15:31:06

Message: 7 of 8

"yossi kfir" <yossikfir@walla.com> wrote in message <ibpgf7$so1$1@fred.mathworks.com>...
> "David Young" <d.s.young.notthisbit@sussex.ac.uk> wrote in message <ibpfdh$mae$1@fred.mathworks.com>...
> > Hi Yossi,
> >
> > Glad it helped.
> >
> > > can u explain it to me why i get diffrent result from my code?
> >
> > Do you mean the numerical results are different, or are you asking about the speed difference?
> >
> > David
> hello David
> the numerical results are diffrent, and also ill be happy if u can explain the speed result

David,
I believe you got it right:
%%%
data = ceil(rand(480,640,30)*10);

y1=zeros(480,640);
for i=1:480
    for j=1:640
     y1(i,j)=median(data(i,j,10:30)); % // taking only 10-30 frames to the median
    end
end

%
y = squeeze(median(shiftdim(data(:,:,10:30), 2)));

%{
isequal(y,y1)

ans =

     1
%}
%%%

Subject: Median to # of frames

From: yossi kfir

Date: 15 Nov, 2010 16:13:05

Message: 8 of 8

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ibrjnq$i6d$1@fred.mathworks.com>...
> "yossi kfir" <yossikfir@walla.com> wrote in message <ibpgf7$so1$1@fred.mathworks.com>...
> > "David Young" <d.s.young.notthisbit@sussex.ac.uk> wrote in message <ibpfdh$mae$1@fred.mathworks.com>...
> > > Hi Yossi,
> > >
> > > Glad it helped.
> > >
> > > > can u explain it to me why i get diffrent result from my code?
> > >
> > > Do you mean the numerical results are different, or are you asking about the speed difference?
> > >
> > > David
> > hello David
> > the numerical results are diffrent, and also ill be happy if u can explain the speed result
>
> David,
> I believe you got it right:
> %%%
> data = ceil(rand(480,640,30)*10);
>
> y1=zeros(480,640);
> for i=1:480
> for j=1:640
> y1(i,j)=median(data(i,j,10:30)); % // taking only 10-30 frames to the median
> end
> end
>
> %
> y = squeeze(median(shiftdim(data(:,:,10:30), 2)));
>
> %{
> isequal(y,y1)
>
> ans =
>
> 1
> %}
> %%%

ok thank you :)

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us