博客
关于我
强烈建议你试试无所不能的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/

你可能感兴趣的文章
02-一般处理程序基础
查看>>
静态函数和一般函数
查看>>
JavaScript值延迟脚本和异步脚本
查看>>
伪前端笔记
查看>>
linux 安装svn最新版本
查看>>
Java--比较方便的特性归纳,持续更新...
查看>>
机器学习算法学习---模型融合和提升的算法(四)
查看>>
【引用】session.flush与transaction.commit
查看>>
Java新知识系列 八
查看>>
Linq to SQL八大子句
查看>>
所谓的日常 #4 - 廢漢帝陳留踐位 謀董賊孟德獻刀
查看>>
[Head First设计模式]生活中学设计模式——迭代器模式
查看>>
[Angularjs]视图和路由(三)
查看>>
[MySql]explain用法及实践
查看>>
uva-757-贪心
查看>>
ajax分页
查看>>
odbc连接数据库
查看>>
任意元素的focus伪类
查看>>
linux下JDK,tomcat的安装与环境变量配置
查看>>
分类器
查看>>