00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #include "QTColorParameterWindow.h"
00050
00051 #include "Color/ColorParameterSet.h"
00052 #include "Image/ImageProcessor.h"
00053 #include "Image/ByteImage.h"
00054 #include "gui/QTWindow.h"
00055 #include "gui/QTApplicationHandler.h"
00056
00057 #include <stdio.h>
00058 #include <string>
00059
00060 #include <qcheckbox.h>
00061 #include <qradiobutton.h>
00062 #include <qbuttongroup.h>
00063 #include <qpushbutton.h>
00064 #include <qlineedit.h>
00065 #include <qstring.h>
00066 #include <qslider.h>
00067 #include <qfiledialog.h>
00068 #include <qlabel.h>
00069
00070
00071
00072
00073
00074
00075
00076 CQTColorParameterWindow::CQTColorParameterWindow(int width, int height) : CQTWindow(width + 450, height)
00077 {
00078 this->width = width;
00079 this->height = height;
00080
00081
00082 m_pColorParameterSet = new CColorParameterSet();
00083
00084
00085 m_pLineEditColorParameterFile = 0;
00086
00087 m_color = eNone;
00088
00089 m_bUpdateParameterSet = true;
00090
00091
00092 QRadioButton *pCheckBoxColorSkin = new QRadioButton("Skin", this);
00093 pCheckBoxColorSkin->setFixedWidth(200);
00094 pCheckBoxColorSkin->move(width + 250, 20);
00095
00096 QRadioButton *pCheckBoxColorYellow = new QRadioButton("Yellow", this);
00097 pCheckBoxColorYellow->setFixedWidth(200);
00098 pCheckBoxColorYellow->move(width + 250, 45);
00099
00100 QRadioButton *pCheckBoxColorOrange = new QRadioButton("Orange", this);
00101 pCheckBoxColorOrange->setFixedWidth(200);
00102 pCheckBoxColorOrange->move(width + 250, 70);
00103
00104 QRadioButton *pCheckBoxColorRed = new QRadioButton("Red", this);
00105 pCheckBoxColorRed->setFixedWidth(200);
00106 pCheckBoxColorRed->move(width + 250, 95);
00107
00108 QRadioButton *pCheckBoxColorBlue = new QRadioButton("Blue", this);
00109 pCheckBoxColorBlue->setFixedWidth(200);
00110 pCheckBoxColorBlue->move(width + 350, 20);
00111
00112 QRadioButton *pCheckBoxColorGreen = new QRadioButton("Green", this);
00113 pCheckBoxColorGreen->setFixedWidth(200);
00114 pCheckBoxColorGreen->move(width + 350, 45);
00115
00116 QRadioButton *pCheckBoxColorCustom = new QRadioButton("Custom", this);
00117 pCheckBoxColorCustom->setFixedWidth(200);
00118 pCheckBoxColorCustom->move(width + 350, 70);
00119
00120
00121 m_pCheckBoxShowSegmentedImage = new QCheckBox("Show Segmented Image", this);
00122 m_pCheckBoxShowSegmentedImage->setChecked(false);
00123 m_pCheckBoxShowSegmentedImage->setFixedWidth(200);
00124 m_pCheckBoxShowSegmentedImage->move(width + 20, 120);
00125
00126 m_pCheckBoxShowRightImage = new QCheckBox("Show Right Image", this);
00127 m_pCheckBoxShowRightImage->setChecked(false);
00128 m_pCheckBoxShowRightImage->setFixedWidth(200);
00129 m_pCheckBoxShowRightImage->move(width + 20, 145);
00130
00131
00132 m_pColorButtonGroup = new QButtonGroup();
00133
00134 #if QT_VERSION >= 0x040000
00135 m_pColorButtonGroup->addButton(pCheckBoxColorSkin);
00136 m_pColorButtonGroup->addButton(pCheckBoxColorYellow);
00137 m_pColorButtonGroup->addButton(pCheckBoxColorOrange);
00138 m_pColorButtonGroup->addButton(pCheckBoxColorRed);
00139 m_pColorButtonGroup->addButton(pCheckBoxColorBlue);
00140 m_pColorButtonGroup->addButton(pCheckBoxColorGreen);
00141 m_pColorButtonGroup->addButton(pCheckBoxColorCustom);
00142 #else
00143 m_pColorButtonGroup->insert(pCheckBoxColorSkin);
00144 m_pColorButtonGroup->insert(pCheckBoxColorYellow);
00145 m_pColorButtonGroup->insert(pCheckBoxColorOrange);
00146 m_pColorButtonGroup->insert(pCheckBoxColorRed);
00147 m_pColorButtonGroup->insert(pCheckBoxColorBlue);
00148 m_pColorButtonGroup->insert(pCheckBoxColorGreen);
00149 m_pColorButtonGroup->insert(pCheckBoxColorCustom);
00150 #endif
00151
00152
00153 m_pSliderColorH = new QSlider(Qt::Horizontal, this);
00154 m_pSliderColorH->setRange(0, 180);
00155 m_pSliderColorH->setPageStep(1);
00156 m_pSliderColorH->setValue(124);
00157 m_pSliderColorH->setFixedWidth(180);
00158 m_pSliderColorH->setFixedHeight(20);
00159 m_pSliderColorH->move(width + 255, 130);
00160
00161 m_pSliderColorHT = new QSlider(Qt::Horizontal, this);
00162 m_pSliderColorHT->setRange(0, 180);
00163 m_pSliderColorHT->setPageStep(1);
00164 m_pSliderColorHT->setValue(26);
00165 m_pSliderColorHT->setFixedWidth(180);
00166 m_pSliderColorHT->setFixedHeight(20);
00167 m_pSliderColorHT->move(width + 255, 155);
00168
00169 m_pSliderColorMinS = new QSlider(Qt::Horizontal, this);
00170 m_pSliderColorMinS->setRange(0, 255);
00171 m_pSliderColorMinS->setPageStep(1);
00172 m_pSliderColorMinS->setValue(109);
00173 m_pSliderColorMinS->setFixedWidth(180);
00174 m_pSliderColorMinS->setFixedHeight(20);
00175 m_pSliderColorMinS->move(width + 255, 180);
00176
00177 m_pSliderColorMaxS = new QSlider(Qt::Horizontal, this);
00178 m_pSliderColorMaxS->setRange(0, 255);
00179 m_pSliderColorMaxS->setPageStep(1);
00180 m_pSliderColorMaxS->setValue(255);
00181 m_pSliderColorMaxS->setFixedWidth(180);
00182 m_pSliderColorMaxS->setFixedHeight(20);
00183 m_pSliderColorMaxS->move(width + 255, 205);
00184
00185 m_pSliderColorMinV = new QSlider(Qt::Horizontal, this);
00186 m_pSliderColorMinV->setRange(0, 255);
00187 m_pSliderColorMinV->setPageStep(1);
00188 m_pSliderColorMinV->setValue(0);
00189 m_pSliderColorMinV->setFixedWidth(180);
00190 m_pSliderColorMinV->setFixedHeight(20);
00191 m_pSliderColorMinV->move(width + 255, 230);
00192
00193 m_pSliderColorMaxV = new QSlider(Qt::Horizontal, this);
00194 m_pSliderColorMaxV->setRange(0, 255);
00195 m_pSliderColorMaxV->setPageStep(1);
00196 m_pSliderColorMaxV->setValue(255);
00197 m_pSliderColorMaxV->setFixedWidth(180);
00198 m_pSliderColorMaxV->setFixedHeight(20);
00199 m_pSliderColorMaxV->move(width + 255, 255);
00200
00201
00202 m_pLineEditColorParameterFile = new QLineEdit(this);
00203 m_pLineEditColorParameterFile->setText("files/colors_kitchen.txt");
00204 m_pLineEditColorParameterFile->setReadOnly(true);
00205 m_pLineEditColorParameterFile->setFixedWidth(250);
00206 m_pLineEditColorParameterFile->setFixedHeight(20);
00207 m_pLineEditColorParameterFile->move(width + 20, 390);
00208
00209
00210 QPushButton *pPrintColorParametersButton = new QPushButton(this);
00211 pPrintColorParametersButton->setText("Print Color Parameters");
00212 pPrintColorParametersButton->setFixedHeight(20);
00213 pPrintColorParametersButton->setFixedWidth(180);
00214 pPrintColorParametersButton->move(width + 20, 190);
00215
00216 QPushButton *pLoadColorParametersButton = new QPushButton(this);
00217 pLoadColorParametersButton->setText("Load Color Parameters");
00218 pLoadColorParametersButton->setFixedHeight(20);
00219 pLoadColorParametersButton->setFixedWidth(180);
00220 pLoadColorParametersButton->move(width + 20, 215);
00221
00222 QPushButton *pSaveColorParametersButton = new QPushButton(this);
00223 pSaveColorParametersButton->setText("Save Color Parameters");
00224 pSaveColorParametersButton->setFixedHeight(20);
00225 pSaveColorParametersButton->setFixedWidth(180);
00226 pSaveColorParametersButton->move(width + 20, 240);
00227
00228 QPushButton *pColorParameterFileBrowseButton = new QPushButton(this);
00229 pColorParameterFileBrowseButton->setText("Browse Color Parameter File");
00230 pColorParameterFileBrowseButton->setFixedHeight(20);
00231 pColorParameterFileBrowseButton->setFixedWidth(155);
00232 pColorParameterFileBrowseButton->move(width + 280, 390);
00233
00234 QLabel *pLabelH = new QLabel(this);
00235 pLabelH->setText("H");
00236 pLabelH->setFixedWidth(40);
00237 pLabelH->setFixedHeight(20);
00238 pLabelH->move(width + 210, 130);
00239
00240 QLabel *pLabelHTol = new QLabel(this);
00241 pLabelHTol->setText("H Tol");
00242 pLabelHTol->setFixedWidth(40);
00243 pLabelHTol->setFixedHeight(20);
00244 pLabelHTol->move(width + 210, 155);
00245
00246 QLabel *pLabelMinS = new QLabel(this);
00247 pLabelMinS->setText("S Min");
00248 pLabelMinS->setFixedWidth(40);
00249 pLabelMinS->setFixedHeight(20);
00250 pLabelMinS->move(width + 210, 180);
00251
00252 QLabel *pLabelMaxS = new QLabel(this);
00253 pLabelMaxS->setText("S Max");
00254 pLabelMaxS->setFixedWidth(40);
00255 pLabelMaxS->setFixedHeight(20);
00256 pLabelMaxS->move(width + 210, 205);
00257
00258 QLabel *pLabelMinV = new QLabel(this);
00259 pLabelMinV->setText("V Min");
00260 pLabelMinV->setFixedWidth(40);
00261 pLabelMinV->setFixedHeight(20);
00262 pLabelMinV->move(width + 210, 230);
00263
00264 QLabel *pLabelMaxV = new QLabel(this);
00265 pLabelMaxV->setText("V Max");
00266 pLabelMaxV->setFixedWidth(40);
00267 pLabelMaxV->setFixedHeight(20);
00268 pLabelMaxV->move(width + 210, 255);
00269
00270
00271 SliderValueChanged(0);
00272
00273
00274 ColorRadioButtonClicked(3);
00275 #if QT_VERSION >= 0x040000
00276 m_pColorButtonGroup->button(3)->click();
00277 #else
00278 m_pColorButtonGroup->setButton(3);
00279 #endif
00280
00281
00282 connect(m_pColorButtonGroup, SIGNAL(clicked(int)), this, SLOT(ColorRadioButtonClicked(int)));
00283 connect(pPrintColorParametersButton, SIGNAL(clicked()), this, SLOT(PrintColorParametersButtonClicked()));
00284 connect(pLoadColorParametersButton, SIGNAL(clicked()), this, SLOT(LoadColorParametersButtonClicked()));
00285 connect(pSaveColorParametersButton, SIGNAL(clicked()), this, SLOT(SaveColorParametersButtonClicked()));
00286 connect(pColorParameterFileBrowseButton, SIGNAL(clicked()), this, SLOT(ColorParameterFileBrowseButtonClicked()));
00287 connect(m_pSliderColorH, SIGNAL(valueChanged(int)), this, SLOT(SliderValueChanged(int)));
00288 connect(m_pSliderColorHT, SIGNAL(valueChanged(int)), this, SLOT(SliderValueChanged(int)));
00289 connect(m_pSliderColorMinS, SIGNAL(valueChanged(int)), this, SLOT(SliderValueChanged(int)));
00290 connect(m_pSliderColorMaxS, SIGNAL(valueChanged(int)), this, SLOT(SliderValueChanged(int)));
00291 connect(m_pSliderColorMinV, SIGNAL(valueChanged(int)), this, SLOT(SliderValueChanged(int)));
00292 connect(m_pSliderColorMaxV, SIGNAL(valueChanged(int)), this, SLOT(SliderValueChanged(int)));
00293 }
00294
00295 CQTColorParameterWindow::~CQTColorParameterWindow()
00296 {
00297 delete m_pColorParameterSet;
00298 }
00299
00300
00301
00302
00303
00304
00305 void CQTColorParameterWindow::LoadColorParametersButtonClicked()
00306 {
00307 #if QT_VERSION >= 0x040000
00308 m_pColorParameterSet->LoadFromFile(m_pLineEditColorParameterFile->text().toLatin1().constData());
00309 QAbstractButton *pActiveButton = m_pColorButtonGroup->checkedButton();
00310 #else
00311 m_pColorParameterSet->LoadFromFile(m_pLineEditColorParameterFile->text().latin1());
00312 QButton *pActiveButton = m_pColorButtonGroup->selected();
00313 #endif
00314
00315 if (pActiveButton)
00316 {
00317 ColorRadioButtonClicked(m_pColorButtonGroup->id(pActiveButton));
00318 }
00319 }
00320
00321 void CQTColorParameterWindow::SaveColorParametersButtonClicked()
00322 {
00323
00324 #if QT_VERSION >= 0x040000
00325 m_pColorParameterSet->SaveToFile(m_pLineEditColorParameterFile->text().toLatin1().constData());
00326 #else
00327 m_pColorParameterSet->SaveToFile(m_pLineEditColorParameterFile->text().latin1());
00328 #endif
00329 }
00330
00331 void CQTColorParameterWindow::PrintColorParametersButtonClicked()
00332 {
00333 printf("color parameters: (%i, %i, %i, %i, %i, %i)\n", m_pSliderColorH->value(), m_pSliderColorHT->value(), m_pSliderColorMinS->value(), m_pSliderColorMaxS->value(), m_pSliderColorMinV->value(), m_pSliderColorMaxV->value());
00334 }
00335
00336 void CQTColorParameterWindow::ColorParameterFileBrowseButtonClicked()
00337 {
00338 #if QT_VERSION >= 0x040000
00339 QString s = QFileDialog::getOpenFileName(this,tr("Choose a file"),"",tr("Text (*.txt)") );
00340 #else
00341 QString s = QFileDialog::getOpenFileName("", "Text (*.txt)", this, "Open File Dialog", "Choose a file");
00342 #endif
00343
00344 if (s.length() > 0)
00345 m_pLineEditColorParameterFile->setText(s);
00346 }
00347
00348 void CQTColorParameterWindow::SliderValueChanged(int nValue)
00349 {
00350 if (!m_bUpdateParameterSet)
00351 return;
00352
00353 if (m_color == eNone)
00354 {
00355 custom_par1 = m_pSliderColorH->value();
00356 custom_par2 = m_pSliderColorHT->value();
00357 custom_par3 = m_pSliderColorMinS->value();
00358 custom_par4 = m_pSliderColorMaxS->value();
00359 custom_par5 = m_pSliderColorMinV->value();
00360 custom_par6 = m_pSliderColorMaxV->value();
00361 }
00362 else
00363 {
00364 m_pColorParameterSet->SetColorParameters(m_color,
00365 m_pSliderColorH->value(),
00366 m_pSliderColorHT->value(),
00367 m_pSliderColorMinS->value(),
00368 m_pSliderColorMaxS->value(),
00369 m_pSliderColorMinV->value(),
00370 m_pSliderColorMaxV->value()
00371 );
00372 }
00373 }
00374
00375 void CQTColorParameterWindow::ColorRadioButtonClicked(int id)
00376 {
00377 m_bUpdateParameterSet = false;
00378
00379 if (id >= 0 && id <= 5)
00380 {
00381 switch (id)
00382 {
00383 case 0: m_color = eSkin; break;
00384 case 1: m_color = eYellow; break;
00385 case 2: m_color = eOrange; break;
00386 case 3: m_color = eRed; break;
00387 case 4: m_color = eBlue; break;
00388 case 5: m_color = eGreen; break;
00389 }
00390
00391 m_pSliderColorH->setValue(m_pColorParameterSet->GetColorParameters(m_color)[0]);
00392 m_pSliderColorHT->setValue(m_pColorParameterSet->GetColorParameters(m_color)[1]);
00393 m_pSliderColorMinS->setValue(m_pColorParameterSet->GetColorParameters(m_color)[2]);
00394 m_pSliderColorMaxS->setValue(m_pColorParameterSet->GetColorParameters(m_color)[3]);
00395 m_pSliderColorMinV->setValue(m_pColorParameterSet->GetColorParameters(m_color)[4]);
00396 m_pSliderColorMaxV->setValue(m_pColorParameterSet->GetColorParameters(m_color)[5]);
00397 }
00398 else if (id == 6)
00399 {
00400 m_pSliderColorH->setValue(custom_par1);
00401 m_pSliderColorHT->setValue(custom_par2);
00402 m_pSliderColorMinS->setValue(custom_par3);
00403 m_pSliderColorMaxS->setValue(custom_par4);
00404 m_pSliderColorMinV->setValue(custom_par5);
00405 m_pSliderColorMaxV->setValue(custom_par6);
00406 m_color = eNone;
00407 }
00408
00409 m_bUpdateParameterSet = true;
00410 }
00411
00412 void CQTColorParameterWindow::Update(const CByteImage * const *ppInputImages, int nImages)
00413 {
00414 const int nIndex = (nImages == 1 || !m_pCheckBoxShowRightImage->isChecked()) ? 0 : 1;
00415
00416 CByteImage hsv_image(ppInputImages[nIndex]);
00417 CByteImage result_image(ppInputImages[nIndex]->width, ppInputImages[nIndex]->height, CByteImage::eGrayScale);
00418
00419 ImageProcessor::CalculateHSVImage(ppInputImages[nIndex], &hsv_image);
00420 ImageProcessor::FilterHSV(&hsv_image, &result_image, m_pSliderColorH->value(), m_pSliderColorHT->value(), m_pSliderColorMinS->value(), m_pSliderColorMaxS->value(), m_pSliderColorMinV->value(), m_pSliderColorMaxV->value());
00421 ImageProcessor::Erode(&result_image, &result_image);
00422 ImageProcessor::Dilate(&result_image, &result_image);
00423
00424 if (m_pCheckBoxShowSegmentedImage->isChecked())
00425 DrawImage(&result_image);
00426 else
00427 DrawImage(ppInputImages[nIndex]);
00428 }
00429
00430 const CColorParameterSet* CQTColorParameterWindow::GetColorParameterSet() const
00431 {
00432 return m_pColorParameterSet;
00433 }