AoiProject.cs
8.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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AOI
{
public class AoiProject
{
private AoiProject()
{
}
public AoiProject(Image theImage, Image OrgImage)
{
this.standardImage = theImage;
this.OrgImage = OrgImage;
}
public AoiMethod BaseROI = new AoiEyemMarkMethod();
/// <summary>
/// 标准的Image
/// </summary>
public Image standardImage { get; set; }
public Image OrgImage { get; set; }
public Eyemlib.EyemImage eyemTemplateImage { get; set; }
/// <summary>
/// 所有的AOI方法
/// </summary>
public Dictionary<string, AoiMethod> methodMap = new Dictionary<string, AoiMethod>();
public List<ResultBean> CheckAll(Image scr, out Image resultImg)
{
// GC.Collect();
Image image =ProcessBaseImage((Bitmap)scr,"auro");
//如果设置了校准方法,先校准图片
//var markMethodMap = methodMap.Where(kv => kv.Value is AoiEyemMarkMethod);
//foreach (var item in markMethodMap)
//{
// ResultBean resultBean = item.Value.Check(standardImage, image);
// if (resultBean.result)
// {
// //校准成功
// image = resultBean.currentRoiImage;
// }
//}
List<ResultBean> resultBeans = new List<ResultBean>();
foreach(var item in methodMap)
{
AoiMethod method = item.Value;
if(method is AoiMarkMethod)
{
//校准图片的,忽略
}
else
{
ResultBean resultBean = method.Check(standardImage, image);
resultBean.roiPath = method.RoiPath;
resultBean.labelKey = item.Key;
resultBeans.Add(resultBean);
}
}
using (Graphics g = Graphics.FromImage(image))
{
foreach (ResultBean resultBean in resultBeans)
{
Pen pen = new Pen(Color.Lime, 5);
if (!resultBean.result)
{
pen = new Pen(Color.Red, 5);
}
if (resultBean.roiPath != null)
{
g.DrawPath(pen, resultBean.roiPath);
}
}
g.Dispose();
}
resultImg = image;
return resultBeans;
}
/// <summary>
/// 保存项目
/// </summary>
/// <param name="filePath"></param>
public void Save(string filePath)
{
Dictionary<string, string> projectMap = new Dictionary<string, string>();
// string base64ImgStr = Base64Util.ToBase64(this.standardImage);
// projectMap.Add("base64Img", base64ImgStr);
//this.standardImage.Save(GetStandardImgPath(filePath), ImageFormat.Bmp);
this.OrgImage.Save(GetStandardImgPath(filePath), ImageFormat.Bmp);
var mapForJson = new Dictionary<string, string>();
foreach(var item in this.methodMap)
{
mapForJson.Add(item.Key, GetItemObj(item.Value));
}
string methodMapJson = JsonUtil.SerializeObject(mapForJson);
projectMap.Add("methodMap", methodMapJson);
projectMap.Add("BaseROI", GetItemObj(BaseROI));
JsonUtil.SerializeObjectToFile(projectMap,filePath,false);
string GetItemObj(AoiMethod itemv) {
JObject obj = JObject.FromObject(itemv);
obj.Add("FullTypeName", itemv.GetType().FullName);
var roiPathData = itemv.GetRoiPathData();
string roiPathDataStr = JsonUtil.SerializeObject(roiPathData);
obj.Add("PathDataStr", roiPathDataStr);
string jsonStr = JsonUtil.SerializeObject(obj);
return jsonStr;
}
}
private static string GetStandardImgPath(string filePath)
{
string imageFilePath = "";
string extension = Path.GetExtension(filePath);
imageFilePath = filePath.Replace( extension, ".bmp");
return imageFilePath;
}
/// <summary>
/// 加载项目
/// </summary>
/// <param name="filePath"></param>
public static AoiProject Load(string filePath, out string msg)
{
Thread.Sleep(1);
GC.Collect();
msg = "";
Thread.Sleep(1);
AoiProject aoiProject = new AoiProject();
try
{
string imageFile = GetStandardImgPath(filePath);
if (!File.Exists(imageFile))
{
msg = "no image ";
return null;
}
aoiProject.OrgImage = Eyemlib.DeepClone( new Bitmap(imageFile));
Dictionary<string, string> projectMap = JsonUtil.DeserializeJsonToObjectFromFile<Dictionary<string, string>>(filePath);
// string base64Img = projectMap["base64Img"];
// aoiProject.standardImage = Base64Util.ToImage(base64Img);
string methodMapJson = projectMap["methodMap"];
var jsonMap = JsonUtil.DeserializeJsonToObject<Dictionary<string, string>>(methodMapJson);
foreach (var item in jsonMap)
{
aoiProject.methodMap.Add(item.Key, GetAOIFromJson(item.Value));
Thread.Sleep(1);
}
aoiProject.BaseROI = GetAOIFromJson(projectMap["BaseROI"]);
aoiProject.standardImage = aoiProject.ProcessBaseImage((Bitmap)aoiProject.OrgImage,"基准图");
return aoiProject;
}
catch (Exception ex)
{
if (aoiProject.standardImage != null)
{
aoiProject.standardImage.Dispose();
}
aoiProject = null;
msg = ex.ToString();
return null;
}
AoiMethod GetAOIFromJson(string jsv)
{
JObject obj = JObject.Parse(jsv);
string fullTypeName = obj.Value<string>("FullTypeName");
Type t = Type.GetType(fullTypeName);
JsonSerializer serializer = new JsonSerializer();
StringReader sr = new StringReader(jsv);
object o = serializer.Deserialize(new JsonTextReader(sr), t);
AoiMethod aoiMethod = (AoiMethod)o;
string PathDataStr = obj.Value<string>("PathDataStr");
PathData pathData = JsonUtil.DeserializeJsonToObject<PathData>(PathDataStr);
if (pathData != null && pathData.Points.Length > 0)
aoiMethod.RoiPath = new GraphicsPath(pathData.Points, pathData.Types);
return aoiMethod;
}
}
public Bitmap ProcessBaseImage(Bitmap orgimage,string name)
{
var markroi = BaseROI;
if (markroi == null || markroi.RoiPath == null || markroi.RoiPath.GetBounds() == RectangleF.Empty)
{
return Eyemlib.DeepClone(orgimage);
}
else
{
var (BaseImg, EyemBaseImg, result) = Eyemlib.ExtractPCB(orgimage, markroi.RoiPath.GetBounds());
if (!result)
{
MessageBox.Show(name+"在框选区域内没有找到PCB");
}
return (Bitmap)BaseImg;
}
}
}
}