zoukankan
html css js c++ java
(转)数字图象处理
using
System;
using
System.Drawing;
using
System.Collections;
using
System.ComponentModel;
using
System.Windows.Forms;
using
System.Data;
namespace
ShowImage
{
/**/
///
<SUMMARY>
///
Form1 的摘要说明。
///
</SUMMARY>
public
partial
class
main : System.Windows.Forms.Form
{
public
string
CurPath;
private
System.Windows.Forms.Button Open;
private
System.Windows.Forms.PictureBox pictureBox1;
private
System.Windows.Forms.Button btnAntiColor;
private
System.Windows.Forms.Button btnCancel;
private
System.Windows.Forms.Button btnSave;
private
System.Windows.Forms.SaveFileDialog saveFileDialog1;
private
System.Windows.Forms.Button btnSharp;
private
System.Windows.Forms.Button btnNormalShow;
private
System.Windows.Forms.Button btnProfile;
private
System.Windows.Forms.Button btnerzhihua;
private
System.Windows.Forms.Button btnMidfiltrate;
private
System.Windows.Forms.Button btnSplit;
private
System.Windows.Forms.Button btnSoftHandle;
private
System.Windows.Forms.Button button1;
private
System.Windows.Forms.Button button2;
private
System.Windows.Forms.Button button3;
private
System.Windows.Forms.Button btnFuDiao;
private
System.Windows.Forms.Button btnFogHandle;
private
System.Windows.Forms.Label label1;
private
System.Windows.Forms.Label label2;
/**/
///
<SUMMARY>
///
必需的设计器变量。
///
</SUMMARY>
private
System.ComponentModel.Container components
=
null
;
/**/
///
<SUMMARY>
///
清理所有正在使用的资源。
///
</SUMMARY>
protected
override
void
Dispose(
bool
disposing )
{
if
( disposing )
{
if
(components
!=
null
)
{
components.Dispose();
}
}
base
.Dispose( disposing );
}
public
main()
{
InitializeComponent();
}
Windows 窗体设计器生成的代码
#region
Windows 窗体设计器生成的代码
/**/
///
<SUMMARY>
///
设计器支持所需的方法 - 不要使用代码编辑器修改
///
此方法的内容。
///
</SUMMARY>
private
void
InitializeComponent()
{
this
.Open
=
new
System.Windows.Forms.Button();
this
.pictureBox1
=
new
System.Windows.Forms.PictureBox();
this
.btnAntiColor
=
new
System.Windows.Forms.Button();
this
.btnCancel
=
new
System.Windows.Forms.Button();
this
.btnSave
=
new
System.Windows.Forms.Button();
this
.saveFileDialog1
=
new
System.Windows.Forms.SaveFileDialog();
this
.btnSharp
=
new
System.Windows.Forms.Button();
this
.btnNormalShow
=
new
System.Windows.Forms.Button();
this
.btnProfile
=
new
System.Windows.Forms.Button();
this
.btnerzhihua
=
new
System.Windows.Forms.Button();
this
.btnMidfiltrate
=
new
System.Windows.Forms.Button();
this
.btnSplit
=
new
System.Windows.Forms.Button();
this
.btnSoftHandle
=
new
System.Windows.Forms.Button();
this
.button1
=
new
System.Windows.Forms.Button();
this
.button2
=
new
System.Windows.Forms.Button();
this
.button3
=
new
System.Windows.Forms.Button();
this
.btnFuDiao
=
new
System.Windows.Forms.Button();
this
.btnFogHandle
=
new
System.Windows.Forms.Button();
this
.label1
=
new
System.Windows.Forms.Label();
this
.label2
=
new
System.Windows.Forms.Label();
this
.SuspendLayout();
//
//
Open
//
this
.Open.ForeColor
=
System.Drawing.Color.Red;
this
.Open.Location
=
new
System.Drawing.Point(
32
,
496
);
this
.Open.Name
=
"
Open
"
;
this
.Open.Size
=
new
System.Drawing.Size(
112
,
32
);
this
.Open.TabIndex
=
1
;
this
.Open.Text
=
"
打开
"
;
this
.Open.Click
+=
new
System.EventHandler(
this
.Open_Click);
//
//
pictureBox1
//
this
.pictureBox1.Location
=
new
System.Drawing.Point(
16
,
8
);
this
.pictureBox1.Name
=
"
pictureBox1
"
;
this
.pictureBox1.Size
=
new
System.Drawing.Size(
784
,
464
);
this
.pictureBox1.TabIndex
=
0
;
this
.pictureBox1.TabStop
=
false
;
//
//
btnAntiColor
//
this
.btnAntiColor.Location
=
new
System.Drawing.Point(
376
,
592
);
this
.btnAntiColor.Name
=
"
btnAntiColor
"
;
this
.btnAntiColor.Size
=
new
System.Drawing.Size(
80
,
32
);
this
.btnAntiColor.TabIndex
=
2
;
this
.btnAntiColor.Text
=
"
反色处理
"
;
this
.btnAntiColor.Click
+=
new
System.EventHandler(
this
.btnAntiColor_Click);
//
//
btnCancel
//
this
.btnCancel.ForeColor
=
System.Drawing.Color.Red;
this
.btnCancel.Location
=
new
System.Drawing.Point(
568
,
496
);
this
.btnCancel.Name
=
"
btnCancel
"
;
this
.btnCancel.Size
=
new
System.Drawing.Size(
128
,
32
);
this
.btnCancel.TabIndex
=
3
;
this
.btnCancel.Text
=
"
退出
"
;
this
.btnCancel.Click
+=
new
System.EventHandler(
this
.btnCancel_Click);
//
//
btnSave
//
this
.btnSave.ForeColor
=
System.Drawing.Color.Red;
this
.btnSave.Location
=
new
System.Drawing.Point(
208
,
496
);
this
.btnSave.Name
=
"
btnSave
"
;
this
.btnSave.Size
=
new
System.Drawing.Size(
112
,
32
);
this
.btnSave.TabIndex
=
4
;
this
.btnSave.Text
=
"
另存为
"
;
this
.btnSave.Click
+=
new
System.EventHandler(
this
.btnSave_Click);
//
//
btnSharp
//
this
.btnSharp.Location
=
new
System.Drawing.Point(
192
,
592
);
this
.btnSharp.Name
=
"
btnSharp
"
;
this
.btnSharp.Size
=
new
System.Drawing.Size(
75
,
32
);
this
.btnSharp.TabIndex
=
5
;
this
.btnSharp.Text
=
"
锐化处理
"
;
this
.btnSharp.Click
+=
new
System.EventHandler(
this
.btnSharp_Click);
//
//
btnNormalShow
//
this
.btnNormalShow.ForeColor
=
System.Drawing.Color.Red;
this
.btnNormalShow.Location
=
new
System.Drawing.Point(
384
,
496
);
this
.btnNormalShow.Name
=
"
btnNormalShow
"
;
this
.btnNormalShow.Size
=
new
System.Drawing.Size(
112
,
32
);
this
.btnNormalShow.TabIndex
=
6
;
this
.btnNormalShow.Text
=
"
正常显示
"
;
this
.btnNormalShow.Click
+=
new
System.EventHandler(
this
.btnNormalShow_Click);
//
//
btnProfile
//
this
.btnProfile.Location
=
new
System.Drawing.Point(
280
,
592
);
this
.btnProfile.Name
=
"
btnProfile
"
;
this
.btnProfile.Size
=
new
System.Drawing.Size(
80
,
32
);
this
.btnProfile.TabIndex
=
7
;
this
.btnProfile.Text
=
"
轮廓提取
"
;
this
.btnProfile.Click
+=
new
System.EventHandler(
this
.btnProfile_Click);
//
//
btnerzhihua
//
this
.btnerzhihua.Location
=
new
System.Drawing.Point(
472
,
592
);
this
.btnerzhihua.Name
=
"
btnerzhihua
"
;
this
.btnerzhihua.Size
=
new
System.Drawing.Size(
104
,
32
);
this
.btnerzhihua.TabIndex
=
8
;
this
.btnerzhihua.Text
=
"
等灰度片二值化
"
;
this
.btnerzhihua.Click
+=
new
System.EventHandler(
this
.btnerzhihua_Click);
//
//
btnMidfiltrate
//
this
.btnMidfiltrate.Location
=
new
System.Drawing.Point(
16
,
544
);
this
.btnMidfiltrate.Name
=
"
btnMidfiltrate
"
;
this
.btnMidfiltrate.Size
=
new
System.Drawing.Size(
75
,
32
);
this
.btnMidfiltrate.TabIndex
=
10
;
this
.btnMidfiltrate.Text
=
"
中值滤波
"
;
this
.btnMidfiltrate.Click
+=
new
System.EventHandler(
this
.btnMidfiltrate_Click);
//
//
btnSplit
//
this
.btnSplit.Location
=
new
System.Drawing.Point(
104
,
544
);
this
.btnSplit.Name
=
"
btnSplit
"
;
this
.btnSplit.Size
=
new
System.Drawing.Size(
75
,
32
);
this
.btnSplit.TabIndex
=
11
;
this
.btnSplit.Text
=
"
图像分割
"
;
this
.btnSplit.Click
+=
new
System.EventHandler(
this
.btnSplit_Click);
//
//
btnSoftHandle
//
this
.btnSoftHandle.Location
=
new
System.Drawing.Point(
192
,
544
);
this
.btnSoftHandle.Name
=
"
btnSoftHandle
"
;
this
.btnSoftHandle.Size
=
new
System.Drawing.Size(
75
,
32
);
this
.btnSoftHandle.TabIndex
=
12
;
this
.btnSoftHandle.Text
=
"
柔化处理
"
;
this
.btnSoftHandle.Click
+=
new
System.EventHandler(
this
.btnSoftHandle_Click);
//
//
button1
//
this
.button1.Location
=
new
System.Drawing.Point(
280
,
544
);
this
.button1.Name
=
"
button1
"
;
this
.button1.Size
=
new
System.Drawing.Size(
80
,
32
);
this
.button1.TabIndex
=
13
;
this
.button1.Text
=
"
B灰度直方图
"
;
this
.button1.Click
+=
new
System.EventHandler(
this
.button1_Click);
//
//
button2
//
this
.button2.Location
=
new
System.Drawing.Point(
376
,
544
);
this
.button2.Name
=
"
button2
"
;
this
.button2.Size
=
new
System.Drawing.Size(
80
,
32
);
this
.button2.TabIndex
=
14
;
this
.button2.Text
=
"
R灰度直方图
"
;
this
.button2.Click
+=
new
System.EventHandler(
this
.button2_Click);
//
//
button3
//
this
.button3.Location
=
new
System.Drawing.Point(
472
,
544
);
this
.button3.Name
=
"
button3
"
;
this
.button3.Size
=
new
System.Drawing.Size(
104
,
32
);
this
.button3.TabIndex
=
15
;
this
.button3.Text
=
"
G灰度直方图
"
;
this
.button3.Click
+=
new
System.EventHandler(
this
.button3_Click);
//
//
btnFuDiao
//
this
.btnFuDiao.Location
=
new
System.Drawing.Point(
16
,
592
);
this
.btnFuDiao.Name
=
"
btnFuDiao
"
;
this
.btnFuDiao.Size
=
new
System.Drawing.Size(
75
,
32
);
this
.btnFuDiao.TabIndex
=
17
;
this
.btnFuDiao.Text
=
"
浮雕处理
"
;
this
.btnFuDiao.Click
+=
new
System.EventHandler(
this
.btnFuDiao_Click);
//
//
btnFogHandle
//
this
.btnFogHandle.Location
=
new
System.Drawing.Point(
104
,
592
);
this
.btnFogHandle.Name
=
"
btnFogHandle
"
;
this
.btnFogHandle.Size
=
new
System.Drawing.Size(
75
,
32
);
this
.btnFogHandle.TabIndex
=
18
;
this
.btnFogHandle.Text
=
"
雾化处理
"
;
this
.btnFogHandle.Click
+=
new
System.EventHandler(
this
.btnFogHandle_Click);
//
//
label1
//
this
.label1.Location
=
new
System.Drawing.Point(
528
,
0
);
this
.label1.Name
=
"
label1
"
;
this
.label1.Size
=
new
System.Drawing.Size(
24
,
16
);
this
.label1.TabIndex
=
19
;
this
.label1.Text
=
"
256
"
;
//
//
label2
//
this
.label2.Location
=
new
System.Drawing.Point(
0
,
0
);
this
.label2.Name
=
"
label2
"
;
this
.label2.Size
=
new
System.Drawing.Size(
8
,
16
);
this
.label2.TabIndex
=
20
;
this
.label2.Text
=
"
0
"
;
//
//
main
//
this
.AutoScaleBaseSize
=
new
System.Drawing.Size(
6
,
14
);
this
.ClientSize
=
new
System.Drawing.Size(
816
,
646
);
this
.Controls.Add(
this
.label2);
this
.Controls.Add(
this
.label1);
this
.Controls.Add(
this
.btnFogHandle);
this
.Controls.Add(
this
.btnFuDiao);
this
.Controls.Add(
this
.button3);
this
.Controls.Add(
this
.button2);
this
.Controls.Add(
this
.button1);
this
.Controls.Add(
this
.btnSoftHandle);
this
.Controls.Add(
this
.btnSplit);
this
.Controls.Add(
this
.btnMidfiltrate);
this
.Controls.Add(
this
.btnerzhihua);
this
.Controls.Add(
this
.btnProfile);
this
.Controls.Add(
this
.btnNormalShow);
this
.Controls.Add(
this
.btnSharp);
this
.Controls.Add(
this
.btnSave);
this
.Controls.Add(
this
.btnCancel);
this
.Controls.Add(
this
.btnAntiColor);
this
.Controls.Add(
this
.Open);
this
.Controls.Add(
this
.pictureBox1);
this
.Name
=
"
main
"
;
this
.Text
=
"
显示图像
"
;
this
.Load
+=
new
System.EventHandler(
this
.main_Load);
this
.ResumeLayout(
false
);
}
#endregion
public
void
turnEnable()
{
this
.btnAntiColor.Enabled
=
true
;
this
.btnCancel.Enabled
=
true
;
this
.btnerzhihua.Enabled
=
true
;
this
.btnFogHandle.Enabled
=
true
;
this
.btnFuDiao.Enabled
=
true
;
this
.btnMidfiltrate.Enabled
=
true
;
this
.btnNormalShow.Enabled
=
true
;
this
.btnProfile.Enabled
=
true
;
this
.btnSave.Enabled
=
true
;
this
.btnSharp.Enabled
=
true
;
this
.btnSoftHandle.Enabled
=
true
;
this
.btnSplit.Enabled
=
true
;
this
.button1.Enabled
=
true
;
this
.button2.Enabled
=
true
;
this
.button3.Enabled
=
true
;
this
.Open.Enabled
=
true
;
}
public
void
turnUnenable()
{
this
.btnAntiColor.Enabled
=
false
;
this
.btnCancel.Enabled
=
false
;
this
.btnerzhihua.Enabled
=
false
;
this
.btnFogHandle.Enabled
=
false
;
this
.btnFuDiao.Enabled
=
false
;
this
.btnMidfiltrate.Enabled
=
false
;
this
.btnNormalShow.Enabled
=
false
;
this
.btnProfile.Enabled
=
false
;
this
.btnSave.Enabled
=
false
;
this
.btnSharp.Enabled
=
false
;
this
.btnSoftHandle.Enabled
=
false
;
this
.btnSplit.Enabled
=
false
;
this
.button1.Enabled
=
false
;
this
.button2.Enabled
=
false
;
this
.button3.Enabled
=
false
;
this
.Open.Enabled
=
false
;
}
private
void
Open_Click(
object
sender, System.EventArgs e)
//
打开图片
{
this
.label1.Visible
=
false
;
this
.label2.Visible
=
false
;
OpenFileDialog myDlg
=
new
OpenFileDialog();
myDlg.Filter
=
"
所有图像格式(*.bmp,*jpg,*.jpeg,*.gif,*,png)|*.bmp;*jpg;*.jepg;*.gif;*.png
"
;
//
设置文件过滤器
if
(myDlg.ShowDialog()
==
DialogResult.OK)
{
CurPath
=
myDlg.FileName;
this
.pictureBox1.Image
=
Bitmap.FromFile(CurPath);
}
}
private
void
btnAntiColor_Click(
object
sender, System.EventArgs e)
//
反色处理
{
if
(
this
.pictureBox1.Image
!=
null
)
{
this
.turnUnenable();
this
.label1.Visible
=
false
;
this
.label2.Visible
=
false
;
//
以反色方式显示图像
int
Height, Width;
if
(
this
.pictureBox1.Image.Width
<
this
.pictureBox1.Width
||
this
.pictureBox1.Image.Height
<
this
.pictureBox1.Height)
{
Width
=
this
.pictureBox1.Image.Width
-
1
;
//
获取当前图像象素高度
Height
=
this
.pictureBox1.Image.Height
-
1
;
//
获取当前图像象素宽度
}
else
{
Width
=
this
.pictureBox1.Width
-
1
;
Height
=
this
.pictureBox1.Height
-
1
;
}
Bitmap bitmap
=
new
Bitmap(
this
.pictureBox1.Image);
//
以当前的图像大小令创一个空图像
Bitmap MyBitmap
=
(Bitmap)
this
.pictureBox1.Image;
Color pixel;
//
申请一个颜色(用ARGB表示)
for
(
int
x
=
1
; x
<
Width; x
++
)
{
for
(
int
y
=
1
; y
<
Height; y
++
)
{
int
r, g, b;
pixel
=
MyBitmap.GetPixel(x, y);
r
=
255
-
pixel.R;
//
对当前r颜色取反
g
=
255
-
pixel.G;
//
对当前g颜色取反
b
=
255
-
pixel.B;
//
对当前b颜色取反
bitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
//
用取反后的argb值图像创建一个图像
}
this
.pictureBox1.Refresh();
this
.pictureBox1.Image
=
bitmap;
}
this
.turnEnable();
}
}
private
void
btnCancel_Click(
object
sender, System.EventArgs e)
//
退出
{
Application.Exit();
}
private
void
btnSave_Click(
object
sender, System.EventArgs e)
//
保存图片
{
if
(
this
.pictureBox1.Image
!=
null
)
{
this
.label1.Visible
=
false
;
this
.label2.Visible
=
false
;
Bitmap newmap
=
new
Bitmap(
this
.pictureBox1.Image);
this
.saveFileDialog1.Filter
=
"
All Files(*.*)|*.*
"
;
this
.saveFileDialog1.ShowDialog();
if
(
this
.saveFileDialog1.ShowDialog()
==
DialogResult.OK)
this
.pictureBox1.Image.Save(saveFileDialog1.FileName);
}
}
/**/
///
<summary>
///
锐化算法:运用拉普拉斯算子,计算该图像f(i,j)这个点的像素值,再减去f(i+1,j),f(i-1,j),f(i,j+1)和f(i,j-1)的象素值,
///
最后加上4倍f(i,j),即为处理后的图像在这一点的像素值。
///
</summary>
private
void
btnSharp_Click(
object
sender, System.EventArgs e)
//
锐化处理
{
if
(
this
.pictureBox1.Image
!=
null
)
{
this
.turnUnenable();
this
.label1.Visible
=
false
;
this
.label2.Visible
=
false
;
Color c1
=
new
Color();
Color c2
=
new
Color();
Color c3
=
new
Color();
Color c4
=
new
Color();
Color c5
=
new
Color();
int
rr, r1, r2, r3, r4, r5, fxr;
int
WIDTH, HEIGH;
if
(
this
.pictureBox1.Image.Width
<
this
.pictureBox1.Width
||
this
.pictureBox1.Image.Height
<
this
.pictureBox1.Height)
{
WIDTH
=
this
.pictureBox1.Image.Width
-
2
;
HEIGH
=
this
.pictureBox1.Image.Height
-
2
;
}
else
{
WIDTH
=
this
.pictureBox1.Width
-
1
;
HEIGH
=
this
.pictureBox1.Height
-
1
;
}
Bitmap bitmap1
=
new
Bitmap(
this
.pictureBox1.Image);
Bitmap bitmap2
=
new
Bitmap(
this
.pictureBox1.Image);
for
(
int
ii
=
1
; ii
<
WIDTH; ii
++
)
{
for
(
int
jj
=
1
; jj
<
HEIGH; jj
++
)
{
c1
=
bitmap1.GetPixel(ii, jj
-
1
);
c2
=
bitmap1.GetPixel(ii
-
1
, jj);
c3
=
bitmap1.GetPixel(ii, jj);
c4
=
bitmap1.GetPixel(ii
+
1
, jj);
c5
=
bitmap1.GetPixel(ii, jj
+
1
);
r1
=
c1.R;
r2
=
c2.R;
r3
=
c3.R;
r4
=
c4.R;
r5
=
c5.R;
fxr
=
5
*
r3
-
(r4
+
r2
+
r5
+
r1);
rr
=
Math.Abs(fxr);
if
(rr
<
0
) rr
=
0
;
if
(rr
>
255
) rr
=
255
;
Color cc
=
Color.FromArgb(rr, rr, rr);
bitmap2.SetPixel(ii, jj, cc);
}
this
.pictureBox1.Refresh();
this
.pictureBox1.Image
=
bitmap2;
}
this
.turnEnable();
}
}
private
void
btnNormalShow_Click(
object
sender, System.EventArgs e)
//
正常显示
{
this
.label1.Visible
=
false
;
this
.label2.Visible
=
false
;
if
(
this
.pictureBox1.Image
!=
null
)
this
.pictureBox1.Image
=
Bitmap.FromFile(CurPath);
}
/**/
///
<summary>
///
8领域微分拉普拉斯算子算法如下:假设原图像为f(i,j),处理后的图像为g(i,j),则
///
g(i,j) = abs( 8 * f(i,j) - f(i,j-1) - f(i-1,j) - f(i+1,j) - f(i,j+1) - f(i-1,j-1)
///
- f(i-1,j+1) - f(i+1,j-1) - f(i+1,j+1) )
///
</summary>
private
void
btnProfile_Click(
object
sender, System.EventArgs e)
//
提取轮廓,8领域微分拉普拉斯算子
{
if
(
this
.pictureBox1.Image
!=
null
)
{
this
.turnUnenable();
this
.label1.Visible
=
false
;
this
.label2.Visible
=
false
;
Color c1
=
new
Color();
Color c2
=
new
Color();
Color c3
=
new
Color();
Color c4
=
new
Color();
Color c5
=
new
Color();
Color c6
=
new
Color();
Color c7
=
new
Color();
Color c8
=
new
Color();
Color c9
=
new
Color();
int
rr, r1, r2, r3, r4, r5, r6, r7, r8, r9, fxr;
int
WIDTH, HEIGH;
if
(
this
.pictureBox1.Image.Width
<
this
.pictureBox1.Width
||
this
.pictureBox1.Image.Height
<
this
.pictureBox1.Height)
{
WIDTH
=
this
.pictureBox1.Image.Width
-
2
;
HEIGH
=
this
.pictureBox1.Image.Height
-
2
;
}
else
{
WIDTH
=
this
.pictureBox1.Width
-
1
;
HEIGH
=
this
.pictureBox1.Height
-
1
;
}
Bitmap bitmap1
=
new
Bitmap(
this
.pictureBox1.Image);
Bitmap bitmap2
=
new
Bitmap(
this
.pictureBox1.Image);
for
(
int
ii
=
1
; ii
<
WIDTH; ii
++
)
{
for
(
int
jj
=
1
; jj
<
HEIGH; jj
++
)
{
c1
=
bitmap1.GetPixel(ii, jj
-
1
);
c2
=
bitmap1.GetPixel(ii
-
1
, jj);
c3
=
bitmap1.GetPixel(ii, jj);
c4
=
bitmap1.GetPixel(ii
+
1
, jj);
c5
=
bitmap1.GetPixel(ii, jj
+
1
);
c6
=
bitmap1.GetPixel(ii
-
1
, jj
-
1
);
c7
=
bitmap1.GetPixel(ii
-
1
, jj
+
1
);
c8
=
bitmap1.GetPixel(ii
+
1
, jj
-
1
);
c9
=
bitmap1.GetPixel(ii
+
1
, jj
+
1
);
r1
=
c1.R;
r2
=
c2.R;
r3
=
c3.R;
r4
=
c4.R;
r5
=
c5.R;
r6
=
c6.R;
r7
=
c7.R;
r8
=
c8.R;
r9
=
c9.R;
fxr
=
8
*
r3
-
r1
-
r2
-
r4
-
r5
-
r6
-
r7
-
r8
-
r9;
rr
=
Math.Abs(fxr);
if
(rr
<
0
) rr
=
0
;
if
(rr
>
255
) rr
=
255
;
Color cc
=
Color.FromArgb(rr, rr, rr);
bitmap2.SetPixel(ii, jj, cc);
}
this
.pictureBox1.Refresh();
this
.pictureBox1.Image
=
bitmap2;
}
this
.turnEnable();
}
}
/**/
///
<summary>
///
等灰度片二值化算法:将输入图像在某两个等宽的灰度级范围内的所有象素全部置为0(黑),其余
///
灰度级的所有象素全部置为255(白),则生成黑白二值图像。本例中的灰度级范围是50和100
///
</summary>
private
void
btnerzhihua_Click(
object
sender, System.EventArgs e)
//
等灰度片二值化
{
if
(
this
.pictureBox1.Image
!=
null
)
{
this
.turnUnenable();
this
.label1.Visible
=
false
;
this
.label2.Visible
=
false
;
Color c
=
new
Color();
int
WIDTH, HEIGH, rr, lev, wid, x, y, m;
int
[] lut
=
new
int
[
256
];
int
[, ,] pic
=
new
int
[
1000
,
1000
,
3
];
lev
=
50
;
wid
=
500
;
y
=
0
;
if
(
this
.pictureBox1.Image.Width
<
this
.pictureBox1.Width
||
this
.pictureBox1.Image.Height
<
this
.pictureBox1.Height)
{
WIDTH
=
this
.pictureBox1.Image.Width
-
2
;
HEIGH
=
this
.pictureBox1.Image.Height
-
2
;
}
else
{
WIDTH
=
this
.pictureBox1.Width
-
1
;
HEIGH
=
this
.pictureBox1.Height
-
1
;
}
Bitmap bitmap1
=
new
Bitmap(
this
.pictureBox1.Image);
Bitmap bitmap2
=
new
Bitmap(
this
.pictureBox1.Image);
while
(y
<=
256
)
//
循环赋值,使lut在0~50和150~200之间为255,在50~150和200~256之间为0
{
for
(x
=
0
; x
<=
lev
-
1
; x
++
)
{
lut[y]
=
255
;
y
=
y
+
1
;
if
(y
>
255
)
goto
loop;
}
for
(x
=
0
; x
<
wid
-
1
; x
++
)
{
lut[y]
=
0
;
y
=
y
+
1
;
if
(y
>
255
)
goto
loop;
}
}
/**/
/*
分别将一个点的RGB值赋给3个二维数组,为以后判别象素在什么范围内和
生成图像做准备
*/
loop:
for
(
int
i
=
0
; i
<
WIDTH; i
++
)
{
for
(
int
j
=
0
; j
<
HEIGH; j
++
)
{
c
=
bitmap1.GetPixel(i, j);
pic[i, j,
0
]
=
c.R;
pic[i, j,
1
]
=
c.G;
pic[i, j,
2
]
=
c.B;
}
}
for
(
int
i
=
0
; i
<
WIDTH; i
++
)
//
判断象素范围并生成新的图像
{
for
(
int
j
=
0
; j
<
HEIGH; j
++
)
{
m
=
pic[i, j,
2
];
//
这里取B值进行判断,依据其大小决定其范围并将数组中的值赋给象素点
rr
=
lut[m];
Color c1
=
Color.FromArgb(rr, rr, rr);
bitmap2.SetPixel(i, j, c1);
}
this
.pictureBox1.Refresh();
this
.pictureBox1.Image
=
bitmap2;
}
this
.turnEnable();
}
}
/**/
///
<summary>
///
中值滤波算法:设当前图像为f(i,j),处理后的图像为g(i,j),以当前象素f(i,j)为中心切出一个N*N象素组成的图像块,
///
则g(i,j)的像素值取这个图像块灰度值排序序列中的中间值
///
</summary>
private
void
btnMidfiltrate_Click(
object
sender, System.EventArgs e)
//
中值滤波
{
if
(
this
.pictureBox1.Image
!=
null
)
{
this
.turnUnenable();
this
.label1.Visible
=
false
;
this
.label2.Visible
=
false
;
Color c
=
new
Color();
Color cc
=
new
Color();
int
WIDTH, HEIGH, rr, r1, g1, b1, k1, k2, i1, j1, dm, m;
int
[] dt
=
new
int
[
20
];
if
(
this
.pictureBox1.Image.Width
<
this
.pictureBox1.Width
||
this
.pictureBox1.Image.Height
<
this
.pictureBox1.Height)
{
WIDTH
=
this
.pictureBox1.Image.Width
-
1
;
HEIGH
=
this
.pictureBox1.Image.Height
-
1
;
}
else
{
WIDTH
=
this
.pictureBox1.Width
-
1
;
HEIGH
=
this
.pictureBox1.Height
-
1
;
}
Bitmap bitmap1
=
new
Bitmap(
this
.pictureBox1.Image);
Bitmap bitmap2
=
new
Bitmap(
this
.pictureBox1.Image);
for
(
int
i
=
1
; i
<
WIDTH; i
++
)
{
for
(
int
j
=
1
; j
<
HEIGH; j
++
)
{
rr
=
0
; m
=
0
;
for
(k1
=
-
1
; k1
<=
1
; k1
++
)
//
循环找出当前点的周围象素点,这里取此点周围的8个点
{
for
(k2
=
-
1
; k2
<=
1
; k2
++
)
{
c
=
bitmap1.GetPixel(i
+
k1, j
+
k2);
r1
=
c.R;
g1
=
c.G;
b1
=
c.B;
dt[m]
=
r1;
//
将8个点中的每个点的象素值存入数组
m
=
m
+
1
;
}
}
for
(i1
=
m
-
1
; i1
>=
1
; i1
=
i1
-
1
)
//
对数组dt进行冒泡排序
{
for
(j1
=
1
; j1
<=
i1; j1
=
j1
+
1
)
{
if
(dt[j1
-
1
]
>
dt[j1])
{
dm
=
dt[j1];
dt[j1]
=
dt[j1
-
1
];
dt[j1
-
1
]
=
dm;
}
}
}
rr
=
dt[(
int
)(m
/
2
)];
//
取中间值,赋给输出点
Color c1
=
Color.FromArgb(rr, rr, rr);
bitmap2.SetPixel(i, j, c1);
}
this
.pictureBox1.Refresh();
this
.pictureBox1.Image
=
bitmap2;
}
this
.turnEnable();
}
}
/**/
///
<summary>
///
柔化处理算法:设当前图像为f(i,j),处理后的图像为g(i-1,j-1),则g(i-1,j-1)的像素值是f(i,j)周围
///
8个点的象素值分别乘以对应的高斯模板,然后再相加。
///
</summary>
private
void
btnSoftHandle_Click(
object
sender, System.EventArgs e)
//
柔化处理
{
if
(
this
.pictureBox1.Image
!=
null
)
{
this
.turnUnenable();
this
.label1.Visible
=
false
;
this
.label2.Visible
=
false
;
int
HEIGH, WIDTH;
if
(
this
.pictureBox1.Image.Width
<
this
.pictureBox1.Width
||
this
.pictureBox1.Image.Height
<
this
.pictureBox1.Height)
{
WIDTH
=
this
.pictureBox1.Image.Width;
HEIGH
=
this
.pictureBox1.Image.Height;
}
else
{
WIDTH
=
this
.pictureBox1.Width;
HEIGH
=
this
.pictureBox1.Height;
}
Bitmap bitmap
=
new
Bitmap(
this
.pictureBox1.Image);
Bitmap MyBitmap
=
(Bitmap)
this
.pictureBox1.Image;
Color pixel;
//
高斯模板
int
[] Gauss
=
{
1
,
2
,
1
,
2
,
4
,
2
,
1
,
2
,
1
}
;
for
(
int
x
=
1
; x
<
WIDTH
-
1
; x
++
)
{
for
(
int
y
=
1
; y
<
HEIGH
-
1
; y
++
)
{
int
r
=
0
, g
=
0
, b
=
0
;
int
Index
=
0
;
for
(
int
col
=
-
1
; col
<=
1
; col
++
)
for
(
int
row
=
-
1
; row
<=
1
; row
++
)
{
pixel
=
MyBitmap.GetPixel(x
+
row, y
+
col);
r
+=
pixel.R
*
Gauss[Index];
g
+=
pixel.G
*
Gauss[Index];
b
+=
pixel.B
*
Gauss[Index];
Index
++
;
}
r
/=
16
;
g
/=
16
;
b
/=
16
;
//
处理颜色值溢出
r
=
r
>
255
?
255
: r;
r
=
r
<
0
?
0
: r;
g
=
g
>
255
?
255
: g;
g
=
g
<
0
?
0
: g;
b
=
b
>
255
?
255
: b;
b
=
b
<
0
?
0
: b;
bitmap.SetPixel(x
-
1
, y
-
1
, Color.FromArgb(r, g, b));
}
this
.pictureBox1.Refresh();
this
.pictureBox1.Image
=
bitmap;
}
this
.turnEnable();
}
}
/**/
///
<summary>
///
图像分割算法:先求出合适的阈值,再根据这个阈值对图像进行二值化
///
</summary>
private
void
btnSplit_Click(
object
sender, System.EventArgs e)
//
图像分割
{
if
(
this
.pictureBox1.Image
!=
null
)
{
this
.turnUnenable();
this
.label1.Visible
=
false
;
this
.label2.Visible
=
false
;
int
HEIGH, WIDTH, Blue, temp0
=
0
, temp1
=
0
, temp2
=
0
, temp3
=
0
, T2, T1
=
127
;
Color c;
int
[] gray
=
new
int
[
256
];
if
(
this
.pictureBox1.Image.Width
<
this
.pictureBox1.Width
||
this
.pictureBox1.Image.Height
<
this
.pictureBox1.Height)
{
WIDTH
=
this
.pictureBox1.Image.Width
-
1
;
HEIGH
=
this
.pictureBox1.Image.Height
-
1
;
}
else
{
WIDTH
=
this
.pictureBox1.Width
-
1
;
HEIGH
=
this
.pictureBox1.Height
-
1
;
}
Bitmap bitmap1
=
new
Bitmap(
this
.pictureBox1.Image);
Bitmap bitmap2
=
new
Bitmap(
this
.pictureBox1.Image);
for
(
int
i
=
0
; i
<
WIDTH; i
++
)
//
计算此图像的灰度值,这里以B灰度为例
for
(
int
j
=
0
; j
<
HEIGH; j
++
)
{
c
=
bitmap1.GetPixel(i, j);
Blue
=
c.B;
gray[Blue]
++
;
}
while
(
true
)
//
循环算出合适的阈值
{
for
(
int
i
=
0
; i
<
T1
+
1
; i
++
)
{
temp0
+=
gray[i]
*
i;
temp1
+=
gray[i];
}
for
(
int
i
=
T1
+
1
; i
<
256
; i
++
)
{
temp2
+=
gray[i]
*
i;
temp3
+=
gray[i];
}
T2
=
(temp0
/
temp1
+
temp2
/
temp3)
/
2
;
if
(T1
==
T2)
break
;
else
T1
=
T2;
}
for
(
int
i
=
0
; i
<
WIDTH; i
++
)
//
生成处理后的图像
{
for
(
int
j
=
0
; j
<
HEIGH; j
++
)
{
int
B
=
bitmap1.GetPixel(i, j).B;
if
(B
<
T1)
B
=
0
;
else
B
=
255
;
bitmap2.SetPixel(i, j, Color.FromArgb(B, B, B));
}
this
.pictureBox1.Refresh();
this
.pictureBox1.Image
=
bitmap2;
}
this
.turnEnable();
}
}
/**/
///
<summary>
///
直方图算法:计算整个图像的灰度值(例如 G灰度值从0~255分别在当前图像中所占的比例),
///
然后用二维坐标表示出来。
///
</summary>
private
void
button1_Click(
object
sender, System.EventArgs e)
//
B灰度直方图
{
if
(
this
.pictureBox1.Image
!=
null
)
{
this
.turnUnenable();
this
.label1.Visible
=
true
;
this
.label2.Visible
=
true
;
//
System.Random random = new Random();
//
int a = random.Next(255);
int
HEIGH, WIDTH, Blue;
Color c;
float
[] gray
=
new
float
[
256
];
if
(
this
.pictureBox1.Image.Width
<
this
.pictureBox1.Width
||
this
.pictureBox1.Image.Height
<
this
.pictureBox1.Height)
{
WIDTH
=
this
.pictureBox1.Image.Width
-
1
;
HEIGH
=
this
.pictureBox1.Image.Height
-
1
;
}
else
{
WIDTH
=
this
.pictureBox1.Width
-
1
;
HEIGH
=
this
.pictureBox1.Height
-
1
;
}
Bitmap bitmap1
=
new
Bitmap(
this
.pictureBox1.Image);
Bitmap bitmap2
=
new
Bitmap(
512
,
512
);
int
sum
=
WIDTH
*
HEIGH;
for
(
int
i
=
0
; i
<
256
; i
++
)
gray[i]
=
0.0f
;
for
(
int
i
=
0
; i
<
WIDTH; i
++
)
//
计算灰度值,此处以B灰度为例
for
(
int
j
=
0
; j
<
HEIGH; j
++
)
{
c
=
bitmap1.GetPixel(i, j);
Blue
=
c.B;
gray[Blue]
++
;
}
for
(
int
i
=
0
; i
<
512
; i
=
i
+
2
)
{
for
(
int
j
=
0
; j
<
512
; j
++
)
{
if
(i
==
0
)
bitmap2.SetPixel(i, j, Color.FromArgb(
100
,
100
,
100
));
if
(j
<=
((gray[i
/
2
]
/
sum)
*
10000
))
//
求出象素比例,再扩大10000倍
bitmap2.SetPixel(i, j, Color.FromArgb(
0
,
0
,
255
));
}
this
.pictureBox1.Refresh();
this
.pictureBox1.Image
=
bitmap2;
}
this
.turnEnable();
}
}
/**/
///
<summary>
///
直方图算法:计算整个图像的灰度值(例如 G灰度值从0~255分别在当前图像中所占的比例),
///
然后用二维坐标表示出来。
///
</summary>
private
void
button2_Click(
object
sender, System.EventArgs e)
//
R灰度直方图
{
if
(
this
.pictureBox1.Image
!=
null
)
{
this
.turnUnenable();
this
.label1.Visible
=
true
;
this
.label2.Visible
=
true
;
int
HEIGH, WIDTH, Blue;
Color c;
float
[] gray
=
new
float
[
256
];
if
(
this
.pictureBox1.Image.Width
<
this
.pictureBox1.Width
||
this
.pictureBox1.Image.Height
<
this
.pictureBox1.Height)
{
WIDTH
=
this
.pictureBox1.Image.Width
-
1
;
HEIGH
=
this
.pictureBox1.Image.Height
-
1
;
}
else
{
WIDTH
=
this
.pictureBox1.Width
-
1
;
HEIGH
=
this
.pictureBox1.Height
-
1
;
}
int
sum
=
WIDTH
*
HEIGH;
Bitmap bitmap1
=
new
Bitmap(
this
.pictureBox1.Image);
Bitmap bitmap2
=
new
Bitmap(
512
,
512
);
for
(
int
i
=
0
; i
<
256
; i
++
)
gray[i]
=
0.0f
;
for
(
int
i
=
0
; i
<
WIDTH; i
++
)
for
(
int
j
=
0
; j
<
HEIGH; j
++
)
{
c
=
bitmap1.GetPixel(i, j);
Blue
=
c.R;
gray[Blue]
++
;
}
for
(
int
i
=
0
; i
<
512
; i
=
i
+
2
)
{
for
(
int
j
=
0
; j
<
512
; j
++
)
{
if
(i
==
0
)
bitmap2.SetPixel(i, j, Color.FromArgb(
100
,
100
,
100
));
if
(j
<=
((gray[i
/
2
]
/
sum)
*
10000
))
//
求出象素比例,再扩大10000倍
bitmap2.SetPixel(i, j, Color.FromArgb(
255
,
0
,
0
));
}
this
.pictureBox1.Refresh();
this
.pictureBox1.Image
=
bitmap2;
}
this
.turnEnable();
}
}
/**/
///
<summary>
///
直方图算法:计算整个图像的灰度值(例如 G灰度值从0~255分别在当前图像中所占的比例),
///
然后用二维坐标表示出来。
///
</summary>
private
void
button3_Click(
object
sender, System.EventArgs e)
//
G灰度直方图
{
if
(
this
.pictureBox1.Image
!=
null
)
{
this
.turnUnenable();
this
.label1.Visible
=
true
;
this
.label2.Visible
=
true
;
int
HEIGH, WIDTH, Blue;
Color c;
float
[] gray
=
new
float
[
256
];
if
(
this
.pictureBox1.Image.Width
<
this
.pictureBox1.Width
||
this
.pictureBox1.Image.Height
<
this
.pictureBox1.Height)
{
WIDTH
=
this
.pictureBox1.Image.Width
-
1
;
HEIGH
=
this
.pictureBox1.Image.Height
-
1
;
}
else
{
WIDTH
=
this
.pictureBox1.Width
-
1
;
HEIGH
=
this
.pictureBox1.Height
-
1
;
}
Bitmap bitmap1
=
new
Bitmap(
this
.pictureBox1.Image);
Bitmap bitmap2
=
new
Bitmap(
512
,
512
);
int
sum
=
WIDTH
*
HEIGH;
for
(
int
i
=
0
; i
<
WIDTH; i
++
)
for
(
int
j
=
0
; j
<
HEIGH; j
++
)
{
c
=
bitmap1.GetPixel(i, j);
Blue
=
c.G;
gray[Blue]
++
;
}
for
(
int
i
=
0
; i
<
512
; i
=
i
+
2
)
{
for
(
int
j
=
0
; j
<
512
; j
++
)
{
if
(i
==
0
)
bitmap2.SetPixel(i, j, Color.FromArgb(
100
,
100
,
100
));
if
(j
<=
((gray[i
/
2
]
/
sum)
*
10000
))
//
求出象素比例,再扩大10000倍
bitmap2.SetPixel(i, j, Color.FromArgb(
0
,
255
,
0
));
}
this
.pictureBox1.Refresh();
this
.pictureBox1.Image
=
bitmap2;
}
this
.turnEnable();
}
}
/**/
///
<summary>
///
浮雕处理算法: 设当前图像为f(i,j),处理后的图像为g(i,j),则g(i,j)的象素值为f(i,j)的像素
///
值减去f(i+1,j+1)的像素值再加上128,最后取和的绝对值
///
</summary>
private
void
btnFuDiao_Click(
object
sender, System.EventArgs e)
//
浮雕处理
{
if
(
this
.pictureBox1.Image
!=
null
)
{
this
.turnUnenable();
this
.label1.Visible
=
false
;
this
.label2.Visible
=
false
;
int
HEIGH, WIDTH;
if
(
this
.pictureBox1.Image.Width
<
this
.pictureBox1.Width
||
this
.pictureBox1.Image.Height
<
this
.pictureBox1.Height)
{
WIDTH
=
this
.pictureBox1.Image.Width
-
1
;
HEIGH
=
this
.pictureBox1.Image.Height
-
1
;
}
else
{
WIDTH
=
this
.pictureBox1.Width
-
1
;
HEIGH
=
this
.pictureBox1.Height
-
1
;
}
Bitmap bitmap2
=
new
Bitmap(
this
.pictureBox1.Image);
Bitmap bitmap1
=
new
Bitmap(
this
.pictureBox1.Image);
Color pixel1, pixel2;
for
(
int
x
=
0
; x
<
WIDTH; x
++
)
{
for
(
int
y
=
0
; y
<
HEIGH; y
++
)
{
int
r
=
0
, g
=
0
, b
=
0
;
pixel1
=
bitmap1.GetPixel(x, y);
pixel2
=
bitmap1.GetPixel(x
+
1
, y
+
1
);
r
=
Math.Abs(pixel1.R
-
pixel2.R
+
128
);
g
=
Math.Abs(pixel1.G
-
pixel2.G
+
128
);
b
=
Math.Abs(pixel1.B
-
pixel2.B
+
128
);
if
(r
>
255
)
r
=
255
;
if
(r
<
0
)
r
=
0
;
if
(g
>
255
)
g
=
255
;
if
(g
<
0
)
g
=
0
;
if
(b
>
255
)
b
=
255
;
if
(b
<
0
)
b
=
0
;
bitmap2.SetPixel(x, y, Color.FromArgb(r, g, b));
}
this
.pictureBox1.Refresh();
this
.pictureBox1.Image
=
bitmap2;
}
this
.turnEnable();
}
}
/**/
///
<summary>
///
雾化处理算法: 设当前图像为f(i,j),处理后的图像为g(i,j),则g(i,j)的象素值其实是f(i,j)点
///
按照某种规律对应的另外一个点的象素值。在此例中的另外一点,是f(i+random % 19,j+random % 19)
///
其中random是随机数。
///
</summary>
private
void
btnFogHandle_Click(
object
sender, System.EventArgs e)
//
雾化处理
{
if
(
this
.pictureBox1.Image
!=
null
)
{
this
.turnUnenable();
this
.label1.Visible
=
false
;
this
.label2.Visible
=
false
;
int
Height, Width;
if
(
this
.pictureBox1.Image.Width
<
this
.pictureBox1.Width
||
this
.pictureBox1.Image.Height
<
this
.pictureBox1.Height)
{
Width
=
this
.pictureBox1.Image.Width
-
1
;
Height
=
this
.pictureBox1.Image.Height
-
1
;
}
else
{
Width
=
this
.pictureBox1.Width
-
1
;
Height
=
this
.pictureBox1.Height
-
1
;
}
Bitmap bitmap
=
new
Bitmap(
this
.pictureBox1.Image);
Bitmap MyBitmap
=
(Bitmap)
this
.pictureBox1.Image;
Color pixel;
for
(
int
x
=
1
; x
<
Width; x
++
)
{
for
(
int
y
=
1
; y
<
Height; y
++
)
{
System.Random MyRandom
=
new
Random();
int
k
=
MyRandom.Next(
123456
);
//
像素块大小
int
dx
=
x
+
k
%
19
;
int
dy
=
y
+
k
%
19
;
if
(dx
>=
Width)
dx
=
Width;
if
(dy
>=
Height)
dy
=
Height;
pixel
=
MyBitmap.GetPixel(dx, dy);
bitmap.SetPixel(x, y, pixel);
}
this
.pictureBox1.Refresh();
this
.pictureBox1.Image
=
bitmap;
}
this
.turnEnable();
}
}
private
void
main_Load(
object
sender, System.EventArgs e)
{
this
.label1.Visible
=
false
;
this
.label2.Visible
=
false
;
}
}
}
查看全文
相关阅读:
Python之标示符和关键字
Python之变量以及类型
python之注释的分类
Python的环境的搭建
Python之第一个helloworld的代码
Linux_ubuntu-命令系统管理
Linux_ubuntu命令-用户、权限管理
Linux-ubuntu命令-文件、磁盘管理
Linux_Ubuntu命令概述
Linux-Ubuntu文件权限
原文地址:https://www.cnblogs.com/gxh973121/p/798702.html
最新文章
dubbo服务简单搭建
Spring Cloud 初认识
ubuntu中rc.local无效
使用Secure Boot后,导致VMware无法启动虚拟机
ubuntu16.04字体设置
ubuntu内部错误的解决办法
python3的pip管理器pip3
Python在ubuntu下常用开发包名称
github提交一个空目录
为什么在c语言中使用gets函数是危险的
热门文章
vim在vps内的终端内支持molokai
ubuntu内窗口最大最小化
if嵌套
elif
if-else判断语句
比较&关系运算符
if判断语句
保存json数据到本地和读取本地json数据
Python之运算符
Python之输入输出
Copyright © 2011-2022 走看看