3x3 average filter

Post any questions, ideas, or topics related to Jython and Python scripting.
User avatar
kbedka1
Posts: 428
Joined: Wed Jan 28, 2009 7:27 pm

3x3 average filter

Post by kbedka1 »

I have some noisy data for which I want to do some spatial averaging. I want to do an average of all 9 pixels in a 3x3 box and then replace the center pixel of the 3x3 with this average. I can't really tell from looking at the Image Filters in the Formulas if any of these filters will accomplish my goal. Could someone let me know how I can accomplish this in Mc-V? Explanations for the user settings in the default image filter formulas should probably be documented in the Users Guide...
User avatar
jayh
Posts: 424
Joined: Thu Jan 15, 2009 10:34 pm

Re: 3x3 average filter

Post by jayh »

Hi Kris-

I forwarded your question to the programmers... and regarding the documentation, that is one of our requirements for version 1.0 and is being worked on at the moment. Thanks for pointing it out.

Thanks, Jay
User avatar
jayh
Posts: 424
Joined: Thu Jan 15, 2009 10:34 pm

Re: 3x3 average filter

Post by jayh »

Hi Kris-

I have received some information on this from a couple of sources.

The Image Filter routines in the Jython library could be used for low-pass filtering ("lowPass2DFilter()"). The documentation for McIDAS-X image filters for explanation of the input parameters in the Remarks section for Low2:
http://www.ssec.wisc.edu/mcidas/doc/use ... gfilt.html

The McIDAS-X code was transcribed into Jython to get it to work like the McIDAS-X image filters. We also do not know if they were implemented as simple 9 point filters or something else.... You could write some Jython to do a 9 point filter if what we have supplied isn't specific enough to your needs.

Please let us know if you have any further questions.
Thanks, Jay
User avatar
kbedka1
Posts: 428
Joined: Wed Jan 28, 2009 7:27 pm

Re: 3x3 average filter

Post by kbedka1 »

do you have a jython code example on how one would write a 9 point mean image filter? These Mc-X filters included in V do not seem to do what I'd like it to.
User avatar
tomw
Posts: 296
Joined: Tue Dec 23, 2008 3:40 pm

Re: 3x3 average filter

Post by tomw »

Kris -- Here is an example of a (brute-force) 9 point average filter, based on Kevin's port of the Mc-X lowpass filter. It is for single-banded images only, but will handle multiple time steps. You can just copy and paste this into the "User" Jython Library, and then make a Formula to call it...enjoy!

tom

Code: Select all

def NinePointFilter(sdataset):
   """ Simple, equal-weighted 9 point filter
   """   
   newData = sdataset.clone()

   for t in xrange(len(newData)):
     rangeObject = newData.getSample(t)
     vals = rangeObject.getFloats()
     srcvals = sdataset.getSample(t).getFloats();
     domain=GridUtil.getSpatialDomain(rangeObject)
     [element_size,line_size]=domain.getLengths()
     
     for i in xrange(1,line_size-1):
       k = i*element_size;
       for j in xrange(1,element_size-1):
          vals[0][k+j] = (srcvals[0][k+j] + srcvals[0][k+j-1] + srcvals[0][k+j+1]
            + srcvals[0][k+j-element_size] + srcvals[0][k+j-element_size-1] +
              srcvals[0][k+j-element_size + 1]
            + srcvals[0][k+j+element_size] + srcvals[0][k+j+element_size-1] +
              srcvals[0][k+j+element_size + 1]) / 9
     rangeObject.setSamples(vals)

   return newData
Last edited by tomw on Mon May 17, 2010 6:11 pm, edited 2 times in total.
User avatar
tomw
Posts: 296
Joined: Tue Dec 23, 2008 3:40 pm

Re: 3x3 average filter

Post by tomw »

Corrected code formatting in previous post...
User avatar
kbedka1
Posts: 428
Joined: Wed Jan 28, 2009 7:27 pm

3x3 average filter, problem with MODIS

Post by kbedka1 »

I'm trying to use this 3x3 filter with MODIS IR data. I choose band 31 and create display, but get the following error:

Traceback (most recent call last):
File "<string>", line 1, in <module>
File "<string>", line 18, in NinePointFilter
AttributeError: 'visad.Real' object has no attribute 'getFloats'

This has worked with GOES data acquired via ADDE, but not MODIS. Any suggestions?
User avatar
tomw
Posts: 296
Joined: Tue Dec 23, 2008 3:40 pm

Re: 3x3 average filter

Post by tomw »

Hi Kris --

What MODIS data are you trying to use this with? The error message basically says that you have a single value, not an array of values...so I'll have to look at the actual data...
User avatar
kbedka1
Posts: 428
Joined: Wed Jan 28, 2009 7:27 pm

Re: 3x3 average filter

Post by kbedka1 »

Just a MOD02 hdf file brought in via Hydra multispectral data chooser
User avatar
tomw
Posts: 296
Joined: Tue Dec 23, 2008 3:40 pm

Re: 3x3 average filter

Post by tomw »

Kris --

The function as written is designed for ADDE accessed (or other) data that includes a "time" dimension. I will send you a version that will work with your data that basically removes the outer loop...but not until next week when I return from Mandatory Furlough Days...
Post Reply