Fanning Software Consulting

Correct Resizing of Images

QUESTION: I'm an astronomer and I have a question about the way you resize images with TVImage. It doesn't seem to be quite correct, especially when I am working with images that don't have a lot of pixels. Congrid seems to work for 2D images if I set the CENTER keyword, but both Congrid and TVImage produce incorrect color images when I work with small true-color images. It seems that the "color" dimension of a true-color image is interpolated when it shouldn't be. And Congrid always does a bilinear interpolation for true-color images. I don't want any pixels in my image to have values that weren't in the original image. Can you help me sort this out?

ANSWER: TVImage was first written--I can hardly believe this!--nearly 15 years ago. In those 15 years, I think I can count on one hand the number of times I've used a TV command in an IDL program.

In the past week or so, I have been updating this program to work more seamlessly with a new series of Coyote Library programs that are meant to bring New Graphics like functionality to all versions of IDL (FSC_Contour, FSC_Plot, etc.).

When I wrote the program there were problems with Congrid not interpolating from the center of a pixel, but from its lower-left corner. This was (and is) a problem for astronomers because if the pixel wasn't centered, the pixel "location" would change slightly. This is almost unnoticeable with large images, but can be a factor when you don't have very many pixels to work with.

This deficiency has since been corrected by a CENTER keyword to the Congrid command. At the time I wrote TVImage, I resized the image with the program CM_Congrid, written by Craig Markwardt. It used a HALF_HALF keyword to perform the pixel centering.

I thought about just switching to Congrid for image resizing, but Congrid has another problem when resizing images. Congrid is not able to recognize the difference between a 3D array, and a true-color image, in which one of the dimensions is a 3. As a result, this "3" dimension is interpolated in the same manner as the other two dimensions. It should not be. If the dimension is interpolated, small differences in pixel color can occur. Again, this is something that is not particularly noticeable, unless you are an astronomer working with relatively few pixels, where color can be extremely important.

Congrid has one further weakness. It always interpolates 3D images with bilinear interpolation. Astronomers, I have discovered, hate interpolated pixels! The old CM_Congrid program was able to use nearest neighbor resampling even for 3D images.

All this background to say that I have written a new program, FSC_Resize_Image, that is ONLY for 2D and true-color images, and that combines the best of Congrid and CM_Congrid to provide a consistent (and I think, correct) interpolation or resizing of images.

2D images are passed directly to Congrid , but with the CENTER keyword set to 1, and with a default MINUS_ONE keyword set to 1, too. (Setting the MINUS_ONE keyword insures that Congrid doesn't extrapolate an extra column or row beyond the bounds of the input image.)

3D or true-color images are handled in a manner that is consistent with the CENTER keyword being set to 1, except that the "3" dimension of the image is not interpolated to avoid color shifts in the image, and nearest neighbor resampling is available. The INTERPOLATE keyword determines the interpolation method, as it always has. (This keyword is ignored for 3D images in Congrid.)

I have updated TVImage and TVScale to take advantage of this new method for image resizing. If you haven't updated these two commands in awhile, you might want to do so. There have been a LOT of new features added recently.

Version of IDL used to prepare this article: IDL 7.1.2.

Web Coyote's Guide to IDL Programming