FrmAoiSetting.cs
11.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using AOI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AccAOI
{
public partial class FrmAoiSetting : Asa.Theme.FlatForm
{
private control.ABaseControl aoiControl = null;
public Image Img = null;
private AoiProject Project = null;
public FrmAoiSetting()
{
InitializeComponent();
this.WindowState = FormWindowState.Maximized;
}
public FrmAoiSetting(string programPath, Image image = null)
{
InitializeComponent();
this.WindowState = FormWindowState.Maximized;
if (!programPath.Equals("") && (image != null))
{
Project = new AoiProject(image);
Project.Load(programPath);
}
ShowPorject();
}
private void FrmAoiSetting_Load(object sender, EventArgs e)
{
comType.ItemAdd("Mark点设置");
comType.ItemAdd("斑点分析");
comType.ItemAdd("颜色抽取");
comType.Text = "Mark点设置";
}
private void btnOpenImage_Click(object sender, EventArgs e)
{
System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
openDialog.Title = "打开本地图片";
openDialog.Filter = "All Supported Images (*.bmp;*.dib;*.rle;*.gif;*.jpg;*.png)|*.bmp;*.dib;*.rle;*.gif;*.jpg;*.png|Bitmaps (*.bmp;*.dib;*.rle)|*.bmp;*.dib;*.rle|Graphics Interchange Format (*.gif)|*.gif|Joint Photographic Experts (*.jpg)|*.jpg|Portable Network Graphics (*.png)|*.png|All Files (*.*)|*.*";
openDialog.DefaultExt = "png";
//openDialog.DefaultExt = "png";
System.Windows.Forms.DialogResult result = openDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.Cancel)
{
return;
}
string fileName = openDialog.FileName;
//读取图片内容
this.Img = (Image)Image.FromFile(fileName).Clone();
imageBox1.Image = Img;
}
private void btnGetCameraImg_Click(object sender, EventArgs e)
{
string camera = cmbCameraList.Text;
if (camera.Equals(""))
{
MessageBox.Show("清先选择相机");
return;
}
}
private void btnOpenPro_Click(object sender, EventArgs e)
{
System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
openDialog.Title = "打开项目";
openDialog.Filter = "(*.data)|*.data|(*.*)|*.*";
//openDialog.DefaultExt = "png";
System.Windows.Forms.DialogResult result = openDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.Cancel)
{
return;
}
string fileName = openDialog.FileName;
Project = new AoiProject(this.Img);
Project.Load(fileName);
ShowPorject();
}
private void btnSavePro_Click(object sender, EventArgs e)
{
if (this.Project == null)
{
MessageBox.Show("没有项目可保存");
return;
}
SaveCurrAoi();
System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog();
saveFileDialog.Title = "保存项目";
saveFileDialog.Filter = "(*.data)|*.data|(*.*)|*.*";
//openDialog.DefaultExt = "png";
System.Windows.Forms.DialogResult result = saveFileDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.Cancel)
{
return;
}
string fileName = saveFileDialog.FileName;
Project.Save(fileName);
MessageBox.Show("保存成功");
}
private void btnNewAoi_Click(object sender, EventArgs e)
{
if (this.Img == null)
{
MessageBox.Show("清先打开图片");
return;
}
if (this.Project == null)
{
Project = new AoiProject(this.Img);
}
if (comType.Text.Equals(""))
{
MessageBox.Show("请选择类型");
comType.Focus();
return;
}
string text = comType.Text;
CloseCurrAoi();
AoiMethod methodInfo = null;
if (text.Equals("Mark点设置"))
{
methodInfo = new AoiMarkMethod();
}
else if (text.Equals("斑点分析"))
{
methodInfo = new AoiBlobMethod();
}
else
{
methodInfo = new AoiMethodRgb();
}
methodInfo.MethodName = Project.methodMap.Values.Count.ToString().PadLeft(2, '0') + "_" + text;
Project.methodMap.Add(methodInfo.MethodName, methodInfo);
ShowAoi(methodInfo);
aoiList.ItemAdd(aoiControl.TitleName);
}
private void ShowAoi(AoiMethod method)
{
if (aoiControl == null)
{
if(method is AoiBlobMethod)
{
aoiControl = new control.AoiBlobControl();
}else if(method is AoiMarkMethod)
{
aoiControl = new control.AioMarkControl();
}else if(method is AoiMethodRgb)
{
aoiControl = new control.AoiRgbControl();
}
}
this.aoiControl.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
aoiControl.FormBorderStyle = FormBorderStyle.None;
aoiControl.StartPosition = FormStartPosition.CenterParent;
aoiControl.Location = new Point(0, 0);
aoiControl.BImageBox=this.imageBox1;
aoiControl.AoiInfo = method;
aoiControl.Size = panAoi.Size;
aoiControl.TitleName = method.MethodName;
aoiControl.TopLevel = false;
aoiControl.Parent = panAoi;
aoiControl.Show();
aoiControl.ShowAoiInfo();
if (method.RoiPath != null)
{
currPath = method.RoiPath;
imageBox1.SelectionRegion = method.RoiPath;
Image threshImage = CutImage(imageBox1.Image, currPath);
cutImage = threshImage;
aoiControl.currPath = currPath;
aoiControl.SetImage( threshImage);
}
}
private void SaveCurrAoi()
{
if (aoiControl != null &&aoiControl.Visible)
{
AoiMethod method = aoiControl.GetAoiInfo();
//更新
if (Project.methodMap.ContainsKey(method.MethodName))
{
Project.methodMap[method.MethodName] = method;
}
}
}
private void CloseCurrAoi()
{
SaveCurrAoi();
if (aoiControl != null)
{
if (aoiControl.Visible)
{
aoiControl.Close();
}
aoiControl.Dispose();
aoiControl = null;
}
}
private void ShowPorject()
{
if (Project != null)
{
aoiList.ItemClear();
imageBox1.Image = Project.standardImage;
imageBox1.SelectionRegion = new GraphicsPath();
if (Project.methodMap.Count > 0)
{
foreach(string key in Project.methodMap.Keys)
{
aoiList.ItemAdd(key);
}
aoiList.Index = 0;
}
}
}
private void aoiList_IndexChanged(object sender, EventArgs e)
{
int index = aoiList.Index;
if (index >= 0 && (Project != null))
{
List<AoiMethod> methods = new List<AoiMethod>(Project.methodMap.Values);
if (index < methods.Count)
{
AoiMethod method = methods[index];
if (aoiControl != null && aoiControl.TitleName.Equals(method.MethodName))
{
return;
}
else
{
CloseCurrAoi();
ShowAoi(method);
}
}
}
}
private Image cutImage = null;
private GraphicsPath currPath = null;
private void imageBox1_SelectionRegionChanged(object sender, EventArgs e)
{
try
{
if (imageBox1.Image == null || this.Img == null || aoiControl == null)
{
return;
}
Matrix translateMatrix = new Matrix();
translateMatrix.Translate(100, 0);
//RectangleF region = imageBox1.SelectionRegion;
//currPath = new GraphicsPath();
//if (aoiControl.AreaType.Equals(1))
//{
// currPath.AddRectangle(region);
//}
//else
//{
// currPath.AddEllipse(region);
//}
currPath = imageBox1.SelectionRegion;
Image threshImage = CutImage(imageBox1.Image, currPath);
cutImage = threshImage;
aoiControl.currPath = currPath;
aoiControl.SetImage( threshImage);
}catch(Exception ex)
{
}
}
private Image CutImage(Image src, GraphicsPath path)
{
var bounds = path.GetBounds();
Bitmap b = new Bitmap(src.Width, src.Height);
using (Graphics g = Graphics.FromImage(b))
{
var br = new TextureBrush(src);
g.FillPath(br, path);
}
if (bounds.Width > 0 && bounds.Height > 0)
{
Bitmap result = new Bitmap((int)bounds.Width, (int)bounds.Height);
var srcRect = bounds;
var dstRect = new RectangleF(0, 0, bounds.Width, bounds.Height);
bounds.Location = System.Drawing.Point.Empty;
using (Graphics g = Graphics.FromImage(result))
{
bounds.Location = System.Drawing.Point.Empty;
g.DrawImage(b, dstRect, srcRect, GraphicsUnit.Pixel);
}
return result;
}
return null;
}
}
}