-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
227 lines (189 loc) · 7.9 KB
/
Form1.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.Util;
namespace Kitchen
{
public partial class Form1 : Form
{
public BFCamera cam0;
Image<Gray, byte> red;
Image<Gray, byte> green;
Image<Gray, byte> blue;
Image<Gray, byte>[] grayList;
Image<Bgr, byte> bgrImage;
Image<Bgr, byte> background;
Image<Gray, byte> backBlue;
Image<Gray, byte> backGreen;
Image<Gray, byte> backRed;
bool backgroundSet = false;
bool collectingCCSamples = false;
int nSamples = 0;
int nBatches = 0;
Image<Gray, byte> imgCC;
CCStatsOp[] statsOp;
MCvPoint2D64f[] centroidPoints;
double[,] statArray = new double[200, 9];
public struct CCStatsOp
{
public Rectangle Rectangle;
public int Area;
}
public Form1()
{
InitializeComponent();
}
private void aOpenCamButton_Click(object sender, EventArgs e)
{
cam0 = new BFCamera(this);
cam0.OpenCamera();
aTimer.Enabled = true;
}
private void aCloseCamButton_Click(object sender, EventArgs e)
{
aTimer.Enabled = false;
cam0.CloseCamera();
}
private void timer1_Tick(object sender, EventArgs e)
{
var (frameID, mat) = cam0.GetOneImage();
Mat[] splitMat = mat.Split();
red = splitMat[0].ToImage<Gray, byte>();
green = splitMat[1].ToImage<Gray, byte>();
blue = splitMat[2].ToImage<Gray, byte>();
grayList = new Image<Gray, byte>[] { blue, green, red };
bgrImage = new Image<Bgr, byte>(grayList);
aFrameID.Value = frameID;
imageBox2.Image = blue;
imageBox3.Image = green;
imageBox4.Image = red;
histogramBox1.ClearHistogram();
histogramBox1.GenerateHistograms(bgrImage, 128);
histogramBox1.Refresh();
if (backgroundSet)
{
// thresold and erode image
Image<Gray, byte> temp = bgrImage.AbsDiff(background).Convert<Gray, byte>();
CvInvoke.Threshold(temp, temp, (double)aThreshUpDown.Value, 255, Emgu.CV.CvEnum.ThresholdType.Binary);
temp = temp.Erode((int)aErodeItersUpDown.Value).Dilate((int)aDilateItersUpDown.Value);
Mat imgLabel = new Mat();
Mat stats = new Mat();
Mat centroids = new Mat();
var nLabels = CvInvoke.ConnectedComponentsWithStats(temp, imgLabel, stats, centroids);
imgCC = imgLabel.ToImage<Gray, byte>();
centroidPoints = new MCvPoint2D64f[nLabels];
centroids.CopyTo(centroidPoints);
statsOp = new CCStatsOp[nLabels];
stats.CopyTo(statsOp);
aNumObjectsUpDown.Value = nLabels - 1;
// collect 10 samples from single object (should be largest object in foreground)
if (collectingCCSamples)
{
if (nLabels == 1)
{
Console.WriteLine("No object found");
}
else if (nLabels >= 3)
{
Console.WriteLine("More than one foreground object found");
}
else
{
double area = statsOp[1].Area;
double aspectRatio = (double)statsOp[1].Rectangle.Height / (double)statsOp[1].Rectangle.Width;
double density = (double)area / ((double)statsOp[1].Rectangle.Width * (double)statsOp[1].Rectangle.Height);
var mask = imgCC.InRange(new Gray(1), new Gray(1));
// imageBox7.Image = mask;
MCvScalar meanCC = new MCvScalar();
MCvScalar stdCC = new MCvScalar();
CvInvoke.MeanStdDev(bgrImage, ref meanCC, ref stdCC, mask);
Console.WriteLine("sample: {0}, area: {1}, density: {2}, aspect: {3}",
nSamples.ToString(), area.ToString(), density.ToString(), aspectRatio.ToString());
statArray[nSamples, 0] = area;
statArray[nSamples, 1] = aspectRatio;
statArray[nSamples, 2] = density;
statArray[nSamples, 3] = meanCC.V0;
statArray[nSamples, 4] = meanCC.V1;
statArray[nSamples, 5] = meanCC.V2;
statArray[nSamples, 6] = stdCC.V0;
statArray[nSamples, 7] = stdCC.V1;
statArray[nSamples, 8] = stdCC.V2;
}
nSamples++;
if (nSamples >= 20)
{
nSamples = 0;
collectingCCSamples = false;
int rowLength = statArray.GetLength(0);
int colLength = statArray.GetLength(1);
for (int i = 0; i < rowLength; i++)
{
for (int j = 0; j < colLength; j++)
{
Console.Write(string.Format("{0} ", statArray[i, j]));
}
Console.Write("\n");
}
nBatches++;
if (nBatches >= 10)
{
// put code here
}
}
}
if (nLabels > 1)
{
imageBox6.Image = imgCC * (255 / (nLabels - 1));
}
imageBox5.Image = temp;
//// get contours
//VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
//Mat hier = new Mat();
//CvInvoke.FindContours(temp, contours, hier, Emgu.CV.CvEnum.RetrType.External, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);
//// sort by area of bounding rectangle
//Dictionary<int, double> dict = new Dictionary<int, double>();
//if (contours.Size > 0)
//{
// for (int i = 0; i < contours.Size; i++)
// {
// double area = CvInvoke.ContourArea(contours[i]);
// dict.Add(i, area);
// }
//}
//var item = dict.OrderByDescending(v => v.Value);
//foreach (var it in item)
//{
// int key = int.Parse(it.Key.ToString());
// CvInvoke.DrawContours(bgrImage, contours, key, new MCvScalar(128, 0, 255), 4);
//}
}
imageBox1.Image = bgrImage;
}
private void aGetGackButton_Click(object sender, EventArgs e)
{
var (_, mat) = cam0.GetOneImage();
Mat[] splitMat = mat.Split();
backRed = splitMat[0].ToImage<Gray, byte>();
backGreen = splitMat[1].ToImage<Gray, byte>();
backBlue = splitMat[2].ToImage<Gray, byte>();
grayList = new Image<Gray, byte>[] { blue, green, red };
background = new Image<Bgr, byte>(grayList);
backgroundSet = true;
}
private void aCollectSamplesButton_Click(object sender, EventArgs e)
{
collectingCCSamples = true;
}
}
}