圖像處理之三種常見(jiàn)雙立方插值算法
雙立方插值計算涉及到16個(gè)像素點(diǎn),其中(i’, j’)表示待計算像素點(diǎn)在源圖像中的包含
小數部分的像素坐標,dx表示X方向的小數坐標,dy表示Y方向的小數坐標。具體
可以看下圖:
根據上述圖示與雙立方插值的數學(xué)表達式可以看出,雙立方插值本質(zhì)上圖像16個(gè)像素點(diǎn)
權重卷積之和作為新的像素值。
其中R(x)表示插值表達式,可以根據需要選擇的表達式不同。常見(jiàn)有基于三角取值、Bell
分布表達、B樣條曲線(xiàn)表達式。
1. 基于三角形采樣數學(xué)公式為
最簡(jiǎn)單的線(xiàn)性分布,代碼實(shí)現如下:
- private double triangleInterpolation( double f )
- {
- f = f / 2.0;
- if( f < 0.0 )
- {
- return ( f + 1.0 );
- }
- else
- {
- return ( 1.0 - f );
- }
- }
2.基于Bell分布采樣的數學(xué)公式如下:Bell分布采樣數學(xué)公式基于三次卷積計算實(shí)現。代碼實(shí)現如下:
- 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.基于B樣條曲線(xiàn)采樣的數學(xué)公式如下:是一種基于多項式的四次卷積的采樣計算,代碼如下:
- 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;
- }
實(shí)現圖像雙立方插值的完整源代碼如下:- package com.gloomyfish.zoom.study;
-
- import java.awt.image.BufferedImage;
- import java.awt.image.ColorModel;
-
- import com.gloomyfish.filter.study.AbstractBufferedImageOp;
-
- public class BicubicInterpolationFilter extends AbstractBufferedImageOp {
- public final static int TRIANGLE__INTERPOLATION = 1;
- public final static int BELL__INTERPOLATION = 2;
- public final static int BSPLINE__INTERPOLATION = 4;
- public final static int CATMULLROOM__INTERPOLATION = 8;
- public final static double B = 0.0;
- public final static double C = 0.5; // constant
- private int destH; // zoom height
- private int destW; // zoom width
- private int type;
- public BicubicInterpolationFilter()
- {
- this.type = BSPLINE__INTERPOLATION;
- }
- public void setType(int type) {
- this.type = type;
- }
- public void setDestHeight(int destH) {
- this.destH = destH;
- }
-
- public void setDestWidth(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*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 )
- {
- return ( ( 12 - 9 * B - 6 * C ) * ( f * 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 * f )
- + ( 6 * B + 30 * C ) * ( f *f ) +
- ( - ( 12 * B ) - 48 * C ) * f +
- 8 * B + 24 * C)/ 6.0;
- }
- else
- {
- return 0.0;
- }
- }
-
- @Override
- public BufferedImage filter(BufferedImage src, BufferedImage dest) {
- int width = src.getWidth();
- int height = src.getHeight();
-
- if (dest == null)
- dest = createCompatibleDestImage(src, null);
-
- int[] inPixels = new int[width * height];
- int[] outPixels = new int[destH * destW];
- getRGB(src, 0, 0, width, height, inPixels);
- float rowRatio = ((float) height) / ((float) destH);
- float colRatio = ((float) width) / ((float) destW);
- int index = 0;
- for (int row = 0; row < destH; row++) {
- int ta = 0, tr = 0, tg = 0, tb = 0;
- double srcRow = ((float) row) * rowRatio;
- // 獲取整數部分坐標 row Index
- double j = Math.floor(srcRow);
- // 獲取行的小數部分坐標
- double t = srcRow - j;
- for (int col = 0; col < destW; col++) {
- double srcCol = ((float) col) * colRatio;
- // 獲取整數部分坐標 column Index
- double k = Math.floor(srcCol);
- // 獲取列的小數部分坐標
- 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__INTERPOLATION)
- {
- f1 = bspLineInterpolation( ((double) m ) - t );
- f2 = bspLineInterpolation ( -(( (double) n ) - u ) );
- }
- else
- {
- f1 = CatMullRomInterpolation( ((double) m ) - t );
- f2 = CatMullRomInterpolation ( -(( (double) n ) - u ) );
- }
- // sum of weight
- rgbCoffeData += f2*f1;
- // sum of the RGB values
- rgbData[0] += rgb[0] * f2 * f1;
- rgbData[1] += rgb[1] * f2 * f1;
- rgbData[2] += rgb[2] * f2 * f1;
- }
- }
- ta = 255;
- // get Red/green/blue value for sample pixel
- tr = (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, destH, 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, destH),
- dstCM.isAlphaPremultiplied(), null);
- }
- }
運行效果:原圖
雙立方插值放大以后:
總結:基于這里三種方法實(shí)現的雙立方插值以后圖片跟原圖像相比,都有一定模糊
這里時(shí)候可以通過(guò)后續處理實(shí)現圖像銳化與對比度提升即可得到Sharpen版本
當然也可以通過(guò)尋找更加合適的R(x)函數來(lái)實(shí)現雙立方卷積插值過(guò)程時(shí)保留
圖像邊緣與對比度。
如果已知一個(gè)函數f(x)以及它在x=0,x=1處的導數,那么函數可以在[0,1]之間插值,當函數
表達為三次多項式時(shí)我們稱(chēng)之謂立方插值。一個(gè)三次多項式及其導數:
f(x) =ax^3 +bx^2 + cx + d
f’(x)=3ax^2 + 2bx +c
多項式在x=0, x=1處值及其導數值為:
f(0)= d;
f(1)= a + b + c + d;
f’(0)=c
f’(1)=3a + 2b + c
上述的四個(gè)等式可以等價(jià)的變換為:
a= 2f(0) – 2f(1) + f’(0) + f’(1)
b= -3f(0) + 3f(1) – 2f’(0) – f’(1)
c= f’(0)
d= f’(1)
假設你有四個(gè)點(diǎn)值p0, p1, p2, p3分別在x=-1, x=0, x=1, x=2, 把值分別指定到f(0), f(1), f’(0),
f’(1)中為:
f(0)= p1
f(1)= p2
f’(0)= (p2 – p0)/2
f’(1)= (p3-p1)/2
這個(gè)我們的立方插值公式變成:
f(p0,p1,p2,p3, x) = (-1/2p0 + 3/2p1 -3/2p2+ 1/2p3)x^3 + (p0-5/2p1 + 2p2 -1/2d)x^2 + (-1/2p0 +
1/2p2)x + p1
雙立方插值是立方插值在二維空間的表達, 插值公式可以表述為:
G(x, y) = f (f (p00, p01, p02, p03, y), f(p10,p11, p12, p13, y), f(p20, p21, p22, p23, y), f(p30, p31, p32, p33, y), x)
解出其中的16個(gè)參數,即可得帶G(x, y)目標插值點(diǎn)的值。