Not all images are the same. Frequently, to do science you might want to compare two different images at the same resolution, or map them to the same grid, different from the original. To map to lower resolution you should co-add pixels, but to map at a higher resolution you might need to interpolate. This article discusses four interpolations schemes, how to use them, and which might be best for you.
If you just have one image to resize, sure, you're better off using ImageMagick, Gimp, or PhotoShop to just read it in and resize. However, if you need to embed your interpolation in code, you'll need to decide how to implement it.
Interpolations: Example of 4 different interpolation styles as applied to mapping from a Cassini/VIMS Titan cube.
The easiest option is nearest neighbor (NN) -- instead of actually interpolating, just assign to each pixel on the target map the value of the original pixel located closest to it. This has the advantage of being easy to code up, and also in that it is honest and faithful in regards to the limitations of the original image. Too bad it looks like crap.
The simplest actual interpolation scheme is linear. Take the geometric average of the 4 original points surrounding the point to be interpolated, weighting the closest points proportionately. This scheme necessarily does not conserve flux -- a single bright original pixel within darker surroundings will show up as a peak of the brightness of the original bright pixel, but not have the same total flux. However, it does provide a good, predictable interpolation.
A more complicated algorithm is to use a 4×4 matrix of original points surrounding the point being interpolated. Using a busy equation you fit a bi-cubic spline surface to the 4×4 grid, assigning the spline-smoothed value to the interpolated point. I thought that this technique, being the most complex, would be the best. However, looking at the results, I don't like it. It smoothes too much, washing out small details, which kind of defeats the whole purpose of interpolation.
The algorithm that command-line-driven ImageMagick uses by default to interpolate is called Lanczos. It assigns the interpolated value based on the contribution of the nearest points, weighted by sinc functions.
float coeff( sin(pi*dist)/(pi*dist) * sin(3.*pi*dist)/(3.*pi*dist) );
For each original point within radius 1.0 of the point to be interpolated, multiply its coefficient times its value and add it to the result. At the end, normalize the result to the sum of the individual coefficient values. The advantage of that 3π stuff is that Lanczos interpolation does a better job at preserving sharp lines present in the original image. In the example above linear does a remarkably good job, but I think Lanczos is slightly better due to its ability to preserve sharpness better.
Comments
Re: Image interpolation
Using sinc functions not only looks better, it is more theoretically correct. As I said here, I need to implement something like this.
More comments
By the way, what software do you use to process VIMS data? VICAR or ISIS or something of your own making? We are in the third category, both for ISS and VIMS data, but it is somewhat frustrating to feel you're reinventing the wheel.
wheel reinvention
*sigh*. Reinvention of the wheel seems to be my modus operandi, I'm afraid. The stuff I've written is all from relative scratch, in C++. The advantage to doing so is that not uncommonly your reinvented wheel can do things that the prepackaged wheels can't. And it's more extensible. Hopefully :)
Resist the temptation
I feel the need to chime in here on the topic of wheel reinvention (not really contributing to answering your question, sorry Matt). The significant downside of personal wheel reinvention is that all of the extensibility has to be done by you. The advantage of spending a little time hunting around for someone else's wheel or even better a community-supported, actively-developed, free and open source wheel is that not only can you modify the code to your needs, you can also benefit from the additions of others over the life of the project. If your modifications are significant, contribute them back to the project, you never know who else might use them.
I have said this again and again, Jason has some good software, but he's the only one that works on it and uses it. My hope is that when ISIS 3.0 gets stable (rewritten from the ground up using C++), Jason will be able to modify his code just a little so that it works under ISIS 3.0. Then other people can benefit from his expertise and he might find that he doesn't have to reinvent so many wheels so often. Everybody wins.
Re: Resist the temptation
The thing about community wheels is that someone needs to be diligent about maintaining a central version of the code, for a wider purpose than his own needs. Not to mention helping newbies debug when they try to apply the code to new tasks. Volunteering to be that person is not to be taken lightly.
On the other hand, I do see the sense in what you're saying, Ross, and I'm interested to know if your experience with this kind of thing differs from my uninformed impressions.
Not to be taken lightly at all
Absolutely, I'm not saying that one should embark onto being an open-source project leader unless they are quite committed. Using ISIS as an example, there are several full-time programmers working on ISIS funded through a primary PG&G grant and supplemented by various mission funding sources. ISIS probably could not exist on a purely volunteer basis.
I don't quite know how to respond to your second paragraph because I don't know exactly what you are asking.
Re: Not to be taken lightly at all
I just meant that I have no experience with an open-source project, and I'm interested to know more. I suppose that a larger and more institutionalized project like ISIS is likely to give a more consistent and manageable workload, compared to just putting your own code out for others to share.
Providing free and open source software
Maybe ISIS is a bad example if you are thinking about some of your own personal software that you want to release or share. Big projects like ISIS and GMT, larger general use projects like the GIMP or Apache, or even whole operating systems (like the various GNU/Linux and BSDs) generally have the benefit of strong central leadership, large numbers of committed people, and usually some kind of monetary inflow via donations or grants that keeps a lead architect or group of core developers employed to work on the code.
Certainly the issues are different when things are smaller and more personal, shall we say. There is the one extreme of just making a web page, posting your code in all of its hacky glory on it, and letting people take it or leave it. Odds are good they will do the latter (if they even find out about it at all) unless you put a little effort into making it usable and documented. I have tried to do this with a number of programs, large and small, with varying amounts of success. Even for the programs that I have no knowledge of anyone having ever used, at least I documented the program and it made it easier for me to return to it and upgrade it down the road. In the best case, I've gotten e-mail from people pointing out how they couldn't get something to work or that the code was broken in some way, and as a result the program is now better both for me and those (2 or 3 other people) that use it.
I have found that the benefits of going through the effort to publish a nice, documented version of a piece of software outweigh the burdens of doing so. As I said, in the worst case, no one uses it, but at least you've got something nicely documented and workable for yourself if there is a hiatus in using the software (now what did this do again?), or if you need to change computers or setups (what libraries do I need for this?), or even if you manage to get a postdoc or student to work with it for you. In the best case, others might use it, trouble shoot it with you, and even contribute improvements. I'm sure that the internet is full of other articles and essays about the benefits of using and contributing free and open source software.
Open Source Projects
Okay, here's some more background. The basis for my image processing code came from a colleague about two years ago, but I've been basically on my own ever since. It probably differs from ISIS mainly in being IDL-based, and focusing on a cylindrical (rings) coordinate system rather than a spherical one. There might be some interest in re-marrying our versions and making the code more widely available, but I'm a little concerned about the potential workload.
Back to my original question about wheel reinvention, I'm starting a new thread on whether ISIS might be something for me to consider.
Rolling your own
Okay, I'll try and answer your ISIS-related questions in the thread that you started. So yes, there is a large potential workload, and the drawback is that image processing of rings data is a pretty specialized niche, so you might not get much help. If there are several folks, each with their own different personal software, everyone might benefit from having a more central codebase that everyone shares, contributes to, and benefits from. On the other hand, you might get resistance from people that have an NIH bias (not invented here) or think that their own personally-developed code has some kind of magical abilities, and sharing it with anyone else would ruin their competitive advantage. However, even if you just start small and have a few people sharing code that is publicly available, those curmudgeons will have the same access as everyone else, and may eventually come around.
If you are thinking about spearheading such an effort, you have a lot of work ahead of you on a number of fronts personal, professional, technical, and financial.
Comment Alert
By the way, is it possible for me to receive an email alert when a comment is added, rather than just when a new thread is created?
subscriptions
You can subscribe to this thread; there's a link to do so at the bottom of the main text, I think.
Subscriptions
Yes. You need to be logged in to Orrery, and then for each story there is a little cluster of links after the article text. One of those links indicates how many comments there are, a link to read the whole article (if you're looking from the main page), and a link whose title is "subscribe post". Following that will cause you to subscribe to that particular item, and you should get an e-mail whenever someone posts. There is some additional information on the subscription feature announcement.
Bug Report?
Okay, I posted comments both to this thread and here last night, and then they did not appear. Thinking that it may have been a problem with my home computer (dynamic IP?), I just re-entered the comments from the office. But instead, the comments I entered last night have appeared, with timestamps from last night, and the comments I just now entered are not apparent. We'll see if this one appears.
Anonymous comments get held
Since you are not logged in, these comments are being posted by the default Anonymous user. In order to keep the site clean from malicious posting, but still allow people to contribute, we allow anonymous comments, but they are held in a queue. If you are logged into the system, your comments post immediately. Our editors check the queues, and approve the posts that are legitimate, and delete the ones that are not.
Just after I had posted your comments from last night, I saw the duplicates arrive and deleted them. Sorry for any confusion.
Re: Anonymous comments get held
Oops. Hadn't noticed that I wasn't logged in. Well, I guess this exchange can stay up as a warning to future boneheads.