图像的放大与缩小(3)——双立方插值算法

-----------------------------转载自jia20003的博客"

图像放缩之双立方插值"

  -----------------------------------

一:数学原理

如果已知一个函数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的程序完全源代码

[java]  view plain  copy
  1. package cn.edu.jxau.luoweifu;  
  2.   
  3. public class BiCubicInterpolationScale {  
  4.   
  5.     private static double a00, a01, a02, a03;  
  6.     private static double a10, a11, a12, a13;  
  7.     private static double a20, a21, a22, a23;  
  8.     private static double a30, a31, a32, a33;  
  9.     private static int srcWidth;  
  10.     private static int srcHeight;  
  11.       
  12.     /** 
  13.      * 双立方插值 
  14.      * @param inPixelsData 像素矩阵数组 
  15.      * @param srcW 原图像的宽 
  16.      * @param srcH 原图像的高 
  17.      * @param destW 目标图像的宽 
  18.      * @param destH 目标图像的高 
  19.      * @return 处理后的推三矩阵数组 
  20.      */  
  21.     public static int[] imgScale(int[] inPixelsData, int srcW, int srcH, int destW, int destH) {  
  22.         double[][][] input3DData = processOneToThreeDeminsion(inPixelsData, srcH, srcW);  
  23.         int[][][] outputThreeDeminsionData = new int[destH][destW][4];  
  24.         double[][] tempPixels = new double[4][4];  
  25.         float rowRatio = ((float)srcH)/((float)destH);  
  26.         float colRatio = ((float)srcW)/((float)destW);  
  27.         srcWidth = srcW;  
  28.         srcHeight = srcH;  
  29.         for(int row=0; row<destH; row++) {  
  30.             // convert to three dimension data  
  31.             double srcRow = ((float)row)*rowRatio;  
  32.             double j = Math.floor(srcRow);  
  33.             double t = srcRow - j;  
  34.             for(int col=0; col<destW; col++) {  
  35.                 double srcCol = ((float)col)*colRatio;  
  36.                 double k = Math.floor(srcCol);  
  37.                 double u = srcCol - k;  
  38.                 for(int i=0; i<4; i++) {  
  39.                     tempPixels[0][0] = getRGBValue(input3DData,j-1, k-1,i);  
  40.                     tempPixels[0][1] = getRGBValue(input3DData,j-1, k, i);  
  41.                     tempPixels[0][2] = getRGBValue(input3DData, j-1,k+1, i);  
  42.                     tempPixels[0][3] = getRGBValue(input3DData, j-1, k+2,i);  
  43.                       
  44.                     tempPixels[1][0] = getRGBValue(input3DData, j, k-1, i);  
  45.                     tempPixels[1][1] = getRGBValue(input3DData, j, k, i);  
  46.                     tempPixels[1][2] = getRGBValue(input3DData, j, k+1, i);  
  47.                     tempPixels[1][3] = getRGBValue(input3DData, j, k+2, i);  
  48.                       
  49.                     tempPixels[2][0] = getRGBValue(input3DData, j+1,k-1,i);  
  50.                     tempPixels[2][1] = getRGBValue(input3DData, j+1, k, i);  
  51.                     tempPixels[2][2] = getRGBValue(input3DData, j+1, k+1, i);  
  52.                     tempPixels[2][3] = getRGBValue(input3DData, j+1, k+2, i);  
  53.                       
  54.                     tempPixels[3][0] = getRGBValue(input3DData, j+2, k-1, i);  
  55.                     tempPixels[3][1] = getRGBValue(input3DData, j+2, k, i);  
  56.                     tempPixels[3][2] = getRGBValue(input3DData, j+2, k+1, i);  
  57.                     tempPixels[3][3] = getRGBValue(input3DData, j+2, k+2, i);  
  58.                       
  59.                     // update coefficients  
  60.                     updateCoefficients(tempPixels);  
  61.                     outputThreeDeminsionData[row][col][i] = getPixelValue(getValue(t, u));  
  62.                 }  
  63.   
  64.             }  
  65.         }  
  66.           
  67.         return convertToOneDim(outputThreeDeminsionData, destW, destH);  
  68.     }  
  69.       
  70.     private static double getRGBValue(double[][][] input3DData, double row, double col, int index) {  
  71.         if(col >= srcWidth) {  
  72.             col = srcWidth - 1;  
  73.         }  
  74.           
  75.         if(col < 0) {  
  76.             col = 0;  
  77.         }  
  78.           
  79.         if(row >= srcHeight) {  
  80.             row = srcHeight - 1;  
  81.         }  
  82.           
  83.         if(row < 0) {  
  84.             row = 0;  
  85.         }  
  86.         return input3DData[(int)row][(int)col][index];  
  87.     }  
  88.       
  89.     private static int getPixelValue(double pixelValue) {  
  90.         return pixelValue < 0 ? 0: pixelValue >255.0d ?255:(int)pixelValue;  
  91.     }  
  92.       
  93.     private static void updateCoefficients (double[][] p) {  
  94.         a00 = p[1][1];  
  95.         a01 = -.5*p[1][0] + .5*p[1][2];  
  96.         a02 = p[1][0] - 2.5*p[1][1] + 2*p[1][2] - .5*p[1][3];  
  97.         a03 = -.5*p[1][0] + 1.5*p[1][1] - 1.5*p[1][2] + .5*p[1][3];  
  98.         a10 = -.5*p[0][1] + .5*p[2][1];  
  99.         a11 = .25*p[0][0] - .25*p[0][2] - .25*p[2][0] + .25*p[2][2];  
  100.         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];  
  101.         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];  
  102.         a20 = p[0][1] - 2.5*p[1][1] + 2*p[2][1] - .5*p[3][1];  
  103.         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];  
  104.         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];  
  105.         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];  
  106.         a30 = -.5*p[0][1] + 1.5*p[1][1] - 1.5*p[2][1] + .5*p[3][1];  
  107.         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];  
  108.         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];  
  109.         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];  
  110.     }  
  111.       
  112.     private static double getValue (double x, double y) {  
  113.         double x2 = x * x;  
  114.         double x3 = x2 * x;  
  115.         double y2 = y * y;  
  116.         double y3 = y2 * y;  
  117.   
  118.         return (a00 + a01 * y + a02 * y2 + a03 * y3) +  
  119.                (a10 + a11 * y + a12 * y2 + a13 * y3) * x +  
  120.                (a20 + a21 * y + a22 * y2 + a23 * y3) * x2 +  
  121.                (a30 + a31 * y + a32 * y2 + a33 * y3) * x3;  
  122.     }  
  123.   
  124.     /* <p> The purpose of this method is to convert the data in the 3D array of ints back into </p> 
  125.      * <p> the 1d array of type int. </p> 
  126.      *  
  127.      */  
  128.     private static int[] convertToOneDim(int[][][] data, int imgCols, int imgRows) {  
  129.         // Create the 1D array of type int to be populated with pixel data  
  130.         int[] oneDPix = new int[imgCols * imgRows * 4];  
  131.   
  132.         // Move the data into the 1D array. Note the  
  133.         // use of the bitwise OR operator and the  
  134.         // bitwise left-shift operators to put the  
  135.         // four 8-bit bytes into each int.  
  136.         for (int row = 0, cnt = 0; row < imgRows; row++) {  
  137.             for (int col = 0; col < imgCols; col++) {  
  138.                 oneDPix[cnt] = ((data[row][col][0] << 24) & 0xFF000000)  
  139.                         | ((data[row][col][1] << 16) & 0x00FF0000)  
  140.                         | ((data[row][col][2] << 8) & 0x0000FF00)  
  141.                         | ((data[row][col][3]) & 0x000000FF);  
  142.                 cnt++;  
  143.             }// end for loop on col  
  144.   
  145.         }// end for loop on row  
  146.   
  147.         return oneDPix;  
  148.     }// end convertToOneDim  
  149.       
  150.     private static double [][][] processOneToThreeDeminsion(int[] oneDPix2, int imgRows, int imgCols) {  
  151.         double[][][] tempData = new double[imgRows][imgCols][4];  
  152.         for(int row=0; row<imgRows; row++) {  
  153.               
  154.             // per row processing  
  155.             int[] aRow = new int[imgCols];  
  156.             for (int col = 0; col < imgCols; col++) {  
  157.                 int element = row * imgCols + col;  
  158.                 aRow[col] = oneDPix2[element];  
  159.             }  
  160.               
  161.             // convert to three dimension data  
  162.             for(int col=0; col<imgCols; col++) {  
  163.                 tempData[row][col][0] = (aRow[col] >> 24) & 0xFF// alpha  
  164.                 tempData[row][col][1] = (aRow[col] >> 16) & 0xFF// red  
  165.                 tempData[row][col][2] = (aRow[col] >> 8) & 0xFF;  // green  
  166.                 tempData[row][col][3] = (aRow[col]) & 0xFF;       // blue  
  167.             }  
  168.         }  
  169.         return tempData;  
  170.     }     
  171.     /*public static void main(String args[]) { 
  172.          
  173.     }*/  
  174. }  
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值