AoiBlobMethod.cs
4.4 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
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.Blob;
using OpenCvSharp.XFeatures2D;
namespace AOI
{
/// <summary>
/// 斑点分析
/// </summary>
public class AoiBlobMethod : AoiMethod
{
/// <summary>
/// 二值化时的阈值, 小于0表示自动计算阈值,绝对值为用户设定的阈值,大于0表示按用户设置的阈值进行二值化
/// </summary>
public int thresh = -1;
/// <summary>
/// 统计Blob时,使用黑色的或白色的进行统计
/// </summary>
public bool whiteOnBlack = false;
/// <summary>
/// 过滤Blob时的最少像素数
/// </summary>
public int minArea = 0;
/// <summary>
/// 过滤Blob时的最多像素数,小于0表示不限制
/// </summary>
public int maxArea = -1;
/// <summary>
/// 经过过滤后的Blob的 最小数量
/// </summary>
public int minNum = 0;
/// <summary>
/// 经过过滤后的Blob的 最大数量, 小于0表示不限制
/// </summary>
public int maxNum = -1;
public override ResultBean Check(Image standardImage, Image imageToCheck)
{
ResultBean resultBean = new ResultBean(MethodName,2,minNum,maxNum);
bool needCut = true;
Image standardRoiImg = GetRoiImage(standardImage, needCut);
resultBean.standardRoiImage = standardRoiImg;
Image cutImg;
int num = GetBlobNum(imageToCheck, out cutImg, out Image dstImg, out List<CvBlob> blobList);
resultBean.currentRoiImage = cutImg;
bool result = false;
if (num >= minNum)
{
if(maxNum <= 0)
{
result = true;
}
else if(num <= maxNum)
{
result = true;
}
}
resultBean.percentValue = num;
// Console.WriteLine(MethodName + "共获取到【" + blobList.Count + "】条CvBlob");
resultBean.checkData =(object) blobList;
resultBean.result = result;
return resultBean;
}
/// <summary>
/// 对源图像进行二值化,找出所有Blob后,根据minArea和maxArea过滤Blob后的数量
/// </summary>
/// <param name="img">相机获取的大图</param>
/// <param name="cutImg">从大图中切割后的原图</param>
/// <param name="dstCutImg">切割并处理后的图片</param>
/// <param name="blobList">所有的Blob</param>
/// <returns>过滤后的Blob数量</returns>
public int GetBlobNum(Image srcImg, out Image cutImg, out Image dstCutImg, out List<CvBlob> blobList)
{
bool needCut = true;
cutImg = GetRoiImage(srcImg, needCut);
if (cutImg == null)
{
dstCutImg = null;
blobList = new List<CvBlob>();
return 0;
}
Mat srcMat = ImageUtil.ToMat(cutImg);
Mat threshMat = new Mat();
Cv2.CvtColor(srcMat, threshMat, ColorConversionCodes.RGB2GRAY);
ThresholdTypes threshType = ThresholdTypes.Binary;
if (whiteOnBlack)
{
threshType = ThresholdTypes.BinaryInv;
}
if (thresh < 0)
{
threshType = threshType | ThresholdTypes.Otsu;
}
Cv2.Threshold(threshMat, threshMat, thresh, 255, threshType);
CvBlobs blobs = new CvBlobs();
blobs.Label(threshMat);
blobList = blobs.Values.ToList();
dstCutImg = ImageUtil.ToImage(threshMat);
List<CvBlob> resultBlobs = blobList.Where(b => {
if (b.Area >= minArea)
{
if (maxArea <= 0 )
{
return true;
}
else if (b.Area <= maxArea)
{
return true;
}
}
return false;
}).ToList();
blobList = resultBlobs;
return resultBlobs.Count;
}
}
}