comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Calculate the mean of many images
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Calculate the mean of many images [message #63623] Mon, 17 November 2008 06:35 Go to next message
Jeremy Bailin is currently offline  Jeremy Bailin
Messages: 618
Registered: April 2008
Senior Member
On Nov 16, 9:57 am, Jeremy Bailin <astroco...@gmail.com> wrote:
> On Nov 16, 1:16 am, Bulrush <Wasit.Weat...@gmail.com> wrote:
>
>
>
>> Hello,
>> I know this topic has been posted several times. But I could not find
>> my answer from these posted.
>> My issue is: I have many images 2 bands in each, one image is QA image
>> and the other one is data.
>> I need to calculate the mean of good pixels. Let's say QA image tells
>> me the location of good pixels, e.g. 1 for good pixels, and other for
>> bad. There are also NaN values. So, if the pixels are "good" in 7
>> images out of ten, then
>> (pixel1+pixel2...+Pixel7) /7
>
>> How can I do that?
>
>> Thanks any advice!
>
>> Elkunn
>
> I'm going to assume that qa and image are both nx x ny x nimage
> arrays, and qa is 1 for good, 0 for bad.
>
> ngoodpix = total(qa, 3)
> imagesum = total(image * qa, 3)
> meanimage = imagesum/ngoodpix
>
> -Jeremy.

Sorry, that doesn't deal with the NaNs properly... and you said that
qa can have other values than 1? How about this:

ngoodpix = total(qa ne 0, 3)
imagesum = total(image*finite(image)*(qa ne 0), 3)
meanimage = imagesum/ngoodpix

-Jeremy.
Re: Calculate the mean of many images [message #63635 is a reply to message #63623] Sun, 16 November 2008 23:13 Go to previous messageGo to next message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
On Nov 16, 7:35 pm, Bulrush <Wasit.Weat...@gmail.com> wrote:
...
>
> Seems like I got lost between the codes. the mean I got with avg[wh]
> is all zero. Here is my code. Thanks for your help.
...
>   mask = (QA EQ 0.00) AND (finite(Data) EQ 1) OR (QA EQ 1.00) AND
> (finite(Data) EQ 1)

So, does MASK select the pixels you want?

Also, you'd better make the order of operations explicit with
parentheses. You may or may not be getting what you expect.

> I am also wondering what does += means?

A += B ; is the same as
A = A + B

Craig
Re: Calculate the mean of many images [message #63639 is a reply to message #63635] Sun, 16 November 2008 16:35 Go to previous messageGo to next message
Wasit.Weather is currently offline  Wasit.Weather
Messages: 62
Registered: February 2008
Member
On Nov 16, 3:04 pm, David Fanning <n...@dfanning.com> wrote:
> Bulrush writes:
>> Thank you. I just run the code, but it always reporting syntax error
>> for sum(wh) +=3D img(wh), for the brackets.
>
> I think that is a hold-over from Craig's days as the King
> of IDL 4.0. ;-)
>
> Cheers,
>
> David
>
> --
> David Fanning, Ph.D.
> Fanning Software Consulting, Inc.
> Coyote's Guide to IDL Programming:http://www.dfanning.com/
> Sepore ma de ni thui. ("Perhaps thou speakest truth.")

Seems like I got lost between the codes. the mean I got with avg[wh]
is all zero. Here is my code. Thanks for your help.

FOR K = 0, images-1 DO BEGIN
ENVI_OPEN_FILE, files[K], r_fid=files_fid
ENVI_FILE_QUERY, files_fid, ......
pos = lindgen(nb)
; get the data
Data = ENVI_GET_DATA(fid=files_fid, dims=dims, pos=pos[0])
QA = ENVI_GET_DATA(fid=files_fid, dims=dims, pos=pos[1])
mask = (QA EQ 0.00) AND (finite(Data) EQ 1) OR (QA EQ 1.00) AND
(finite(Data) EQ 1)

; Sum Valid Pixels
npix += mask
wh = where(mask, ct)
if ct GT 0 then sum[wh] += Data[wh]
EndFor

; Positions where there are good pixels
qa_avg = (npix GT 0)
wh = where(qa_avg EQ 1)

; Compute average for valid pixels
avg = sum*0
avg[wh] = sum[wh] / npix[wh]

I am also wondering what does += means?

Thank you!!!1
Re: Calculate the mean of many images [message #63644 is a reply to message #63639] Sun, 16 November 2008 13:04 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Bulrush writes:

> Thank you. I just run the code, but it always reporting syntax error
> for sum(wh) +=3D img(wh), for the brackets.

I think that is a hold-over from Craig's days as the King
of IDL 4.0. ;-)

Cheers,

David

--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Re: Calculate the mean of many images [message #63645 is a reply to message #63644] Sun, 16 November 2008 12:52 Go to previous messageGo to next message
Wasit.Weather is currently offline  Wasit.Weather
Messages: 62
Registered: February 2008
Member
On Nov 16, 2:04 pm, Craig Markwardt <cbmarkwa...@gmail.com> wrote:
> On Nov 16, 1:30 pm, Bulrush <Wasit.Weat...@gmail.com> wrote:
>
>
>
>> On Nov 16, 2:54 am, Craig Markwardt <cbmarkwa...@gmail.com> wrote:
>
>>> On Nov 16, 1:16 am, Bulrush <Wasit.Weat...@gmail.com> wrote:
>
>>>> Hello,
>>>> I know this topic has been posted several times. But I could not find
>>>> my answer from these posted.
>>>> My issue is: I have many images 2 bands in each, one image is QA image
>>>> and the other one is data.
>>>> I need to calculate the mean of good pixels. Let's say QA image tells
>>>> me the location of good pixels, e.g. 1 for good pixels, and other for
>>>> bad. There are also NaN values. So, if the pixels are "good" in 7
>>>> images out of ten, then
>>>> (pixel1+pixel2...+Pixel7) /7
>
>>> I would loop over input images, and keep track of the cumulative sum
>>> of the number of valid pixels (NPIX), and the cumulative sum of the
>>> pixel values (SUM).  Something like the following.  Since there are
>>> only a few images, there will be very little overhead in the FOR-loop.
>
>>> Craig
>
>>> npix = 0 & sum = 0
>>> for i = 0, n_images-1 do begin
>>>   qa = ... the ith QA image ...
>>>   img = ... the ith image ...
>>>   mask = (qa EQ 1) AND (finite(img) EQ 1)
>
>>>   ;; Sum valid pixels
>>>   npix += mask
>>>   wh = where(mask, ct)
>>>   if ct GT 0 then sum(wh) += img(wh)
>>> endfor
>
>>> ;; Positions where there are good pixels
>>> qa_avg = (npix GT 0)
>>> wh = where(qa_avg EQ 1)
>
>>> ;; Compute average for valid pixels
>>> avg = sum*0
>>> avg(wh) = sum(wh) / npix(wh)
>
>> Thanks for all the comments. I think Craig's method would work for me.
>> the Actual QA image contains more than one value, such as 0.00 (good
>> pixel), 1.000 (between good and band), 2.000 (cloud), 3.000(snow),
>> etc.
>> What does finite(img) EQ 1 mean here? Can I write this statement as
>> the following?
>
>> mask = (qa EQ 1.000 and qa EQ 0.000 ) AND (finite(img) EQ 1.000 &&
>> 0.000)
>
> As Chris points out, this is a logically inconsistent expression.
> Probably you want,
>   (qa EQ 1 OR qa EQ 0)
> for the first part of your expression.
>
> I'm not sure what you are getting at from the second part of the
> expression.  FINITE(img) is 0 if the pixel value is infinite or NaN, 1
> otherwise.  Looks like your "&& 0.000" is superfluous.
>
> To answer your other question: the WHERE occurs a few lines later.
> I'm using a MASK since you need to accumulate the number of pixels as
> well as the pixel values.

Thank you. I just run the code, but it always reporting syntax error
for sum(wh) += img(wh), for the brackets.
Re: Calculate the mean of many images [message #63646 is a reply to message #63645] Sun, 16 November 2008 12:04 Go to previous messageGo to next message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
On Nov 16, 1:30 pm, Bulrush <Wasit.Weat...@gmail.com> wrote:
> On Nov 16, 2:54 am, Craig Markwardt <cbmarkwa...@gmail.com> wrote:
>
>
>
>> On Nov 16, 1:16 am, Bulrush <Wasit.Weat...@gmail.com> wrote:
>
>>> Hello,
>>> I know this topic has been posted several times. But I could not find
>>> my answer from these posted.
>>> My issue is: I have many images 2 bands in each, one image is QA image
>>> and the other one is data.
>>> I need to calculate the mean of good pixels. Let's say QA image tells
>>> me the location of good pixels, e.g. 1 for good pixels, and other for
>>> bad. There are also NaN values. So, if the pixels are "good" in 7
>>> images out of ten, then
>>> (pixel1+pixel2...+Pixel7) /7
>
>> I would loop over input images, and keep track of the cumulative sum
>> of the number of valid pixels (NPIX), and the cumulative sum of the
>> pixel values (SUM).  Something like the following.  Since there are
>> only a few images, there will be very little overhead in the FOR-loop.
>
>> Craig
>
>> npix = 0 & sum = 0
>> for i = 0, n_images-1 do begin
>>   qa = ... the ith QA image ...
>>   img = ... the ith image ...
>>   mask = (qa EQ 1) AND (finite(img) EQ 1)
>
>>   ;; Sum valid pixels
>>   npix += mask
>>   wh = where(mask, ct)
>>   if ct GT 0 then sum(wh) += img(wh)
>> endfor
>
>> ;; Positions where there are good pixels
>> qa_avg = (npix GT 0)
>> wh = where(qa_avg EQ 1)
>
>> ;; Compute average for valid pixels
>> avg = sum*0
>> avg(wh) = sum(wh) / npix(wh)
>
> Thanks for all the comments. I think Craig's method would work for me.
> the Actual QA image contains more than one value, such as 0.00 (good
> pixel), 1.000 (between good and band), 2.000 (cloud), 3.000(snow),
> etc.
> What does finite(img) EQ 1 mean here? Can I write this statement as
> the following?
>
> mask = (qa EQ 1.000 and qa EQ 0.000 ) AND (finite(img) EQ 1.000 &&
> 0.000)

As Chris points out, this is a logically inconsistent expression.
Probably you want,
(qa EQ 1 OR qa EQ 0)
for the first part of your expression.

I'm not sure what you are getting at from the second part of the
expression. FINITE(img) is 0 if the pixel value is infinite or NaN, 1
otherwise. Looks like your "&& 0.000" is superfluous.

To answer your other question: the WHERE occurs a few lines later.
I'm using a MASK since you need to accumulate the number of pixels as
well as the pixel values.
Re: Calculate the mean of many images [message #63647 is a reply to message #63646] Sun, 16 November 2008 11:08 Go to previous messageGo to next message
Chris[6] is currently offline  Chris[6]
Messages: 84
Registered: July 2008
Member
> What does finite(img) EQ 1 mean here? Can I write this statement as
> the following?

finitie(img) eq 1 will evaluate to the same thing as finite(img) - the
finite function returns a 1 or a zero depending on whether the input
is finite.

> mask = (qa EQ 1.000 and qa EQ 0.000 ) AND (finite(img) EQ 1.000 &&
> 0.000)

this statement is mutually exclusive (qa can't equal 1 and zero at the
same time). The finite part is equally problematic. I'm not sure what
you want mask to be. But anyways, once you know how to properly phrase
the logical test for your mask, I would do the following:

;- calculate the average
mask = .... logical test on QA which is TRUE for a pixel which you
want to consider ...
average_image = total(im * mask, 3) / total(mask, 3)

;- check for and pixels where there were no good images
bad = where(total(mask,3) eq 0, ct)
if ct eq 0 then average_image[bad] = !values.f_nan

maybe a good mask would look like
mask = finite(qa) if anything finite is good or,
if you want to pull out specific qa values,
mask = ((qa eq 1) or (qa eq 2))

chris
Re: Calculate the mean of many images [message #63648 is a reply to message #63647] Sun, 16 November 2008 10:32 Go to previous messageGo to next message
Wasit.Weather is currently offline  Wasit.Weather
Messages: 62
Registered: February 2008
Member
On Nov 16, 12:30 pm, Bulrush <Wasit.Weat...@gmail.com> wrote:
> On Nov 16, 2:54 am, Craig Markwardt <cbmarkwa...@gmail.com> wrote:
>
>
>
>
>
>> On Nov 16, 1:16 am, Bulrush <Wasit.Weat...@gmail.com> wrote:
>
>>> Hello,
>>> I know this topic has been posted several times. But I could not find
>>> my answer from these posted.
>>> My issue is: I have many images 2 bands in each, one image is QA image
>>> and the other one is data.
>>> I need to calculate the mean of good pixels. Let's say QA image tells
>>> me the location of good pixels, e.g. 1 for good pixels, and other for
>>> bad. There are also NaN values. So, if the pixels are "good" in 7
>>> images out of ten, then
>>> (pixel1+pixel2...+Pixel7) /7
>
>> I would loop over input images, and keep track of the cumulative sum
>> of the number of valid pixels (NPIX), and the cumulative sum of the
>> pixel values (SUM).  Something like the following.  Since there are
>> only a few images, there will be very little overhead in the FOR-loop.
>
>> Craig
>
>> npix = 0 & sum = 0
>> for i = 0, n_images-1 do begin
>>   qa = ... the ith QA image ...
>>   img = ... the ith image ...
>>   mask = (qa EQ 1) AND (finite(img) EQ 1)
>
>>   ;; Sum valid pixels
>>   npix += mask
>>   wh = where(mask, ct)
>>   if ct GT 0 then sum(wh) += img(wh)
>> endfor
>
>> ;; Positions where there are good pixels
>> qa_avg = (npix GT 0)
>> wh = where(qa_avg EQ 1)
>
>> ;; Compute average for valid pixels
>> avg = sum*0
>> avg(wh) = sum(wh) / npix(wh)
>
> Thanks for all the comments. I think Craig's method would work for me.
> the Actual QA image contains more than one value, such as 0.00 (good
> pixel), 1.000 (between good and band), 2.000 (cloud), 3.000(snow),
> etc.
> What does finite(img) EQ 1 mean here? Can I write this statement as
> the following?
>
> mask = (qa EQ 1.000 and qa EQ 0.000 ) AND (finite(img) EQ 1.000 &&
> 0.000)
>
> Thanks- Hide quoted text -
>
> - Show quoted text -

Craig, is there Where routine is missing in the statement?

thanks
Re: Calculate the mean of many images [message #63649 is a reply to message #63648] Sun, 16 November 2008 10:30 Go to previous messageGo to next message
Wasit.Weather is currently offline  Wasit.Weather
Messages: 62
Registered: February 2008
Member
On Nov 16, 2:54 am, Craig Markwardt <cbmarkwa...@gmail.com> wrote:
> On Nov 16, 1:16 am, Bulrush <Wasit.Weat...@gmail.com> wrote:
>
>> Hello,
>> I know this topic has been posted several times. But I could not find
>> my answer from these posted.
>> My issue is: I have many images 2 bands in each, one image is QA image
>> and the other one is data.
>> I need to calculate the mean of good pixels. Let's say QA image tells
>> me the location of good pixels, e.g. 1 for good pixels, and other for
>> bad. There are also NaN values. So, if the pixels are "good" in 7
>> images out of ten, then
>> (pixel1+pixel2...+Pixel7) /7
>
> I would loop over input images, and keep track of the cumulative sum
> of the number of valid pixels (NPIX), and the cumulative sum of the
> pixel values (SUM).  Something like the following.  Since there are
> only a few images, there will be very little overhead in the FOR-loop.
>
> Craig
>
> npix = 0 & sum = 0
> for i = 0, n_images-1 do begin
>   qa = ... the ith QA image ...
>   img = ... the ith image ...
>   mask = (qa EQ 1) AND (finite(img) EQ 1)
>
>   ;; Sum valid pixels
>   npix += mask
>   wh = where(mask, ct)
>   if ct GT 0 then sum(wh) += img(wh)
> endfor
>
> ;; Positions where there are good pixels
> qa_avg = (npix GT 0)
> wh = where(qa_avg EQ 1)
>
> ;; Compute average for valid pixels
> avg = sum*0
> avg(wh) = sum(wh) / npix(wh)

Thanks for all the comments. I think Craig's method would work for me.
the Actual QA image contains more than one value, such as 0.00 (good
pixel), 1.000 (between good and band), 2.000 (cloud), 3.000(snow),
etc.
What does finite(img) EQ 1 mean here? Can I write this statement as
the following?

mask = (qa EQ 1.000 and qa EQ 0.000 ) AND (finite(img) EQ 1.000 &&
0.000)

Thanks
Re: Calculate the mean of many images [message #63654 is a reply to message #63649] Sun, 16 November 2008 06:57 Go to previous messageGo to next message
Jeremy Bailin is currently offline  Jeremy Bailin
Messages: 618
Registered: April 2008
Senior Member
On Nov 16, 1:16 am, Bulrush <Wasit.Weat...@gmail.com> wrote:
> Hello,
> I know this topic has been posted several times. But I could not find
> my answer from these posted.
> My issue is: I have many images 2 bands in each, one image is QA image
> and the other one is data.
> I need to calculate the mean of good pixels. Let's say QA image tells
> me the location of good pixels, e.g. 1 for good pixels, and other for
> bad. There are also NaN values. So, if the pixels are "good" in 7
> images out of ten, then
> (pixel1+pixel2...+Pixel7) /7
>
> How can I do that?
>
> Thanks any advice!
>
> Elkunn

I'm going to assume that qa and image are both nx x ny x nimage
arrays, and qa is 1 for good, 0 for bad.

ngoodpix = total(qa, 3)
imagesum = total(image * qa, 3)
meanimage = imagesum/ngoodpix

-Jeremy.
Re: Calculate the mean of many images [message #63658 is a reply to message #63654] Sun, 16 November 2008 00:54 Go to previous messageGo to next message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
On Nov 16, 1:16 am, Bulrush <Wasit.Weat...@gmail.com> wrote:
> Hello,
> I know this topic has been posted several times. But I could not find
> my answer from these posted.
> My issue is: I have many images 2 bands in each, one image is QA image
> and the other one is data.
> I need to calculate the mean of good pixels. Let's say QA image tells
> me the location of good pixels, e.g. 1 for good pixels, and other for
> bad. There are also NaN values. So, if the pixels are "good" in 7
> images out of ten, then
> (pixel1+pixel2...+Pixel7) /7

I would loop over input images, and keep track of the cumulative sum
of the number of valid pixels (NPIX), and the cumulative sum of the
pixel values (SUM). Something like the following. Since there are
only a few images, there will be very little overhead in the FOR-loop.

Craig


npix = 0 & sum = 0
for i = 0, n_images-1 do begin
qa = ... the ith QA image ...
img = ... the ith image ...
mask = (qa EQ 1) AND (finite(img) EQ 1)

;; Sum valid pixels
npix += mask
wh = where(mask, ct)
if ct GT 0 then sum(wh) += img(wh)
endfor

;; Positions where there are good pixels
qa_avg = (npix GT 0)
wh = where(qa_avg EQ 1)

;; Compute average for valid pixels
avg = sum*0
avg(wh) = sum(wh) / npix(wh)
Re: Calculate the mean of many images [message #63659 is a reply to message #63658] Sun, 16 November 2008 00:08 Go to previous messageGo to next message
greg.addr is currently offline  greg.addr
Messages: 160
Registered: May 2007
Senior Member
How about:

q=where(QA eq 1, n)
mean=total(image[q])/n

cheers,
Greg
Re: Calculate the mean of many images [message #63749 is a reply to message #63659] Mon, 17 November 2008 08:38 Go to previous message
raghuram is currently offline  raghuram
Messages: 32
Registered: February 2008
Member
On Nov 16, 12:08 am, greg.a...@googlemail.com wrote:
> How about:
>
> q=where(QA eq 1, n)
> mean=total(image[q])/n
>
> cheers,
> Greg

To add to Greg's solution- For the NaN, you could use mean=total(image
[q]/n,/nan)
That should work.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: IDL and sqlite
Next Topic: A Simple IDL Manifesto

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 19:22:35 PDT 2025

Total time taken to generate the page: 0.00636 seconds