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

Home » Public Forums » archive » Re: 3D congrid without interpolation
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: 3D congrid without interpolation [message #53405] Sat, 14 April 2007 10:59 Go to next message
James Kuyper is currently offline  James Kuyper
Messages: 425
Registered: March 2000
Senior Member
JD Smith wrote:
> On Thu, 12 Apr 2007 15:40:07 -0700, mgalloy@gmail.com wrote:
>
>> On Apr 12, 3:19 pm, David Fanning <n...@dfanning.com> wrote:
>>> Humm. Hard for me to imagine what you are using to
>>> do this that is interpolating anything for you.
>>> CONGRID is normally used, but that won't interpolate
>>> unless you explicitly tell it to.
>>
>> CONGRID interpolates 3-dimensional arrays by default. From the online
>> help for the INTERP keyword for CONGRID:
>>
>> INTERP
>> Set this keyword to force CONGRID to use linear interpolation when
>> resizing a 1- or 2-dimensional array. CONGRID automatically uses
>> linear interpolation if the input array is 3-dimensional. When the
>> input array is 1- or 2-dimensional, the default is to employ nearest-
>> neighbor sampling.
>
> How is "nearest neighbor sampling" not interpolation? Does it
> explicitly avoid knowledge of how the new array cell is positioned
> w.r.t. the old one, and simply grab averages of nearby neighbors? ...

No. It grabs the value of the one nearest neighbor, with appropriate
rules for breaking ties. No averaging of any kind is done on that
value, which is why it's inappropriate to call this 'interpolation'.

> ... Why
> would this ever be preferable to a linear interpolation?

Well, for one thing, it's a lot faster.

However, another good reason is if you're re-binning categorical
data, where the codes representing each category are arbitrary, and
it's simply not meaningful to take the average of the category codes.
If category 1 means 'corn' and category 3 means 'wheat', you don't
want a thin barrier line of category 2 (meaning 'barley') to ocurr at
the boundaries between wheat fields and corn fields. when you rebin
your data. Nearest neighbor interpolation will always generate either
1 or 3 along that boundary.

A third case that I'm very familiar with is mainly of use for
debugging purposes. I'm responsible for programs which calibrate and
geolocate satellite images. Occasionally I want to create a plot with
a resolution much higher than the resolution of our images, using
nearest negghbor interpolation. For any given low-resolution pixel,
there's multiple high-resolution pixels for which it is the nearest
neighbor, and they all get assigned the same color. As a result, I can
very clearly where the boundaries are between the low-resolution
pixels. That helps me decide whether or not we've geolocated those
pixels correctly.
Re: 3D congrid without interpolation [message #53409 is a reply to message #53405] Fri, 13 April 2007 18:00 Go to previous messageGo to next message
JD Smith is currently offline  JD Smith
Messages: 850
Registered: December 1999
Senior Member
On Fri, 13 Apr 2007 17:33:53 -0700, David Fanning wrote:

> JD Smith writes:
>
>> How is "nearest neighbor sampling" not interpolation? Does it
>> explicitly avoid knowledge of how the new array cell is positioned
>> w.r.t. the old one, and simply grab averages of nearby neighbors? Why
>> would this ever be preferable to a linear interpolation?
>
> I don't know. I guess it is preferable because it
> doesn't add new numbers to your data. (I never really
> thought about or cared how it was done, but I suppose
> someone ought to.)

Aha, well I guess it really does just pick the nearest neighboring cell,
so it is not interpolation (I presumed it was averaging over neighbors
without weighting).

IDL> a=findgen(5,5)
IDL> print,a
0.00000 1.00000 2.00000 3.00000 4.00000
5.00000 6.00000 7.00000 8.00000 9.00000
10.0000 11.0000 12.0000 13.0000 14.0000
15.0000 16.0000 17.0000 18.0000 19.0000
20.0000 21.0000 22.0000 23.0000 24.0000
IDL> print,congrid(a,4,4)
0.00000 1.00000 2.00000 3.00000
5.00000 6.00000 7.00000 8.00000
10.0000 11.0000 12.0000 13.0000
15.0000 16.0000 17.0000 18.0000
IDL> print,congrid(a,4,4,/INTERP)
0.00000 1.25000 2.50000 3.75000
6.25000 7.50000 8.75000 10.0000
12.5000 13.7500 15.0000 16.2500
18.7500 20.0000 21.2500 22.5000

JD
Re: 3D congrid without interpolation [message #53410 is a reply to message #53409] Fri, 13 April 2007 17:33 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
JD Smith writes:

> How is "nearest neighbor sampling" not interpolation? Does it
> explicitly avoid knowledge of how the new array cell is positioned
> w.r.t. the old one, and simply grab averages of nearby neighbors? Why
> would this ever be preferable to a linear interpolation?

I don't know. I guess it is preferable because it
doesn't add new numbers to your data. (I never really
thought about or cared how it was done, but I suppose
someone ought to.)

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: 3D congrid without interpolation [message #53411 is a reply to message #53410] Fri, 13 April 2007 15:57 Go to previous messageGo to next message
JD Smith is currently offline  JD Smith
Messages: 850
Registered: December 1999
Senior Member
On Thu, 12 Apr 2007 15:40:07 -0700, mgalloy@gmail.com wrote:

> On Apr 12, 3:19 pm, David Fanning <n...@dfanning.com> wrote:
>> Humm. Hard for me to imagine what you are using to
>> do this that is interpolating anything for you.
>> CONGRID is normally used, but that won't interpolate
>> unless you explicitly tell it to.
>
> CONGRID interpolates 3-dimensional arrays by default. From the online
> help for the INTERP keyword for CONGRID:
>
> INTERP
> Set this keyword to force CONGRID to use linear interpolation when
> resizing a 1- or 2-dimensional array. CONGRID automatically uses
> linear interpolation if the input array is 3-dimensional. When the
> input array is 1- or 2-dimensional, the default is to employ nearest-
> neighbor sampling.

How is "nearest neighbor sampling" not interpolation? Does it
explicitly avoid knowledge of how the new array cell is positioned
w.r.t. the old one, and simply grab averages of nearby neighbors? Why
would this ever be preferable to a linear interpolation?

BTW, there has be a good deal of progress on interpolators, especially
for image data, which IDL hasn't taken advantage of. For example,
when downsizing, you need to take care to avoid moire artifacts and
aliasing.

Here's a classic comparison of various interpolators:

http://www.all-in-one.ee/~dersch/interpolator/interpolator.h tml

JD
Re: 3D congrid without interpolation [message #53425 is a reply to message #53411] Fri, 13 April 2007 08:21 Go to previous messageGo to next message
Mike[2] is currently offline  Mike[2]
Messages: 99
Registered: December 2005
Member
On Apr 12, 6:40 pm, "mgal...@gmail.com" <mgal...@gmail.com> wrote:
> CONGRID interpolates 3-dimensional arrays by default.

If you are really looking for a method that requires no interpolation,
you will have to choose your new array to overlap a subset of the
array indexes of the original data. I suspect you might really be
thinking about nearest neighbor interpolation. If so, you can do that
by

1 - calculating the [x,y,z] coordinates at which I want to evaluate my
data.

2 - round the coordinates

3 - interpolate a new array with interpolate(data,x,y,z)

This can be a memory hog for large arrays since you need 4 arrays for
each point in the new array. In the case where this leads to lots of
swapping, I usually do it slice by slice.

Mike
Re: 3D congrid without interpolation [message #53436 is a reply to message #53425] Thu, 12 April 2007 19:45 Go to previous messageGo to next message
Qing is currently offline  Qing
Messages: 12
Registered: February 2007
Junior Member
On Apr 13, 10:30 am, David Fanning <n...@dfanning.com> wrote:
> mgal...@gmail.com writes:
>> CONGRID automatically uses
>> linear interpolation if the input array is 3-dimensional.
>
> Aaaugghh! I *always* forget that! :-(
>

What about to do it in two steps:
(1) loop through the 3rd dimension while using CONGRID in 2D mode;
(2) if you need to resize the 3rd dimension, then loop through 2nd
dimension with 2D CONGRID again.
Qing :-)
Re: 3D congrid without interpolation [message #53438 is a reply to message #53436] Thu, 12 April 2007 17:30 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
mgalloy@gmail.com writes:

> CONGRID automatically uses
> linear interpolation if the input array is 3-dimensional.

Aaaugghh! I *always* forget that! :-(

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: 3D congrid without interpolation [message #53439 is a reply to message #53438] Thu, 12 April 2007 15:40 Go to previous messageGo to next message
Michael Galloy is currently offline  Michael Galloy
Messages: 1114
Registered: April 2006
Senior Member
On Apr 12, 3:19 pm, David Fanning <n...@dfanning.com> wrote:
> Humm. Hard for me to imagine what you are using to
> do this that is interpolating anything for you.
> CONGRID is normally used, but that won't interpolate
> unless you explicitly tell it to.

CONGRID interpolates 3-dimensional arrays by default. From the online
help for the INTERP keyword for CONGRID:

INTERP
Set this keyword to force CONGRID to use linear interpolation when
resizing a 1- or 2-dimensional array. CONGRID automatically uses
linear interpolation if the input array is 3-dimensional. When the
input array is 1- or 2-dimensional, the default is to employ nearest-
neighbor sampling.

Mike
--
www.michaelgalloy.com
Re: 3D congrid without interpolation [message #53447 is a reply to message #53439] Thu, 12 April 2007 14:19 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
mxhamidi@gmail.com writes:

> Is it possible to resize a 3D array in idl without being forced to
> interpolate? I am using discrete values to represent different
> information and interpolation alters those values. I can't use rebin
> since I'd like to change the image size by a non-integral factor.

Humm. Hard for me to imagine what you are using to
do this that is interpolating anything for you.
CONGRID is normally used, but that won't interpolate
unless you explicitly tell it to.

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: 3D congrid without interpolation [message #53525 is a reply to message #53405] Mon, 16 April 2007 11:43 Go to previous messageGo to next message
mxhamidi is currently offline  mxhamidi
Messages: 3
Registered: April 2007
Junior Member
On Apr 14, 12:59 pm, kuy...@wizard.net wrote:

> However, another good reason is if you're re-binning categorical
> data, where the codes representing each category are arbitrary, and
> it's simply not meaningful to take the average of the category codes.
> If category 1 means 'corn' and category 3 means 'wheat', you don't
> want a thin barrier line of category 2 (meaning 'barley') to ocurr at
> the boundaries between wheat fields and corn fields. when you rebin
> your data. Nearest neighbor interpolation will always generate either
> 1 or 3 along that boundary.

This is very similar to my concern. I have maps of brain activity
with each value representing seeing a different angle in visual
space. I need resize my brain activity map (64 x 64 x 30) to fit onto
the anatomical image of the brain (256 x 256 x 256). With congrid (at
least with 3D congrid) the label of each coordinate is altered making
the resulting image uninterpretable. I think that I see if Qing's
idea of doing two steps of 2-D congrid will resize the activity map
without any averaging of the values.

Thanks for your replies.
Re: 3D congrid without interpolation [message #53728 is a reply to message #53405] Mon, 23 April 2007 09:53 Go to previous message
JD Smith is currently offline  JD Smith
Messages: 850
Registered: December 1999
Senior Member
On Sat, 14 Apr 2007 10:59:26 -0700, kuyper wrote:

> JD Smith wrote:
>> On Thu, 12 Apr 2007 15:40:07 -0700, mgalloy@gmail.com wrote:
>>
>>> On Apr 12, 3:19 pm, David Fanning <n...@dfanning.com> wrote:
>>>> Humm. Hard for me to imagine what you are using to
>>>> do this that is interpolating anything for you.
>>>> CONGRID is normally used, but that won't interpolate
>>>> unless you explicitly tell it to.
>>>
>>> CONGRID interpolates 3-dimensional arrays by default. From the online
>>> help for the INTERP keyword for CONGRID:
>>>
>>> INTERP
>>> Set this keyword to force CONGRID to use linear interpolation when
>>> resizing a 1- or 2-dimensional array. CONGRID automatically uses
>>> linear interpolation if the input array is 3-dimensional. When the
>>> input array is 1- or 2-dimensional, the default is to employ nearest-
>>> neighbor sampling.
>>
>> How is "nearest neighbor sampling" not interpolation? Does it
>> explicitly avoid knowledge of how the new array cell is positioned
>> w.r.t. the old one, and simply grab averages of nearby neighbors? ...
>
> No. It grabs the value of the one nearest neighbor, with appropriate
> rules for breaking ties. No averaging of any kind is done on that
> value, which is why it's inappropriate to call this 'interpolation'.

Right, David corrected me. I guess my brain was registering "nearest
neighbor interpolation" with some weighted averaging.

>> ... Why
>> would this ever be preferable to a linear interpolation?
>
> Well, for one thing, it's a lot faster.
>
> However, another good reason is if you're re-binning categorical
> data, where the codes representing each category are arbitrary, and
> it's simply not meaningful to take the average of the category codes.
> If category 1 means 'corn' and category 3 means 'wheat', you don't
> want a thin barrier line of category 2 (meaning 'barley') to ocurr at
> the boundaries between wheat fields and corn fields. when you rebin
> your data. Nearest neighbor interpolation will always generate either
> 1 or 3 along that boundary.

Good example.

Thanks,

JD
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Pasting subarray into array with compound assignment
Next Topic: Re: newbie's question for writing files

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

Current Time: Wed Oct 08 13:33:22 PDT 2025

Total time taken to generate the page: 0.00594 seconds