• 图像处理之基础---图像缩放中的双线性插值c实现


    在进入频域变换之前, 我们还是轻松一下,再搞点平面上的变化来看看。这把选了一个双线性插值(Bilinear interpolation)来实现是源于看到了csdn上别人的问题, 权且实现一个函数,方便大家的使用吧。

    双线性插值简单的说,就是扩展了之后的图像像素坐标映射回原来的坐标空间的时候, 如果出现了没有对应到整数点的情况。这时候需要做2次线性的插值计算出新的坐标的像素值,比如说:

    这里可以看到这个P点落在了ABCD区间内, 如果我们本着最朴素的这个P点最靠近谁权重就越大的加权平均的思想, 我们很容易得到这样的论断:

    b3

    A点对P的影响就是Sa的面积, B点的影响就是Sb, C点就Sc, d就是Sd。这样越近就是权重越大,基本上就是这样的逻辑。

    这样P的像素可以简单的用 (A*Sa+B*Sb+C*Sc+D*Sd )/(Sa+Sb+Sc+Sd);来得到了。以我的雷厉风行,马上写出了如下的代码:

    1. /** 
    2. ** method to remove sharp the raw image with unsharp mask 
    3. * @param src input grayscale binary array  
    4. * @param dst output grayscale result, the memory need to be allocated outside of the function 
    5. * @param srcWidth width of the input grayscale image 
    6. * @param srcHeight height of the input grayscale image 
    7. * @param scalePercent, scale percentage (0-xxx) 
    8. */  
    9. void stretchImage (const unsigned char* src, unsigned char* dst, int srcWidth, int srcHeight, int scalePercent)  
    10. {     
    11.     if (scalePercent < 0)  
    12.         return;  
    13.     int x, y;  
    14.     int ox, oy;  
    15.     int tmpx,tmpy;  
    16.     int ratio = (100 << 8)/scalePercent;  
    17.     int dstWidth = srcWidth * scalePercent / 100;  
    18.     int dstHeight = srcHeight * scalePercent / 100;  
    19.     unsigned char color[2][2];  
    20.     for (int j = 0; j < dstHeight; j ++)  
    21.     {  
    22.         for (int i = 0; i < dstWidth; i ++)  
    23.         {  
    24.             tmpx = i * ratio;  
    25.             tmpy = j * ratio;  
    26.             ox = tmpx >> 8;  
    27.             oy = tmpy >> 8;  
    28.             x = tmpx & 0xFF;  
    29.             y = tmpy & 0xFF;  
    30.             color[0][0] = src[ oy*srcWidth + ox ];   
    31.             color[1][0] = src[ oy*srcWidth + ox +1 ];   
    32.             color[0][1] = src[ (oy+1)*srcWidth + ox ];   
    33.             color[1][1] = src[ (oy+1)*srcWidth + ox+1 ];  
    34.             int final = (0x100 - x)*(0x100 - y)*color[0][0] + x*(0x100 - y)*color[1][0] + (0x100-x)*y*color[0][1] + x*y*color[1][1];  
    35.             final = final >> 16;  
    36.             if (final>255)  
    37.                 final = 255;  
    38.             if (final<0)  
    39.                 final = 0;  
    40.             dst [ j*dstWidth + i] = (unsigned char)final;  
    41.         }  
    42.     }  
    43. }  

    需要说明的事情是, 浮点数需要引入效率上一定的损失, 当然我们这里就用大数来和谐。但是只是随便写写的代码, 我们没有加入超出int范围的检查或者说明,暂时也只能这样了:)。用了这个函数的效果还是不错的, 我们来看看在75%,125%和250%时候的效果:

    原图:

    sample

    %75效果图:

    75%

    125%效果图:

    125%

    250%效果图:

    250%

    其实从效果图多少可以看出一些的问题就是, 随着图像的拉伸, 图像的锐度其实降低了, 这个比较容易想象的,因为我们这个拉伸的办法本身就是线性的,无疑来扩大的时候把锐利的边缘模糊化了,所以自然在图像扩大很多倍的时候效果不是很好了。

    http://blog.csdn.net/hhygcy/article/details/4434870#comments

  • 相关阅读:
    bzoj1098 1301
    bzoj3237
    bzoj3170
    bzoj4008
    一些题解
    bzoj4028
    bzoj3196
    redis学习
    quartz学习
    电商618 压测、优化、降级预案
  • 原文地址:https://www.cnblogs.com/pengkunfan/p/4042121.html
Copyright © 2020-2023  润新知