Three Common two-cube interpolation algorithms for image processing and image processing

Source: Internet
Author: User

Three Common two-cube interpolation algorithms for image processing and image processing

Three Common two-cube interpolation algorithms for image processing

The two-cube interpolation involves 16 pixels. ('', J') indicates that the pixels to be calculated contain

The pixel coordinate of the fractional part. dx indicates the decimal coordinate in the X direction, and dy indicates the decimal coordinate in the Y direction. Details

You can see:


According to the above figure and the mathematical expression of the Two-cube interpolation, we can see that the two-cube interpolation is essentially like 16 pixels.

The sum of weighted convolution is used as the new pixel value.

R (x) indicates the interpolation expression, which can be different based on the selected expression. Common values include triangle-based values, Bell

Distribution expression and B-Spline expression.

1. Based on the triangle sampling mathematical formula


The simplest linear distribution, the code implementation is as follows:

private double triangleInterpolation( double f ){f = f / 2.0;if( f < 0.0 ){return ( f + 1.0 );}else{return ( 1.0 - f );}}
2. The mathematical formula based on Bell distributed sampling is as follows:


The Bell distribution sampling mathematical formula is implemented based on cubic convolution calculation. The code is implemented as follows:

private double bellInterpolation( double x ){double f = ( x / 2.0 ) * 1.5;if( f > -1.5 && f < -0.5 ){return( 0.5 * Math.pow(f + 1.5, 2.0));}else if( f > -0.5 && f < 0.5 ){return 3.0 / 4.0 - ( f * f );}else if( ( f > 0.5 && f < 1.5 ) ){return( 0.5 * Math.pow(f - 1.5, 2.0));}return 0.0;}
3. The mathematical formula for B-Spline-based sampling is as follows:


It is a polynomial-based four-convolution sampling computation. The Code is as follows:

private double bspLineInterpolation( double f ){if( f < 0.0 ){f = -f;}if( f >= 0.0 && f <= 1.0 ){return ( 2.0 / 3.0 ) + ( 0.5 ) * ( f* f * f ) - (f*f);}else if( f > 1.0 && f <= 2.0 ){return 1.0 / 6.0 * Math.pow( ( 2.0 - f  ), 3.0 );}return 1.0;}
Complete source code for dual-cube interpolation of images is as follows:

Package com. gloomyfish. zoom. study; import java. awt. image. bufferedImage; import java. awt. image. colorModel; import com. gloomyfish. filter. study. extends; public class BicubicInterpolationFilter extends actbufferedimageop {public final static int triangle1_interpolation = 1; public final static int bell1_interpolation = 2; public final static int bspline1_interpolation = 4; public final st Atic int catmullroomation interpolation = 8; public final static double B = 0.0; public final static double C = 0.5; // constantprivate int desomething; // zoom heightprivate int destW; // zoom widthprivate int type; public BicubicInterpolationFilter () {this. type = BSPLINE__INTERPOLATION;} public void setType (int type) {this. type = type;} public void setDestHeight (int desomething) {this. desomething = desomething;} public void set DestWidth (int destW) {this. destW = destW;} private double bellInterpolation (double x) {double f = (x/2.0) * 1.5; if (f>-1.5 & f <-0.5) {return (0.5 * Math. pow (f + 1.5, 2.0);} else if (f>-0.5 & f <0.5) {return 3.0/4.0-(f * f );} else if (f> 0.5 & f <1.5) {return (0.5 * Math. pow (f-1.5, 2.0);} return 0.0;} private double bspLineInterpolation (double f) {if (f <0.0) {f =-f;} if (F> = 0.0 & f <= 1.0) {return (2.0/3.0) + (0.5) * (f * f)-(f * f );} else if (f> 1.0 & f <= 2.0) {return 1.0/6.0 * Math. pow (2.0-f), 3.0);} return 1.0;} private double triangleInterpolation (double f) {f = f/2.0; if (f <0.0) {return (f + 1.0) ;}else {return (1.0-f) ;}} private double CatMullRomInterpolation (double f) {if (f <0.0) {f = Math. abs (f);} if (f <1.0) {retu Rn (12-9 * B-6 * C) * (f * f) + (-18 + 12 * B + 6 * C) * (f * f) + (6-2 * B)/6.0;} else if (f >=1.0 & f <2.0) {return (-B-6 * C) * (f * f) + (6 * B + 30 * C) * (f * f) + (-(12 * B)-48 * C) * f + 8 * B + 24 * C)/6.0;} else {return 0.0 ;}@overridepublic BufferedImage filter (BufferedImage src, BufferedImage dest) {int width = src. getWidth (); in T height = src. getHeight (); if (dest = null) dest = createCompatibleDestImage (src, null); int [] inPixels = new int [width * height]; int [] outPixels = new int [desomething * destW]; getRGB (src, 0, 0, width, height, inPixels); float rowRatio = (float) height) /(float) desomething); float colRatio = (float) width)/(float) destW); int index = 0; for (int row = 0; row <desomething; row ++) {int ta = 0, tr = 0, tg = 0, tb = 0; double srcRow = (float) row) * rowRatio; // obtain the integer part coordinate row Indexdouble j = Math. floor (srcRow); // obtain the decimal coordinate of a row. double t = srcRow-j; for (int col = 0; col <destW; col ++) {double srcCol = (float) col) * colRatio; // obtain the integer coordinate column Indexdouble k = Math. floor (srcCol); // obtain the decimal coordinate of a column. double u = srcCol-k; double [] rgbData = new double [3]; double rgbCoffeData = 0.0; for (int m =-1; m <3; m ++) {for (int n =-1; n <3; N ++) {int [] rgb = getPixel (j + m, k + n, width, height, inPixels); double f1 = 0.0d; double f2 = 0.0d; if (type = TRIANGLE__INTERPOLATION) {f1 = triangleInterpolation (double) m)-t); f2 = triangleInterpolation (-(double) n)-u ));} else if (type = BELL__INTERPOLATION) {f1 = bellInterpolation (double) m)-t); f2 = bellInterpolation (-(double) n) -u);} else if (type = bspline?interpo LATION) {f1 = bspLineInterpolation (double) m)-t); f2 = bspLineInterpolation (-(double) n)-u ));} else {f1 = CatMullRomInterpolation (double) m)-t); f2 = CatMullRomInterpolation (-(double) n)-u ));} // sum of weightrgbCoffeData + = f2 * f1; // sum of the RGB valuesrgbData [0] + = rgb [0] * f2 * f1; rgbData [1] + = rgb [1] * f2 * f1; rgbData [2] + = rgb [2] * f2 * f1;} ta = 255; // get Red/gr Een/blue value for sample pixeltr = (int) (rgbData [0]/rgbCoffeData); tg = (int) (rgbData [1]/rgbCoffeData); tb = (int) (rgbData [2]/rgbCoffeData); index = row * destW + col; outPixels [index] = (ta <24) | (clamp (tr) <16) | (clamp (tg) <8) | clamp (tb) ;}} setRGB (dest, 0, 0, destW, deth, outPixels); return dest ;} public int clamp (int value) {return value> 255? 255 :( value <0? 0: value);} private int [] getPixel (double j, double k, int width, int height, int [] inPixels) {int row = (int) j; int col = (int) k; if (row> = height) {row = height-1;} if (row <0) {row = 0 ;} if (col <0) {col = 0;} if (col> = width) {col = width-1;} int index = row * width + col; int [] rgb = new int [3]; rgb [0] = (inPixels [index]> 16) & 0xff; rgb [1] = (inPixels [index]> 8) & 0xff; rgb [2] = inPixels [index] & 0xff; return rgb;} public BufferedImage createCompatibleDestImage (BufferedImage src, colorModel dstCM) {if (dstCM = null) dstCM = src. getColorModel (); return new BufferedImage (dstCM, dstCM. createCompatibleWritableRaster (destW, desomething), dstCM. isAlphaPremultiplied (), null );}}
Running Effect: source Image

After the two-cube interpolation is enlarged:


Summary:

After two-cube interpolation based on the three methods, the image is blurred compared with the original image.

In this case, you can Sharpen the image and increase the contrast through subsequent processing to obtain the Sharpen version.

Of course, you can also find a more appropriate R (x) function to implement the retention during the dual-cubic convolution interpolation process.

Image Edge and contrast.

Reprinted, please note


Principle of bilinear interpolation algorithm in image processing

He dongjian's "digital image processing" section 6 contains the first two procedures
The complete program has been sent to your mailbox
The core code is as follows:
For (y = 0; y <nNewHeight; y ++)
{
// Point to row y of the new image
// Note that the width and height here are the width and height of the new image.
PNewTemp = pNewBits;
PNewTemp + = (nNewHeight-1-y) * nNewWidthBytes;
// Perform operations on each column of the image
For (x = 0; x <nNewWidth; x ++)
{
// Calculate the coordinates of the pixel in the source Image
Int y0 = (long) (y/fYZoomRatio + 0.5 );
Int x0 = (long) (x/fXZoomRatio + 0.5 );

// Determine whether it is in the source image range
If (x0> = 0) & (x0 <nOldWidth )&&
(Y0> = 0) & (y0 <nOldHeight ))
{
// Use bilinear interpolation
If (bBilinear)
{
Unsigned char * pTemp = Interpolation (nOldWidth, nOldHeight,
(Float) x0, (float) y0,
NOldWidthBytes, nMovedBits, pOldBits );
// Copy pixels
Memcpy (pNewTemp, pTemp, nMovedBits );

Delete [] pTemp;
}
Else
// Nearest neighbor interpolation
{
// Indicates the y0 and x0 pixels of the source image.
// Note that the width and height here should be exchanged
POldTemp = pOldBits;
POldTemp + = (nOldHeight-1-y0) * nOldWidthBytes;
POldTemp + = x0 * nMovedBits;

// Copy pixels
Memcpy (pNewTemp, pOldTemp, nMovedBits );
}
}
PNewTemp + = nMovedBits;

}

}
References: Baidu

There are only three common image interpolation algorithms?

The computer camera has a maximum of 1.3 million pixels, and 8 million is modified by software.
What is digital interpolation (software interpolation)
Interpolation, also known as "resetting samples", is a method to increase the pixel size of an image without generating pixels, calculate the color of the lost Pixel based on the mathematical formula of the surrounding pixel color. To put it simply, interpolation is a method used to simulate peripheral pixel values based on the color parameters of the central pixel. It is a unique software means for digital cameras to enlarge digital photos.
I. Recognition of interpolation algorithms
"Interpolation" was originally a computer term and was later referenced in digital images. When the image is enlarged, pixels also increase accordingly. But where do these added pixels come from? At this time, interpolation comes in handy. Interpolation is a method to increase the pixel size of an image without generating pixels. Based on the color of the surrounding pixels, mathematical formulas are used to calculate the color of the lost pixels. (Some cameras also use interpolation, artificially increase the image resolution ). Therefore, when enlarging an image, the image looks smooth and clean. However, interpolation does not add image information. As shown in figure 1 (see figure 1), the following are images processed by different interpolation algorithms.
1. Nearest pixel interpolation algorithm
Nearest Neighbour Interpolation is the simplest Interpolation algorithm. When an image is enlarged, the missing pixels are generated by directly using the colors of the closest original pixels, that is to say, the pixel next to the image is copied, and the result of this operation is obviously visible (see figure 2 ).
2. bilinear interpolation algorithm
Each pixel of the image output by Bilinear Interpolation is the result of the four pixels (2 × 2) operation in the source image, this algorithm greatly removes the Sawtooth phenomenon (see figure 3 ). 3. Double three interpolation algorithms
Bicubic Interpolation is an improved algorithm of the previous algorithm. Each pixel of the output image is the result of 16 pixels (4 × 4) of the original image (see figure 4 ). This algorithm is a common algorithm that is widely used in image editing software, printer drivers, and digital cameras. 4. Fractal algorithms
Fractal Interpolation is an algorithm proposed by Altamira Group. The image obtained by this algorithm is clearer and clearer than other algorithms (see figure 5 ).
Nowadays, many digital camera manufacturers use interpolation algorithms on digital cameras and publicize the resolution values obtained through the algorithms. Although their algorithms are much more advanced than double three interpolation algorithms, however, the fact is that the image details are not made out of thin air. Because the interpolation resolution is used by a digital camera to increase the pixel of the image through its own built-in software, so as to increase the resolution.
Ii. Effects of Interpolation
The photos taken with digital zoom are unclear, which is the most common cause of digital zoom. In fact, this is only a one-sided statement.
The effect of digital zoom on the image definition depends on whether the CCD performs interpolation when the digital camera is Zoom. When high pixels are used, if digital zoom is used for shooting, CCD does not have any interpolation operation at this time, the effect of digital zoom on the final clarity of digital photos will become extremely limited. For example, if a digital camera with a CCD pixel of 5.2 million and a maximum resolution of 2560x1920 is used for 2 x digital zoom, in this case, only half of CCD is working during imaging. In other words, digital cameras do not use interpolation algorithms similar to "adding eight pixels around a pixel", but rather reduce resolution, that is, the resolution indicator 1280 × 960 is used for imaging. For general digital photos, the 1280x960 resolution indicator is good enough. The difference between it and the 2560x1920 resolution will become acceptable because there is no interpolation operation involved. However, this phenomenon is only limited to some advanced digital cameras. For fixed-focus digital cameras with less than a thousand yuan, digital zoom means an inevitable interpolation operation, the consequence of sacrificing resolution allows the photographer to have only two options: either ...... remaining full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.