AoiMarkMethod.cs
6.8 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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp;
using OpenCvSharp.XFeatures2D;
namespace AOI
{
/// <summary>
/// 从搜索区域中查找Mark区域,对图片进行校准
/// </summary>
public class AoiMarkMethod : AoiMethod
{
/// <summary>
/// 放大RoiPath作为SearchPath
/// </summary>
public float SearchPathZoom = 2.0f;
/// <summary>
/// 放大RoiPath作为SearchPath
/// </summary>
/// <param name="zoom">放大倍率</param>
/// <returns></returns>
public GraphicsPath GetSearchPath()
{
if(RoiPath != null && SearchPathZoom > 0)
{
GraphicsPath SearchPath = new GraphicsPath(RoiPath.PathPoints, RoiPath.PathTypes);
Matrix matrix = new Matrix();
matrix.Scale(SearchPathZoom, SearchPathZoom);
SearchPath.Transform(matrix);
var oldBounds = this.RoiPath.GetBounds();
var newBounds = SearchPath.GetBounds();
var oldCenterX = oldBounds.X + oldBounds.Width / 2;
var oldCenterY = oldBounds.Y + oldBounds.Height / 2;
var newCenterX = newBounds.X + newBounds.Width / 2;
var newCenterY = newBounds.Y + newBounds.Height / 2;
matrix.Reset();
matrix.Translate(oldCenterX - newCenterX, oldCenterY - newCenterY);
SearchPath.Transform(matrix);
return SearchPath;
}
return RoiPath;
}
/// <summary>
/// 根据Mark点校正相机获取的图片
/// </summary>
/// <param name="standardImage">标准图片</param>
/// <param name="imageToCheck">相机获取的图片</param>
/// <returns></returns>
public override ResultBean Check(Image standardImage, Image imageToCheck)
{
ResultBean resultBean = new ResultBean();
resultBean.standardRoiImage = standardImage;
Image resultImage = FixImage(standardImage, imageToCheck);
if(resultImage != null)
{
resultBean.result = true;
}
return resultBean;
}
public Image FixImage(Image standardImage, Image imageToCheck)
{
bool needCut = false;
//标准图中的Mart区域
Image markImage = GetRoiImage(standardImage, RoiPath, needCut);
//搜索区域
var SearchPath = GetSearchPath();
Image searchImage = GetRoiImage(imageToCheck, SearchPath, needCut);
if (markImage != null && searchImage != null)
{
var affine = GetAffineMat(markImage, searchImage);
if (affine != null)
{
var matToCheck = ImageUtil.ToMat(imageToCheck);
var fixedMat = FixImage(affine, matToCheck);
return ImageUtil.ToImage(fixedMat);
}
}
return null;
}
/// <summary>
/// 校准图片
/// </summary>
/// <param name="affineMat"></param>
/// <param name="srcMat"></param>
/// <returns></returns>
private Mat FixImage(Mat affineMat, Mat srcMat)
{
Mat resultMat = new Mat();
Cv2.WarpAffine(srcMat, resultMat, affineMat, srcMat.Size());
return resultMat;
}
/// <summary>
/// 获取映射距阵
/// </summary>
/// <param name="markImage"></param>
/// <param name="srcImage"></param>
/// <returns></returns>
private Mat GetAffineMat(Image markImage, Image srcImage)
{
Mat markMat = ImageUtil.ToMat(new Bitmap(markImage));
Mat originalMat = ImageUtil.ToMat(new Bitmap(srcImage));
Mat srcMat = new Mat();
//灰度图转换
Cv2.CvtColor(markMat, markMat, ColorConversionCodes.RGB2GRAY);
Cv2.CvtColor(originalMat, srcMat, ColorConversionCodes.RGB2GRAY);
//提取特征点
SIFT sift = SIFT.Create(200);
KeyPoint[] markKeyPoints, srcKeyPoints;
MatOfFloat roiDescriptors = new MatOfFloat();
MatOfFloat srcDescriptors = new MatOfFloat();
sift.DetectAndCompute(markMat, null, out markKeyPoints, roiDescriptors);
sift.DetectAndCompute(srcMat, null, out srcKeyPoints, srcDescriptors);
var flannMatcher = new FlannBasedMatcher();
DMatch[] matchePoints = flannMatcher.Match(srcDescriptors, roiDescriptors);
//提取强特征点
double minMatch = 1;
double maxMatch = 0;
for (int i = 0; i < matchePoints.Length; i++)
{
double distance = matchePoints[i].Distance;
//匹配值最大最小值获取
if (distance < minMatch)
{
minMatch = distance;
}
if (distance > maxMatch)
{
maxMatch = distance;
}
}
List<DMatch> goodMatchePoints = new List<DMatch>();
for (int i = 0; i < matchePoints.Length; i++)
{
if (matchePoints[i].Distance < minMatch + (maxMatch - minMatch) / 4)
{
goodMatchePoints.Add(matchePoints[i]);
}
}
//获取排在前N个的最优匹配特征点
int num = goodMatchePoints.Count;
if (num >= 3)
{
num = 3;
}
else
{
//不匹配
return null;
}
List<Point2f> markPoints = new List<Point2f>();
List<Point2f> srcPoints = new List<Point2f>();
goodMatchePoints.Sort((left, right) =>
{
if (left.Distance > right.Distance)
return 1;
else if (left.Distance == right.Distance)
return 0;
else
return -1;
});
//Mat matchMat = new Mat();
//Cv2.DrawMatches(srcMat, srcKeyPoints, markMat, roiKeyPoints, goodMatchePoints.Take(num), matchMat);
//Cv2.ImShow("Match", matchMat);
for (int i = 0; i < num; i++)
{
srcPoints.Add(srcKeyPoints[goodMatchePoints[i].QueryIdx].Pt);
markPoints.Add(markKeyPoints[goodMatchePoints[i].TrainIdx].Pt);
}
//获取图像1到图像2的投影映射矩阵 尺寸为3*3
Mat affineMat = Cv2.GetAffineTransform(srcPoints, markPoints);
return affineMat;
}
}
}