• C++模拟OpenGL库——图片处理及纹理系统(三):图片缩放操作:简单插值&二次线性插值


    目录

    简单插值

    二次线性插值


    简单插值

    如图,我们想把一张小图缩放成一张大图,自然的想法就是按照它们的长宽比例进行缩放(zoomX)。

    但是问题也显而易见,在缩放的过程中,小图的像素并不能一一映射到大图的每一个像素中,会导致失真,也就是说大图中的像素与像素之间并不是原图像素的连续。

    开始动手,我们在Image.h中添加方法:

     实现如下:

    通过简单的设置缩放比例来实现对像素的操作

    1. Image* Image::zoomImage(const Image* _image, float _zoomX, float _zoomY)
    2. {
    3. int _width = _image->getWidth() * _zoomX;
    4. int _height = _image->getHeight() * _zoomY;
    5. byte* _data = new byte[_width * _height * sizeof(RGBA)];
    6. Image* _resultImage = nullptr;
    7. for (int i = 0; i < _width; ++i) {
    8. for (int j = 0; j < _height; ++j) {
    9. int _imageX = (float)i / _zoomX;
    10. int _imageY = (float)j / _zoomY;
    11. _imageX = _imageX < _image->getWidth() ? _imageX : (_image->getWidth() - 1);
    12. _imageY = _imageY < _image->getHeight() ? _imageY : (_image->getHeight() - 1);
    13. RGBA _color = _image->getColor(_imageX, _imageY);
    14. memcpy(_data + (j * _width + i) * sizeof(RGBA), &_color, sizeof(RGBA));
    15. }
    16. }
    17. _resultImage = new Image(_width, _height, _data);
    18. delete[]_data;
    19. return _resultImage;
    20. }

    我们把它放大三倍 

    效果如下,非常粗糙。 

    5倍:

    二次线性插值

    刚才简单插值的效果我们也看到了,非常的糊。

    原因在于我们直接进行了整数上的乘除操作,也就意味着要截断浮点数的值。

    那如果我们要考虑这些浮点数具体是多少呢?

    先上图

    总体的原理就是:我们考虑一个点的颜色值时,需要考虑周围四个像素的颜色值,进行一个判断来决定最后的颜色值。

    在图中,我们可以根据分割出来的区域来确定周围四个像素对其目标点的贡献权重值。

    对于(x1,y1)对其目标点的贡献为:disX2 * disY2;

    对于(x2,y1)对其目标点的贡献为:disX1 * disY2;

    对于(x1,y2)对其目标点的贡献为:disX2 * disY1;

    对于(x2,y2)对其目标点的贡献为:disX1 * disY1;

    其实可以看出规律,就是想对应的面积区域所占整体面积(为1)的比值,因为是负相关的。

    同样的,在Image中实现:

    1. Image* Image::zoomImageBilinear(const Image* _image, float _zoomX, float _zoomY)
    2. {
    3. int _width = _image->getWidth() * _zoomX;
    4. int _height = _image->getHeight() * _zoomY;
    5. byte* _data = new byte[_width * _height * sizeof(RGBA)];
    6. Image* _resultImage = nullptr;
    7. float coordX = 0.0, coordY = 0.0;
    8. int x1 = 0, x2 = 0, y1 = 0, y2 = 0;
    9. float disX1 = 0.0, disY1 = 0.0, disX2 = 0.0, disY2 = 0.0;
    10. for (int i = 0; i < _width; ++i) {
    11. //disX1 disX2的计算
    12. coordX = i / _zoomX;
    13. x1 = (int)coordX;
    14. if (x1 >= _image->getWidth() - 1) {
    15. x1 = _image->getWidth() - 1;
    16. x2 = x1;
    17. }
    18. else {
    19. x2 = x1 + 1;
    20. }
    21. disX1 = coordX - x1;
    22. disX2 = 1.0 - disX1;
    23. for (int j = 0; j < _height; ++j) {
    24. //disY1 disY2的计算
    25. coordY = j / _zoomY;
    26. y1 = (int)coordY;
    27. if (y1 >= _image->getHeight() - 1) {
    28. y1 = _image->getHeight() - 1;
    29. y2 = y1;
    30. }
    31. else {
    32. y2 = y1 + 1;
    33. }
    34. disY1 = coordY - y1;
    35. disY2 = 1.0 - disY1;
    36. //取周围四个像素的颜色值
    37. RGBA _color11 = _image->getColor(x1, y1);
    38. RGBA _color21 = _image->getColor(x1, y2);
    39. RGBA _color12 = _image->getColor(x2, y1);
    40. RGBA _color22 = _image->getColor(x2, y2);
    41. RGBA _targetColor;
    42. _targetColor.m_r =
    43. (float)_color11.m_r * disX2 * disY2 +
    44. (float)_color12.m_r * disX2 * disY1 +
    45. (float)_color21.m_r * disX1 * disY2 +
    46. (float)_color22.m_r * disX1 * disX2;
    47. _targetColor.m_g =
    48. (float)_color11.m_g * disX2 * disY2 +
    49. (float)_color12.m_g * disX2 * disY1 +
    50. (float)_color21.m_g * disX1 * disY2 +
    51. (float)_color22.m_g * disX1 * disX2;
    52. _targetColor.m_b =
    53. (float)_color11.m_b * disX2 * disY2 +
    54. (float)_color12.m_b * disX2 * disY1 +
    55. (float)_color21.m_b * disX1 * disY2 +
    56. (float)_color22.m_b * disX1 * disX2;
    57. _targetColor.m_a =
    58. (float)_color11.m_a * disX2 * disY2 +
    59. (float)_color12.m_a * disX2 * disY1 +
    60. (float)_color21.m_a * disX1 * disY2 +
    61. (float)_color22.m_a * disX1 * disX2;
    62. memcpy(_data + (j * _width + i) * sizeof(RGBA), &_targetColor, sizeof(RGBA));
    63. }
    64. }
    65. _resultImage = new Image(_width, _height, _data);
    66. delete[]_data;
    67. return _resultImage;
    68. }

    我们来对比一下两种方法:

     

    下面是简单插值,上面是二次线性插值,明显二次线性插值会更清晰一些,这里由于图片颜色比较特殊,所以看起来有些瑕疵,但原理大致是正确的。

  • 相关阅读:
    Apache Hudi 0.13.0版本重磅发布!
    Vue学习
    PHP接口自动化测试框架实现
    树和森林知识
    Linux防火墙之iptables(上)
    JVM内存结构详解
    基于单片机的智能交通控制系统研究
    不用 Spring 居然连最基本的接口都不会写了!
    乌卡时代下,企业供应链管理体系的应对策略
    int main(int argc,char* argv[]) 的含义和用法
  • 原文地址:https://blog.csdn.net/Jason6620/article/details/127947367