Algorithm allowing to resize a 2d image.
This algorithm allows to resize a 2d image using one of the available interpolation methods
The user can resize a 2d image either to the expected output image size or by specifying the scale factors along the x and y axis.
If the user provides the buffer of the output image, he has to initialize its dimensions with the correct values. If the user requests a resize operation by specifying the scale factors, for instance, the dimensions of the output image must equal to:
- xOutImgSize = integer part of (xInImgSize * xScaleFactor + .5)
- yOutImgSize = integer part of (yInImgSize * yScaleFactor + .5) The user can call the utility function ipsdk::imaproc::gtrans::getZoom2dOutputImageSize to automatically compute the dimensions of his output image from the dimensions of the input image and from the scale factors.
The enumerate ipsdk::attr::eZoomInterpolationMethod permits to specify the interpolation method to use to resize the image. The following interpolation methods are available:
- eZoomInterpolationMethod::eZIM_NearestNeighbour: the pixel value of the output image at position (xOut, yOut) equals to the value of the pixel nearest to the associated position in the input image. More precisely: ImgOut(xOut, yOut) = ImgIn(integer_part(xOut/xScaleFactor), integer_part(yOut/yScaleFactor)) This is the certainly one of the fastest methods, but it generates a coarse image, with aliasing.
- eZoomInterpolationMethod::eZIM_Linear: for each pixel of output image, pixel value is computed by doing a bilinear interpolation on the 4 nearest neighbours. It gives satisfying results, and it is a good compromise between speed and quality.
- eZoomInterpolationMethod::eZIM_Cubic: for each pixel of output image, pixel value is computed by doing a bicubic interpolation on the 16 nearest neighbours. This method is slower than the bilinear interpolation, but it usually gives better results, generating sharper edges than bilinear interpolation.
- eZoomInterpolationMethod::eZIM_VolumeWeightedMean: this method must be prefered if the scale factors are lower than 1.0 (in other words, if the user wants to shrink his input image). For each output pixel, it computes the mean of associated pixel values in the input image, weighted by the surface of these pixels "covered" by the associated output pixel.
Here is, from the following input image:
an example of zoom with a scale factor that equals to 10 along x and y axis, with different interpolation methods (left image: nearest neighbour; center image: bilinear interpolation; right image: bicubic interpolation):
Example of Python code :
Example imports
import PyIPSDK
import PyIPSDK.IPSDKIPLGeometricTransform as gtrans
Code Example
inImg = PyIPSDK.loadTiffImageFile(inputImgPath)
xScaleFactor = 11.4
yScaleFactor = 10.2
inImg2dSize = PyIPSDK.createImg2dSize(inImg.getSizeX(), inImg.getSizeY())
scaleFactor = PyIPSDK.createScaleFactor2d(xScaleFactor, yScaleFactor)
outImg2dSize = gtrans.getZoom2dOutputImageSize(inImg2dSize, scaleFactor)
outImg2dSizeX = outImg2dSize.xSz
outImg2dSizeY = outImg2dSize.ySz
outGeometry = PyIPSDK.geometry2d(inImg.getBufferType(), outImg2dSizeX, outImg2dSizeY)
outImg = PyIPSDK.createImage(outGeometry)
gtrans.zoom2dImg(inImg, xScaleFactor, yScaleFactor, PyIPSDK.eZoomInterpolationMethod.eZIM_NearestNeighbour, outImg)
Example of C++ code :
Example informations
Header file
#include <IPSDKIPL/IPSDKIPLGeometricTransform/Processor/Zoom2dImg/Zoom2dImg.h>
Code Example
if(inImageBufferType != pInImg->getBufferType())
ImagePtr pOutImg = zoom2dImg(pInImg, xScaleFactor, yScaleFactor, interpolationMethod);