Linear Methods for Image Interpolation
|
Pascal Getreuer
This software was written by Pascal Getreuer and is distributed under the terms of the simplified BSD licence.
This package implements linear interpolation methods as described in the accompanying IPOL demo “Linear Methods for Image Interpolation.” The methods are implemented in C as a command line program linterp.
Also included are sources for building imcoarsen and imdiff, two useful command line tools for coarsening and comparing images.
If Doxygen and Graphviz are installed, HTML documentation of the project source code is generated by
doxygen doxygen.conf
The compilation is configurable. At a minimum, the FFTW library is needed to compile. Support for reading and writing with additional image formats can be added by compiling with the libaries as summarized in the table:
Format | Library | Add preprocessor flag |
---|---|---|
JPEG | libjpeg | LIBJPEG_SUPPORT |
PNG | libpng | LIBPNG_SUPPORT |
TIFF | libtiff | LIBTIFF_SUPPORT |
BMP | (native) | — |
On Ubuntu and other Debian-based Linux systems, the libraries can be installed by running the following line in a terminal:
sudo apt-get install build-essential libfftw3-dev libjpeg8-dev libpng3-dev libtiff4-dev
On Fedora:
sudo yum install gcc fftw-devel libjpeg-devel libpng-devel libtiff-devel
On Mac OSX, the libraries can be installed with Fink:
sudo fink install fftw libjpeg libpng libtiff
To compile, extract the package, cd into the linterp folder, and run make:
tar -xf linterp.tar.gz cd linterp make -f makefile.gcc
This should produce three executables linterp, imcoarsen, and imdiff.
The included makefile will try to use libjpeg, libpng, and libtiff. If linking with these libraries is a problem, they can be disabled by commenting their line at the top of the makefile.
##
# The following three statements determine the build configuration.
# For handling different image formats, the program can be linked with
# the libjpeg, libpng, and libtiff libraries. For each library, set
# the flags needed for linking. To disable use of a library, comment
# its statement. You can disable all three (BMP is always supported).
LDLIBJPEG=-ljpeg
LDLIBPNG=-lpng
LDLIBTIFF=-ltiff
The makefile will automatically set the corresponding preprocessor symbols; only these lines need to be changed. For example, to disable libjpeg and libtiff but to keep libpng support, comment the first and third lines
#LDLIBJPEG=-ljpeg LDLIBPNG=-lpng #LDLIBTIFF=-ltiff
The code can be compiled using Microsoft Visual C++ (MSVC). Microsoft Visual Studio Express can be downloaded for free. Since libraries are problematic under Windows, no libraries are used by default and the program will only support BMP images.
These instructions assume familiarity with the MS-DOS Command Prompt. See for example How to use DOS.
The FFTW single precision library is required to compile. To obtain FFTW, download the precompiled Windows DLL files from
http://www.fftw.org/install/windows.html
In order for MSVC to link with FFTW, we must create the LIB import library. Open a Visual Studio Command Prompt by selecting Start Menu → Programs → Microsoft Visual Studio → Visual Studio Tools → Visual Studio Command Prompt. (Alternatively, open a regular command prompt and run vcvarsall.bat.) Then, navigate to the FFTW DLL files and run the command
lib /def:libfftw3f-3.def
This should produce the LIB file libfftw3f-3.lib.
# Please specify the locations of fftw3.h and the FFTW libs FFTW_DIR = "D:/libs/fftw-3.2.2.pl1-dll32" FFTW_INCLUDE = -I(FFTW_DIR) FFTW_LIB = $(FFTW_DIR)/libfftw3-3.lib $(FFTW_DIR)/libfftw3f-3.lib
nmake -f makefile.vcThis should produce three executables linterp.exe, imcoarsen.exe, and imdiff.exe.
It is possible under Windows to compile the program with libjpeg and libpng to add support for JPEG and PNG images (libtiff should be possible as well, but it is not explored here). To avoid incompatibility problems, the reliable way to compile with a library is to build that library from source using the same compiler.
First, download the libjpeg, libpng, and also the zlib library sources. The zlib library is needed to compile libpng.
Create a folder to contain the libraries, C:\libs for instance. Unzip the library sources into the libs folder so that they are structured as
libs | ||
jpeg-8b | ||
lpng143 | ||
zlib | ||
This structure will help keep the code organized. Take care to rename the folder for zlib to “zlib” since libpng will look for it. Below are the steps to build each library. If you want JPEG support, build libjpeg. For PNG support, build zlib first and then build libpng.
Building libjpeg
nmake -f makefile.vc libjpeg.libThis should produce libjpeg.lib.
Building zlib
287 #if 0 /* HAVE_UNISTD_H -- this line is updated by ./configure */ 288 # include <sys/types.h> /* for off_t */ 289 # include <unistd.h> /* for SEEK_* and off_t */
vcbuild -upgrade zlib.dsp vcbuild zlib.vcproj "LIB Release|Win32"This should produce a folder “Win32_LIB_Release” containing zlib.lib.
Building libpng
CFLAGS = -nologo -DPNG_NO_MMX_CODE -MT -O2 -W3 -I..\zlib
nmake -f scripts\makefile.vcwin32This should produce libpng.lib.
Once the libraries are built, linterp can be compiled with JPEG and/or PNG support by adjusting its makefile. Uncomment and edit the lines at the top of linterp\makefile.vc to reflect the locations of libjpeg, libpng, and zlib:
# # Uncomment and edit the following lines for JPEG support. # LIBJPEG_DIR = "C:/libs/jpeg-8b" LIBJPEG_INCLUDE = -I$(LIBJPEG_DIR) LIBJPEG_LIB = $(LIBJPEG_DIR)/libjpeg.lib # # Uncomment and edit the following lines for PNG support. # ZLIB_DIR = "C:/libs/zlib" ZLIB_INCLUDE = -I$(ZLIB_DIR) ZLIB_LIB = $(ZLIB_DIR)/zlib.lib LIBPNG_DIR = "C:/libs/lpng143" LIBPNG_INCLUDE = -I$(LIBPNG_DIR) LIBPNG_LIB = $(LIBPNG_DIR)/libpng.lib
The makefile will automatically add the corresponding preprocessor symbols based on which libraries are defined. Then from a Visual Studio Command Prompt, compile with
nmake -f makefile.vc
This should produce linterp.exe, imcoarsen.exe, and imdiff.exe.
A script called “demo” is included to run an example interpolation with the program. There is also an equivalent BAT program for MS-DOS.
demo | sh script | |
demo.bat | MS-DOS batch script |
Image credits: the demo test image is by John D. Willson, USGS Amphibian Research and Monitoring Initiative.
The usage syntax for linterp is
linterp [options] input-file output-file
where input-file is the file name of the image to be interpolated, and output-file is the file name to use for saving the interpolated image.
Sorry, only BMP/JPEG/PNG/TIFF images are supported.
The program only supports BMP, JPEG, PNG, and TIFF images. If you disabled some of the libraries when compiling, the support for the corresponding formats will be disabled. Regardless of compilation settings, the program always supports Windows Bitmap BMP images.
To use an image that is in an unsupported format, please convert it to a supported format. Images can be conveniently converted using the command line program convert from ImageMagick. Alternatively, an image can be converted by opening the image in an image editor, selecting “Save As...,” and setting “Type” to a supported format.
The program has several option arguments
-m method | interpolation method to apply, choices for method are | |
nearest | nearest neighbor (pixel duplication) | |
bilinear | standard bilinear interpolation | |
bicubic | Keys bicubic with parameter −0.5 | |
lanczosN | Lanczos radius-N sinc approximation, N = 2, 3, 4 | |
bsplineN | B-spline of degree N, N = 2, 3, 5, 7, 9, 11 | |
omomsN | o-Moms of degree N, N = 3, 5, 7 | |
fourier | Fourier zero-padding (sinc) | |
-x scale | the scale factor (may be non-integer) | |
-x x-scale,y-scale | set horizontal and vertical scale factors | |
-x widthxheight | set maximum interpolated size in pixels, preserves aspect ratio | |
-x widthxheight^ | set minimum interpolated size in pixels, preserves aspect ratio | |
-x widthxheight! | set actual interpolated size in pixels, ignores aspect ratio | |
-r number | rotation, counter clockwise in degrees (if specified, preserves aspect ratio regardless of -x) | |
-p number | σh, the blur size of the point spread function | |
-b ext | extension to use for boundary handling, choices for ext are | |
const | constant extension | |
hsym | half-sample symmetric | |
wsym | whole-sample symmetric | |
-g grid | grid to use for resampling, choices for grid are | |
centered | grid with centered alignment (default) | |
topleft | the top-left anchored grid | |
-q number | quality for saving JPEG images (0 to 100), this option has no effect on other image formats and is only present if compiled with libjpeg |
For example, to run the program on “frog.bmp” with factor-4 scaling, σh = 0.35, using quintic B-spline interpolation, run
linterp -x 4 -p 0.35 -m bspline5 frog.bmp frog-4x.bmp
The scale factor may be non-integer. The size of the output image is determined by multiplying the input image size with the scale factor and rounding up.
This package also includes two tools, imcoarsen and imdiff. The imcoarsen tool coarsens an input image by convolving with a Gaussian followed by downsampling. The imdiff tool compares two images with various image metrics. These tools are useful for interpolation experiments: a high-resolution image is given to imcoarsen to create a coarse image, the coarse image is interpolated by linterp, and the interpolation is compared to the original using imdiff.
The usage syntax of imcoarsen is
imcoarsen [options] input-file output-file
Options:
-x number | the coarsening factor (≥1.0, may be non-integer) | |||
-x x-scale,y-scale | set horizontal and vertical coarsening factors | |||
-x widthxheight | set maximum coarsened size in pixels, preserves aspect ratio | |||
-x widthxheight^ | set minimum coarsened size in pixels, preserves aspect ratio | |||
-x widthxheight! | set actual coarsened size in pixels, ignores aspect ratio | |||
-p number | σh, the blur size of the point spread function | |||
-b ext | extension to use for boundary handling, choices for ext are | |||
const | constant extension | |||
hsym | half-sample symmetric | |||
wsym | whole-sample symmetric | |||
-g grid | grid to use for resampling, choices for grid are | |||
centered | grid with centered alignment (default) | |||
topleft | the top-left anchored grid | |||
-q number | quality for saving JPEG images (0 to 100) |
Note that the -x, -p, -b, and -g options are analogous to the same options in linterp. The coarsening factor given with option -x may be non-integer. The size of the output image is determined by dividing the input image size by the coarsening factor and rounding up.
The usage syntax of imdiff is
imdiff [options] exact-file distorted-file
Options:
-m metric | metric to use for comparison, choices are | ||
max | maximum absolute difference, maxn |An − Bn| | ||
mse | mean squared error, 1/N sum |An − Bn|2 | ||
rmse | root mean squared error, (MSE)½ | ||
psnr | peak signal-to-noise ratio, −10 log10(MSE/2552) | ||
mssim | mean structural similarity index | ||
-s | compute metric separately for each channel | ||
-p pad | remove a margin of pad pixels before comparison | ||
-D number | D parameter for difference image (explained below) | ||
-q number | quality for saving JPEG images (0 to 100) |
Alternatively, a difference image is generated by the syntax
imdiff [-D number] exact-file distorted-file output-file
The difference image is computed as Dn = 255/D (An − Bn) + 255/2. Values outside of the range [0,255] are saturated.
The usage information for these programs is also displayed when executing them without arguments.
Copyright © 2010–2011, Pascal Getreuer
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This material is based upon work supported by the National Science Foundation under Award No. DMS-1004694. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.