图像处理之图像快速插值放缩算法

简介: 图像处理之图像快速插值放缩算法

算法思想:

基于双线性算法的分解,分别进行水平与垂直两个方向的放缩,完成对整张图像的放大或

者缩小。基于的数学思想为矩阵的乘法,对一个scale矩阵可以拆分为水平与垂直方向的两

个关联矩阵,具体如下:

1348487435_3142.png

关于什么是双线性插值参加这里:http://blog.csdn.net/jia20003/article/details/6915185

程序解释:

类ScaleFilter完成对图像的快速放大与缩小,接受输入参数为XY方向的放缩比例值。

hscal, vscale的默认值为1.5f即将输入图像在XY放大1.5倍。XY方向的Scale方法参考与运用

了移动窗口的算法。感兴趣可以自己研究,我也是改写一段c语言代码得到。感觉非常的精妙。

程序效果如下:

errno-404.png

Scale Filter的源代码如下:

package com.gloomyfish.filter.study;
/**
 * @author gloomyfish
 * @date 2012-09-23
 * @BLOGPAGE:http://blog.csdn.net/jia20003
 */
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
 
public class ScaleFilter extends AbstractBufferedImageOp {
  
  /**
   * default will zoom in 2.0 * input size of original image.
   */
  private float hscale = 1.5f;
  private float vscale = 1.5f;
  public ScaleFilter() {
    
  }
  
  public void setHscale(float hscale) {
    this.hscale = hscale;
  }
 
  public void setVscale(float vscale) {
    this.vscale = vscale;
  }
 
  @Override
  public BufferedImage filter(BufferedImage src, BufferedImage dest) {
    int width = src.getWidth();
        int height = src.getHeight();
 
        if ( dest == null )
          dest = createCompatibleDestImage( src, null );
 
        // initialization pixel data
        int[] inPixels = new int[width*height];
        int outwidth = (int)(hscale * (float)width);
        int outheight = (int)(vscale * (float)height);
        int[] outhPixels = new int[outwidth*height];
        int[] outPixels = new int[outwidth*outheight];
 
        // start to zoom in/out here
        getRGB( src, 0, 0, width, height, inPixels );
        hscale(inPixels, outhPixels, width, height);
        vscale(outhPixels, outPixels, outwidth, height);
 
        // create buffered image and return it with result image data 
        setRGB( dest, 0, 0, outwidth, outheight, outPixels);
        return dest;
  }
  
    public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel dstCM) {
        if ( dstCM == null )
            dstCM = src.getColorModel();
        int outwidth = (int)(hscale * (float)src.getWidth());
        int outheight = (int)(vscale * (float)src.getHeight());
        return new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(outwidth, outheight), dstCM.isAlphaPremultiplied(), null);
    }
  
  private void hscale(int[] input, int[] output, int width, int height) {
    int ta1 = 0, tr1 = 0, tg1 = 0, tb1 = 0;
    int ta2 = 0, tr2 = 0, tg2 = 0, tb2 = 0;
    int sumred = 0, sumgreen = 0, sumblue = 0;
    double accred = 0, accgreen = 0, accblue = 0;
    int p, q;
    int outwidth = (int)(this.hscale * width);
    double area = (outwidth * width);
    int inCol = 0, outCol = 0;
    int inIndex1 = 0, inIndex2 = 0, outIndex = 0;
    for (int row = 0; row < height; row++) {
      q = width;
      p = outwidth;
      accred = accgreen = accblue = 0;
      inCol = outCol = 0;
      while (outCol < outwidth) {
        if(outCol == 299) {
          System.out.println("what are you doing...");
        }
        if ((inCol + 1) < 2) {
          inIndex1 = row * width + inCol;
          inIndex2 = row * width + (inCol + 1);
              ta1 = (input[inIndex1] >> 24) & 0xff;
                  tr1 = (input[inIndex1] >> 16) & 0xff;
                  tg1 = (input[inIndex1] >> 8) & 0xff;
                  tb1 = input[inIndex1] & 0xff;
                  
              ta2 = (input[inIndex2] >> 24) & 0xff;
                  tr2 = (input[inIndex2] >> 16) & 0xff;
                  tg2 = (input[inIndex2] >> 8) & 0xff;
                  tb2 = input[inIndex2] & 0xff;
                  sumred = p * tr1 + (outwidth - p) * tr2;
                  sumgreen = p * tg1 + (outwidth - p) * tg2;
                  sumblue = p * tb1 + (outwidth - p) * tb2;
        }
        else 
        {
          inIndex1 = row * width + inCol;
              ta1 = (input[inIndex1] >> 24) & 0xff;
                  tr1 = (input[inIndex1] >> 16) & 0xff;
                  tg1 = (input[inIndex1] >> 8) & 0xff;
                  tb1 = input[inIndex1] & 0xff;
                  sumred = outwidth * tr1;
                  sumgreen = outwidth * tg1;
                  sumblue = outwidth * tb1;
        }
        if (p < q) {
          accred += sumred * p;
          accgreen += sumgreen * p;
          accblue += sumblue * p;
          q -= p;
          p = outwidth;
          inCol++;
        } else {
          accred += sumred * q;
          accgreen += sumgreen * q;
          accblue += sumblue * q;
          outIndex = row * outwidth + outCol;
          output[outIndex] = ta1 << 24 | ((int)(accred / area) << 16) | ((int)(accgreen / area) << 8) | (int)(accblue / area);
          accred = accgreen = accblue = 0;
          p -= q;
          q = width;
          outCol++;
        }
      }
    }
  }
 
  private void vscale(int[] input, int[] output, int width, int height) {
    int ta1 = 0, tr1 = 0, tg1 = 0, tb1 = 0;
    int ta2 = 0, tr2 = 0, tg2 = 0, tb2 = 0;
    int sumred = 0, sumgreen = 0, sumblue = 0;
    double accred = 0, accgreen = 0, accblue = 0;
    int inRow = 0, outRow = 0;
    int inIndex1 = 0, inIndex2 = 0, outIndex = 0;
    int p, q;
    int ih = height;
    int oh = (int)(height * vscale);
    int area = (ih * oh);
    for (int col = 0; col < width; col++) {
      q = ih;
      p = oh;
      accred = accgreen = accblue = 0;
      inRow = outRow = 0;
      while (outRow < oh) {
        if (inRow+1 < ih) {
          inIndex1 = inRow * width + col;
          inIndex2 = (inRow+1) * width + col;
              ta1 = (input[inIndex1] >> 24) & 0xff;
                  tr1 = (input[inIndex1] >> 16) & 0xff;
                  tg1 = (input[inIndex1] >> 8) & 0xff;
                  tb1 = input[inIndex1] & 0xff;
                  
              ta2 = (input[inIndex2] >> 24) & 0xff;
                  tr2 = (input[inIndex2] >> 16) & 0xff;
                  tg2 = (input[inIndex2] >> 8) & 0xff;
                  tb2 = input[inIndex2] & 0xff;
                  sumred = p * tr1 + (oh - p) * tr2;
                  sumgreen = p * tg1 + (oh - p) * tg2;
                  sumblue = p * tb1 + (oh - p) * tb2;
        }
        else
        {
          inIndex1 = inRow * width + col;
              ta1 = (input[inIndex1] >> 24) & 0xff;
                  tr1 = (input[inIndex1] >> 16) & 0xff;
                  tg1 = (input[inIndex1] >> 8) & 0xff;
                  tb1 = input[inIndex1] & 0xff;
                  sumred = oh * tr1;
                  sumgreen = oh * tg1;
                  sumblue = oh * tb1;
        }
        if (p < q) {
          accred += sumred * p;
          accgreen += sumgreen * p;
          accblue += sumblue * p;
          q -= p;
          p = oh;
          inRow++;
        } else {
          accred += sumred * q;
          accgreen += sumgreen * q;
          accblue += sumblue * q;
          outIndex = outRow * width + col;
          
          output[outIndex] = ta1 << 24 | ((int)(accred / area) << 16) | ((int)(accgreen / area) << 8) | (int)(accblue / area);
          accred = accgreen = accblue = 0;
          p -= q;
          q = ih;
          outRow++;
        }
      }
    }   
  }
}

后注:其效果近似与双线性内插值算法,但是运行速度却是它的几十倍之多,感兴趣者

可以自己测试。证明我没有信口开河。


相关文章
|
机器学习/深度学习 算法 机器人
【水下图像增强融合算法】基于融合的水下图像与视频增强研究(Matlab代码实现)
【水下图像增强融合算法】基于融合的水下图像与视频增强研究(Matlab代码实现)
1194 0
|
编解码 算法
改进SIFT算法实现光学图像和SAR图像配准
改进SIFT算法实现光学图像和SAR图像配准
|
机器学习/深度学习 算法 自动驾驶
基于导向滤波的暗通道去雾算法在灰度与彩色图像可见度复原中的研究(Matlab代码实现)
基于导向滤波的暗通道去雾算法在灰度与彩色图像可见度复原中的研究(Matlab代码实现)
846 8
|
canal 算法 vr&ar
【图像处理】基于电磁学优化算法的多阈值分割算法研究(Matlab代码实现)
【图像处理】基于电磁学优化算法的多阈值分割算法研究(Matlab代码实现)
358 1
|
存储 监控 算法
基于文化优化算法图像量化(Matlab代码实现)
基于文化优化算法图像量化(Matlab代码实现)
647 1
|
存储 算法 生物认证
基于Zhang-Suen算法的图像细化处理FPGA实现,包含testbench和matlab验证程序
本项目基于Zhang-Suen算法实现图像细化处理,支持FPGA与MATLAB双平台验证。通过对比,FPGA细化效果与MATLAB一致,可有效减少图像数据量,便于后续识别与矢量化处理。算法适用于字符识别、指纹识别等领域,配套完整仿真代码及操作说明。
|
机器学习/深度学习 监控 算法
基于单尺度Retinex和多尺度Retinex的图像增强算法实现
基于单尺度Retinex(SSR)和多尺度Retinex(MSR)的图像增强算法实现
1603 1
|
机器学习/深度学习 监控 并行计算
【图像增强】局部对比度增强的CLAHE算法直方图增强研究(Matlab代码实现)
【图像增强】局部对比度增强的CLAHE算法直方图增强研究(Matlab代码实现)
1569 0
|
监控 算法 决策智能
基于盲源分离与贝叶斯非局部均值的图像降噪算法
基于盲源分离与贝叶斯非局部均值的图像降噪算法
528 0
|
数据采集 分布式计算 并行计算
mRMR算法实现特征选择-MATLAB
mRMR算法实现特征选择-MATLAB
651 2

热门文章

最新文章