-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
2031 lines (1784 loc) · 113 KB
/
mainwindow.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
/*#-------------------------------------------------
#
# Dominant colors from image with openCV
#
# by AbsurdePhoton - www.absurdephoton.fr
#
# v2.0 - 2020/02/06
#
#-------------------------------------------------*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QSizeGrip>
#include <QGridLayout>
#include <QDesktopWidget>
#include <QCursor>
#include <QMouseEvent>
#include <QPainter>
#include <QScrollBar>
#include <QWhatsThis>
#include <fstream>
#include "mat-image-tools.h"
#include "dominant-colors.h"
#include "color-spaces.h"
#include "angles.h"
/////////////////// Window init //////////////////////
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// window
setWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::WindowMinMaxButtonsHint); // just minimize and size buttons
this->setWindowState(Qt::WindowMaximized); // maximize window
setFocusPolicy(Qt::StrongFocus); // catch keyboard and mouse in priority
//statusBar()->setVisible(false); // no status bar
setWindowIcon(QIcon(":/icons/color.png")); // icon in title bar
// add size grip to wheel image
/*ui->label_wheel->setWindowFlags(Qt::SubWindow);
QSizeGrip * sizeGrip = new QSizeGrip(ui->label_wheel);
QGridLayout * layout = new QGridLayout(ui->label_wheel);
layout->addWidget(sizeGrip, 0,0,1,1,Qt::AlignBottom | Qt::AlignRight);*/
// initial variable values
InitializeValues(); // intial variable values and GUI elements
}
MainWindow::~MainWindow()
{
delete ui;
}
/////////////////// GUI //////////////////////
void MainWindow::InitializeValues() // Global variables and GUI elements init
{
loaded = false; // main image NOT loaded
computed = false; // dominant colors NOT computed
basedirinifile = QDir::currentPath().toUtf8().constData(); // where to store the folder ini file
basedirinifile += "/dir.ini";
cv::FileStorage fs(basedirinifile, cv::FileStorage::READ); // open dir ini file
if (fs.isOpened()) {
fs["BaseDir"] >> basedir; // load dir
}
else basedir = "/home/"; // default base path and file
basefile = "example";
// Timer
QPalette pal = ui->timer->palette(); // use a palette
pal.setColor(QPalette::Normal, QPalette::Light, QColor(255,0,0)); // set values for QLCDNumber
pal.setColor(QPalette::Normal, QPalette::Foreground, QColor(255,92,92));
pal.setColor(QPalette::Normal, QPalette::Dark, QColor(164,0,0));
ui->timer->setPalette(pal); // set palette to QLCDNumber
ui->timer->display("-------"); // reset timer
// Wheel
nb_palettes= -1; // no palette yet
ShowWheel(); // draw empty wheel
pickedColor = cv::Vec3b(-1, -1, -1); // dummy values
// limits for blacks, grays and whites, angle and distance values
blacksLimit = blacksLimitIni;
whitesLimit = whitesLimitIni;
graysLimit = graysLimitIni;
on_button_reset_params_clicked(); // set default ui slider values
// scroll areas
ui->scrollArea_quantized->setWidget(ui->label_quantized); // attach quantized label to scroll area
ui->label_quantized->setGeometry(0, 0, 1, 1);
ui->scrollArea_image->setWidget(ui->label_image); // attach source image label to scroll area
ui->label_image->setGeometry(0, 0, 1, 1);
zoom = false; // not zoomed
// synchronize source and quantized images when zoomed
connect(this->ui->scrollArea_image->verticalScrollBar(), SIGNAL(valueChanged(int)),
this->ui->scrollArea_quantized->verticalScrollBar(), SLOT(setValue(int)));
connect(this->ui->scrollArea_quantized->verticalScrollBar(), SIGNAL(valueChanged(int)),
this->ui->scrollArea_image->verticalScrollBar(), SLOT(setValue(int)));
connect(this->ui->scrollArea_image->horizontalScrollBar(), SIGNAL(valueChanged(int)),
this->ui->scrollArea_quantized->horizontalScrollBar(), SLOT(setValue(int)));
connect(this->ui->scrollArea_quantized->horizontalScrollBar(), SIGNAL(valueChanged(int)),
this->ui->scrollArea_image->horizontalScrollBar(), SLOT(setValue(int)));
// other GUI items
ui->frame_analysis->setVisible(false); // frames
ui->frame_analyze->setVisible(false);
ui->frame_rgb->setVisible(false);
ui->frame_mean_shift_parameters->setVisible(false);
ui->frame_sectored_means_parameters->setVisible(false);
ui->radioButton_sectored_means->setChecked(true); // select sectored-means algorithm -> this show its parameters
ui->checkBox_regroup->setChecked(true); // select regroup algorithm for sectored-means
// populate sort palette combobox
ui->comboBox_sort->blockSignals(true); // don't launch automatic update of palette, it would crash
ui->comboBox_sort->addItem("Percentage");
ui->comboBox_sort->addItem("Hue (HSL)");
ui->comboBox_sort->addItem("Hue (CIE LCHab)");
ui->comboBox_sort->addItem("Lightness");
ui->comboBox_sort->addItem("Chroma");
ui->comboBox_sort->addItem("Saturation");
ui->comboBox_sort->addItem("RGB (hexa)");
ui->comboBox_sort->addItem("Rainbow6");
ui->comboBox_sort->blockSignals(false); // return to normal behavior
// read color names from .csv file
std::string line; // line to read in text file
std::ifstream names; // file to read
names.open("color-names.csv"); // read color names file
if (names) { // if successfully read
nb_color_names = -1; // index of color names array
size_t pos; // index for find function
std::string s; // used for item extraction
getline(names, line); // read first line (header)
while (getline(names, line)) { // read each line of text file: R G B name
pos = 0; // find index at the beginning of the line
nb_color_names++; // current index in color names array
int pos2 = line.find(";", pos); // find first semicolon char
s = line.substr(pos, pos2 - pos); // extract R value
color_names[nb_color_names].R = std::stoi(s); // R in array
pos = pos2 + 1; // next char
pos2 = line.find(";", pos); // find second semicolon char
s = line.substr(pos, pos2 - pos); // extract G value
color_names[nb_color_names].G = std::stoi(s); // G in array
pos = pos2 + 1; // next char
pos2 = line.find(";", pos); // find third semicolon char
s = line.substr(pos, pos2 - pos); // extract B value
color_names[nb_color_names].B = std::stoi(s); // B in array
s = line.substr(pos2 + 1, line.length() - pos2); // color name is at the end of the line
color_names[nb_color_names].name = QString::fromStdString(s); // color name in array
}
names.close(); // close text file
}
else {
QMessageBox::critical(this, "Colors CSV file not found!", "You forgot to put 'color-names.csv' in the same folder as the executable! This tool will crash as soon as you quantize an image...");
}
/*for (int n = 0; n <= 24; n++) {
long double R, G, B;
HSLtoRGB(n * 15.0L / 360.0L, 1, 0.5, R, G, B);
CreateCIELabPalettefromRGB(round(R * 255.0L), round(G * 255.0L), round(B * 255.0L), 1000, 100, QString("%1").arg(n * 15, 3, 10, QChar('0')).toUtf8().constData(), true, 1, true);
}*/
/*for (int n = 0; n < nb_color_sectors; n++)
AnalyzeCIELabCurveImage(100, "LAB-palette-" + color_sectors[n].name + "-nogap");*/
}
void MainWindow::on_button_whats_this_clicked() // What's this function
{
QWhatsThis::enterWhatsThisMode();
}
void MainWindow::on_button_quit_clicked() // quit GUI
{
int quit = QMessageBox::question(this, "Quit this wonderful program", "Are you sure you want to quit?", QMessageBox::Yes|QMessageBox::No); // quit, are you sure ?
if (quit == QMessageBox::No) // don't quit !
return;
QCoreApplication::quit(); // quit
}
void MainWindow::on_button_compute_clicked() // compute dominant colors and result images
{
Compute();
}
void MainWindow::on_pushButton_color_complementary_clicked() // hide/show color scheme : complementary
{
OverlayWheel();
}
void MainWindow::on_pushButton_color_split_complementary_clicked() // hide/show color scheme : split-complementary
{
OverlayWheel();
}
void MainWindow::on_pushButton_color_analogous_clicked() // hide/show color scheme : analogous
{
OverlayWheel();
}
void MainWindow::on_pushButton_color_triadic_clicked() // hide/show color scheme : triadic
{
OverlayWheel();
}
void MainWindow::on_pushButton_color_tetradic_clicked() // hide/show color scheme : tetradic
{
OverlayWheel();
}
void MainWindow::on_pushButton_color_square_clicked() // hide/show color scheme : square
{
OverlayWheel();
}
void MainWindow::on_button_reset_params_clicked() // reset all filter values in GUI
{
ui->horizontalSlider_nb_blacks->setValue(blacksLimitIni); // sliders
ui->horizontalSlider_nb_grays->setValue(graysLimitIni);
ui->horizontalSlider_nb_whites->setValue(whitesLimitIni);
ui->horizontalSlider_regroup_distance->setValue(regroupDistanceIni);
ui->horizontalSlider_filter_percentage->setValue(filterPercentageIni);
ui->horizontalSlider_mean_shift_spatial->setValue(nbMeanShiftSpatialIni);
ui->horizontalSlider_mean_shift_color->setValue(nbMeanShiftColorIni);
ui->horizontalSlider_sectored_means_levels->setValue(nbSectoredMeansLevels);
ui->checkBox_regroup->setChecked(true); // checkboxes
ui->checkBox_filter_grays->setChecked(true);
ui->checkBox_filter_percent->setChecked(true);
ui->spinBox_nb_palettes->setValue(12);
ui->checkBox_sectored_means_levels->setChecked(false);
ui->checkBox_filter_grays->setChecked(true);
}
void MainWindow::on_checkBox_palette_scale_stateChanged(int state) // scale or not the palette colored zones
{
pickedColor = cv::Vec3b(-1, -1, -1); // dummy values
ComputePaletteImage(); // new palette image
ShowResults(); // show it
}
void MainWindow::on_button_palette_plus_clicked() // add one color to palette image in the limits of found colors
{
if (nb_palettes == nb_palettes_found) // no more than maximum !
return;
nb_palettes++; // one more color
ui->spinBox_nb_palettes->setValue(nb_palettes); // show new number of colors
pickedColor = cv::Vec3b(-1, -1, -1); // dummy values
if (palettes[nb_palettes - 1].name == "Not computed") // compute color name if not already done
FindColorName(nb_palettes - 1);
ComputePaletteImage(); // new palette image
ShowResults(); // show it
}
void MainWindow::on_button_palette_minus_clicked() // delete one color from palette image
{
if (nb_palettes == 1) // no less than 1 !
return;
nb_palettes--; // one less color
ui->spinBox_nb_palettes->setValue(nb_palettes); // show new number of colors
pickedColor = cv::Vec3b(-1, -1, -1); // dummy values
ComputePaletteImage(); // new palette image
ShowResults(); // show it
}
void MainWindow::on_comboBox_sort_currentIndexChanged(int index) // sort palette
{
SortPalettes(); // call sorting method and palette image reconstruction
pickedColor = cv::Vec3b(-1, -1, -1); // dummy values
ComputePaletteImage(); // new palette image
ShowResults(); // show it
}
void MainWindow::on_verticalSlider_circle_size_valueChanged(int value) // change color base circle size in wheel
{
SetCircleSize(value);
}
void MainWindow::on_horizontalSlider_filter_percentage_valueChanged(int value) // update corresponding label
{
ui->label_filter_percentage->setText(QString::number(value) + "%");
}
void MainWindow::on_horizontalSlider_nb_blacks_valueChanged(int value) // update corresponding label
{
ui->label_nb_blacks->setText(QString::number(value) + "%");
blacksLimit = (long double)(value);
}
void MainWindow::on_horizontalSlider_nb_grays_valueChanged(int value) // update corresponding label
{
ui->label_nb_grays->setText(QString::number(value) + "%");
graysLimit = (long double)(value);
}
void MainWindow::on_horizontalSlider_nb_whites_valueChanged(int value) // update corresponding label
{
ui->label_nb_whites->setText(QString::number(value) + "%");
whitesLimit = (long double)(value);
}
void MainWindow::on_horizontalSlider_regroup_distance_valueChanged(int value) // update corresponding label
{
ui->label_regroup_distance->setText(QString::number(value));
}
void MainWindow::on_horizontalSlider_mean_shift_spatial_valueChanged(int value) // update corresponding label
{
ui->label_mean_shift_spatial->setText(QString::number(value));
}
void MainWindow::on_horizontalSlider_mean_shift_color_valueChanged(int value) // update corresponding label
{
ui->label_mean_shift_color->setText(QString::number(value));
}
void MainWindow::on_horizontalSlider_sectored_means_levels_valueChanged(int value) // update corresponding label
{
ui->label_sectored_means_levels->setText(QString::number(value));
}
void MainWindow::on_checkBox_color_approximate_stateChanged(int state) // if limiting to 12 hues
{
if (ui->checkBox_color_approximate->isChecked()) {
ui->checkBox_color_borders->blockSignals(true);
ui->checkBox_color_borders->setChecked(true);
ui->checkBox_color_borders->blockSignals(false);
}
ui->checkBox_color_borders->setVisible(!ui->checkBox_color_approximate->isChecked());
}
void MainWindow::on_checkBox_color_borders_stateChanged(int state) // projection of color circleson wheel borders
{
if (ui->checkBox_color_approximate->isChecked()) {
ui->checkBox_color_borders->blockSignals(true);
ui->checkBox_color_borders->setChecked(true);
ui->checkBox_color_borders->blockSignals(false);
}
}
void MainWindow::on_radioButton_mean_shift_toggled() // mean-shift algorithm options
{
ui->frame_mean_shift_parameters->setVisible(ui->radioButton_mean_shift->isChecked());
}
void MainWindow::on_radioButton_sectored_means_toggled() // sectored-means algorithm options
{
ui->frame_sectored_means_parameters->setVisible(ui->radioButton_sectored_means->isChecked());
}
void MainWindow::on_button_zoom_image_clicked() // zoom image
{
zoom = !zoom; // change zoom state : false = entire image, true = 1:1 scale
ShowResults();
}
void MainWindow::OverlayWheel() // draw layers on wheel
{
wheel.copyTo(wheel_result); // get what's already drawn on wheel
// add found color schemes to wheel image
if (ui->pushButton_color_complementary->isChecked())
cv::addWeighted(wheel_mask_complementary, 1, wheel_result, 1, 0, wheel_result, -1);
if (ui->pushButton_color_split_complementary->isChecked())
cv::addWeighted(wheel_mask_split_complementary, 1, wheel_result, 1, 0, wheel_result, -1);
if (ui->pushButton_color_analogous->isChecked())
cv::addWeighted(wheel_mask_analogous, 0.99, wheel_result, 1, 0, wheel_result, -1);
if (ui->pushButton_color_triadic->isChecked())
cv::addWeighted(wheel_mask_triadic, 0.99, wheel_result, 1, 0, wheel_result, -1);
if (ui->pushButton_color_tetradic->isChecked())
cv::addWeighted(wheel_mask_tetradic, 0.99, wheel_result, 1, 0, wheel_result, -1);
if (ui->pushButton_color_square->isChecked())
cv::addWeighted(wheel_mask_square, 0.99, wheel_result, 1, 0, wheel_result, -1);
ui->label_wheel->setPixmap(Mat2QPixmap(wheel_result)); // update wheel view
}
void MainWindow::on_button_analyze_clicked() // analyze image to find color schemes and other information
{
if (!computed) { // nothing computed yet = get out
QMessageBox::critical(this, "Nothing to do!", "You have to load then compute before analyzing an image");
return;
}
QApplication::setOverrideCursor(Qt::WaitCursor); // wait cursor
timer.start(); // reinit timer
ShowTimer(true); // show it
qApp->processEvents();
ShowWheel(); // get a fresh empty wheel
// init color schemes layers to empty
wheel_mask_complementary = cv::Mat::zeros(cv::Size(wheel.cols, wheel.rows), CV_8UC3);
wheel_mask_split_complementary = cv::Mat::zeros(cv::Size(wheel.cols, wheel.rows), CV_8UC3);
wheel_mask_analogous = cv::Mat::zeros(cv::Size(wheel.cols, wheel.rows), CV_8UC3);
wheel_mask_triadic = cv::Mat::zeros(cv::Size(wheel.cols, wheel.rows), CV_8UC3);
wheel_mask_tetradic = cv::Mat::zeros(cv::Size(wheel.cols, wheel.rows), CV_8UC3);
wheel_mask_square = cv::Mat::zeros(cv::Size(wheel.cols, wheel.rows), CV_8UC3);
// only keep colors in palette for schemes discovery : no grays, no whites, no blacks + keep significant percentage only
struct_palette palet[nb_palettes_max]; // temp copy of palette
int nb_palet = 0; // index of this copy
for (int n = 0; n < nb_palettes; n++) // parse original palette
if ((palettes[n].distanceBlack > blacksLimit) and (palettes[n].distanceWhite > whitesLimit) and (palettes[n].distanceGray > graysLimit)
and (palettes[n].percentage >= double(ui->spinBox_color_percentage->value()) / 100.0)) { // test chroma, lightness and percentage
palet[nb_palet] = palettes[n]; // copy color value
if (ui->checkBox_color_approximate->isChecked()) { // only 12 hues if needed
double hPrime = int(round(palet[nb_palet].H * 12.0)) % 12; // get a rounded value in [0..11]
palet[nb_palet].H = hPrime / 12.0; // rewrite rounded value to palette
}
nb_palet++; // temp palette index
}
// draw hues on wheel external circle
for (int n = 0; n < nb_palet; n++) { // parse temp palette
long double S = 1; // max chroma and normal lightness
long double L = 0.5;
long double R, G, B;
HSLtoRGB(palet[n].H, S, L, R, G, B); // convert maxed current hue to RGB
DrawOnWheelBorder(int(round(R * 255.0)), int(round(G * 255.0)), int(round(B * 255.0)), 10, true); // draw a black dot on external circle of wheel
}
// compute angles between dots
long double H_max = 0; // to keep maximum angle between all dots
for (int x = 0; x < nb_palet; x++) { // populate double-entry angles array
for (int y = 0; y < nb_palet; y++)
if (x !=y) { // no angle between a dot and itself !
angles[x][y] = Angle::DifferenceDeg(Angle::NormalizedToDeg(palet[x].H), Angle::NormalizedToDeg(palet[y].H)); // angle between 2 dots
if (angles[x][y] > H_max) // get maximum angle difference
H_max = angles[x][y];
}
else
angles[x][y] = -1000.0; // dummy value
}
bool complementaryFound = false, analogousFound = false, triadicFound = false,
splitComplementaryFound = false, tetradicFound = false, squareFound = false; // indicators of found color schemes
long double colorRadius1, colorRadius2, colorRadius3, colorRadius4; // radius of color dots on the wheel
for (int x = 0; x < nb_palet; x++) { // parse the angles array
for (int y = 0; y < nb_palet; y++) {
if (abs(180.0 - angles[x][y]) <= 25) { // complementary : a 180° angle
// first dot coordinates
if (ui->checkBox_color_borders->isChecked()) // draw on dot or external circle ?
colorRadius1 = wheel_radius; // external circle
else
colorRadius1 = (long double)(wheel_radius_center) * palet[x].L; // color distance from center
long double angle1 = -Angle::NormalizedToRad(palet[x].H + 0.25); // angle convert normalized value to radians + shift to have red on top
long double xOffset1 = wheel_center.x + cosf(angle1) * colorRadius1; // position from center of circle
long double yOffset1 = wheel_center.y + sinf(angle1) * colorRadius1;
// second dot coordinates
if (ui->checkBox_color_borders->isChecked())
colorRadius2 = wheel_radius;
else
colorRadius2 = (long double)(wheel_radius_center) * palet[y].L; // color distance from center
long double angle2 = -Angle::NormalizedToRad(palet[y].H + 0.25); // angle convert normalized value to radians
long double xOffset2 = wheel_center.x + cosf(angle2) * colorRadius2; // position from center of circle
long double yOffset2 = wheel_center.y + sinf(angle2) * colorRadius2;
// draw a line between the two dots : red with white inside
cv::line(wheel_mask_complementary, cv::Point(xOffset1, yOffset1), cv::Point(xOffset2, yOffset2), cv::Vec3b(0, 0, 255), 5, cv::LINE_AA);
cv::line(wheel_mask_complementary, cv::Point(xOffset1, yOffset1), cv::Point(xOffset2, yOffset2), cv::Vec3b(255, 255, 255), 1, cv::LINE_AA);
complementaryFound = true; // color scheme found !
}
if ((x != y) and (abs(30.0 - angles[x][y]) < 15)) { // analogous : 3 dots, separated by ~30°
// no need to add much comments now, it is all the same than before
// first dot coordinates
if (ui->checkBox_color_borders->isChecked())
colorRadius1 = wheel_radius;
else
colorRadius1 = (long double)(wheel_radius_center) * palet[x].L; // color distance from center
long double angle1 = -Angle::NormalizedToRad(palet[x].H + 0.25); // angle convert normalized value to radians + shift to have red on top
long double xOffset1 = wheel_center.x + cosf(angle1) * colorRadius1; // position from center of circle
long double yOffset1 = wheel_center.y + sinf(angle1) * colorRadius1;
// second dot coordinates
if (ui->checkBox_color_borders->isChecked())
colorRadius2 = wheel_radius;
else
colorRadius2 = (long double)(wheel_radius_center) * palet[y].L; // color distance from center
long double angle2 = -Angle::NormalizedToRad(palet[y].H + 0.25); // angle convert normalized value to radians + shift to have red on top
long double xOffset2 = wheel_center.x + cosf(angle2) * colorRadius2; // position from center of circle
long double yOffset2 = wheel_center.y + sinf(angle2) * colorRadius2;
for (int z = 0; z < nb_palet; z++) // look for 3rd dot
if ((z != x) and (z != y) and (abs(30.0 - angles[y][z]) < 15) and (angles[x][z] > 45)) { // with the angle : ~30°
// 3rd dot coordinates
if (ui->checkBox_color_borders->isChecked())
colorRadius3 = wheel_radius;
else
colorRadius3 = (long double)(wheel_radius_center) * palet[z].L; // color distance from center
long double angle3 = -Angle::NormalizedToRad(palet[z].H + 0.25); // angle convert normalized value to radians + shift to have red on top
long double xOffset3 = wheel_center.x + cosf(angle3) * colorRadius3; // position from center of circle
long double yOffset3 = wheel_center.y + sinf(angle3) * colorRadius3;
// draw lines between the 3 dots : green with white inside
cv::line(wheel_mask_analogous, cv::Point(xOffset1, yOffset1), cv::Point(xOffset2, yOffset2), cv::Vec3b(0, 255, 0), 5, cv::LINE_AA);
cv::line(wheel_mask_analogous, cv::Point(xOffset2, yOffset2), cv::Point(xOffset3, yOffset3), cv::Vec3b(0, 255, 0), 5, cv::LINE_AA);
cv::line(wheel_mask_analogous, cv::Point(xOffset1, yOffset1), cv::Point(xOffset2, yOffset2), cv::Vec3b(255, 255, 255), 1, cv::LINE_AA);
cv::line(wheel_mask_analogous, cv::Point(xOffset2, yOffset2), cv::Point(xOffset3, yOffset3), cv::Vec3b(255, 255, 255), 1, cv::LINE_AA);
analogousFound = true;
}
}
if (abs(120.0 - angles[x][y]) <= 25) { // triadic : 3 dots equally distanced => angle = 120°
// 1st dot coordinates
if (ui->checkBox_color_borders->isChecked())
colorRadius1 = wheel_radius;
else
colorRadius1 = (long double)(wheel_radius_center) * palet[x].L; // color distance from center
long double angle1 = -Angle::NormalizedToRad(palet[x].H + 0.25); // angle convert normalized value to radians + shift to have red on top
long double xOffset1 = wheel_center.x + cosf(angle1) * colorRadius1; // position from center of circle
long double yOffset1 = wheel_center.y + sinf(angle1) * colorRadius1;
// 2nd dot coordinates
if (ui->checkBox_color_borders->isChecked())
colorRadius2 = wheel_radius;
else
colorRadius2 = (long double)(wheel_radius_center) * palet[y].L; // color distance from center
long double angle2 = -Angle::NormalizedToRad(palet[y].H + 0.25); // angle convert normalized value to radians + shift to have red on top
long double xOffset2 = wheel_center.x + cosf(angle2) * colorRadius2; // position from center of circle
long double yOffset2 = wheel_center.y + sinf(angle2) * colorRadius2;
for (int z = 0; z < nb_palet; z++) // look for 3rd dot
if ((z != x) and (z != y) and (abs(120.0 - angles[y][z]) <= 25) and (angles[x][z] > 90)) { // angle = 120°
// 3rd dot coordinates
if (ui->checkBox_color_borders->isChecked())
colorRadius3 = wheel_radius;
else
colorRadius3 = (long double)(wheel_radius_center) * palet[z].L; // color distance from center
long double angle3 = -Angle::NormalizedToRad(palet[z].H + 0.25); // angle convert normalized value to radians + shift to have red on top
long double xOffset3 = wheel_center.x + cosf(angle3) * colorRadius3; // position from center of circle
long double yOffset3 = wheel_center.y + sinf(angle3) * colorRadius3;
// draw lines between the 3 dots : blue with white inside
cv::line(wheel_mask_triadic, cv::Point(xOffset1, yOffset1), cv::Point(xOffset2, yOffset2), cv::Vec3b(255, 0, 0), 5, cv::LINE_AA);
cv::line(wheel_mask_triadic, cv::Point(xOffset1, yOffset1), cv::Point(xOffset3, yOffset3), cv::Vec3b(255, 0, 0), 5, cv::LINE_AA);
cv::line(wheel_mask_triadic, cv::Point(xOffset3, yOffset3), cv::Point(xOffset2, yOffset2), cv::Vec3b(255, 0, 0), 5, cv::LINE_AA);
cv::line(wheel_mask_triadic, cv::Point(xOffset1, yOffset1), cv::Point(xOffset2, yOffset2), cv::Vec3b(255, 255, 255), 1, cv::LINE_AA);
cv::line(wheel_mask_triadic, cv::Point(xOffset1, yOffset1), cv::Point(xOffset3, yOffset3), cv::Vec3b(255, 255, 255), 1, cv::LINE_AA);
cv::line(wheel_mask_triadic, cv::Point(xOffset3, yOffset3), cv::Point(xOffset2, yOffset2), cv::Vec3b(255, 255, 255), 1, cv::LINE_AA);
triadicFound = true;
}
}
if (abs(60.0 - angles[x][y]) <= 25) { // split-complementary : one dot with two opposites separated by ~60°
// 1st dot coordinates
if (ui->checkBox_color_borders->isChecked())
colorRadius1 = wheel_radius;
else
colorRadius1 = (long double)(wheel_radius_center) * palet[x].L; // color distance from center
long double angle1 = -Angle::NormalizedToRad(palet[x].H + 0.25); // angle convert normalized value to radians + shift to have red on top
long double xOffset1 = wheel_center.x + cosf(angle1) * colorRadius1; // position from center of circle
long double yOffset1 = wheel_center.y + sinf(angle1) * colorRadius1;
// 2nd dot coordinates
if (ui->checkBox_color_borders->isChecked())
colorRadius2 = wheel_radius;
else
colorRadius2 = (long double)(wheel_radius_center) * palet[y].L; // color distance from center
long double angle2 = -Angle::NormalizedToRad(palet[y].H + 0.25); // angle convert normalized value to radians + shift to have red on top
long double xOffset2 = wheel_center.x + cosf(angle2) * colorRadius2; // position from center of circle
long double yOffset2 = wheel_center.y + sinf(angle2) * colorRadius2;
for (int z = 0; z < nb_palet; z++) // looi for 3rd dot
if ((z != x) and (z != y) and (abs(150.0 - angles[y][z]) <= 15) and (angles[x][z] > 130)) { // this time with angle ~150°
// 3rd dot coordinates
if (ui->checkBox_color_borders->isChecked())
colorRadius3 = wheel_radius;
else
colorRadius3 = (long double)(wheel_radius_center) * palet[z].L; // color distance from center
long double angle3 = -Angle::NormalizedToRad(palet[z].H + 0.25); // angle convert normalized value to radians + shift to have red on top
long double xOffset3 = wheel_center.x + cosf(angle3) * colorRadius3; // position from center of circle
long double yOffset3 = wheel_center.y + sinf(angle3) * colorRadius3;
// draw lines between the dots : cyan with black inside
cv::line(wheel_mask_split_complementary, cv::Point(xOffset1, yOffset1), cv::Point(xOffset2, yOffset2), cv::Vec3b(255, 255, 0), 5, cv::LINE_AA);
cv::line(wheel_mask_split_complementary, cv::Point(xOffset1, yOffset1), cv::Point(xOffset3, yOffset3), cv::Vec3b(255, 255, 0), 5, cv::LINE_AA);
cv::line(wheel_mask_split_complementary, cv::Point(xOffset3, yOffset3), cv::Point(xOffset2, yOffset2), cv::Vec3b(255, 255, 0), 5, cv::LINE_AA);
cv::line(wheel_mask_split_complementary, cv::Point(xOffset1, yOffset1), cv::Point(xOffset2, yOffset2), cv::Vec3b(0, 0, 0), 1, cv::LINE_AA);
cv::line(wheel_mask_split_complementary, cv::Point(xOffset1, yOffset1), cv::Point(xOffset3, yOffset3), cv::Vec3b(0, 0, 0), 1, cv::LINE_AA);
cv::line(wheel_mask_split_complementary, cv::Point(xOffset3, yOffset3), cv::Point(xOffset2, yOffset2), cv::Vec3b(0, 0, 0), 1, cv::LINE_AA);
splitComplementaryFound = true;
}
}
if (abs(60.0 - angles[x][y]) <= 25) { // tetradic : a rectangle of 2 pairs of complementary dots separated by ~60°
// 1st dot coordinates
if (ui->checkBox_color_borders->isChecked())
colorRadius1 = wheel_radius;
else
colorRadius1 = (long double)(wheel_radius_center) * palet[x].L; // color distance from center
long double angle1 = -Angle::NormalizedToRad(palet[x].H + 0.25); // angle convert normalized value to radians + shift to have red on top
long double xOffset1 = wheel_center.x + cosf(angle1) * colorRadius1; // position from center of circle
long double yOffset1 = wheel_center.y + sinf(angle1) * colorRadius1;
// 2nd dot coordinatescv::LINE_AA
if (ui->checkBox_color_borders->isChecked())
colorRadius2 = wheel_radius;
else
colorRadius2 = (long double)(wheel_radius_center) * palet[y].L; // color distance from center
long double angle2 = -Angle::NormalizedToRad(palet[y].H + 0.25); // angle convert normalized value to radians + shift to have red on top
long double xOffset2 = wheel_center.x + cosf(angle2) * colorRadius2; // position from center of circle
long double yOffset2 = wheel_center.y + sinf(angle2) * colorRadius2;
for (int z = 0; z < nb_palet; z++) // look for 3rd dot
if ((z != x) and (z != y) and (abs(120.0 - angles[y][z]) <= 25) and (angles[z][x] > 140)) { // with an angle ~120°
// 3rd dot coordinates
if (ui->checkBox_color_borders->isChecked())
colorRadius3 = wheel_radius;
else
colorRadius3 = (long double)(wheel_radius_center) * palet[z].L; // color distance from center
long double angle3 = -Angle::NormalizedToRad(palet[z].H + 0.25); // angle convert normalized value to radians + shift to have red on top
long double xOffset3 = wheel_center.x + cosf(angle3) * colorRadius3; // position from center of circle
long double yOffset3 = wheel_center.y + sinf(angle3) * colorRadius3;
for (int w = 0; w < nb_palet; w++) // look for 4th dot
if ((w != x) and (w != y) and (w != z) and (abs(60.0 - angles[z][w]) <= 25) and (angles[y][w] > 140)) { // with an angle ~60°
// 4th dot coordinates
if (ui->checkBox_color_borders->isChecked())
colorRadius4 = wheel_radius;
else
colorRadius4 = (long double)(wheel_radius_center) * palet[w].L; // color distance from center
long double angle4 = -Angle::NormalizedToRad(palet[w].H + 0.25); // angle convert normalized value to radians + shift to have red on top
long double xOffset4 = wheel_center.x + cosf(angle4) * colorRadius4; // position from center of circle
long double yOffset4 = wheel_center.y + sinf(angle4) * colorRadius4;
// draw lines between the dots : violet with white inside
cv::line(wheel_mask_tetradic, cv::Point(xOffset1, yOffset1), cv::Point(xOffset2, yOffset2), cv::Vec3b(255, 0, 255), 5, cv::LINE_AA);
cv::line(wheel_mask_tetradic, cv::Point(xOffset2, yOffset2), cv::Point(xOffset3, yOffset3), cv::Vec3b(255, 0, 255), 5, cv::LINE_AA);
cv::line(wheel_mask_tetradic, cv::Point(xOffset3, yOffset3), cv::Point(xOffset4, yOffset4), cv::Vec3b(255, 0, 255), 5, cv::LINE_AA);
cv::line(wheel_mask_tetradic, cv::Point(xOffset4, yOffset4), cv::Point(xOffset1, yOffset1), cv::Vec3b(255, 0, 255), 5, cv::LINE_AA);
cv::line(wheel_mask_tetradic, cv::Point(xOffset1, yOffset1), cv::Point(xOffset2, yOffset2), cv::Vec3b(255, 255, 255), 1, cv::LINE_AA);
cv::line(wheel_mask_tetradic, cv::Point(xOffset2, yOffset2), cv::Point(xOffset3, yOffset3), cv::Vec3b(255, 255, 255), 1, cv::LINE_AA);
cv::line(wheel_mask_tetradic, cv::Point(xOffset3, yOffset3), cv::Point(xOffset4, yOffset4), cv::Vec3b(255, 255, 255), 1, cv::LINE_AA);
cv::line(wheel_mask_tetradic, cv::Point(xOffset4, yOffset4), cv::Point(xOffset1, yOffset1), cv::Vec3b(255, 255, 255), 1, cv::LINE_AA);
tetradicFound = true;
}
}
}
if (abs(90.0 - angles[x][y]) <= 25) { // square : almost the same as before, but all angles are equal ~90°
// 1st dot coordinates
if (ui->checkBox_color_borders->isChecked())
colorRadius1 = wheel_radius;
else
colorRadius1 = (long double)(wheel_radius_center) * palet[x].L; // color distance from center
long double angle1 = -Angle::NormalizedToRad(palet[x].H + 0.25); // angle convert normalized value to radians + shift to have red on top
long double xOffset1 = wheel_center.x + cosf(angle1) * colorRadius1; // position from center of circle
long double yOffset1 = wheel_center.y + sinf(angle1) * colorRadius1;
// 2nd dot coordinates
if (ui->checkBox_color_borders->isChecked())
colorRadius2 = wheel_radius;
else
colorRadius2 = (long double)(wheel_radius_center) * palet[y].L; // color distance from center
long double angle2 = -Angle::NormalizedToRad(palet[y].H + 0.25); // aangle convert normalized value to radians + shift to have red on top
long double xOffset2 = wheel_center.x + cosf(angle2) * colorRadius2; // position from center of circle
long double yOffset2 = wheel_center.y + sinf(angle2) * colorRadius2;
for (int z = 0; z < nb_palet; z++) // look for 3rd dot
if ((z != x) and (z != y) and (abs(90.0 - angles[y][z]) <= 25) and (angles[z][x] > 140)) { // angle ~90°
// 3rd dot coordinates
if (ui->checkBox_color_borders->isChecked())
colorRadius3 = wheel_radius;
else
colorRadius3 = (long double)(wheel_radius_center) * palet[z].L; // color distance from center
long double angle3 = -Angle::NormalizedToRad(palet[z].H + 0.25); // angle convert normalized value to radians + shift to have red on top
long double xOffset3 = wheel_center.x + cosf(angle3) * colorRadius3; // position from center of circle
long double yOffset3 = wheel_center.y + sinf(angle3) * colorRadius3;
for (int w = 0; w < nb_palet; w++) // look for 4th dot
if ((w != x) and (w != y) and (w != z) and (abs(90.0 - angles[z][w]) <= 25) and (angles[y][w] > 140)) { // angle ~90°
// 4th dot coordinates
if (ui->checkBox_color_borders->isChecked())
colorRadius4 = wheel_radius;
else
colorRadius4 = (long double)(wheel_radius_center) * palet[w].L; // color distance from center
long double angle4 = -Angle::NormalizedToRad(palet[w].H + 0.25); // angle convert normalized value to radians + shift to have red on top
long double xOffset4 = wheel_center.x + cosf(angle4) * colorRadius4; // position from center of circle
long double yOffset4 = wheel_center.y + sinf(angle4) * colorRadius4;
// draw lines between the dots : orange with white inside
cv::line(wheel_mask_square, cv::Point(xOffset1, yOffset1), cv::Point(xOffset2, yOffset2), cv::Vec3b(0, 127, 255), 5, cv::LINE_AA);
cv::line(wheel_mask_square, cv::Point(xOffset2, yOffset2), cv::Point(xOffset3, yOffset3), cv::Vec3b(0, 127, 255), 5, cv::LINE_AA);
cv::line(wheel_mask_square, cv::Point(xOffset3, yOffset3), cv::Point(xOffset4, yOffset4), cv::Vec3b(0, 127, 255), 5, cv::LINE_AA);
cv::line(wheel_mask_square, cv::Point(xOffset4, yOffset4), cv::Point(xOffset1, yOffset1), cv::Vec3b(0, 127, 255), 5, cv::LINE_AA);
cv::line(wheel_mask_square, cv::Point(xOffset1, yOffset1), cv::Point(xOffset2, yOffset2), cv::Vec3b(0, 0, 0), 1, cv::LINE_AA);
cv::line(wheel_mask_square, cv::Point(xOffset2, yOffset2), cv::Point(xOffset3, yOffset3), cv::Vec3b(0, 0, 0), 1, cv::LINE_AA);
cv::line(wheel_mask_square, cv::Point(xOffset3, yOffset3), cv::Point(xOffset4, yOffset4), cv::Vec3b(0, 0, 0), 1, cv::LINE_AA);
cv::line(wheel_mask_square, cv::Point(xOffset4, yOffset4), cv::Point(xOffset1, yOffset1), cv::Vec3b(0, 0, 0), 1, cv::LINE_AA);
squareFound = true;
}
}
}
}
}
// check found color schemes in UI
ui->pushButton_color_analogous->setChecked(analogousFound);
ui->pushButton_color_complementary->setChecked(complementaryFound);
ui->pushButton_color_split_complementary->setChecked(splitComplementaryFound);
ui->pushButton_color_square->setChecked(squareFound);
ui->pushButton_color_tetradic->setChecked(tetradicFound);
ui->pushButton_color_triadic->setChecked(triadicFound);
ui->label_color_analogous->setVisible(analogousFound);
ui->label_color_complementary->setVisible(complementaryFound);
ui->label_color_split_complementary->setVisible(splitComplementaryFound);
ui->label_color_square->setVisible(squareFound);
ui->label_color_tetradic->setVisible(tetradicFound);
ui->label_color_triadic->setVisible(triadicFound);
if (!(analogousFound or complementaryFound or splitComplementaryFound or squareFound or tetradicFound or triadicFound)) { // nothing found
if (H_max <= 40) // is there a monochromatic scheme ?
ui->pushButton_color_monochromatic->setChecked(true); // show it
}
// cold and warm colors, blacks whites and grays, color stats. Here we work on the original image without filters
cv::Vec3b RGB;
long double H, S, L;
int countCold = 0; // number of "cold" pixels
int countWarm = 0; // number of "warm" pixels
int countNeutralPlus = 0; // number of "neutral+" pixels (neutral but a bit "warm")
int countNeutralMinus = 0; // number of "neutral-" pixels (neutral but a bit "warm")
int countColors = 0; // number of "colored" pixels
int countBlack = 0; // number of "black" pixels
int countGray = 0; // number of "gray" pixels
int countWhite = 0; // number of "white" pixels
double countP = 0; // sum of perceived brightness
int countAll = image.rows * image.cols; // total number of pixels in image
int stats[nb_color_sectors] = {0}; // count of 24 main hues in wheel
for (int x = 0; x < image.cols; x++) // parse image
for (int y = 0; y < image.rows; y++) {
RGB = image.at<cv::Vec3b>(y, x); // get current color
long double C, h;
HSLChfromRGB((long double)(RGB[2]) / 255.0, (long double)(RGB[1]) / 255.0, (long double)(RGB[0]) / 255.0, H, S, L, C, h); // HSLCh from pixel
H = Angle::NormalizedToDeg(H); // Hue in degrees
int hPrime = WhichColorSector(H); // get color sector for this pixel
double P = PerceivedBrightnessRGB(double(RGB[2]) / 255.0, double(RGB[1]) / 255.0, double(RGB[0]) / 255.0); // perceived brightness
countP += P; // total Perceived brightness
long double dBlack = DistanceFromBlackRGB((long double)(RGB[2]) / 255.0, (long double)(RGB[1]) / 255.0, (long double)(RGB[0]) / 255.0); // distances from black, white and gray
long double dWhite = DistanceFromWhiteRGB((long double)(RGB[2]) / 255.0, (long double)(RGB[1]) / 255.0, (long double)(RGB[0]) / 255.0);
long double dGray = DistanceFromGrayRGB((long double)(RGB[2]) / 255.0, (long double)(RGB[1]) / 255.0, (long double)(RGB[0]) / 255.0);
if (dBlack < blacksLimit) { // black is considered cold
countCold++;
}
else { // color is not in blacks
if ((dWhite > whitesLimit) and (dGray > graysLimit)) { // is it really a color ?
countColors++; // one more color
stats[hPrime]++; // one more color in 12 colors stats
}
// cold/warm
if ((dWhite > 5) and (dGray > 5)) { // nor too gray or too white
if ((H > 80) and (H <= 150)) // neutral+
countNeutralPlus++;
else
if ((H > 150) and (H <= 270)) // cold
countCold++;
else
if ((H > 270) and (H <= 330)) // neutral-
countNeutralMinus++;
else
countWarm++;
}
else { // poxel too white or too gray
countCold++;
}
}
// increase black or white or gray counter
if (dBlack < blacksLimit) // blacks
countBlack++;
else if (dWhite < whitesLimit) // whites
countWhite++;
else if (dGray < graysLimit) // neutrals
countGray++;
}
// cold/warm : compared to colored pixels only
int maximum=std::max(std::max(std::max(countWarm,countCold), countNeutralPlus),countNeutralMinus); // which cold/warm value is the highest ?
QString coldAndWarm; // for display
if (countCold == maximum) { // more cold colors
if (double(countCold) / countAll * 100 > 70) // is the color really cold ?
coldAndWarm = "Cold "; //cold = very cool
else // no
coldAndWarm = "Cool ";
ui->label_color_cold_warm->setStyleSheet("QLabel{color:cyan;background-color:rgb(0,0,128);border: 2px inset #8f8f91;}"); // show relusts in gui
ui->label_color_cold_warm->setText(coldAndWarm + QString::number(double(countCold) / countAll * 100, 'f', 1) + "%");
}
else if (countWarm == maximum) { // more warm colors : orange on dark red background
if (double(countWarm) / countAll * 100 > 70) // is the color warm ?
coldAndWarm = "Hot "; // hot is very warm
else
coldAndWarm = "Warm ";
ui->label_color_cold_warm->setStyleSheet("QLabel{color:orange;background-color:rgb(128,0,0);border: 2px inset #8f8f91;}");
ui->label_color_cold_warm->setText(coldAndWarm + QString::number(double(countWarm) / countAll * 100, 'f', 1) + "%");
}
else if (countNeutralPlus == maximum) { // more neutral+ colors : yellow on gray background
coldAndWarm = "Neutral+ ";
ui->label_color_cold_warm->setStyleSheet("QLabel{color:yellow;background-color:rgb(128,128,128);border: 2px inset #8f8f91;}");
ui->label_color_cold_warm->setText(coldAndWarm + QString::number(double(countNeutralPlus) / countAll * 100, 'f', 1) + "%");
}
else { // more neutral- colors : purple on gray background
coldAndWarm = "Neutral- ";
ui->label_color_cold_warm->setStyleSheet("QLabel{color:purple;background-color:rgb(128,128,128);border: 2px inset #8f8f91;}");
ui->label_color_cold_warm->setText(coldAndWarm + QString::number(double(countNeutralMinus) / countAll * 100, 'f', 1) + "%");
}
// grays : compared to whole picture colors (colors + grays)
maximum=std::max(std::max(countGray,countBlack), countGray); // maximum value of grays
if (maximum == 0) { // no gray or black or white colors in image : red on gray background
ui->label_color_blacks->setStyleSheet("QLabel{color:rgb(255,0,0);background-color:rgb(64,64,64);border: 2px inset #8f8f91;text-align: center;}");
ui->label_color_blacks->setText("- - - - - -"); // no blacks
ui->label_color_whites->setStyleSheet("QLabel{color:rgb(255,0,0);background-color:rgb(64,64,64);border: 2px inset #8f8f91;text-align: center;}");
ui->label_color_whites->setText("- - - - - -"); // no whites
ui->label_color_grays->setStyleSheet("QLabel{color:rgb(255,0,0);background-color:rgb(64,64,64);border: 2px inset #8f8f91;text-align: center;}");
ui->label_color_grays->setText("- - - - - -"); // no grays
}
else { // blacks whites and gray values
ui->label_color_blacks->setStyleSheet("QLabel{color:rgb(224,224,224);background-color:rgb(64,64,64);border: 2px inset #8f8f91;text-align: center;}");
ui->label_color_blacks->setText("Dark " + QString::number(double(countBlack) / countAll * 100.0, 'f', 1) + "%");
ui->label_color_whites->setStyleSheet("QLabel{color:grey;background-color:rgb(255,255,255);border: 2px inset #8f8f91;text-align: center;}");
ui->label_color_whites->setText("Bright " + QString::number(double(countWhite) / countAll * 100.0, 'f', 1) + "%");
ui->label_color_grays->setStyleSheet("QLabel{color:rgb(224,224,224);background-color:rgb(128,128,128);border: 2px inset #8f8f91;text-align: center;}");
ui->label_color_grays->setText("Neutral " + QString::number(double(countGray) / countAll * 100.0, 'f', 1) + "%");
}
// colors
if (countColors > 0) { // colors found ?
cv::Mat tempQuantized = ImgRGBtoLab(palette);
std::vector<cv::Vec3f> palette_vec = DominantColorsEigenCIELab(tempQuantized, 1, tempQuantized); // get dominant palette, palette image and quantized image for 1 color only
long double L = palette_vec[0][0]; // CIELab value of global color
long double a = palette_vec[0][1];
long double b = palette_vec[0][2];
long double X, Y, Z, r, g;
// convert CIELAb value to RGB
LABtoXYZ(L, a, b, X, Y, Z);
XYZtoRGB(X, Y, Z, r, g, b);
int R = round(r * 255.0);
int G = round(g * 255.0);
int B = round(b * 255.0);
cv::Vec3b textColor;
if (IsRGBColorDark(R, G, B)) // text color and background must be in accordance
textColor = cv::Vec3b(255, 255, 255); // dark -> white text
else
textColor = cv::Vec3b(0, 0, 0); // bright -> dark text
ui->label_color_colored->setStyleSheet("QLabel{background-color:rgb("
+ QString::number(R) + "," + QString::number(G) + "," + QString::number(B) +
");color:rgb("
+ QString::number(textColor[2]) + "," + QString::number(textColor[1]) + "," + QString::number(textColor[0])
+");border: 2px inset #8f8f91;}"); // background = global color
ui->label_color_colored->setText("Colors " + QString::number(double(countColors) / countAll * 100.0, 'f', 1) + "%"); // colors value
}
else { // no colors to display
ui->label_color_colored->setStyleSheet("QLabel{background-color:rgb(64,64,64);color:rgb(255,0,0);border: 2px inset #8f8f91;}"); // red on dark gray background
ui->label_color_colored->setText("- - - - - -"); // no colors
}
// perceived brightness
QString brightness;
if (countP / countAll > 0.6) { // is the image bright ?
brightness = "Bright ";
ui->label_color_brightness->setStyleSheet("QLabel{background-color:rgb(255,255,255);color:rgb(128,128,128);border: 2px inset #8f8f91;}"); // gray on white background
} else
if (countP / countAll < 0.20) { // or is it dark ?
brightness = "Dark ";
ui->label_color_brightness->setStyleSheet("QLabel{background-color:rgb(0,0,0);color:rgb(164,164,164);border: 2px inset #8f8f91;}"); // gray on black background
} else {
brightness = "Normal "; // if not, it is normally bright
ui->label_color_brightness->setStyleSheet("QLabel{background-color:rgb(128,128,128);color:rgb(224,224,224);border: 2px inset #8f8f91;}"); // whitish on gray background
}
ui->label_color_brightness->setText(brightness + QString::number(countP / countAll * 100.0, 'f', 1) + "%"); // show result
// graph
if (countColors > 0) { // colors found ?
ui->color_graph->setVisible(true); // graph is visible
int w = ui->color_graph->width(); // width and height of graph
int h = ui->color_graph->height(); // vertical scale for bars
int margin = 5; // graph variables
int zero = h - margin; // vertical "zero"
int size_h = h - 4 * margin;
QPixmap pic(w, h); // surface on which to draw
pic.fill(Qt::lightGray); // fill it with ligh gray first
QPainter painter(&pic); // painter
painter.setRenderHint(QPainter::Antialiasing); // with antialias
double stat_max = double(*std::max_element(stats, stats + nb_color_sectors)); // vertical scale
for (int n = 0; n < nb_color_sectors; n++) { // for each color sector
painter.fillRect(QRectF(margin + n * 16 + 2, zero - 1, 14, -(round(double(stats[n]) / stat_max * double(size_h)))),
QColor(color_sectors[n].R, color_sectors[n].G, color_sectors[n].B)); // draw color bar
if (round(double(stats[n]) / stat_max * double(size_h)) == 0) { // no value ?
painter.setPen(QPen(Qt::black));
painter.drawLine(QPoint(margin + n * 16 + 2, zero + 2), QPoint(margin + n * 16 + 2 + 10, zero + 2)); // paint a black anti-bar
}
}
// draw axes
painter.setPen(QPen(Qt::black, 2)); // in black
painter.drawLine(QPointF(margin, zero), QPointF(w - margin, zero)); // horizontal
painter.drawLine(QPointF(margin, h - margin), QPointF(margin, margin)); // vertical
ui->color_graph->setPixmap(pic); // show graph
graph = QPixmap2Mat(pic);
}
else { // no colors to display
ui->color_graph->setVisible(false); // no graph
graph = cv::Mat();
}
// show Analyze frame and Color wheel
ui->frame_analysis->setVisible(true); // show analysis frame
OverlayWheel(); // show Wheel layers
ShowTimer(false); // show elapsed time
QApplication::restoreOverrideCursor(); // Restore cursor
}
/////////////////// Mouse events //////////////////////
void MainWindow::mousePressEvent(QMouseEvent *eventPress) // event triggered by a mouse click
{
mouseButton = eventPress->button(); // mouse button value
if (mouseButton == Qt::RightButton) { // right mouse button ?
if ((ui->label_image->underMouse()) or (ui->label_quantized->underMouse())) { // over Image or Quantized ?
zoom = !zoom; // change zoom
ShowResults(); // show change
}
}
bool color_found = false; // valid color found ?
cv::Vec3b oldPickedColor = pickedColor; // BGR values of old picked color
if (ui->label_wheel->underMouse()) { // mouse over color wheel ?
mouse_pos = ui->label_wheel->mapFromGlobal(QCursor::pos()); // mouse position
if ((mouseButton == Qt::LeftButton) and (!wheel.empty())) { // mouse left button clicked
pickedColor = wheel.at<cv::Vec3b>(mouse_pos.y(), mouse_pos.x()); // get BGR "color" under mouse cursor
color_found = true; // found !
}
}
if (ui->label_palette->underMouse()) { // mouse over palette ?
mouse_pos = ui->label_palette->mapFromGlobal(QCursor::pos()); // mouse position
if ((mouseButton == Qt::LeftButton) and (!palette.empty())) { // mouse left button clicked
const QPixmap* q = ui->label_palette->pixmap(); // stored reduced quantized image in GUI
int x = round(palette.cols * double(mouse_pos.x() - (ui->label_palette->width() - q->width()) / 2) / double(q->width())); // real x position in palette image
int y = round(palette.rows * double(mouse_pos.y() - (ui->label_palette->height() - q->height()) / 2) / double(q->height())); // real y position in palette image
if ((x > 0) and (x < palette.cols) and (y > 0) and (y < palette.rows)) { // not off-limits ?
pickedColor = palette.at<cv::Vec3b>(0, x); // pick color in palette image
color_found = true; // found !
}
else
color_found = false; // no color under mouse
}
}
if (ui->label_quantized->underMouse()) { // mouse over quantized image ?
mouse_pos = ui->label_quantized->mapFromGlobal(QCursor::pos()); // mouse position
if ((mouseButton == Qt::LeftButton) and (!quantized.empty())) { // mouse left button clicked
const QPixmap* q = ui->label_quantized->pixmap(); // stored reduced quantized image in GUI
double percentX = double(mouse_pos.x() - (ui->label_quantized->width() - q->width()) / 2) / double(q->width()); // real x and y position in quantized image
double percentY = double(mouse_pos.y() - (ui->label_quantized->height() - q->height()) / 2) / double(q->height());
if ((percentX >= 0) and (percentX < 1) and (percentY >= 0) and (percentY < 1)) { // not off-limits ?
pickedColor = quantized.at<cv::Vec3b>(round(percentY * quantized.rows), round(percentX * quantized.cols)); // pick color in quantized image at x,y
color_found = true; // found !
}
}
}
if (color_found) { // color picked ?
int R = pickedColor[2]; // RGB values
int G = pickedColor[1];
int B = pickedColor[0];
// find color in palette
bool found = false; // picked color found in palette ?
for (int n = 0; n < nb_palettes; n++) { // search in palette
if ((palettes[n].R == R) and (palettes[n].G == G) and (palettes[n].B == B)) { // identical RGB values found ?
QString value = QString::number(palettes[n].percentage * 100, 'f', 2) + "%"; // picked color percentage in quantized image