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

你可能感兴趣的文章
exc_bad_instruction(code=EXC_I386_INVOP,subcode=0x0) 错误
查看>>
C#编译器
查看>>
Loj #2529. 「ZJOI2018」胖
查看>>
C# WebForm 屏蔽输入框的验证
查看>>
xpath解析数据
查看>>
chrome的input输入框填充后背景颜色变成黄色 (input:-webkit-autofill 导致的)
查看>>
设置系统自动登录
查看>>
Android内存优化(使用SparseArray和ArrayMap代替HashMap)
查看>>
python编写规范
查看>>
eruka
查看>>
C语言函数中的参数有const的问题
查看>>
20145314郑凯杰《信息安全系统设计基础》第6周学习总结 part A
查看>>
PIE SDK介绍
查看>>
MongoDB起步
查看>>
Android中进度条控件使用
查看>>
JAVA创建对象方法
查看>>
js解决checkbox全选和反选的问题
查看>>
编程之美2014 资格赛题目2 : 大神与三位小伙伴
查看>>
Android Studio代码字体模糊解决方法
查看>>
c语言代写
查看>>