-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDefineROI.cs
392 lines (378 loc) · 19.7 KB
/
DefineROI.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace GrzMotion {
public partial class DefineROI : Form {
// public property
public List<MainForm.oneROI> ROIsList { get; set; }
// locals
bool dirtyFlag = false;
int currListNdx = 0;
bool currRectDrawing = false;
Rectangle currRect = new Rectangle();
public DefineROI() {
InitializeComponent();
}
private void DefineROI_Shown(object sender, EventArgs e) {
while ( ROIsList[currListNdx].rect.Width <= 0 || ROIsList[currListNdx].rect.Height <= 0 ) {
currListNdx++;
if ( currListNdx >= ROIsList.Count ) {
currListNdx = 0;
break;
}
}
this.pictureBox.Invalidate();
this.pictureBox.Update();
}
// form close methods
private void buttonOk_Click(object sender, EventArgs e) {
// latest change might not yet be applied
if ( dirtyFlag ) {
DialogResult res = MessageBox.Show("There is at least one unsaved change to the settings.\n\nDiscard this change?", "Note", MessageBoxButtons.YesNo);
if ( res == DialogResult.Yes ) {
this.DialogResult = DialogResult.Cancel;
this.Close();
} else {
if ( res == DialogResult.No ) {
return;
}
}
}
// standard flow
this.DialogResult = DialogResult.OK;
this.Close();
}
private void buttonCancel_Click(object sender, EventArgs e) {
this.DialogResult = DialogResult.Cancel;
this.Close();
}
// set image to be shown in pictureBox
public void SetImage(Bitmap bmp) {
this.pictureBox.Image = (Bitmap)bmp.Clone();
}
// set dirty flag at any change
private void numericUpDownPosX_ValueChanged(object sender, EventArgs e) {
this.numericUpDownPosX.ValueChanged -= new System.EventHandler(this.numericUpDownPosX_ValueChanged);
this.numericUpDownPosX.Value = Math.Min(this.numericUpDownPosX.Value, this.pictureBox.Image.Width - this.numericUpDownWidthX.Value);
this.numericUpDownPosX.ValueChanged += new System.EventHandler(this.numericUpDownPosX_ValueChanged);
dirtyFlag = true;
this.pictureBox.Invalidate();
this.pictureBox.Update();
}
private void numericUpDownWidthX_ValueChanged(object sender, EventArgs e) {
this.numericUpDownWidthX.ValueChanged -= new System.EventHandler(this.numericUpDownWidthX_ValueChanged);
this.numericUpDownWidthX.Value = Math.Min(this.numericUpDownWidthX.Value, this.pictureBox.Image.Width - this.numericUpDownPosX.Value);
this.numericUpDownWidthX.ValueChanged += new System.EventHandler(this.numericUpDownWidthX_ValueChanged);
dirtyFlag = true;
this.pictureBox.Invalidate();
this.pictureBox.Update();
}
private void numericUpDownPosY_ValueChanged(object sender, EventArgs e) {
this.numericUpDownPosY.ValueChanged -= new System.EventHandler(this.numericUpDownPosY_ValueChanged);
this.numericUpDownPosY.Value = Math.Min(this.numericUpDownPosY.Value, this.pictureBox.Image.Height - this.numericUpDownHeightY.Value);
this.numericUpDownPosY.ValueChanged += new System.EventHandler(this.numericUpDownPosY_ValueChanged);
dirtyFlag = true;
this.pictureBox.Invalidate();
this.pictureBox.Update();
}
private void numericUpDownHeightY_ValueChanged(object sender, EventArgs e) {
this.numericUpDownHeightY.ValueChanged -= new System.EventHandler(this.numericUpDownHeightY_ValueChanged);
this.numericUpDownHeightY.Value = Math.Min(this.numericUpDownHeightY.Value, this.pictureBox.Image.Height - this.numericUpDownPosY.Value);
this.numericUpDownHeightY.ValueChanged += new System.EventHandler(this.numericUpDownHeightY_ValueChanged);
dirtyFlag = true;
this.pictureBox.Invalidate();
this.pictureBox.Update();
}
private void numericUpDownIntensity_ValueChanged(object sender, EventArgs e) {
dirtyFlag = true;
this.pictureBox.Invalidate();
this.pictureBox.Update();
}
private void textBoxThreshold_TextChanged(object sender, EventArgs e) {
double tmp = double.Parse(this.textBoxThreshold.Text);
if ( tmp > 100 ) {
this.textBoxThreshold.Text = "100.0";
}
if ( tmp < 0 ) {
this.textBoxThreshold.Text = "0.0";
}
dirtyFlag = true;
this.pictureBox.Invalidate();
this.pictureBox.Update();
}
private void textBoxThresholdUpperLimit_TextChanged(object sender, EventArgs e) {
double tmp = double.Parse(this.textBoxThresholdUpperLimit.Text);
if ( tmp > 100 ) {
this.textBoxThresholdUpperLimit.Text = "100.0";
}
if ( tmp < 0 ) {
this.textBoxThresholdUpperLimit.Text = "0.0";
}
dirtyFlag = true;
this.pictureBox.Invalidate();
this.pictureBox.Update();
}
private void numericUpDownBoxScaler_ValueChanged(object sender, EventArgs e) {
dirtyFlag = true;
this.pictureBox.Invalidate();
this.pictureBox.Update();
}
private void checkBoxReferenceROI_CheckedChanged(object sender, EventArgs e) {
dirtyFlag = true;
this.pictureBox.Invalidate();
this.pictureBox.Update();
}
// apply changes from UI to currently selected ROI
private void buttonApply_Click(object sender, EventArgs e) {
// new ROI rect
ROIsList[currListNdx].rect = new Rectangle((int)this.numericUpDownPosX.Value, (int)this.numericUpDownPosY.Value, (int)this.numericUpDownWidthX.Value, (int)this.numericUpDownHeightY.Value);
// new ROI intensity
ROIsList[currListNdx].thresholdIntensity = (int)this.numericUpDownIntensity.Value;
// new ROI percentages: threshold + upper limit
ROIsList[currListNdx].thresholdChanges = double.Parse(this.textBoxThreshold.Text, System.Globalization.CultureInfo.InvariantCulture) / 100.0f;
ROIsList[currListNdx].thresholdUpperLimit = double.Parse(this.textBoxThresholdUpperLimit.Text, System.Globalization.CultureInfo.InvariantCulture) / 100.0f;
// new ROI scaler
ROIsList[currListNdx].boxScaler = (int)this.numericUpDownBoxScaler.Value;
// only if reference status was changed at all
if ( ROIsList[currListNdx].reference != this.checkBoxReferenceROI.Checked ) {
// new reference ROI: before a new reference ROI is declared, make sure all other ROIS are not a reference
for ( int i = 0; i < ROIsList.Count; i++ ) {
if ( ROIsList[i].rect.Width >= 0 && ROIsList[i].rect.Height >= 0 ) {
ROIsList[i].reference = false;
}
}
ROIsList[currListNdx].reference = this.checkBoxReferenceROI.Checked;
}
// no longer dirty
dirtyFlag = false;
// update UI
this.pictureBox.Invalidate();
this.pictureBox.Update();
}
// reset changes of currently selected ROI
private void buttonReset_Click(object sender, EventArgs e) {
dirtyFlag = false;
this.pictureBox.Invalidate();
this.pictureBox.Update();
}
// delete currently selected ROI
private void buttonDeleteROI_Click(object sender, EventArgs e) {
ROIsList[currListNdx].rect = new Rectangle();
ROIsList[currListNdx].thresholdIntensity = 15;
ROIsList[currListNdx].thresholdChanges = 0.05f;
ROIsList[currListNdx].thresholdUpperLimit = 1.0f;
ROIsList[currListNdx].boxScaler = 1;
ROIsList[currListNdx].reference = false;
int fstRoiNdx = -1;
for ( int i = 0; i < ROIsList.Count; i++ ) {
if ( ROIsList[i].rect.Width >= 0 && ROIsList[i].rect.Height >= 0 ) {
fstRoiNdx = i;
break;
}
}
currListNdx = fstRoiNdx == -1 ? fstRoiNdx : 0;
dirtyFlag = false;
this.pictureBox.Invalidate();
this.pictureBox.Update();
}
// draw a rectangle into pictureBox
private void pictureBox_MouseDown(object sender, MouseEventArgs e) {
if ( e.X >= this.pictureBox.Image.Width || e.Y >= this.pictureBox.Image.Height ) {
return;
}
currRectDrawing = true;
currRect = new Rectangle();
currRect.X = e.X;
currRect.Y = e.Y;
this.pictureBox.Invalidate();
this.pictureBox.Update();
}
private void pictureBox_MouseMove(object sender, MouseEventArgs e) {
if ( e.X >= this.pictureBox.Image.Width || e.Y >= this.pictureBox.Image.Height ) {
return;
}
if ( !currRectDrawing ) {
return;
}
currRect.Width = e.X - currRect.X;
currRect.Height = e.Y - currRect.Y;
this.pictureBox.Invalidate();
this.pictureBox.Update();
}
private void pictureBox_MouseUp(object sender, MouseEventArgs e) {
currRect.Width = e.X - currRect.X;
currRect.Height = e.Y - currRect.Y;
this.pictureBox.Invalidate();
this.pictureBox.Update();
currRectDrawing = false;
}
private void buttonAddROI_Click(object sender, EventArgs e) {
// plausibility test
if ( currRect.Width <= 0 || currRect.Height <= 0 || this.buttonAddROI.Font.Italic ) {
return;
}
// find the first ROI with empty width and height
int newRoiNdx = -1;
for ( int i = 0; i < ROIsList.Count; i++ ) {
// take ROI, which has empty width and height
if ( ROIsList[i].rect.Width == 0 && ROIsList[i].rect.Height == 0 ) {
newRoiNdx = i;
break;
}
}
// empty ROI (aka unused) found
if ( newRoiNdx != -1 ) {
ROIsList[newRoiNdx].rect = currRect;
ROIsList[newRoiNdx].thresholdIntensity = 15;
ROIsList[newRoiNdx].thresholdChanges = 0.05f;
ROIsList[newRoiNdx].thresholdUpperLimit = 1.0f;
ROIsList[newRoiNdx].boxScaler = 1;
ROIsList[newRoiNdx].reference = false;
currListNdx = newRoiNdx;
dirtyFlag = false;
currRect = new Rectangle();
this.pictureBox.Invalidate();
this.pictureBox.Update();
} else {
currRect = new Rectangle();
MessageBox.Show("Error", "Number of ROIs is limited to 10.", MessageBoxButtons.OK);
}
this.pictureBox.Invalidate();
this.pictureBox.Update();
}
// switch ROI
private void buttonPrevROI_Click(object sender, EventArgs e) {
currListNdx--;
if ( currListNdx < 0 ) {
currListNdx = ROIsList.Count - 1;
}
int iterationCount = 0;
while ( ROIsList[currListNdx].rect.Width <= 0 || ROIsList[currListNdx].rect.Height <= 0 ) {
currListNdx--;
if ( currListNdx < 0 ) {
currListNdx = ROIsList.Count - 1;
}
iterationCount++;
if ( iterationCount >= ROIsList.Count ) {
currListNdx = 0;
ROIsList[0].rect = currRect;
ROIsList[0].thresholdIntensity = 15;
ROIsList[0].thresholdChanges = 0.05f;
ROIsList[0].thresholdUpperLimit = 1.0f;
ROIsList[0].boxScaler = 1;
ROIsList[0].reference = false;
break;
}
}
this.pictureBox.Invalidate();
this.pictureBox.Update();
}
private void buttonNextROI_Click(object sender, EventArgs e) {
currListNdx++;
if ( currListNdx >= ROIsList.Count ) {
currListNdx = 0;
}
int iterationCount = 0;
while ( ROIsList[currListNdx].rect.Width <= 0 || ROIsList[currListNdx].rect.Height <= 0 ) {
currListNdx++;
if ( currListNdx >= ROIsList.Count ) {
currListNdx = 0;
}
iterationCount++;
if ( iterationCount >= ROIsList.Count ) {
currListNdx = 0;
ROIsList[0].rect = currRect;
ROIsList[0].thresholdIntensity = 15;
ROIsList[0].thresholdChanges = 0.05f;
ROIsList[0].thresholdUpperLimit = 1.0f;
ROIsList[0].boxScaler = 1;
ROIsList[0].reference = false;
break;
}
}
this.pictureBox.Invalidate();
this.pictureBox.Update();
}
// all the drawing
private void pictureBox_Paint(object sender, PaintEventArgs e) {
// draw existing rectangles inclusive their respective ROI number
for ( int i = 0; i < ROIsList.Count; i++ ) {
// skip default ROIs, which have empty width and height
if ( ROIsList[i].rect.Width > 0 && ROIsList[i].rect.Height > 0 ) {
// update panel to active ROI
if ( i == currListNdx ) {
// update shall only take place, if there was no parameter change at all, AKA scrolling thru ROIs
if ( !dirtyFlag ) {
// update panel with ROI parameters according to active ROI
this.labelActiveROI.Text = currListNdx.ToString();
// disable event handlers
this.numericUpDownPosX.ValueChanged -= new System.EventHandler(this.numericUpDownPosX_ValueChanged);
this.numericUpDownWidthX.ValueChanged -= new System.EventHandler(this.numericUpDownWidthX_ValueChanged);
this.numericUpDownPosY.ValueChanged -= new System.EventHandler(this.numericUpDownPosY_ValueChanged);
this.numericUpDownHeightY.ValueChanged -= new System.EventHandler(this.numericUpDownHeightY_ValueChanged);
// update numeric controls, which do normally some min/max checks
this.numericUpDownPosX.Value = ROIsList[i].rect.X;
this.numericUpDownWidthX.Value = ROIsList[i].rect.Width;
this.numericUpDownPosY.Value = ROIsList[i].rect.Y;
this.numericUpDownHeightY.Value = ROIsList[i].rect.Height;
// enable event handlers
this.numericUpDownPosX.ValueChanged += new System.EventHandler(this.numericUpDownPosX_ValueChanged);
this.numericUpDownWidthX.ValueChanged += new System.EventHandler(this.numericUpDownWidthX_ValueChanged);
this.numericUpDownPosY.ValueChanged += new System.EventHandler(this.numericUpDownPosY_ValueChanged);
this.numericUpDownHeightY.ValueChanged += new System.EventHandler(this.numericUpDownHeightY_ValueChanged);
// update other params
this.numericUpDownIntensity.Value = ROIsList[i].thresholdIntensity;
this.textBoxThreshold.Text = (ROIsList[i].thresholdChanges * 100.0f).ToString("0.0", System.Globalization.CultureInfo.InvariantCulture);
this.textBoxThresholdUpperLimit.Text = (ROIsList[i].thresholdUpperLimit * 100.0f).ToString("0.0", System.Globalization.CultureInfo.InvariantCulture);
this.numericUpDownBoxScaler.Value = ROIsList[i].boxScaler;
this.checkBoxReferenceROI.Checked = ROIsList[i].reference;
// intentional set to false: because some input controls were updated, what sets dirtyFlag to true - but here we are only scrolling thru ROIs !! not changing them !!
dirtyFlag = false;
}
}
// ROI border color depends on: selected rect vs. reference vs. 'normal' show
using ( Pen pen = currListNdx == i ? new Pen(Color.Red, 2) : ROIsList[i].reference ? new Pen(Color.Yellow, 1) : new Pen(Color.Green, 1) ) {
// special treatment for currently selected ROI
if ( i == currListNdx ) {
if ( dirtyFlag ) {
// print out ROI number
e.Graphics.DrawString(i.ToString(), new Font("Arial", 15, FontStyle.Bold, GraphicsUnit.Pixel), Brushes.Red, (int)this.numericUpDownPosX.Value, (int)this.numericUpDownPosY.Value - 16);
// show changes to ROI rect
e.Graphics.DrawRectangle(pen, new Rectangle((int)this.numericUpDownPosX.Value, (int)this.numericUpDownPosY.Value, (int)this.numericUpDownWidthX.Value, (int)this.numericUpDownHeightY.Value));
} else {
// print out ROI number
e.Graphics.DrawString(i.ToString(), new Font("Arial", 15, FontStyle.Bold, GraphicsUnit.Pixel), Brushes.Red, ROIsList[i].rect.X, ROIsList[i].rect.Y - 16);
// just show
e.Graphics.DrawRectangle(pen, ROIsList[i].rect);
}
} else {
// print out ROI number
e.Graphics.DrawString(i.ToString(), new Font("Arial", 15, FontStyle.Bold, GraphicsUnit.Pixel), Brushes.Red, ROIsList[i].rect.X, ROIsList[i].rect.Y - 16);
// just show
e.Graphics.DrawRectangle(pen, ROIsList[i].rect);
}
}
}
}
// control whether to allow changes/reset to selected ROI & disallow to switch to next/previous ROI & add new ROI
this.buttonApply.Enabled = dirtyFlag;
this.buttonReset.Enabled = dirtyFlag;
this.buttonNextROI.Enabled = !dirtyFlag;
this.buttonPrevROI.Enabled = !dirtyFlag;
this.buttonAddROI.Enabled = !dirtyFlag;
this.buttonDeleteROI.Enabled = !dirtyFlag;
// draw currently created rectangle
if ( currRect.Width > 0 && currRect.Height > 0 ) {
Font font = new Font("Microsoft Sans Serif", 12, FontStyle.Regular, GraphicsUnit.Point);
this.buttonAddROI.Font = font;
using ( Pen pen = new Pen(Color.Red, 1) ) {
e.Graphics.DrawRectangle(pen, currRect);
}
} else {
Font font = new Font("Microsoft Sans Serif", 12, FontStyle.Italic, GraphicsUnit.Point);
this.buttonAddROI.Font = font;
}
}
}
}