BlockTester.cs
2.7 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
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace wafer_die_counter
{
internal class BlockTester
{
internal static List<KeyPoint> FindBlock(Mat src)
{
// 创建一个Mat对象
Mat mat = new Mat(src.Size(), MatType.CV_8UC1, new Scalar(255));
List<List<Point>> k = new List<List<Point>>();
// 遍历Mat对象的元素
for (int row = 0; row < mat.Rows; row++)
{
for (int col = 0; col < mat.Cols; col++)
{
// 使用Get方法获取元素值
byte value = mat.Get<byte>(row, col);
byte value2 = src.Get<byte>(row, col);
//Console.WriteLine($"{row},{col} = {value2}");
if (value == 255 && value2==0)
{
Cv2.FloodFill(src, new Point(col,row), new Scalar(125), out Rect rect, Scalar.All(0), Scalar.All(0), FloodFillFlags.Link8);
if (rect.Width == 746)
continue;
List<Point> selectedPixels = new List<Point>();
for (int y = 0; y < src[rect].Rows; y++)
{
for (int x = 0; x < src[rect].Cols; x++)
{
if (src[rect].Get<byte>(y, x) == 125)
{
mat.Set(y+rect.Y,x + rect.X, 125);
selectedPixels.Add(new Point(x + rect.X, y + rect.Y));
}
}
}
k.Add(selectedPixels);
}
}
}
int count = 0;
for (int i = 0; i < k.Count; i++)
{
if (k[i].Count > 10)
{
count = count + 3;
}
if (k[i].Count > 8)
{
count = count + 2;
}
//else if (k[i].Count > 7)
// count = count + 2;
//else if (k[i].Count > 6)
// count = count + 2;
else
count++;
}
// 显示结果图像
Cv2.ImShow("Magic Wand Result", mat);
Cv2.ImWrite("d:\\111.png",mat);
Cv2.ImWrite("d:\\222.png",src);
Cv2.WaitKey(1000000);
return new List<KeyPoint>();
}
}
}