Three Common two-cube interpolation algorithms for image processing

Source: Internet
Author: User

Three Common two-cube interpolation algorithms for 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. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD48cD7IqNbYvu27/examples/J0tS4 + examples/samples + s/fse2078q9oaM8L3A + PHA + MS4gu/examples = "http://www.2cto.com/uploadfile/Collfiles/20141013/20141013085622164.png" alt = "\"/>

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

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.