- created by gloomyfish
一:数学原理
如果已知一个函数f(x)以及它在x=0,x=1处的导数,那么函数可以在[0,1]之间插值,当函数
表达为三次多项式时我们称之谓立方插值。一个三次多项式及其导数:
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
上述的四个等式可以等价的变换为:
a= 2f(0) – 2f(1) + f’(0) + f’(1)
b= -3f(0) + 3f(1) – 2f’(0) – f’(1)
c= f’(0)
d= f’(1)
假设你有四个点值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
这个我们的立方插值公式变成:
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(x, y)目标插值点的值。
二:双立方插值优缺点
双立方插值在图像放大过程可以保留更多的图像细节,放大以后的图像带有反锯齿的功能,
同时图像和源图像相比效果更加真实, 缺点是计算量比较大,是常见的三种图像放大算法中
计算量最大的一种,据说Photoshop的图像放大就是基本双立方插值的优化算法
三:程序运行效果如下:
四:关键代码解析
不想解释太多,最重要的是代入计算的是浮点数坐标的小数部分,即 x, y的取值范围均在[0,1]之间
五:基于Java的程序完全源代码
package cn.edu.jxau.luoweifu;
public class BiCubicInterpolationScale {
private static double a00, a01, a02, a03;
private static double a10, a11, a12, a13;
private static double a20, a21, a22, a23;
private static double a30, a31, a32, a33;
private static int srcWidth;
private static int srcHeight;
/**
* 双立方插值
* @param inPixelsData 像素矩阵数组
* @param srcW 原图像的宽
* @param srcH 原图像的高
* @param destW 目标图像的宽
* @param destH 目标图像的高
* @return 处理后的推三矩阵数组
*/
public static int[] imgScale(int[] inPixelsData, int srcW, int srcH, int destW, int destH) {
double[][][] input3DData = processOneToThreeDeminsion(inPixelsData, srcH, srcW);
int[][][] outputThreeDeminsionData = new int[destH][destW][4];
double[][] tempPixels = new double[4][4];
float rowRatio = ((float)srcH)/((float)destH);
float colRatio = ((float)srcW)/((float)destW);
srcWidth = srcW;
srcHeight = srcH;
for(int row=0; row<destH; row++) {
// convert to three dimension data
double srcRow = ((float)row)*rowRatio;
double j = Math.floor(srcRow);
double t = srcRow - j;
for(int col=0; col<destW; col++) {
double srcCol = ((float)col)*colRatio;
double k = Math.floor(srcCol);
double u = srcCol - k;
for(int i=0; i<4; i++) {
tempPixels[0][0] = getRGBValue(input3DData,j-1, k-1,i);
tempPixels[0][1] = getRGBValue(input3DData,j-1, k, i);
tempPixels[0][2] = getRGBValue(input3DData, j-1,k+1, i);
tempPixels[0][3] = getRGBValue(input3DData, j-1, k+2,i);
tempPixels[1][0] = getRGBValue(input3DData, j, k-1, i);
tempPixels[1][1] = getRGBValue(input3DData, j, k, i);
tempPixels[1][2] = getRGBValue(input3DData, j, k+1, i);
tempPixels[1][3] = getRGBValue(input3DData, j, k+2, i);
tempPixels[2][0] = getRGBValue(input3DData, j+1,k-1,i);
tempPixels[2][1] = getRGBValue(input3DData, j+1, k, i);
tempPixels[2][2] = getRGBValue(input3DData, j+1, k+1, i);
tempPixels[2][3] = getRGBValue(input3DData, j+1, k+2, i);
tempPixels[3][0] = getRGBValue(input3DData, j+2, k-1, i);
tempPixels[3][1] = getRGBValue(input3DData, j+2, k, i);
tempPixels[3][2] = getRGBValue(input3DData, j+2, k+1, i);
tempPixels[3][3] = getRGBValue(input3DData, j+2, k+2, i);
// update coefficients
updateCoefficients(tempPixels);
outputThreeDeminsionData[row][col][i] = getPixelValue(getValue(t, u));
}
}
}
return convertToOneDim(outputThreeDeminsionData, destW, destH);
}
private static double getRGBValue(double[][][] input3DData, double row, double col, int index) {
if(col >= srcWidth) {
col = srcWidth - 1;
}
if(col < 0) {
col = 0;
}
if(row >= srcHeight) {
row = srcHeight - 1;
}
if(row < 0) {
row = 0;
}
return input3DData[(int)row][(int)col][index];
}
private static int getPixelValue(double pixelValue) {
return pixelValue < 0 ? 0: pixelValue >255.0d ?255:(int)pixelValue;
}
private static void updateCoefficients (double[][] p) {
a00 = p[1][1];
a01 = -.5*p[1][0] + .5*p[1][2];
a02 = p[1][0] - 2.5*p[1][1] + 2*p[1][2] - .5*p[1][3];
a03 = -.5*p[1][0] + 1.5*p[1][1] - 1.5*p[1][2] + .5*p[1][3];
a10 = -.5*p[0][1] + .5*p[2][1];
a11 = .25*p[0][0] - .25*p[0][2] - .25*p[2][0] + .25*p[2][2];
a12 = -.5*p[0][0] + 1.25*p[0][1] - p[0][2] + .25*p[0][3] + .5*p[2][0] - 1.25*p[2][1] + p[2][2] - .25*p[2][3];
a13 = .25*p[0][0] - .75*p[0][1] + .75*p[0][2] - .25*p[0][3] - .25*p[2][0] + .75*p[2][1] - .75*p[2][2] + .25*p[2][3];
a20 = p[0][1] - 2.5*p[1][1] + 2*p[2][1] - .5*p[3][1];
a21 = -.5*p[0][0] + .5*p[0][2] + 1.25*p[1][0] - 1.25*p[1][2] - p[2][0] + p[2][2] + .25*p[3][0] - .25*p[3][2];
a22 = p[0][0] - 2.5*p[0][1] + 2*p[0][2] - .5*p[0][3] - 2.5*p[1][0] + 6.25*p[1][1] - 5*p[1][2] + 1.25*p[1][3] + 2*p[2][0] - 5*p[2][1] + 4*p[2][2] - p[2][3] - .5*p[3][0] + 1.25*p[3][1] - p[3][2] + .25*p[3][3];
a23 = -.5*p[0][0] + 1.5*p[0][1] - 1.5*p[0][2] + .5*p[0][3] + 1.25*p[1][0] - 3.75*p[1][1] + 3.75*p[1][2] - 1.25*p[1][3] - p[2][0] + 3*p[2][1] - 3*p[2][2] + p[2][3] + .25*p[3][0] - .75*p[3][1] + .75*p[3][2] - .25*p[3][3];
a30 = -.5*p[0][1] + 1.5*p[1][1] - 1.5*p[2][1] + .5*p[3][1];
a31 = .25*p[0][0] - .25*p[0][2] - .75*p[1][0] + .75*p[1][2] + .75*p[2][0] - .75*p[2][2] - .25*p[3][0] + .25*p[3][2];
a32 = -.5*p[0][0] + 1.25*p[0][1] - p[0][2] + .25*p[0][3] + 1.5*p[1][0] - 3.75*p[1][1] + 3*p[1][2] - .75*p[1][3] - 1.5*p[2][0] + 3.75*p[2][1] - 3*p[2][2] + .75*p[2][3] + .5*p[3][0] - 1.25*p[3][1] + p[3][2] - .25*p[3][3];
a33 = .25*p[0][0] - .75*p[0][1] + .75*p[0][2] - .25*p[0][3] - .75*p[1][0] + 2.25*p[1][1] - 2.25*p[1][2] + .75*p[1][3] + .75*p[2][0] - 2.25*p[2][1] + 2.25*p[2][2] - .75*p[2][3] - .25*p[3][0] + .75*p[3][1] - .75*p[3][2] + .25*p[3][3];
}
private static double getValue (double x, double y) {
double x2 = x * x;
double x3 = x2 * x;
double y2 = y * y;
double y3 = y2 * y;
return (a00 + a01 * y + a02 * y2 + a03 * y3) +
(a10 + a11 * y + a12 * y2 + a13 * y3) * x +
(a20 + a21 * y + a22 * y2 + a23 * y3) * x2 +
(a30 + a31 * y + a32 * y2 + a33 * y3) * x3;
}
/* <p> The purpose of this method is to convert the data in the 3D array of ints back into </p>
* <p> the 1d array of type int. </p>
*
*/
private static int[] convertToOneDim(int[][][] data, int imgCols, int imgRows) {
// Create the 1D array of type int to be populated with pixel data
int[] oneDPix = new int[imgCols * imgRows * 4];
// Move the data into the 1D array. Note the
// use of the bitwise OR operator and the
// bitwise left-shift operators to put the
// four 8-bit bytes into each int.
for (int row = 0, cnt = 0; row < imgRows; row++) {
for (int col = 0; col < imgCols; col++) {
oneDPix[cnt] = ((data[row][col][0] << 24) & 0xFF000000)
| ((data[row][col][1] << 16) & 0x00FF0000)
| ((data[row][col][2] << 8) & 0x0000FF00)
| ((data[row][col][3]) & 0x000000FF);
cnt++;
}// end for loop on col
}// end for loop on row
return oneDPix;
}// end convertToOneDim
private static double [][][] processOneToThreeDeminsion(int[] oneDPix2, int imgRows, int imgCols) {
double[][][] tempData = new double[imgRows][imgCols][4];
for(int row=0; row<imgRows; row++) {
// per row processing
int[] aRow = new int[imgCols];
for (int col = 0; col < imgCols; col++) {
int element = row * imgCols + col;
aRow[col] = oneDPix2[element];
}
// convert to three dimension data
for(int col=0; col<imgCols; col++) {
tempData[row][col][0] = (aRow[col] >> 24) & 0xFF; // alpha
tempData[row][col][1] = (aRow[col] >> 16) & 0xFF; // red
tempData[row][col][2] = (aRow[col] >> 8) & 0xFF; // green
tempData[row][col][3] = (aRow[col]) & 0xFF; // blue
}
}
return tempData;
}
/*public static void main(String args[]) {
}*/
}
图像处理之三种常见双立方插值算法
双立方插值计算涉及到16个像素点,其中(i’, j’)表示待计算像素点在源图像中的包含
小数部分的像素坐标,dx表示X方向的小数坐标,dy表示Y方向的小数坐标。具体
可以看下图:
根据上述图示与双立方插值的数学表达式可以看出,双立方插值本质上图像16个像素点
权重卷积之和作为新的像素值。
其中R(x)表示插值表达式,可以根据需要选择的表达式不同。常见有基于三角取值、Bell
分布表达、B样条曲线表达式。
1. 基于三角形采样数学公式为
最简单的线性分布,代码实现如下:
private double triangleInterpolation( double f )
{
f = f / 2.0;
if( f < 0.0 )
{
return ( f + 1.0 );
}
else
{
return ( 1.0 - f );
}
}
Bell分布采样数学公式基于三次卷积计算实现。代码实现如下:
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样条曲线采样的数学公式如下:
是一种基于多项式的四次卷积的采样计算,代码如下:
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;
}
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);
}
}
运行效果:原图
双立方插值放大以后:
总结:
基于这里三种方法实现的双立方插值以后图片跟原图像相比,都有一定模糊
这里时候可以通过后续处理实现图像锐化与对比度提升即可得到Sharpen版本
当然也可以通过寻找更加合适的R(x)函数来实现双立方卷积插值过程时保留
图像边缘与对比度。