(原創) 如何對有Noise圖片做Box Filter? (.NET) (C/C++) (C++/CLI) (GDI+) (Image Processing)

 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /* 
 2InBlock.gif(C) OOMusou 2006 http://oomusou.cnblogs.com
 3InBlock.gif
 4InBlock.gifFilename    : BoxFilter.cpp
 5InBlock.gifCompiler    : Visual C++ 8.0 / C++/CLI
 6InBlock.gifDescription : Demo how to use BoxFilter
 7InBlock.gifRelease     : 12/20/2006 1.0
 8ExpandedBlockEnd.gif*/

 9 None.gif#include  " stdafx.h "
10 None.gif#include  < iostream >
11 None.gif
12 None.gif using   namespace  System::Drawing;
13 None.gif using   namespace  System::Drawing::Imaging;
14 None.gif using   namespace  std;
15 None.gif
16 None.gif void  boxFilter(Bitmap ^ , Bitmap ^ int int );
17 None.gif int  colorToInt(Color);
18 None.gif
19 ExpandedBlockStart.gifContractedBlock.gif int  main()  dot.gif {
20InBlock.gif  // Read Gaussian noise image for lena.jpg
21InBlock.gif  Bitmap^ gauImg = gcnew Bitmap("lena_gaussian.jpg");
22InBlock.gif  // Declare Box Filter image for lena.jpg
23InBlock.gif  Bitmap^ bfImg = gcnew Bitmap(gauImg->Width, gauImg->Height);
24InBlock.gif  boxFilter(gauImg, bfImg, 33);
25InBlock.gif  bfImg->Save("lena_gaussian_boxfilter3x3.jpg");
26InBlock.gif
27InBlock.gif  return 0;
28ExpandedBlockEnd.gif}

29 None.gif
30 ExpandedBlockStart.gifContractedBlock.gif void  boxFilter(Bitmap ^  oriImg, Bitmap ^  bfImg,  int  filterW,  int  filterH)  dot.gif {
31InBlock.gif  int adjustX = (filterW % 2? 1 : 0;
32InBlock.gif  int adjustY = (filterH % 2? 1 : 0;
33InBlock.gif  int xBound = (int)(filterW/2);
34InBlock.gif  int yBound = (int)(filterH/2);
35InBlock.gif
36ExpandedSubBlockStart.gifContractedSubBlock.gif  for (int y = 0; y != oriImg->Height; ++y) dot.gif{
37ExpandedSubBlockStart.gifContractedSubBlock.gif    for (int x = 0; x != oriImg->Width; ++x) dot.gif{
38InBlock.gif      int sum = 0, cnt = 0;
39ExpandedSubBlockStart.gifContractedSubBlock.gif      for (int v = -yBound; v != yBound + adjustY; ++v) dot.gif{
40ExpandedSubBlockStart.gifContractedSubBlock.gif        for (int u = -xBound; u != xBound + adjustX; ++u) dot.gif{
41InBlock.gif          // check boundary
42InBlock.gif          if (x + u < 0 || y + v < 0 || x + u >= oriImg->Width || y+>= oriImg->Height)
43InBlock.gif            continue;
44InBlock.gif
45InBlock.gif          sum += colorToInt(oriImg->GetPixel(x + u, y + v));
46InBlock.gif          ++cnt;
47ExpandedSubBlockEnd.gif        }

48ExpandedSubBlockEnd.gif      }

49InBlock.gif
50InBlock.gif      double val = (double)sum / cnt;
51InBlock.gif      bfImg->SetPixel(x, y, Color::FromArgb(val,val,val));
52ExpandedSubBlockEnd.gif    }

53ExpandedSubBlockEnd.gif  }

54ExpandedBlockEnd.gif}

55 None.gif
56 None.gif //  Convert RGB to gray level int
57 ExpandedBlockStart.gifContractedBlock.gif int  colorToInt(Color color)  dot.gif {
58InBlock.gif  return (color.R + color.G + color.B) / 3;
59ExpandedBlockEnd.gif}


原圖


執行結果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值