-
Notifications
You must be signed in to change notification settings - Fork 3
/
colorcreator.cpp
1328 lines (1260 loc) · 40.3 KB
/
colorcreator.cpp
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Main file. This is a color calibration tool (for now). Pops up a window
that can load image frames and display them in three ways:
1) The raw image
2) The image calibrated according to a color table, or this tool's controls
3) Lots of other things (e.g. just the Y channel of the image)
We make color tables with this tool by using its sliders. The sliders control
ranges (e.g. set a max and minimum value for Y) for each color. Those define
a region of color space that the color is defined for. What this does is
to quickly build a highly filled in color region. For now we then go back
to the old tool to refine the image. In the future it would be nice to
be able to refine directly in this tool.
Some things we don't have yet:
1) The ability to grab key images and save them into a "key image" directory
2) The ability to run our vision code and display object results
3) The ability to specify one color's dominance over another (e.g. if pink
&& orange then choose pink)
4) Lots of other stuff I haven't thought of yet
Things that aren't really done that well (this was intended as a prototype)
1) Data management
2) More or less everything - this tool is oddly slow, I don't know QT well enough
to know why.
*/
#include "colorcreator.h"
#include "ui_colorcreator.h"
#include "math.h"
#include <QTextStream>
#include <QFileDialog>
#include <QString>
#include <QStringRef>
#include <QRgb>
#include <QImage>
#include <QPixmap>
ColorCreator::ColorCreator(QWidget *parent) :
QWidget(parent),
ui(new Ui::ColorCreator),
roboimage(WIDTH, HEIGHT)
{
// Display images
img = new QImage(640, 480, QImage::Format_RGB32);
img2 = new QImage(320, 240, QImage::Format_RGB32);
img3 = new QImage(320, 240, QImage::Format_RGB32);
img4 = new QImage(320, 240, QImage::Format_RGB32);
wheel = new QImage(200, 200, QImage::Format_RGB32);
// Each color gets its own value for everything specified by sliders
hMin = new float[COLORS];
hMax = new float[COLORS];
sMin = new float[COLORS];
sMax = new float[COLORS];
zMin = new float[COLORS];
zMax = new float[COLORS];
yMin = new int[COLORS];
yMax = new int[COLORS];
vMin = new int[COLORS];
vMax = new int[COLORS];
cols = new QColor[COLORS+SOFT];
bitColor = new unsigned[COLORS];
table = new ColorTable();
// initialize colors for drawing thresholds
cols[Orange] = QColor(255, 128, 0);
cols[Blue] = QColor(100, 149, 237);
cols[Green] = QColor(0, 139, 0);
cols[Yellow] = QColor(255, 255, 0);
cols[White] = QColor(255, 255, 255);
cols[Pink] = QColor(255, 181, 197);
cols[Navy] = QColor(0, 0, 205);
cols[Black] = QColor(100, 100, 100);
cols[OrangeRed] = QColor(238, 64, 0);
cols[BlueGreen] = QColor(72, 209, 234);
cols[BlueNavy] = QColor(105, 89, 205);
// initialize bitColors for generating color tables
bitColor[Orange] = ORANGE_COL;
bitColor[Blue] = BLUE_COL;
bitColor[Yellow] = YELLOW_COL;
bitColor[Green] = GREEN_COL;
bitColor[White] = WHITE_COL;
bitColor[Pink] = RED_COL;
bitColor[Navy] = NAVY_COL;
bitColor[Black] = GREY_COL;
ui->setupUi(this);
// Default file directory - obviously this should not be user specific
baseDirectory = "/Users/ericchown/nbites/data/frames";
baseColorTable = "/Users/ericchown/nbites/data/tables";
haveFile = false;
viewerEnabled = false;
tableMode = false;
defineMode = false;
cornerStatus = true;
// color selection UI item
ui->colorSelect->addItem(tr("Orange"), Orange);
ui->colorSelect->addItem(tr("Blue"), Blue);
ui->colorSelect->addItem(tr("Yellow"), Yellow);
ui->colorSelect->addItem(tr("Green"), Green);
ui->colorSelect->addItem(tr("White"), White);
ui->colorSelect->addItem(tr("Pink"), Pink);
ui->colorSelect->addItem(tr("Navy"), Navy);
ui->colorSelect->addItem(tr("Black"), Black);
ui->viewChoice->addItem(tr("Single Color"), Single);
ui->viewChoice->addItem(tr("All Colors"), Multiple);
ui->channel->addItem(tr("Y"), Y);
ui->channel->addItem(tr("U"), U);
ui->channel->addItem(tr("V"), V);
ui->channel->addItem(tr("Blue"), Bluec);
ui->channel->addItem(tr("Red"), Redc);
ui->channel->addItem(tr("Green"), Greenc);
ui->channel->addItem(tr("H"), H);
ui->channel->addItem(tr("S"), S);
ui->channel->addItem(tr("Z"), Z);
ui->channel->addItem(tr("Edge"), EDGE);
ui->channel->addItem(tr("Table"), Table);
mode = Single;
shape = Y;
currentColor = Orange;
currentDirectory = baseDirectory;
currentColorDirectory = baseColorTable;
zSlice = 0.75f;
edgediff = 12;
// initialize all of our values. Ideally these will serve as a pretty good table
// for virtually any environment
// Note: it would be nice to be able to save values from various locations and load
// the appropriate one here
for (int i = 0; i < COLORS; i++)
{
switch(i)
{
case Orange:
hMin[i] = 0.80f;
hMax[i] = 0.13f;
sMin[i] = 0.25f;
sMax[i] = 1.0f;
zMin[i] = 0.12f;
zMax[i] = 1.0f;
yMin[i] = 34;
yMax[i] = 145;
vMin[i] = 115;
vMax[i] = 171;
break;
case Green:
hMin[i] = 0.37f;
hMax[i] = 0.45f;
sMin[i] = 0.28f;
sMax[i] = 0.46f;
zMin[i] = 0.22f;
zMax[i] = 0.53f;
yMin[i] = 55;
yMax[i] = 105;
vMin[i] = 90;
vMax[i] = 131;
break;
case Yellow:
hMin[i] = 0.17f;
hMax[i] = 0.26f;
sMin[i] = 0.32f;
sMax[i] = 0.69f;
zMin[i] = 0.27f;
zMax[i] = 0.48f;
yMin[i] = 56;
yMax[i] = 105;
vMin[i] = 111;
vMax[i] = 128;
break;
case Blue:
hMin[i] = 0.54f;
hMax[i] = 0.67f;
sMin[i] = 0.30f;
sMax[i] = 0.65f;
zMin[i] = 0.23f;
zMax[i] = 0.48f;
yMin[i] = 33;
yMax[i] = 105;
vMin[i] = 109;
vMax[i] = 127;
break;
case White:
hMin[i] = 0.01f;
hMax[i] = 0.01f;
sMin[i] = 0.0f;
sMax[i] = 0.38f;
zMin[i] = 0.39f;
zMax[i] = 1.0f;
yMin[i] = 102;
yMax[i] = 250;
vMin[i] = 99;
vMax[i] = 128;
break;
case Pink:
hMin[i] = 0.75f;
hMax[i] = 0.22f;
sMin[i] = 0.0f;
sMax[i] = 0.29f;
zMin[i] = 0.21f;
zMax[i] = 0.54f;
yMin[i] = 58;
yMax[i] = 139;
vMin[i] = 127;
vMax[i] = 143;
break;
case Navy:
hMin[i] = 0.57f;
hMax[i] = 0.68f;
sMin[i] = 0.23f;
sMax[i] = 0.42f;
zMin[i] = 0.17f;
zMax[i] = 0.45f;
yMin[i] = 39;
yMax[i] = 105;
vMin[i] = 106;
vMax[i] = 132;
break;
default:
hMin[i] = 0.0f;
hMax[i] = 0.01f;
sMin[i] = 0.99f;
sMax[i] = 1.0f;
zMin[i] = 0.0f;
zMax[i] = 1.0f;
yMin[i] = 30;
yMax[i] = 230;
vMin[i] = 40;
vMax[i] = 150;
break;
}
}
// set the sliders to start at the correct values
ui->hMin->setValue(hMin[currentColor] * 100);
ui->hMax->setValue(hMax[currentColor] * 100);
ui->sMin->setValue(sMin[currentColor] * 100);
ui->sMax->setValue(sMax[currentColor] * 100);
ui->zMin->setValue(zMin[currentColor] * 100);
ui->zMax->setValue(zMax[currentColor] * 100);
ui->yMin->setValue(yMin[currentColor]);
ui->yMax->setValue(yMax[currentColor]);
ui->vMin->setValue(vMin[currentColor]);
ui->vMax->setValue(vMax[currentColor]);
ui->zSlice->setValue(zSlice);
firstPoint.setX(-1);
firstPoint.setY(-1);
setMouseTracking(true);
}
ColorCreator::~ColorCreator()
{
delete ui;
}
void ColorCreator::mouseMoveEvent(QMouseEvent *event)
{
QTextStream out(stdout);
QPoint thePoint = event->pos();
int x = thePoint.x();
int y = thePoint.y();
if (x > IMAGE_X && x < IMAGE_X + IMAGE_WIDTH && y > IMAGE_Y && y < IMAGE_Y + IMAGE_HEIGHT && haveFile) {
x = x - IMAGE_X;
y = y - IMAGE_Y;
int Y = roboimage.getY(x, y);
int U = roboimage.getU(x, y);
int V = roboimage.getV(x, y);
int H = roboimage.getH(x, y);
int S = roboimage.getS(x, y);
int Z = roboimage.getZ(x, y);
out << "YUV :" << Y << " " << U << " " << V << "\n";
out << "HSZ :" << H << " " << S << " " << Z << "\n";
}
}
void ColorCreator::mouseReleaseEvent(QMouseEvent *event)
{
lastPoint = event->pos();
lastPoint.setX(lastPoint.x() - IMAGE_X);
lastPoint.setY(lastPoint.y() - IMAGE_Y);
if (lastPoint.x() - firstPoint.x() > 20) {
largeDisplay();
}
}
void ColorCreator::mousePressEvent(QMouseEvent *event)
{
firstPoint = event->pos();
firstPoint.setX(firstPoint.x() - IMAGE_X);
firstPoint.setY(firstPoint.y() - IMAGE_Y);
}
/* When something major changes we need to update all displays and file info.
*/
void ColorCreator::updateDisplays()
{
QString next;
next.setNum(currentFrameNumber+1, 10);
QString prev;
prev.setNum(currentFrameNumber-1, 10);
QString plusTen;
plusTen.setNum(currentFrameNumber+10, 10);
QString minusTen;
minusTen.setNum(currentFrameNumber-10, 10);
nextFrame = currentDirectory + "/" + next + EXTENSION;
previousFrame = currentDirectory + "/" + prev + EXTENSION;
tenthFrame = currentDirectory + "/" + plusTen + EXTENSION;
minusTenthFrame = currentDirectory + "/" + plusTen + EXTENSION;
haveFile = true;
updateThresh(true, false, false);
}
/* We display a color wheel to indicate the current color's region. It is a 2D slice
in 3D space (slicing on the V dimension (called Z here)) of HSV space.
Here we just loop through the display area, calculate an angle and distance to the
center (angle = H, distance = S) and determine whether any given pixel ought to
be displayed for the current color settings.
*/
void ColorCreator::updateColors()
{
// we draw by using a QImage - turn it into a Pixmap, then put it on a label
//QImage img(200, 200, QImage::Format_RGB32);
bool display;
QColor c;
if (!tableMode) {
/* Our color wheel has a radius of 100. Loop through the rectangle
looking for pixels within that radius. For good pixels we calculate
the H value based on the angle from the origin. The S value is
set according to the distance / radius, and the V is fixed (but
settable by a slider).
*/
for (int i = 0; i < 200; i++)
{
for (int j = 0; j < 200; j++)
{
float dist = sqrt((i - 100) * (i - 100) + (j - 100) * (j - 100));
if (dist < 100.0)
{
display = true;
float s = dist / 100.0f;
float h = atan2(i - 100, j - 100) / (2.0f * 3.14156);
if (h < 0)
{
h = 1.0f + h;
}
// Since H is an angle the math is modulo.
if (hMax[currentColor] > hMin[currentColor])
{
if (hMin[currentColor] > h || hMax[currentColor] < h)
{
display = false;
}
} else if (hMin[currentColor] > h && hMax[currentColor] < h )
{
display = false;
}
if (s < sMin[currentColor] || s > sMax[currentColor])
{
display = false;
}
ColorSpace col;
col.setHsz(h, s, zSlice);
int y = col.getYb();
int v = col.getVb();
if (y < yMin[currentColor] || y > yMax[currentColor])
{
display = false;
}
if (v < vMin[currentColor] || v > vMax[currentColor])
{
display = false;
}
if (display)
{
c.setHsvF(h, s, zSlice);
} else{
c.setHsvF(0.0, 0.0, 1.0f);
}
} else{
c.setHsvF(0.0, 0.0, 1.0f);
}
wheel->setPixel(i, j, c.rgb());
}
}
QPixmap pix;
pix.convertFromImage(*wheel);
ui->colorWheel->setPixmap(pix);
ui->colorWheel->repaint();
updateThresh(false, false, true);
}
}
/* One of our displays is typically used to display a single color
channel of our images. That is controlled here. Can also display
edge information if desired.
*/
QColor ColorCreator::getChannelView(int j, int i)
{
bool found;
int red, green, blue, edge;
switch (shape) {
case V:
red = green = blue = roboimage.getV(j, i);
break;
case Bluec:
red = green = 0;
blue = roboimage.getBlue(j, i);
break;
case Y:
red = green = blue = roboimage.getY(j, i);
break;
case U:
red = green = blue = roboimage.getU(j, i);
break;
case Greenc:
red = blue = 0;
green = roboimage.getGreen(j, i);
break;
case Redc:
blue = green = 0;
red = roboimage.getRed(j, i);
break;
case H:
red = green = blue = roboimage.getH(j, i);
break;
case S:
red = green = blue = roboimage.getS(j, i);
break;
case Z:
red = green = blue = roboimage.getZ(j, i);
break;
case Table:
if (table->isEnabled())
{
return displayColorTable(j, i);
}
break;
case EDGE:
red = green = blue = 255; //roboimage.getY(j, i);
found = false;
if (j > 0 && i > 1) {
edge = abs(roboimage.getY(j - 1, i) - roboimage.getY(j, i));
edge = max(abs(roboimage.getY(j, i) - roboimage.getY(j, i - 1)), edge);
if (edge > edgediff) {
red = 255;
green = 0;
blue = 0;
found = true;
}
edge = abs(roboimage.getU(j - 1, i) - roboimage.getU(j, i));
edge = max(abs(roboimage.getU(j, i) - roboimage.getU(j, i - 1)), edge);
if (edge > edgediff) {
green = 255;
blue = 0;
if (!found) {
red = 0;
}
found = true;
}
edge = abs(roboimage.getV(j - 1, i) - roboimage.getV(j, i));
edge = max(abs(roboimage.getV(j, i) - roboimage.getV(j, i - 1)), edge);
if (edge > edgediff) {
blue = 255;
if (!found) {
red = 0;
green = 0;
}
}
}
break;
}
QColor col(red, green, blue);
return col;
}
/* One of our displays normally displays a thresholded image that is thresholded
according to our sliders. However, we can also display an image that is
thresholded according to a color table. This is mainly to check that our
tables/tools are working as desired.
@param i The x value in the image
@param j The y value in the image
@return The thresholded color for that pixel
*/
QColor ColorCreator::displayColorTable(int i, int j)
{
QColor c;
int y = roboimage.getY(i, j);
int u = roboimage.getU(i, j);
int v = roboimage.getV(i, j);
unsigned col = table->index(y, u, v);
if ((col & ORANGE_COL) && (col & RED_COL))
{
c = cols[OrangeRed];
}
else if (col & ORANGE_COL) {
c = cols[Orange];
}
else if ((col & GREEN_COL) && (col & BLUE_COL))
{
c = cols[BlueGreen];
} else if ((col & GREEN_COL))
{
c = cols[Green];
}
else if ((col & WHITE_COL))
{
c = cols[White];
}
else if ((col & BLUE) && (col & NAVY))
{
c = cols[BlueNavy];
}
else if ((col & BLUE_COL))
{
c = cols[Blue];
}
else if ((col & NAVY_COL))
{
c = cols[Navy];
}
else if ((col & RED_COL))
{
c = cols[Pink];
}
else if ((col & YELLOW_COL))
{
c = cols[Yellow];
}
else {
c = cols[Black];
}
return c;
}
/* Tests if the given parameters are legal for the given color.
@param h the H value
@param s the S value
@param z the Z (V) value
@param y the Y value
@param v the V value
@param color the color we're checking
@return true if the params are in accord with the color definition
*/
bool ColorCreator::testValue(float h, float s, float z, int y, int u, int v, int color)
{
if (!tableMode || !haveFile)
{
if (hMax[color] > hMin[color])
{
if (hMin[color] > h || hMax[color] < h)
{
return false;
}
} else if (hMin[color] > h && hMax[color] < h )
{
return false;
}
if (s < sMin[color] || s > sMax[color])
{
return false;
}
else if (z < zMin[color] || z > zMax[color])
{
return false;
}
else if (y < yMin[color] || y > yMax[color])
{
return false;
} else if (v < vMin[color] || v > vMax[color])
{
return false;
}
return true;
} else {
unsigned col = table->index(y, u, v);
return col & bitColor[color];
}
}
/* Controller for the big display. Normally just displays the current image
in its raw form.
*/
void ColorCreator::largeDisplay()
{
bool display;
QColor c;
bool regionSet = firstPoint.x() > -1;
for (int i = 0; i < WIDTH; i++)
{
for (int j = 0; j < HEIGHT; j++)
{
bool looping = true;
int start = Orange;
if (mode == Single) {
looping = false;
start = currentColor;
}
c.setRgb(255,255,255);
do {
display = true;
int y = roboimage.getY(i, j);
int u = roboimage.getU(i, j);
int v = roboimage.getV(i, j);
float s = (float)roboimage.getS(i, j) / 256.0f;
float h = (float)roboimage.getH(i, j) / 256.0f;
float z = (float)roboimage.getZ(i, j) / 256.0f;
// Since H is an angle the math is modulo.
display = testValue(h, s, z, y, u, v, start);
c = cols[start];
if (display)
{
looping = false;
// check for some common overlaps
if (start == Orange)
{
if (testValue(h, s, z, y, u, v, Pink))
{
c = cols[OrangeRed];
}
} else if (start == Green)
{
if (testValue(h, s, z, y, u, v, Blue))
{
c = cols[BlueGreen];
}
}
else if (start == Blue)
{
if (testValue(h, s, z, y, u, v, Navy))
{
c = cols[BlueNavy];
}
}
} else{
c = cols[Black];
}
img->setPixel(i, j, c.rgb());
start++;
if (start == Black)
{
looping = false;
}
} while (looping);
}
}
if (regionSet && tableMode) {
c = cols[Black];
for (int k = firstPoint.x(); k < lastPoint.x(); k++)
{
img->setPixel(k, firstPoint.y(), c.rgb());
img->setPixel(k, firstPoint.y()+1, c.rgb());
img->setPixel(k, lastPoint.y(), c.rgb());
img->setPixel(k, lastPoint.y()+1, c.rgb());
}
for (int l = firstPoint.y(); l < lastPoint.y(); l++)
{
img->setPixel(firstPoint.x(), l, c.rgb());
img->setPixel(firstPoint.x()+1, l, c.rgb());
img->setPixel(lastPoint.x(), l, c.rgb());
img->setPixel(lastPoint.x()+1, l, c.rgb());
}
}
QPixmap pix;
pix.convertFromImage(*img);
ui->thresh->setPixmap(pix);
ui->thresh->repaint();
}
/* This is called when something happens that requires updating the
thresholded image (e.g. a color definition changed). It makes sure
that everything gets update accordingly.
*/
void ColorCreator::updateThresh(bool imageChanged, bool choiceChanged, bool colorsChanged)
{
if (haveFile)
{
bool display;
bool stats = false;
QColor c, c2;
int red, blue, green;
initStats();
largeDisplay();
for (int i = 0; i < WIDTH; i+=2)
{
for (int j = 0; j < HEIGHT; j+=2)
{
bool looping = true;
int start = Orange;
if (mode == Single) {
looping = false;
stats = true;
start = currentColor;
}
if (imageChanged) {
red = roboimage.getRed(i, j);
green = roboimage.getGreen(i, j);
blue = roboimage.getBlue(i, j);
c.setRgb(red, green, blue);
img2->setPixel(i/2, j/2, c.rgb());
}
if (imageChanged || choiceChanged) {
c = getChannelView(i, j);
img3->setPixel(i/2, j/2, c.rgb());
c.setRgb(0, 0, 0);
c.setRgb(255,255,255);
}
}
}
if (imageChanged) {
QPixmap pix2;
pix2.convertFromImage(*img2);
ui->view2->setPixmap(pix2);
ui->view2->repaint();
}
if (imageChanged || choiceChanged) {
QPixmap pix3;
pix3.convertFromImage(*img3);
ui->view3->setPixmap(pix3);
ui->view3->repaint();
}
if (stats)
{
outputStats();
}
}
}
/* We can keep stats on the colors. Init them for use.
*/
void ColorCreator::initStats()
{
statsSMin = 1.0f;
statsSMax = 0.0f;
statsHMin = 1.0f;
statsHMax = 0.0f;
statsZMin = 1.0f;
statsZMax = 0.0f;
statsYMin = 255;
statsYMax = 0;
statsUMin = 255;
statsUMax = 0;
statsVMin = 255;
statsVMax = 0;
}
/* Collect the stats. THis is called in a loop. Each pixel
is added to the stats.
Note: check if needed or if this is slowing things down.
@param x x value of pixel
@param y y value of pixel
*/
void ColorCreator::collectStats(int x, int y)
{
float s = (float)roboimage.getS(x, y) / 256.0f;
float h = (float)roboimage.getH(x, y) / 256.0f;
float z = (float)roboimage.getZ(x, y) / 256.0f;
int yy = roboimage.getY(x, y);
int u = roboimage.getU(x, y);
int v = roboimage.getV(x, y);
statsSMin = min(statsSMin, s);
statsSMax = max(statsSMax, s);
statsHMin = min(statsHMin, h);
statsHMax = max(statsHMax, h);
statsZMin = min(statsZMin, z);
statsZMax = max(statsZMax, z);
statsYMin = min(statsYMin, yy);
statsYMax = max(statsYMax, yy);
statsUMin = min(statsUMin, u);
statsUMax = max(statsUMax, u);
statsVMin = min(statsVMin, v);
statsVMax = max(statsVMax, v);
}
/* Prints out the stats.
*/
void ColorCreator::outputStats()
{
QTextStream out(stdout);
out << "Stats for current color\n";
out << "H: " << statsHMin << " " << statsHMax << "\n";
out << "S: " << statsSMin << " " << statsSMax << "\n";
out << "Z: " << statsZMin << " " << statsZMax << "\n";
out << "Y: " << statsYMin << " " << statsYMax << "\n";
out << "U: " << statsUMin << " " << statsUMax << "\n";
out << "V: " << statsVMin << " " << statsVMax << "\n";
}
/* Called when the file button is pushed. We need a new image.
*/
void ColorCreator::on_pushButton_clicked()
{
currentDirectory = QFileDialog::getOpenFileName(this, tr("Open Image"),
currentDirectory,
tr("Image Files (*.frm)"));
roboimage.read(currentDirectory);
int last = currentDirectory.lastIndexOf("/");
int period = currentDirectory.lastIndexOf(".") - last - 1;
QString temp = currentDirectory.mid(last+1, period);
bool ok;
currentFrameNumber = temp.toInt(&ok, 10);
currentDirectory.chop(currentDirectory.size() - last);
updateDisplays();
}
/* Called when the "previous" button is pushed. We need the previous
image.
To Do: Disable the button when no previous image available
*/
void ColorCreator::on_previousButton_clicked()
{
roboimage.read(previousFrame);
currentFrameNumber--;
updateDisplays();
}
/* Called when the "next" button is pushed. We need the previous
image.
To Do: Disable the button when no next image available
*/
void ColorCreator::on_nextButton_clicked()
{
roboimage.read(nextFrame);
currentFrameNumber++;
updateDisplays();
}
/* Called when the "H Min" slider is moved.
@param value the new value selected
*/
void ColorCreator::on_hMin_valueChanged(int value)
{
hMin[currentColor] = (float)value / 100.0f;
updateColors();
QTextStream out(stdout);
out << "Set H Min value to " << value << "\n";
}
/* Called when the "H Max" slider is moved.
@param value the new value selected
*/
void ColorCreator::on_hMax_valueChanged(int value)
{
hMax[currentColor] = (float)value / 100.0f;
updateColors();
QTextStream out(stdout);
out << "Set H Max value to " << value << "\n";
}
/* Called when the "S Min" slider is moved.
@param value the new value selected
*/
void ColorCreator::on_sMin_valueChanged(int value)
{
sMin[currentColor] = (float)value / 100.0f;
updateColors();
QTextStream out(stdout);
out << "Set S Min value to " << value << "\n";
}
/* Called when the "S Max" slider is moved.
@param value the new value selected
*/
void ColorCreator::on_sMax_valueChanged(int value)
{
sMax[currentColor] = (float)value / 100.0f;
updateColors();
QTextStream out(stdout);
out << "Set S Max value to " << value << "\n";
}
/* Called when the "Y Min" slider is moved.
@param value the new value selected
*/
void ColorCreator::on_yMin_valueChanged(int value)
{
yMin[currentColor] = value;
updateColors();
QTextStream out(stdout);
out << "Set Y Min value to " << value << "\n";
}
/* Called when the "Y Max" slider is moved.
@param value the new value selected
*/
void ColorCreator::on_yMax_valueChanged(int value)
{
yMax[currentColor] = value;
updateColors();
QTextStream out(stdout);
out << "Set Y Max value to " << value << "\n";
}
/* Called when the "Z Slice" slider is moved. This slider
controls where you slice through the 3D space to view
the 2D color wheel
*/
void ColorCreator::on_zSlice_valueChanged(int value)
{
zSlice = (float)value / 100.0f;
updateColors();
}
/* Called when the user picks a new color to work on.
*/
void ColorCreator::on_colorSelect_currentIndexChanged(int index)
{
currentColor = index;
ui->hMin->setValue(hMin[currentColor] * 100);
ui->hMax->setValue(hMax[currentColor] * 100);
ui->sMin->setValue(sMin[currentColor] * 100);
ui->sMax->setValue(sMax[currentColor] * 100);
ui->zMin->setValue(zMin[currentColor] * 100);
ui->zMax->setValue(zMax[currentColor] * 100);
ui->yMin->setValue(yMin[currentColor]);
ui->yMax->setValue(yMax[currentColor]);
ui->vMin->setValue(vMin[currentColor]);
ui->vMax->setValue(vMax[currentColor]);
ui->zSlice->setValue((zMin[currentColor] + zMax[currentColor]) * 50);
}
/* Called when the user wants to view something different.
@param value the new value selected
*/
void ColorCreator::on_viewChoice_currentIndexChanged(int index)
{
mode = index;
updateThresh(false, true, false);
}
/* Called when the "Z Min" slider is moved.
@param value the new value selected
*/
void ColorCreator::on_zMin_valueChanged(int value)
{
zMin[currentColor] = (float)value / 100.0f;
updateColors();
QTextStream out(stdout);
out << "Set Z Min value to " << value << "\n";
}
/* Called when the "Z Max" slider is moved.
@param value the new value selected
*/
void ColorCreator::on_zMax_valueChanged(int value)
{
zMax[currentColor] = (float)value / 100.0f;
updateColors();
QTextStream out(stdout);
out << "Set Z Max value to " << value << "\n";
}
/* Gets a color table. From there you can view how the color table
thresholds images.
*/
void ColorCreator::on_getColorTable_clicked()
{
currentColorDirectory = QFileDialog::getOpenFileName(this, tr("Open Color Table"),
currentColorDirectory,
tr("Table Files (*.mtb)"));
table->read(currentColorDirectory);
int last = currentColorDirectory.lastIndexOf("/");
currentColorDirectory.chop(currentColorDirectory.size() - last);
}
/* Writes out a color table. The "new" part of the format is that it
writes the color table using bitwise color definitions instead of the
old integer definitions.
@param filename the name to write
*/
void ColorCreator::writeNewFormat(QString filename)
{
QFile file(filename);
QTextStream out(stdout);
QByteArray temp;
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
out << "The file would not open properly" << "\n";
return;
}
// loop through all possible table values - our tables are v-u-y
int count = 0;
for (int z = 0; z < 128; ++z)
{
for (int x = 0; x < 128; x ++)
{
for (int y = 0; y < 128; y++)
{
temp[0] = GREY_COL;
ColorSpace col;