图像放缩之双立方插值
- 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 ); }
}
2.基于Bell分布采样的数学公式如下:
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; // constantprivate int destH; // 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 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;}} @Overridepublic 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 Indexdouble j = Math.floor(srcRow);// 获取行的小数部分坐标double t = srcRow - j;for (int col = 0; col < destW; col++) {double srcCol = ((float) col) * colRatio;// 获取整数部分坐标 column Indexdouble 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 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/green/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, 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)函数来实现双立方卷积插值过程时保留
图像边缘与对比度。
图像放缩之双立方插值相关推荐
- 【图像缩放】双立方(三次)卷积插值
前言 图像处理中有三种常用的插值算法: 最邻近插值 双线性插值 双立方(三次卷积)插值 其中效果最好的是双立方(三次卷积)插值,本文介绍它的原理以及使用 如果想先看效果和源码,可以拉到最底部 本文的契 ...
- matlab彩色图像缩放(双线性与双立方插值)
双线性插值原理可以参考这篇博文:双线性内插法 立方插值的推导我参考的这篇文章:Cubic interpolation 数学推导过程上面两篇文章解释得还是比较清楚,可以自己拿笔推一推,至于双线性和双立方 ...
- 【图像去噪】基于双立方插值和稀疏表示实现图像去噪matlab源码
1 内容介绍 本文解决了从单个低分辨率输入图像生成超分辨率 (SR) 图像的问题.我们从压缩感知的角度来解决这个问题.低分辨率图像被视为高分辨率图像的下采样版本,假设其补丁相对于原型信号原子的过完备字 ...
- 图像插值算法——双立方(三次)卷积插值
双立方(三次)卷积插值是一种数据点插值方法. 在对图像进行缩放,旋转等处理时,有些像素点会因为这些操作变得没有意义,比如二维图像A(2*2)放大为原来的二倍后B(4*4)就会缺失一些像素,如图所示: ...
- matlab双立方插值法_双三次插值(Bicubic interpolation)缩放图片
References:https://en.wikipedia.org/wiki/Bicubic_interpolation Bicubic interpolation是一种常用的插值方法 1.数学 ...
- 图形图像处理-之-高质量的快速的图像缩放 中篇 二次线性插值和三次卷积插值
from:http://blog.csdn.net/housisong/article/details/1452249 图形图像处理-之-高质量的快速的图像缩放 中篇 二次线性插值和三次卷积插值 ...
- [图像]图像缩放算法-双线性内插法
原创文章,欢迎转载.转载请注明:转载自 祥的博客 原文链接:http://blog.csdn.net/humanking7/article/details/45014879 简介: 图像缩放算法–双线 ...
- 基于深度学习的目标检测:数据增强(一)图像翻转、图像旋转、图像放缩
1.数据增强简介 数据增强(data augmentation),又名数据增广或数据扩充,其本质是通过使用图像处理方法,基于有限的数据产生更多的数据,以此增加训练样本的数量以及多样性,进而提升模型的泛 ...
- Opencv下双线性插值法进行图像放缩
关于图像放缩的算法有很多,本文主要介绍双线性插值法进行图像放缩,本文参考了: http://www.cnblogs.com/funny-world/p/3162003.html 我们设源图像src的大 ...
最新文章
- 数学仍然是人类的“火炬”
- Karush-Kuhn-Tucker 最优化条件 (KKT 条件)(转载)
- 看完这篇,Oracle数据库运维不用愁
- Redisson官方文档 - 目录
- FFmpeg Filter基本使用
- require的key一个坑
- cassandra随机获取数据,Cassandra适合写入和少读,HBASE随机读取写入
- python 正则表达式提取数据_Python爬虫教程-19-数据提取-正则表达式(re)
- 107. 二叉树的层次遍历 II
- php js 循环对象属性,js 遍历对象的属性的代码_javascript技巧
- Java包的命名规则
- (总结)Linux下多行合并成一行,中间加分隔符
- scrollLeft. float
- “产品助理最重要的工作是 Android 版本的设计与测试”
- 如何扩展计算机c盘的控件,电脑C盘空间不足,怎么把c盘空间可以扩大
- histogram函数 python_Python numpy.histogram函数方法的使用
- zoho邮箱收费和免费区别_使用Zoho Reports和XML在云中探索分析
- python三维建模需要用到哪些知识_参加数学建模需要学习哪些方面的知识?
- jsp文件木马代码分析
- 雷军赞赏有加,黑鲨游戏手机2打造“操控之王”