Author Topic: Alarming: Resample - IntegerResample Errors  (Read 3869 times)

Offline darkownt

  • PixInsight Enthusiast
  • **
  • Posts: 92
Alarming: Resample - IntegerResample Errors
« on: 2012 April 04 06:25:39 »
Hi:

I have a really strange problem with resampling which seems a bit alarming.

I have an image with a central peak pixel I want to use to generate other small images.  The original, by definition and of necessity, has odd dimensions (1001x1001).  The rest of the image is perfectly symmetric about the central peak as well.  I don't know if I am making sense here, but to my mind, it would stand to reason, that an image with a central peak which is perfectly symmetric should produce a symmetric centrally peaked smaller image, regardless of its size,as long as it has odd dimensions.  As a matter of fact I should assume as long as I am resizing amoungst odd dimension images, I should never lose symmetry about the center, nor lose the center itself.  Conversely, I would very well expect some funky results and loss of symmetry trying to convert from odd to even dimensions etc.

However, whenever I use the Resample or IntegerResample tools to shrink my 1001x1001 to a small enough image of odd dimensions, the peak is no longer present in the center pixel (I assume the pixel readout tool is working properly BTW), and the entire image is no longer symmetric about the center.   :o

Interestingly, I can get the result I want (dim*dim) if I resample up from 1001*1001 to dim*100, and then shrink with IntegerResample (downsample average) by 100x.  It's a two stepper with different tools... not ideal, and not intuitive.

I have attached an original 1001x1001 centrally peaked image with perfectly symmetric surrounding pixels.

I also have attached a result from a Resample (Auto algorithm) down to 35x35, and a result from IntegerResample (Average) down to 35x35.

I play a lot with odd-dimension images with central features, and a fix would be be... comforting? :)


cheers
Colin

Offline darkownt

  • PixInsight Enthusiast
  • **
  • Posts: 92
Re: Alarming: Resample - IntegerResample Errors
« Reply #1 on: 2012 April 05 16:09:47 »
Edit: Oh my.  I had too many coffees when I wrote the following.  Sorry about the insane sounding geek humor.  I was trying to subtly point out that all "fair" imaging algorithms should be just as good in the space of odd image matrices as they are in the space of even image matrices.

I am starting to worry that no one else agrees this is actually a problem...?

Anyone?

________________________
Soooooo... am I making any sense to anyone?    ???

Am I being reasonable? 8)

Everyone **knows** that the "natural" dimensions for any raster image should be "odd" both in height and width... it's just a natural preference of the integers right?? I mean yeah... "even" numbers are OK by virtue of being divisible by 2, but we can't give them *EQUAL* status to the odd numbers now can we?  It's not as if we WANT to make the "even" numbers feel like second class citizens but you know... they kind of ARE.  I mean the number 1 is so prime (even though its isn't a "prime number"), so important, so cool, and its rather ODD, and the "prime image" size, the first basis image, the pixel, the smallest IS  1x1  afterall...   ;)

=> ODD owns EVEN.

 :surprised:

_____________________________
« Last Edit: 2012 April 06 09:08:23 by darkownt »

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: Alarming: Resample - IntegerResample Errors
« Reply #2 on: 2012 April 07 17:00:02 »
Hi Colin,

These are the expected results. For example, consider the following image:

a0 a1 a2 a3 a4
b0 b1 b2 b3 b4
c0 c1 c2 c3 c4
d0 d1 d2 d3 d4
e0 e1 e2 e3 e4


It has 5 rows and 5 columns, which we have numbered from a to e and 0 to 4, respectively. The 2x2 binning operation (for example, IntegerResample with downsample ratio = 2 and mode = average) reduces it to a 2x2 image as follows:

(a0+a1+b0+b1)/4   (a2+a3+b2+b3)/4
(c0+c1+d0+d1)/4   (c2+c3+d2+d3)/4


If we apply the algorithm to this image

0 0 0 0 0
0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0


the result is:

0 0
0 0.25


As you see, the bright pixel is now at the bottom right corner, not at the center coordinates. The bright pixel remains at the center for image dimensions of 3x3, 7x7, 11x11, 15x15 ..., but not for 5x5, 9x9, 13x13 ...

With other interpolation algorithms the result also depends on the sizes of the image and the interpolation kernel or filter function.
« Last Edit: 2012 April 07 17:10:19 by Juan Conejero »
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline darkownt

  • PixInsight Enthusiast
  • **
  • Posts: 92
Re: Alarming: Resample - IntegerResample Errors
« Reply #3 on: 2012 April 08 10:27:37 »
Hi Juan:

Thanks for your explanation.  I think I am beginning to understand.


Two questions and a wishlist proposal:

Q1.  Am I correct in assuming IntegerResample truncates (discards) all of the pixel data of the bottom and right hand edge *before binning* to get the size into a multiple of the shrinking factor, ragardless of the content of that image data?

Q2.  Am I correct in assuming all of PI's existing resample algorithms (non integer) always do filtering in such a manner as to cause an off center for 5x5, 9x9, when going from other odd dimensions?


Wish list Proposal:

Would it be possible to implement a resample algorithm that behaved in a manner that treated the original image as a continuous area of tiles which are simply redividied into a different number of (possibly differently shaped rectangular) tiles.  The color of each new tile being a result of the proportion of area of each of the colors of the original tiles?  i.e. no holes and no edges or truncation or "filtering"?

Something like doing it in the following manner:

For decreasing an MxN image into an KxL image (any integers M>K, N>L)

1.  Treat an MxN image as a set of MxN continuous tiles in continuous space (each having a square area of S and width and height of 1)
2.  Overlay the entire area (MxN tiles) by a grid of KxL rectangles.  Each rectangle having a width of M/K and a height of N/L.
3.  Determine a color for each rectangle of the grid KxL by the proportion of area of each color present (from the MxN squares) within the rectangle.
4.  Generating a KxL image having pixels the same color of the rectangles

There is probably an elegant way to do this in 2D space, but the equivalent using existing PI integer resample is :  a) Integer resize (increase) the MxN image vertically by an integer factor of L and horizontally by an integer factor of K ->  the result is (M*K)x(N*L)  b) Integer resize (decrease) the (M*K)x(N*L) image horizontally by an integer factor of M and vertically by an integer factor of N.

This result should lose no data (none at egdes, no holes or rounding errors etc) and be identical to a continuous 2d space resubdivision as described above.

I can do this manually for now although it "feels" like I am compensating for rounding errors... and image sizes can get insane.

This kind of continuous resubdivision must have a name?  Is it a common algorithm out there?  Seems intuitive to me... so "linear"...


Still a little confused, but I am beginning to understand.

Any feedback would greatly be appreciated!


cheers
Colin



Offline darkownt

  • PixInsight Enthusiast
  • **
  • Posts: 92
Re: Alarming: Resample - IntegerResample Errors
« Reply #4 on: 2012 April 10 12:20:14 »
Hi Juan:

I've done more digging.  I have been able to find a program which does the area averaging I describe above.  Paint Shop Pro Photo X3 (which I refer to as PSP).

I have attached a LargeDemo JPG to illustrate what I mean.  An original 20x20 image is rescaled to 7x7.  The grids are superimposed on the large view to illustrate.  The original as well as the ideal rescaled result (from PSP) are also attached.

I have shown PixInsight results (which I refer to as PI) using bilinear, bicubic, and bicubic bspline.  As you can see bilinear and bicubic are actually missing image information.  I.e. some areas are COMPLETELY black where there should be some value.  Bicubic b-spline is showing results where the pixels should be black, and pixel value has migrated.  None of the resampling algorithms here has the center remaining in the center, except for the PSP processed image.  (PSP is by no stretch of the imagination a scientific or a mathematics processing software package...yet it seems to do this in at least "one way" I would imagine it should be done)

As you can see the PSP result looks very much like a straight forward re-division of the area and assignment of an "integration" of the color within each new pixel.  This is what I want!

I would appreciate your comments or questions about my demo image.  Perhaps you can show me clearly that I am wrong!

As an aside I am trying to figure out an analytical way of performing subdivision and calculating ratios of areas etc. in order to make a script for use in PI which does what I show above (blue and red grids) for ANY two images of ANY dimensions.

Thank you as always for your insightful and honest replies.

I sincerely adore the PixInsight software package.

Cheers
Colin