博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(原創) 如何對圖片加入Salt and Pepper Noise? (.NET) (C/C++) (C++/CLI) (GDI+) (Image Processing)...
阅读量:7218 次
发布时间:2019-06-29

本文共 1519 字,大约阅读时间需要 5 分钟。

Salt and Pepper的公式如下

None.gif
I(nim
,
 i
,
 j) 
=
 
0
 if uniform(
0
,
 
1
) < salt
None.gifI(nim
,
 i
,
 j) 
=
 
255
 if uniform(
0
,
 
1
) > 
1
 - pepper
None.gifI(nim
,
 i
,
 j) 
=
 I(im
,
 i
,
 j) otherwise
None.gif
None.gifuniform(
0
,
 
1
) : ramdom variable uniformly distributed over
[
0, 1
]
 1
ExpandedBlockStart.gif
ContractedBlock.gif
/**/
/* 
 2InBlock.gif(C) OOMusou 2006 http://oomusou.cnblogs.com
 3InBlock.gif
 4InBlock.gifFilename    : SaltAndPepperNoise.cpp
 5InBlock.gifCompiler    : Visual C++ 8.0 / C++/CLI
 6InBlock.gifDescription : Demo how to process Salt and Pepper Noise
 7InBlock.gifRelease     : 12/20/2006 1.0
 8ExpandedBlockEnd.gif*/
 9
None.gif
10
None.gif#include 
"
stdafx.h
"
11
None.gif#include 
"
stdlib.h
"
12
None.gif
13
None.gif
using
 
namespace
 System::Drawing;
14
None.gif
using
 
namespace
 System::Drawing::Imaging;
15
None.gif
16
None.gif
void
 saltpepperNoise(Bitmap
^
, Bitmap
^
double&
double&
);
17
None.gif
18
ExpandedBlockStart.gifContractedBlock.gif
int
 main() 
dot.gif
{
19InBlock.gif   // Read lena.jpg
20InBlock.gif  Bitmap^ oriImg = gcnew Bitmap("lena.jpg");
21InBlock.gif  // Declare Gaussian image for lena.jpg
22InBlock.gif  Bitmap^ spImg = gcnew Bitmap(oriImg->Width, oriImg->Height);
23InBlock.gif
24InBlock.gif  // Salt-and-Pepper noise with salt rate=5% and pepper rate=5%
25InBlock.gif  double salt = 0.05, pepper = 0.05;
26InBlock.gif  saltpepperNoise(oriImg, spImg, salt, pepper);
27InBlock.gif  spImg->Save("lena_saltpepper.jpg");
28InBlock.gif
29InBlock.gif  return 0;
30InBlock.gif
31ExpandedBlockEnd.gif}
32
None.gif
33
ExpandedBlockStart.gifContractedBlock.gif
void
 saltpepperNoise(Bitmap
^
 oriImg, Bitmap
^
 spImg, 
double&
 salt, 
double&
 pepper) 
dot.gif
{
34ExpandedSubBlockStart.gifContractedSubBlock.gif  for(int y = 0; y != oriImg->Height; ++y) dot.gif{
35ExpandedSubBlockStart.gifContractedSubBlock.gif    for(int x = 0; x != oriImg->Width; ++x) dot.gif{
36InBlock.gif      double val = (double)rand() / RAND_MAX;
37InBlock.gif
38ExpandedSubBlockStart.gifContractedSubBlock.gif      if (val < salt) dot.gif{
39InBlock.gif        spImg->SetPixel(x, y, Color::Black);
40ExpandedSubBlockEnd.gif      }
41ExpandedSubBlockStart.gifContractedSubBlock.gif      else if (val > 1-pepper) dot.gif{
42InBlock.gif        spImg->SetPixel(x, y, Color::White);
43ExpandedSubBlockEnd.gif      }
44ExpandedSubBlockStart.gifContractedSubBlock.gif      else dot.gif{
45InBlock.gif        spImg->SetPixel(x, y, oriImg->GetPixel(x, y));
46ExpandedSubBlockEnd.gif      }
47ExpandedSubBlockEnd.gif    }
48ExpandedSubBlockEnd.gif  }
49ExpandedBlockEnd.gif}

原圖

執行結果

转载地址:http://dnxym.baihongyu.com/

你可能感兴趣的文章
ISCSI完全指南
查看>>
我的友情链接
查看>>
和我一起打造个简单搜索之SpringDataElasticSearch关键词高亮
查看>>
音视频即时通讯开发平台SDK
查看>>
直线与直线相交 直线与线段相交 线段与线段相交
查看>>
awstats日志分析系统部署
查看>>
关于Lync2010与Exchange2010 集成失败的原因分析过程
查看>>
Ext.form.field.HtmlEditor编辑器字段
查看>>
趁年轻川藏线-Happiness only real when shared!
查看>>
Another app is currently holding the yum lock; waiting for it to exit...
查看>>
刘宇凡:浅谈文笔、文采、文案的区别
查看>>
Android广告Banner实现
查看>>
Linux学习之路--MySQL(1)数据库介绍及安装方法【18】---20180117
查看>>
使用brctl工具创建桥接设备
查看>>
我的友情链接
查看>>
切换问题定位
查看>>
【原创】MySQL 存储过程调试工具商业和免费
查看>>
python常用正则表达式
查看>>
[Oracle] 分析AWR报告的方法
查看>>
Dev DateEdit控件格式设置
查看>>