f



Unexpected rebin behavior

Performing rebin on 2-d (or any higher dimension) integer arrays sometimes
give results I didn't expect.  Here's a trivial example array:

IDL> help, b
B               INT       = Array[2, 2]

IDL> print, b
       8       9
      10      13

Minifying this to a 1x1 array with rebin gives:

IDL> print, rebin(b,1,1)
       9

But,

IDL> print, fix(total(b)/4)
      10

which is what I would have expected as the result from rebin.  I think
what rebin is doing is first performing a rebin in one direction and then
performing it in the other direction on the new, intermediate rebinned
array.  So the above is equivalent to:

IDL> print, rebin(rebin(b,1,2),1,1)
       9

Switching the order of the rebinning yields a different result:

IDL> print, rebin(rebin(b,2,1),1,1)
      10

Or equivalently:

IDL> print, rebin(transpose(b),1,1)
      10


I was using rebin to reduce the size of an image and expected it to do
neighborhood averaging for the new array.  The integer truncation after
rebinning along each direction can lead to different results than
averaging the neighborhood in a single step.  I guess this isn't really a
bug (the manual only describes how rebin works in one dimension), but was
unexpected, at least for me.  I simple work-around is to cast the array to
float and then back to integer (or just keep as float) after the rebin:

IDL> print, fix(rebin(float(b),1,1))
      10


--Mike Chinander

-- 
Michael Chinander
m-chinander@uchicago.edu
Department of Radiology
University of Chicago
0
mchinand (39)
9/3/2003 4:20:10 PM
comp.lang.idl-pvwave 12260 articles. 4 followers. Post Follow

2 Replies
611 Views

Similar Articles

[PageSpeed] 50

Mike Chinander wrote:
> 
> Performing rebin on 2-d (or any higher dimension) integer arrays sometimes
> give results I didn't expect.  Here's a trivial example array:
> 
> IDL> help, b
> B               INT       = Array[2, 2]
> 
> IDL> print, b
>        8       9
>       10      13
> 
> Minifying this to a 1x1 array with rebin gives:
> 
> IDL> print, rebin(b,1,1)
>        9
> 
> But,
> 
> IDL> print, fix(total(b)/4)
>       10

What it's doing is total(b/4), rather than total(b)/4. This has the
advantage of handling large numbers without producing overflow. It has
the disadvantage of underflowing for very small numbers, and producing
wierd results when re-binning integer data.
0
kuyper (156)
9/3/2003 4:31:30 PM
In article <3F561762.B0B31F0B@saicmodis.com>,
James Kuyper  <kuyper@saicmodis.com> wrote:
>Mike Chinander wrote:
>> 
>> Performing rebin on 2-d (or any higher dimension) integer arrays sometimes
>> give results I didn't expect.  Here's a trivial example array:
>> 
>> IDL> help, b
>> B               INT       = Array[2, 2]
>> 
>> IDL> print, b
>>        8       9
>>       10      13
>> 
>> Minifying this to a 1x1 array with rebin gives:
>> 
>> IDL> print, rebin(b,1,1)
>>        9
>> 
>> But,
>> 
>> IDL> print, fix(total(b)/4)
>>       10
>
>What it's doing is total(b/4), rather than total(b)/4. This has the
>advantage of handling large numbers without producing overflow. It has
>the disadvantage of underflowing for very small numbers, and producing
>wierd results when re-binning integer data.

Then why does rebin of the transpose give a different result?

IDL> print, rebin(transpose(b),1,1)
      10

If it were just doing total(b/4), b and tranpose(b) should give the same
results.

--Mike Chinander

-- 
Michael Chinander
m-chinander@uchicago.edu
Department of Radiology
University of Chicago
0
mchinand (39)
9/3/2003 5:02:31 PM
Reply: