zoukankan      html  css  js  c++  java
  • 图像处理之三种常见双立方插值算法

    http://blog.csdn.net/jia20003/article/details/40020775

    图像处理之三种常见双立方插值算法

    双立方插值计算涉及到16个像素点,其中(i’, j’)表示待计算像素点在源图像中的包含

    小数部分的像素坐标,dx表示X方向的小数坐标,dy表示Y方向的小数坐标。具体

    可以看下图:

    根据上述图示与双立方插值的数学表达式可以看出,双立方插值本质上图像16个像素点

    权重卷积之和作为新的像素值。

    其中R(x)表示插值表达式,可以根据需要选择的表达式不同。常见有基于三角取值、Bell

    分布表达、B样条曲线表达式。

    1. 基于三角形采样数学公式为

    最简单的线性分布,代码实现如下:

    [java] view plain copy
     
    1. private double triangleInterpolation( double f )  
    2. {  
    3.     f = f / 2.0;  
    4.     if( f < 0.0 )  
    5.     {  
    6.         return ( f + 1.0 );  
    7.     }  
    8.     else  
    9.     {  
    10.         return ( 1.0 - f );  
    11.     }  
    12. }  

    2.基于Bell分布采样的数学公式如下:

    Bell分布采样数学公式基于三次卷积计算实现。代码实现如下:

    [java] view plain copy
     
    1. private double bellInterpolation( double x )  
    2. {  
    3.     double f = ( x / 2.0 ) * 1.5;  
    4.     if( f > -1.5 && f < -0.5 )  
    5.     {  
    6.         return( 0.5 * Math.pow(f + 1.5, 2.0));  
    7.     }  
    8.     else if( f > -0.5 && f < 0.5 )  
    9.     {  
    10.         return 3.0 / 4.0 - ( f * f );  
    11.     }  
    12.     else if( ( f > 0.5 && f < 1.5 ) )  
    13.     {  
    14.         return( 0.5 * Math.pow(f - 1.5, 2.0));  
    15.     }  
    16.     return 0.0;  
    17. }  

    3.基于B样条曲线采样的数学公式如下:

    是一种基于多项式的四次卷积的采样计算,代码如下:

    [java] view plain copy
     
    1. private double bspLineInterpolation( double f )  
    2. {  
    3.     if( f < 0.0 )  
    4.     {  
    5.         f = -f;  
    6.     }  
    7.   
    8.     if( f >= 0.0 && f <= 1.0 )  
    9.     {  
    10.         return ( 2.0 / 3.0 ) + ( 0.5 ) * ( f* f * f ) - (f*f);  
    11.     }  
    12.     else if( f > 1.0 && f <= 2.0 )  
    13.     {  
    14.         return 1.0 / 6.0 * Math.pow( ( 2.0 - f  ), 3.0 );  
    15.     }  
    16.     return 1.0;  
    17. }  

    实现图像双立方插值的完整源代码如下:

    [java] view plain copy
     
    1. package com.gloomyfish.zoom.study;  
    2.   
    3. import java.awt.image.BufferedImage;  
    4. import java.awt.image.ColorModel;  
    5.   
    6. import com.gloomyfish.filter.study.AbstractBufferedImageOp;  
    7.   
    8. public class BicubicInterpolationFilter extends AbstractBufferedImageOp  {  
    9.     public final static int TRIANGLE__INTERPOLATION = 1;  
    10.     public final static int BELL__INTERPOLATION = 2;  
    11.     public final static int BSPLINE__INTERPOLATION = 4;  
    12.     public final static int CATMULLROOM__INTERPOLATION = 8;  
    13.     public final static double B = 0.0;  
    14.     public final static double C = 0.5; // constant  
    15.     private int destH; // zoom height  
    16.     private int destW; // zoom width  
    17.     private int type;  
    18.     public BicubicInterpolationFilter()  
    19.     {  
    20.         this.type = BSPLINE__INTERPOLATION;  
    21.     }  
    22.     public void setType(int type) {  
    23.         this.type = type;  
    24.     }  
    25.     public void setDestHeight(int destH) {  
    26.         this.destH = destH;  
    27.     }  
    28.   
    29.     public void setDestWidth(int destW) {  
    30.         this.destW = destW;  
    31.     }  
    32.       
    33.     private double bellInterpolation( double x )  
    34.     {  
    35.         double f = ( x / 2.0 ) * 1.5;  
    36.         if( f > -1.5 && f < -0.5 )  
    37.         {  
    38.             return( 0.5 * Math.pow(f + 1.5, 2.0));  
    39.         }  
    40.         else if( f > -0.5 && f < 0.5 )  
    41.         {  
    42.             return 3.0 / 4.0 - ( f * f );  
    43.         }  
    44.         else if( ( f > 0.5 && f < 1.5 ) )  
    45.         {  
    46.             return( 0.5 * Math.pow(f - 1.5, 2.0));  
    47.         }  
    48.         return 0.0;  
    49.     }  
    50.       
    51.     private double bspLineInterpolation( double f )  
    52.     {  
    53.         if( f < 0.0 )  
    54.         {  
    55.             f = -f;  
    56.         }  
    57.   
    58.         if( f >= 0.0 && f <= 1.0 )  
    59.         {  
    60.             return ( 2.0 / 3.0 ) + ( 0.5 ) * ( f* f * f ) - (f*f);  
    61.         }  
    62.         else if( f > 1.0 && f <= 2.0 )  
    63.         {  
    64.             return 1.0 / 6.0 * Math.pow( ( 2.0 - f  ), 3.0 );  
    65.         }  
    66.         return 1.0;  
    67.     }  
    68.       
    69.     private double triangleInterpolation( double f )  
    70.     {  
    71.         f = f / 2.0;  
    72.         if( f < 0.0 )  
    73.         {  
    74.             return ( f + 1.0 );  
    75.         }  
    76.         else  
    77.         {  
    78.             return ( 1.0 - f );  
    79.         }  
    80.     }  
    81.       
    82.     private double CatMullRomInterpolation( double f )  
    83.     {  
    84.         if( f < 0.0 )  
    85.         {  
    86.             f = Math.abs(f);  
    87.         }  
    88.         if( f < 1.0 )  
    89.         {  
    90.             return ( ( 12 - 9 * B - 6 * C ) * ( f * f * f ) +  
    91.                 ( -18 + 12 * B + 6 *C ) * ( f * f ) +  
    92.                 ( 6 - 2 * B ) ) / 6.0;  
    93.         }  
    94.         else if( f >= 1.0 && f < 2.0 )  
    95.         {  
    96.             return ( ( -B - 6 * C ) * ( f * f * f )  
    97.                 + ( 6 * B + 30 * C ) * ( f *f ) +  
    98.                 ( - ( 12 * B ) - 48 * C  ) * f +  
    99.                 8 * B + 24 * C)/ 6.0;  
    100.         }  
    101.         else  
    102.         {  
    103.             return 0.0;  
    104.         }  
    105.     }   
    106.   
    107.     @Override  
    108.     public BufferedImage filter(BufferedImage src, BufferedImage dest) {  
    109.         int width = src.getWidth();  
    110.         int height = src.getHeight();  
    111.   
    112.         if (dest == null)  
    113.             dest = createCompatibleDestImage(src, null);  
    114.   
    115.         int[] inPixels = new int[width * height];  
    116.         int[] outPixels = new int[destH * destW];  
    117.         getRGB(src, 0, 0, width, height, inPixels);  
    118.         float rowRatio = ((float) height) / ((float) destH);  
    119.         float colRatio = ((float) width) / ((float) destW);  
    120.         int index = 0;  
    121.         for (int row = 0; row < destH; row++) {  
    122.             int ta = 0, tr = 0, tg = 0, tb = 0;  
    123.             double srcRow = ((float) row) * rowRatio;  
    124.             // 获取整数部分坐标 row Index  
    125.             double j = Math.floor(srcRow);  
    126.             // 获取行的小数部分坐标  
    127.             double t = srcRow - j;  
    128.             for (int col = 0; col < destW; col++) {  
    129.                 double srcCol = ((float) col) * colRatio;  
    130.                 // 获取整数部分坐标 column Index  
    131.                 double k = Math.floor(srcCol);  
    132.                 // 获取列的小数部分坐标  
    133.                 double u = srcCol - k;  
    134.                 double[] rgbData = new double[3];  
    135.                 double rgbCoffeData = 0.0;  
    136.                 for(int m=-1; m<3; m++)  
    137.                 {  
    138.                     for(int n=-1; n<3; n++)  
    139.                     {  
    140.                         int[] rgb = getPixel(j+m, k+n, width, height, inPixels);  
    141.                         double f1 = 0.0d;  
    142.                         double f2 = 0.0d;  
    143.                         if(type == TRIANGLE__INTERPOLATION)  
    144.                         {  
    145.                             f1  = triangleInterpolation( ((double) m ) - t );  
    146.                             f2 = triangleInterpolation ( -(( (double) n ) - u ) );    
    147.                         }  
    148.                         else if(type == BELL__INTERPOLATION)  
    149.                         {  
    150.                             f1  = bellInterpolation( ((double) m ) - t );  
    151.                             f2 = bellInterpolation ( -(( (double) n ) - u ) );    
    152.                         }  
    153.                         else if(type == BSPLINE__INTERPOLATION)  
    154.                         {  
    155.                             f1  = bspLineInterpolation( ((double) m ) - t );  
    156.                             f2 = bspLineInterpolation ( -(( (double) n ) - u ) );     
    157.                         }  
    158.                         else  
    159.                         {  
    160.                             f1  = CatMullRomInterpolation( ((double) m ) - t );  
    161.                             f2 = CatMullRomInterpolation ( -(( (double) n ) - u ) );                              
    162.                         }  
    163.                         // sum of weight  
    164.                         rgbCoffeData += f2*f1;  
    165.                         // sum of the RGB values  
    166.                         rgbData[0] += rgb[0] * f2 * f1;  
    167.                         rgbData[1] += rgb[1] * f2 * f1;  
    168.                         rgbData[2] += rgb[2] * f2 * f1;  
    169.                     }  
    170.                 }  
    171.                 ta = 255;  
    172.                 // get Red/green/blue value for sample pixel  
    173.                 tr = (int) (rgbData[0]/rgbCoffeData);  
    174.                 tg = (int) (rgbData[1]/rgbCoffeData);  
    175.                 tb = (int) (rgbData[2]/rgbCoffeData);  
    176.                 index = row * destW + col;  
    177.                 outPixels[index] = (ta << 24) | (clamp(tr) << 16)  
    178.                         | (clamp(tg) << 8) | clamp(tb);  
    179.             }  
    180.         }  
    181.         setRGB(dest, 0, 0, destW, destH, outPixels);  
    182.         return dest;  
    183.     }  
    184.       
    185.     public int clamp(int value) {  
    186.         return value > 255 ? 255 :  
    187.             (value < 0 ? 0 : value);  
    188.     }  
    189.       
    190.     private int[] getPixel(double j, double k, int width, int height,  
    191.             int[] inPixels) {  
    192.         int row = (int) j;  
    193.         int col = (int) k;  
    194.         if (row >= height) {  
    195.             row = height - 1;  
    196.         }  
    197.         if (row < 0) {  
    198.             row = 0;  
    199.         }  
    200.         if (col < 0) {  
    201.             col = 0;  
    202.         }  
    203.         if (col >= width) {  
    204.             col = width - 1;  
    205.         }  
    206.         int index = row * width + col;  
    207.         int[] rgb = new int[3];  
    208.         rgb[0] = (inPixels[index] >> 16) & 0xff;  
    209.         rgb[1] = (inPixels[index] >> 8) & 0xff;  
    210.         rgb[2] = inPixels[index] & 0xff;  
    211.         return rgb;  
    212.     }  
    213.     public BufferedImage createCompatibleDestImage(  
    214.             BufferedImage src, ColorModel dstCM) {  
    215.         if ( dstCM == null )  
    216.             dstCM = src.getColorModel();  
    217.         return new BufferedImage(dstCM,   
    218.                 dstCM.createCompatibleWritableRaster(destW, destH),   
    219.                 dstCM.isAlphaPremultiplied(), null);  
    220.     }  
    221. }  

    运行效果:原图

    双立方插值放大以后:


    总结:

    基于这里三种方法实现的双立方插值以后图片跟原图像相比,都有一定模糊

    这里时候可以通过后续处理实现图像锐化与对比度提升即可得到Sharpen版本

    当然也可以通过寻找更加合适的R(x)函数来实现双立方卷积插值过程时保留

    图像边缘与对比度。

  • 相关阅读:
    20170926-构建之法:现代软件工程-阅读笔记
    我的swift的ui标签
    内存管理:内存泄漏和空悬指针
    闭包
    泛型,修饰符和异常处理
    类型转换,接口和扩展
    初始化2
    类的继承和初始化1
    枚举与可选值
    swift中的类和结构
  • 原文地址:https://www.cnblogs.com/jukan/p/7056858.html
Copyright © 2011-2022 走看看