-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cs
194 lines (175 loc) · 5.56 KB
/
Main.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers;
using System.Threading;
using Emgu.CV;
using Emgu.CV.Structure;
namespace SSD2
{
public partial class Main : Form
{
// variables (replaces a settings class)
double sweeptime = 1.0;
double gaptime = 0.1;
int xRes = 80;
int yRes = 80;
// Used internally
private double colTicks; // Ticks per column (sweeptime/xRes)
private int col = -1; // Column. From 0 to xRes. -1 is flag for "stopped"
private double startTime; // Start time (in ticks) of the cycle
private System.Timers.Timer timer; // Timer object
private bool stopflag = false;
// Image stores
protected internal Image<Bgr, Byte> sourceImage;
protected internal Image<Bgr, Byte> finalImage;
// Classes
WebCam cam;
Output output;
// Diagnostics
int colsProcessed = 0;
//public Util.RRQueue colsPerStep;
//public Util.RRQueue secsPerCycle;
public int cycles = 0;
// Constructor
public Main()
{
// set up requisite classes
cam = new WebCam();
cam.Init();
output = new Output(yRes);
// Set up the variables
colTicks = CalcColTicks();
// Set the timer to fire like crazy (1ms)
timer = new System.Timers.Timer(1);
timer.AutoReset = true;
timer.Elapsed += new ElapsedEventHandler(nextCol);
InitializeComponent();
}
private int CalcColTicks()
{
return (int)Math.Round(sweeptime * 10000000 / xRes);
}
private void nextCol(object sender, ElapsedEventArgs e)
{
if (!stopflag)
{
stopflag = true;
int colDiff = 0;
double elapsed = DateTime.Now.Ticks - startTime;
int _col = (int)Math.Floor(elapsed / colTicks);
if (_col >= xRes)
{
newCycle();
col = 0;
colDiff = _col - xRes;
}
else
{
colDiff = _col - col;
col = _col;
}
//statusText.Text = cycles.ToString() + ":" + col.ToString();
if (colDiff > 0)
{
processCol(col);
}
stopflag = false;
}
}
private void newCycle()
{
if (gaptime > 0)
{
timer.Enabled = false;
output.Pause();
Thread.Sleep((int)(gaptime * 1000));
}
double efficiency = (100 * colsProcessed / xRes);
Console.WriteLine("{0} / {1} = {2}%", colsProcessed.ToString(), xRes, efficiency.ToString("0"));
UpdateStatus("Running (" + efficiency.ToString() + "% efficiency)");
sourceImage = cam.grab();
Image<Bgr,Byte> lowRes = ImagePrep.ResizeImage(sourceImage, xRes, yRes);
finalImage = ImagePrep.GreyscaleImage(lowRes);
this.imageBox.Image = finalImage;
startTime = DateTime.Now.Ticks;
cycles++;
colsProcessed = 0;
if (gaptime > 0)
{
timer.Enabled = true;
}
}
public double cycleProgress()
{
double progress;
if (col == 0) progress = 0;
if (col > 0)
{
progress = (double)col / (double)xRes;
}
else progress = -1;
return progress;
}
private void processCol(int col)
{
if (finalImage != null)
{
double[] colData = new double[yRes];
for (int i = 0; i < yRes; i++)
{
try
{
// cunning trick - because the image has been "monochromed" it doesn't matter if we choose R, G, or B
colData[i] = finalImage.Data[i, col, 0];
// third index is RGB, 0 = R
}
catch
{
return;
}
}
output.useCol(colData, cycleProgress());
colsProcessed++;
}
}
private void Main_Load(object sender, EventArgs e)
{
timer.Enabled = true;
this.newCycle();
UpdateStatus("Running");
}
private void imageBox_Click(object sender, EventArgs e)
{
Console.WriteLine("Image clicked");
if (timer.Enabled)
{
timer.Enabled = false;
output.Pause();
UpdateStatus("Paused");
}
else
{
timer.Enabled = true;
}
}
delegate void UpdateStatusCallback(string text);
private void UpdateStatus(string status)
{
if (this.statusStrip1.InvokeRequired)
{
UpdateStatusCallback d = new UpdateStatusCallback(UpdateStatus);
this.Invoke(d, new object[] { status });
}
else
{
toolStripStatusLabel1.Text = status;
}
}
}
}