MainForm.cs
6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using Acc.Img;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Acc.Demo
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void OpenImage()
{
using (FileDialog dialog = new OpenFileDialog())
{
dialog.Filter = "图片 (*.raw,*.bmp;*.gif;*.jpeg;*.jpg;*.png)|*.raw;*.bmp;*.dib;*.rle;*.gif;*.jpg;*.png|RAW (*.raw)|*.raw|Bitmaps (*.bmp;*.dib;*.rle)|*.bmp;*.dib;*.rle|Graphics Interchange Format (*.gif)|*.gif|Joint Photographic Experts (*.jpg)|*.jpg|Portable Network Graphics (*.png)|*.png|All Files (*.*)|*.*";
dialog.DefaultExt = "png";
if (dialog.ShowDialog(this) == DialogResult.OK)
{
try
{
string imagePath = dialog.FileName;
labelImageName.Text = imagePath;
Image image = ImageUtil.ReadImage(imagePath);
orginalImage = image;
imageBox.Image = image;
imageBox.ZoomToFit();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void buttonOpen_Click(object sender, EventArgs e)
{
ClearStatusBar();
OpenImage();
}
Image orginalImage = null;
private void buttonCount_Click(object sender, EventArgs e)
{
Count();
}
private void buttonThesh_Click(object sender, EventArgs e)
{
int theshValue = -1;
if (!checkBoxAutoThresh.Checked)
{
theshValue = int.Parse(textBoxThesh.Text);
}
Image result;
result = ImageUtil.Threshhold(orginalImage, theshValue);
//if (theshValue == -1)
//{
// result = ImageUtil.Threshhold(imageBox.Image, theshValue);
//}
//else
//{
// result = ImageUtil.DistanceTransform(imageBox.Image);
//}
imageBox.Image = result;
}
private void checkBoxAutoThresh_CheckedChanged(object sender, EventArgs e)
{
textBoxThesh.Enabled = !checkBoxAutoThresh.Checked;
}
private void 原图ToolStripMenuItem_Click(object sender, EventArgs e)
{
ClearStatusBar();
imageBox.Image = orginalImage;
}
private static int markX = -1;
private static int markY = -1;
private void imageBox_MouseDown(object sender, MouseEventArgs e)
{
base.OnMouseUp(e);
if (e.Button == MouseButtons.Right)
{
bool inImage = imageBox.IsPointInImage(e.Location);
System.Drawing.Point markPoint = imageBox.PointToImage(e.Location);
markX = markPoint.X;
markY = markPoint.Y;
元件特征ToolStripMenuItem.Enabled = inImage;
原图ToolStripMenuItem.Enabled = inImage;
contextMenuStrip.Show(this, e.Location);
}
}
private void Count()
{
ClearStatusBar();
Task.Factory.StartNew(delegate {
Stopwatch sw = new Stopwatch();
sw.Start();
int itemFeature = int.Parse(textBoxFeature.Text);
Image image = orginalImage;
int count = ImageUtil.CountItems(ref image, itemFeature);
sw.Stop();
labelTime.Text = "耗时:" + sw.ElapsedMilliseconds + " ms";
labelCount.Text = "数量:" + count;
imageBox.Image = image;
});
}
private void ClearStatusBar()
{
labelFeature.Text = "";
labelCount.Text = "";
labelTime.Text = "";
}
private void 打开图片ToolStripMenuItem_Click(object sender, EventArgs e)
{
ClearStatusBar();
OpenImage();
}
private void 计数ToolStripMenuItem_Click(object sender, EventArgs e)
{
Count();
}
private void 元件特征ToolStripMenuItem_Click(object sender, EventArgs e)
{
ClearStatusBar();
Image image = orginalImage;
//int feature = ImageUtil.GetFeature(ref image, markX, markY);
int feature = ImageUtil.GetGrayValue(ref image, markX, markY);
textBoxFeature.Text = feature.ToString();
imageBox.Image = image;
//int theshValue = -1;
//if (!checkBoxAutoThresh.Checked)
//{
// theshValue = int.Parse(textBoxThesh.Text);
//}
//Stopwatch sw = new Stopwatch();
//sw.Start();
//Image image = ImageUtil.Mark(orginalImage, markX, markY, theshValue);
//imageBox.Image = image;
//sw.Stop();
//Console.WriteLine("总耗时:" + sw.ElapsedMilliseconds + " ms");
}
private void 保存当前图片ToolStripMenuItem_Click(object sender, EventArgs e)
{
using (FileDialog dialog = new SaveFileDialog())
{
dialog.Filter = "图片 (*.raw,*.bmp;*.gif;*.jpeg;*.jpg;*.png)|*.raw;*.bmp;*.dib;*.rle;*.gif;*.jpg;*.png|RAW (*.raw)|*.raw|Bitmaps (*.bmp;*.dib;*.rle)|*.bmp;*.dib;*.rle|Graphics Interchange Format (*.gif)|*.gif|Joint Photographic Experts (*.jpg)|*.jpg|Portable Network Graphics (*.png)|*.png|All Files (*.*)|*.*";
dialog.DefaultExt = "png";
//保存对话框是否记忆上次打开的目录
dialog.RestoreDirectory = true;
if (dialog.ShowDialog(this) == DialogResult.OK)
{
try
{
string imagePath = dialog.FileName;
imageBox.Image.Save(imagePath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}
}