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

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

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

双立方插值计算涉及到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; // 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)函数来实现双立方卷积插值过程时保留

图像边缘与对比度。

转载请务必注明

相关文章
|
22天前
|
算法 计算机视觉
图像处理之积分图应用四(基于局部均值的图像二值化算法)
图像处理之积分图应用四(基于局部均值的图像二值化算法)
90 0
|
22天前
|
监控 算法 图计算
图像处理之积分图应用三(基于NCC快速相似度匹配算法)
图像处理之积分图应用三(基于NCC快速相似度匹配算法)
20 0
|
22天前
|
算法 Java 计算机视觉
图像处理之积分图算法
图像处理之积分图算法
14 2
|
22天前
|
资源调度 算法 计算机视觉
图像处理之积分图应用二(快速边缘保留滤波算法)
图像处理之积分图应用二(快速边缘保留滤波算法)
12 0
|
22天前
|
算法 BI 计算机视觉
图像处理之积分图应用一(半径无关的快速模糊算法)
图像处理之积分图应用一(半径无关的快速模糊算法)
13 0
|
3天前
|
机器学习/深度学习 自然语言处理 算法
m基于深度学习的OFDM+QPSK链路信道估计和均衡算法误码率matlab仿真,对比LS,MMSE及LMMSE传统算法
**摘要:** 升级版MATLAB仿真对比了深度学习与LS、MMSE、LMMSE的OFDM信道估计算法,新增自动样本生成、复杂度分析及抗频偏性能评估。深度学习在无线通信中,尤其在OFDM的信道估计问题上展现潜力,解决了传统方法的局限。程序涉及信道估计器设计,深度学习模型通过学习导频信息估计信道响应,适应频域变化。核心代码展示了信号处理流程,包括编码、调制、信道模拟、降噪、信道估计和解调。
23 8
|
5天前
|
算法
基于GA遗传优化的混合发电系统优化配置算法matlab仿真
**摘要:** 该研究利用遗传算法(GA)对混合发电系统进行优化配置,旨在最小化风能、太阳能及电池储能的成本并提升系统性能。MATLAB 2022a用于实现这一算法。仿真结果展示了一系列图表,包括总成本随代数变化、最佳适应度随代数变化,以及不同数据的分布情况,如负荷、风速、太阳辐射、弃电、缺电和电池状态等。此外,代码示例展示了如何运用GA求解,并绘制了发电单元的功率输出和年变化。该系统原理基于GA的自然选择和遗传原理,通过染色体编码、初始种群生成、适应度函数、选择、交叉和变异操作来寻找最优容量配置,以平衡成本、效率和可靠性。
|
6天前
|
机器学习/深度学习 算法
基于鲸鱼优化的knn分类特征选择算法matlab仿真
**基于WOA的KNN特征选择算法摘要** 该研究提出了一种融合鲸鱼优化算法(WOA)与K近邻(KNN)分类器的特征选择方法,旨在提升KNN的分类精度。在MATLAB2022a中实现,WOA负责优化特征子集,通过模拟鲸鱼捕食行为的螺旋式和包围策略搜索最佳特征。KNN则用于评估特征子集的性能。算法流程包括WOA参数初始化、特征二进制编码、适应度函数定义(以分类准确率为基准)、WOA迭代搜索及最优解输出。该方法有效地结合了启发式搜索与机器学习,优化特征选择,提高分类性能。
|
2天前
|
机器学习/深度学习 存储 算法
基于SFLA算法的神经网络优化matlab仿真
**摘要:** 使用MATLAB2022a,基于SFLA算法优化神经网络,降低训练误差。程序创建12个神经元的前馈网络,训练后计算性能。SFLA算法寻找最优权重和偏置,更新网络并展示训练与测试集的预测效果,以及误差对比。SFLA融合蛙跳与遗传算法,通过迭代和局部全局搜索改善网络性能。通过调整算法参数和与其他优化算法结合,可进一步提升模型预测精度。
|
7天前
|
机器学习/深度学习 算法 数据可视化
基于BP神经网络的64QAM解调算法matlab性能仿真
**算法预览图省略** MATLAB 2022A版中,运用BP神经网络进行64QAM解调。64QAM通过6比特映射至64复数符号,提高数据速率。BP网络作为非线性解调器,学习失真信号到比特的映射,对抗信道噪声和多径效应。网络在处理非线性失真和复杂情况时展现高适应性和鲁棒性。核心代码部分未显示。