高斯滤波器平滑图像代码

简介:

说明

    以下的所有代码针对BGR像素排列的渲染缓存

灰度化代码

  void GrayScaleProcess(unsigned char*  pBuf, int width, int height)

  {

    for (int i=0; i<height; i++)

    {

      for (int j=0; j<width;j=j+1)

      {

        unsigned char* p = pBuf;

        p = p + i*width*3+j*3;

        unsigned char b = *(p);

        unsigned char g = *(p+1);

        unsigned char r = *(p+2);

        unsigned char gray = 0.3*r+0.59*g+0.11*b;

        *(p) = gray;

        *(p+1) = gray;

        *(p+2) = gray;

      }

    }

  }


高斯滤波代码

  void GaussianFilter (unsigned char* corrupted, unsigned char* smooth, int width, int height)

  {

    int templates[25] = { 

      1, 4, 7, 4, 1, 

      4, 16, 26, 16, 4, 

      7, 26, 41, 26, 7,

      4, 16, 26, 16, 4, 

      1, 4, 7, 4, 1 };


      memcpy ( smooth, corrupted, width*height*3*sizeof(unsigned char) );

      for (int j=2;j<height-2;j++)

      {

        for (int i=6;i<width*3;i=i+3)

        {

          int sum = 0;

          int index = 0;

          for ( int m=j-2; m<j+3; m++)

          {

            for (int n=i-6; n<=i+6; n=n+3)

            {

              sum += corrupted [ m*width*3 + n] * templates[index] ;

              index++;

            }

          }

          sum /= 273;

          if (sum > 255)

            sum = 255;

          smooth [ j*width*3+i ] = sum;

          smooth [ j*width*3+i+1 ] = sum;

          smooth [ j*width*3+i+2 ] = sum;

        }

      }

  }



在AGG中的源代码效果对比

 void GaussianFilter (unsigned char* corrupted, unsigned char* smooth, int width, int height)

  {

    int templates[25] = { 

      1, 4, 7, 4, 1, 

      4, 16, 26, 16, 4, 

      7, 26, 41, 26, 7,

      4, 16, 26, 16, 4, 

      1, 4, 7, 4, 1 };


      memcpy ( smooth, corrupted, width*height*3*sizeof(unsigned char) );

      for (int j=2;j<height-2;j++)

      {

        for (int i=6;i<width*3;i=i+3)

        {

          int sum = 0;

          int index = 0;

          for ( int m=j-2; m<j+3; m++)

          {

            for (int n=i-6; n<=i+6; n=n+3)

            {

              sum += corrupted [ m*width*3 + n] * templates[index] ;

              index++;

            }

          }

          sum /= 273;

          if (sum > 255)

            sum = 255;

          smooth [ j*width*3+i ] = sum;

          smooth [ j*width*3+i+1 ] = sum;

          smooth [ j*width*3+i+2 ] = sum;

        }

      }

  }


  void GrayScaleProcess(unsigned char*  pBuf, int width, int height)

  {

    for (int i=0; i<height; i++)

    {

      for (int j=0; j<width;j=j+1)

      {

        unsigned char* p = pBuf;

        p = p + i*width*3+j*3;

        unsigned char b = *(p);

        unsigned char g = *(p+1);

        unsigned char r = *(p+2);

        unsigned char gray = 0.3*r+0.59*g+0.11*b;

        *(p) = gray;

        *(p+1) = gray;

        *(p+2) = gray;

      }

    }

  }


  void GaussianFilterTest()

  {

    agg::rendering_buffer &rbuf = rbuf_window();

    agg::pixfmt_bgr24 pixf(rbuf);



    typedef agg::renderer_base<agg::pixfmt_bgr24> renderer_base_type;

    renderer_base_type renb(pixf);


    agg::rasterizer_scanline_aa<> ras;

    agg::scanline_u8 sl;

    ren_bas.clear(agg::rgba8(255,255,255));


    agg::ellipse ell(100,100,150,150); 

    agg::pixel_map pm_img;

    if(!pm_img.load_from_bmp("E:/docs/agg2/beauty.bmp")) return;


    agg::rendering_buffer rbuf_img(pm_img.buf(), pm_img.width(), pm_img.height(), -pm_img.stride());


    agg::pixfmt_bgr24 pixf_img(rbuf_img);

    rbuf.copy_from(rbuf_img);

    GrayScaleProcess(rbuf.buf(), rbuf.width(), rbuf.height());

    unsigned char* smooth = (unsigned char*)malloc(3600*1058*3);

    int num = sizeof(smooth);

    memset(smooth, 0x00, 3600*1058*3);

    GaussianFilter(pm_img.buf(), smooth, pm_img.width(), pm_img.height());

    unsigned char* bufPtr = rbuf.buf();

    int smoothPtr=0;

    for (int i=pm_img.height(); i<2*pm_img.height(); i++)

    {

      unsigned char* curHorizenPtr = bufPtr + i*rbuf.width()*3;

      for (int j=0; j<pm_img.width()*3; j++)

      {

        *curHorizenPtr = smooth[smoothPtr];

        curHorizenPtr++;

        smoothPtr++;

      }

    }

    ::free(smooth);

    return;

  }



     本文转自fengyuzaitu 51CTO博客,原文链接:http://blog.51cto.com/fengyuzaitu/1730529,如需转载请自行联系原作者

相关文章
|
数据采集 存储 JavaScript
PHP爬虫技术:利用simple_html_dom库分析汽车之家电动车参数
本文旨在介绍如何利用PHP中的simple_html_dom库结合爬虫代理IP技术来高效采集和分析汽车之家网站的电动车参数。通过实际示例和详细说明,读者将了解如何实现数据分析和爬虫技术的结合应用,从而更好地理解和应用相关技术。
154 0
PHP爬虫技术:利用simple_html_dom库分析汽车之家电动车参数
|
人工智能 大数据 机器人
|
Java Android开发
idea 快键键
debug快键键 F9 resume programe 恢复程序 Alt+F10 show execution point 显示执行断点 F8 Step Over 相当于eclipse的f6 跳到下一步...
756 0
|
7天前
|
人工智能 运维 安全
|
5天前
|
人工智能 异构计算
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
|
6天前
|
机器学习/深度学习 人工智能 自然语言处理
B站开源IndexTTS2,用极致表现力颠覆听觉体验
在语音合成技术不断演进的背景下,早期版本的IndexTTS虽然在多场景应用中展现出良好的表现,但在情感表达的细腻度与时长控制的精准性方面仍存在提升空间。为了解决这些问题,并进一步推动零样本语音合成在实际场景中的落地能力,B站语音团队对模型架构与训练策略进行了深度优化,推出了全新一代语音合成模型——IndexTTS2 。
624 23
|
6天前
|
人工智能 测试技术 API
智能体(AI Agent)搭建全攻略:从概念到实践的终极指南
在人工智能浪潮中,智能体(AI Agent)正成为变革性技术。它们具备自主决策、环境感知、任务执行等能力,广泛应用于日常任务与商业流程。本文详解智能体概念、架构及七步搭建指南,助你打造专属智能体,迎接智能自动化新时代。
|
12天前
|
人工智能 JavaScript 测试技术
Qwen3-Coder入门教程|10分钟搞定安装配置
Qwen3-Coder 挑战赛简介:无论你是编程小白还是办公达人,都能通过本教程快速上手 Qwen-Code CLI,利用 AI 轻松实现代码编写、文档处理等任务。内容涵盖 API 配置、CLI 安装及多种实用案例,助你提升效率,体验智能编码的乐趣。
994 110