MainForm.cs
5.1 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
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 = ImageUtil.Threshhold(orginalImage, theshValue);
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 int markX = -1;
private 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();
int theshValue = -1;
if (!checkBoxAutoThresh.Checked)
{
theshValue = int.Parse(textBoxThesh.Text);
}
Task.Factory.StartNew(delegate {
//Image image = ImageUtil.FindCircle(orginalImage, theshValue, inv);
//imageBox.Image = image;
Stopwatch sw = new Stopwatch();
sw.Start();
int itemFeature = int.Parse(textBoxFeature.Text);
Image image = orginalImage;
int count = ImageUtil.CountItems(ref image, itemFeature, theshValue);
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();
int theshValue = -1;
if (!checkBoxAutoThresh.Checked)
{
theshValue = int.Parse(textBoxThesh.Text);
}
int feature = ImageUtil.GetItemFeature(orginalImage, markX, markY, theshValue);
labelFeature.Text = "特征值:" + feature + "";
textBoxFeature.Text = feature + "";
}
}
}