IVT
QTColorParameterWindow.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: QTColorParameterWindow.cpp
37 // Author: Pedram Azad
38 // Date: 2007
39 // ****************************************************************************
40 // Changes: 13.06.2008, Miguel Bernal Marin
41 // * Updated code to be compatible with Qt4
42 // ****************************************************************************
43 
44 
45 // ****************************************************************************
46 // Includes
47 // ****************************************************************************
48 
49 #include "QTColorParameterWindow.h"
50 
52 #include "Image/ImageProcessor.h"
53 #include "Image/ByteImage.h"
54 #include "gui/QTWindow.h"
56 
57 #include <stdio.h>
58 #include <string>
59 
60 #include <qcheckbox.h>
61 #include <qradiobutton.h>
62 #include <qbuttongroup.h>
63 #include <qpushbutton.h>
64 #include <qlineedit.h>
65 #include <qstring.h>
66 #include <qslider.h>
67 #include <qfiledialog.h>
68 #include <qlabel.h>
69 
70 
71 
72 // ****************************************************************************
73 // Constructor / Destructor
74 // ****************************************************************************
75 
76 CQTColorParameterWindow::CQTColorParameterWindow(int width, int height) : CQTWindow(width + 450, height)
77 {
78  this->width = width;
79  this->height = height;
80 
81  // color parameter set
82  m_pColorParameterSet = new CColorParameterSet();
83 
84  // pointers to line edit fields
85  m_pLineEditColorParameterFile = 0;
86 
87  m_color = eNone;
88 
89  m_bUpdateParameterSet = true;
90 
91  // radio buttons
92  QRadioButton *pCheckBoxColorSkin = new QRadioButton("Skin", this);
93  pCheckBoxColorSkin->setFixedWidth(200);
94  pCheckBoxColorSkin->move(width + 250, 20);
95 
96  QRadioButton *pCheckBoxColorYellow = new QRadioButton("Yellow", this);
97  pCheckBoxColorYellow->setFixedWidth(200);
98  pCheckBoxColorYellow->move(width + 250, 45);
99 
100  QRadioButton *pCheckBoxColorOrange = new QRadioButton("Orange", this);
101  pCheckBoxColorOrange->setFixedWidth(200);
102  pCheckBoxColorOrange->move(width + 250, 70);
103 
104  QRadioButton *pCheckBoxColorRed = new QRadioButton("Red", this);
105  pCheckBoxColorRed->setFixedWidth(200);
106  pCheckBoxColorRed->move(width + 250, 95);
107 
108  QRadioButton *pCheckBoxColorBlue = new QRadioButton("Blue", this);
109  pCheckBoxColorBlue->setFixedWidth(200);
110  pCheckBoxColorBlue->move(width + 350, 20);
111 
112  QRadioButton *pCheckBoxColorGreen = new QRadioButton("Green", this);
113  pCheckBoxColorGreen->setFixedWidth(200);
114  pCheckBoxColorGreen->move(width + 350, 45);
115 
116  QRadioButton *pCheckBoxColorCustom = new QRadioButton("Custom", this);
117  pCheckBoxColorCustom->setFixedWidth(200);
118  pCheckBoxColorCustom->move(width + 350, 70);
119 
120  // check boxes
121  m_pCheckBoxShowSegmentedImage = new QCheckBox("Show Segmented Image", this);
122  m_pCheckBoxShowSegmentedImage->setChecked(false);
123  m_pCheckBoxShowSegmentedImage->setFixedWidth(200);
124  m_pCheckBoxShowSegmentedImage->move(width + 20, 120);
125 
126  m_pCheckBoxShowRightImage = new QCheckBox("Show Right Image", this);
127  m_pCheckBoxShowRightImage->setChecked(false);
128  m_pCheckBoxShowRightImage->setFixedWidth(200);
129  m_pCheckBoxShowRightImage->move(width + 20, 145);
130 
131  // radio button group
132  m_pColorButtonGroup = new QButtonGroup();
133 
134  #if QT_VERSION >= 0x040000
135  m_pColorButtonGroup->addButton(pCheckBoxColorSkin);
136  m_pColorButtonGroup->addButton(pCheckBoxColorYellow);
137  m_pColorButtonGroup->addButton(pCheckBoxColorOrange);
138  m_pColorButtonGroup->addButton(pCheckBoxColorRed);
139  m_pColorButtonGroup->addButton(pCheckBoxColorBlue);
140  m_pColorButtonGroup->addButton(pCheckBoxColorGreen);
141  m_pColorButtonGroup->addButton(pCheckBoxColorCustom);
142  #else
143  m_pColorButtonGroup->insert(pCheckBoxColorSkin);
144  m_pColorButtonGroup->insert(pCheckBoxColorYellow);
145  m_pColorButtonGroup->insert(pCheckBoxColorOrange);
146  m_pColorButtonGroup->insert(pCheckBoxColorRed);
147  m_pColorButtonGroup->insert(pCheckBoxColorBlue);
148  m_pColorButtonGroup->insert(pCheckBoxColorGreen);
149  m_pColorButtonGroup->insert(pCheckBoxColorCustom);
150  #endif
151 
152  // slider
153  m_pSliderColorH = new QSlider(Qt::Horizontal, this);
154  m_pSliderColorH->setRange(0, 180);
155  m_pSliderColorH->setPageStep(1);
156  m_pSliderColorH->setValue(124);
157  m_pSliderColorH->setFixedWidth(180);
158  m_pSliderColorH->setFixedHeight(20);
159  m_pSliderColorH->move(width + 255, 130);
160 
161  m_pSliderColorHT = new QSlider(Qt::Horizontal, this);
162  m_pSliderColorHT->setRange(0, 180);
163  m_pSliderColorHT->setPageStep(1);
164  m_pSliderColorHT->setValue(26);
165  m_pSliderColorHT->setFixedWidth(180);
166  m_pSliderColorHT->setFixedHeight(20);
167  m_pSliderColorHT->move(width + 255, 155);
168 
169  m_pSliderColorMinS = new QSlider(Qt::Horizontal, this);
170  m_pSliderColorMinS->setRange(0, 255);
171  m_pSliderColorMinS->setPageStep(1);
172  m_pSliderColorMinS->setValue(109);
173  m_pSliderColorMinS->setFixedWidth(180);
174  m_pSliderColorMinS->setFixedHeight(20);
175  m_pSliderColorMinS->move(width + 255, 180);
176 
177  m_pSliderColorMaxS = new QSlider(Qt::Horizontal, this);
178  m_pSliderColorMaxS->setRange(0, 255);
179  m_pSliderColorMaxS->setPageStep(1);
180  m_pSliderColorMaxS->setValue(255);
181  m_pSliderColorMaxS->setFixedWidth(180);
182  m_pSliderColorMaxS->setFixedHeight(20);
183  m_pSliderColorMaxS->move(width + 255, 205);
184 
185  m_pSliderColorMinV = new QSlider(Qt::Horizontal, this);
186  m_pSliderColorMinV->setRange(0, 255);
187  m_pSliderColorMinV->setPageStep(1);
188  m_pSliderColorMinV->setValue(0);
189  m_pSliderColorMinV->setFixedWidth(180);
190  m_pSliderColorMinV->setFixedHeight(20);
191  m_pSliderColorMinV->move(width + 255, 230);
192 
193  m_pSliderColorMaxV = new QSlider(Qt::Horizontal, this);
194  m_pSliderColorMaxV->setRange(0, 255);
195  m_pSliderColorMaxV->setPageStep(1);
196  m_pSliderColorMaxV->setValue(255);
197  m_pSliderColorMaxV->setFixedWidth(180);
198  m_pSliderColorMaxV->setFixedHeight(20);
199  m_pSliderColorMaxV->move(width + 255, 255);
200 
201  // text edit fields for file input
202  m_pLineEditColorParameterFile = new QLineEdit(this);
203  m_pLineEditColorParameterFile->setText("files/colors_kitchen.txt");
204  m_pLineEditColorParameterFile->setReadOnly(true);
205  m_pLineEditColorParameterFile->setFixedWidth(250);
206  m_pLineEditColorParameterFile->setFixedHeight(20);
207  m_pLineEditColorParameterFile->move(width + 20, 390);
208 
209  // buttons
210  QPushButton *pPrintColorParametersButton = new QPushButton(this);
211  pPrintColorParametersButton->setText("Print Color Parameters");
212  pPrintColorParametersButton->setFixedHeight(20);
213  pPrintColorParametersButton->setFixedWidth(180);
214  pPrintColorParametersButton->move(width + 20, 190);
215 
216  QPushButton *pLoadColorParametersButton = new QPushButton(this);
217  pLoadColorParametersButton->setText("Load Color Parameters");
218  pLoadColorParametersButton->setFixedHeight(20);
219  pLoadColorParametersButton->setFixedWidth(180);
220  pLoadColorParametersButton->move(width + 20, 215);
221 
222  QPushButton *pSaveColorParametersButton = new QPushButton(this);
223  pSaveColorParametersButton->setText("Save Color Parameters");
224  pSaveColorParametersButton->setFixedHeight(20);
225  pSaveColorParametersButton->setFixedWidth(180);
226  pSaveColorParametersButton->move(width + 20, 240);
227 
228  QPushButton *pColorParameterFileBrowseButton = new QPushButton(this);
229  pColorParameterFileBrowseButton->setText("Browse Color Parameter File");
230  pColorParameterFileBrowseButton->setFixedHeight(20);
231  pColorParameterFileBrowseButton->setFixedWidth(155);
232  pColorParameterFileBrowseButton->move(width + 280, 390);
233 
234  QLabel *pLabelH = new QLabel(this);
235  pLabelH->setText("H");
236  pLabelH->setFixedWidth(40);
237  pLabelH->setFixedHeight(20);
238  pLabelH->move(width + 210, 130);
239 
240  QLabel *pLabelHTol = new QLabel(this);
241  pLabelHTol->setText("H Tol");
242  pLabelHTol->setFixedWidth(40);
243  pLabelHTol->setFixedHeight(20);
244  pLabelHTol->move(width + 210, 155);
245 
246  QLabel *pLabelMinS = new QLabel(this);
247  pLabelMinS->setText("S Min");
248  pLabelMinS->setFixedWidth(40);
249  pLabelMinS->setFixedHeight(20);
250  pLabelMinS->move(width + 210, 180);
251 
252  QLabel *pLabelMaxS = new QLabel(this);
253  pLabelMaxS->setText("S Max");
254  pLabelMaxS->setFixedWidth(40);
255  pLabelMaxS->setFixedHeight(20);
256  pLabelMaxS->move(width + 210, 205);
257 
258  QLabel *pLabelMinV = new QLabel(this);
259  pLabelMinV->setText("V Min");
260  pLabelMinV->setFixedWidth(40);
261  pLabelMinV->setFixedHeight(20);
262  pLabelMinV->move(width + 210, 230);
263 
264  QLabel *pLabelMaxV = new QLabel(this);
265  pLabelMaxV->setText("V Max");
266  pLabelMaxV->setFixedWidth(40);
267  pLabelMaxV->setFixedHeight(20);
268  pLabelMaxV->move(width + 210, 255);
269 
270  // initialize custom_par1 - custom_par6
271  SliderValueChanged(0);
272 
273  // set color to red
274  ColorRadioButtonClicked(3);
275  #if QT_VERSION >= 0x040000
276  m_pColorButtonGroup->button(3)->click();
277  #else
278  m_pColorButtonGroup->setButton(3);
279  #endif
280 
281  // signal/slots
282  connect(m_pColorButtonGroup, SIGNAL(clicked(int)), this, SLOT(ColorRadioButtonClicked(int)));
283  connect(pPrintColorParametersButton, SIGNAL(clicked()), this, SLOT(PrintColorParametersButtonClicked()));
284  connect(pLoadColorParametersButton, SIGNAL(clicked()), this, SLOT(LoadColorParametersButtonClicked()));
285  connect(pSaveColorParametersButton, SIGNAL(clicked()), this, SLOT(SaveColorParametersButtonClicked()));
286  connect(pColorParameterFileBrowseButton, SIGNAL(clicked()), this, SLOT(ColorParameterFileBrowseButtonClicked()));
287  connect(m_pSliderColorH, SIGNAL(valueChanged(int)), this, SLOT(SliderValueChanged(int)));
288  connect(m_pSliderColorHT, SIGNAL(valueChanged(int)), this, SLOT(SliderValueChanged(int)));
289  connect(m_pSliderColorMinS, SIGNAL(valueChanged(int)), this, SLOT(SliderValueChanged(int)));
290  connect(m_pSliderColorMaxS, SIGNAL(valueChanged(int)), this, SLOT(SliderValueChanged(int)));
291  connect(m_pSliderColorMinV, SIGNAL(valueChanged(int)), this, SLOT(SliderValueChanged(int)));
292  connect(m_pSliderColorMaxV, SIGNAL(valueChanged(int)), this, SLOT(SliderValueChanged(int)));
293 }
294 
296 {
297  delete m_pColorParameterSet;
298 }
299 
300 
301 // ****************************************************************************
302 // Methods
303 // ****************************************************************************
304 
305 void CQTColorParameterWindow::LoadColorParametersButtonClicked()
306 {
307  #if QT_VERSION >= 0x040000
308  m_pColorParameterSet->LoadFromFile(m_pLineEditColorParameterFile->text().toLatin1().constData());
309  QAbstractButton *pActiveButton = m_pColorButtonGroup->checkedButton();
310  #else
311  m_pColorParameterSet->LoadFromFile(m_pLineEditColorParameterFile->text().latin1());
312  QButton *pActiveButton = m_pColorButtonGroup->selected();
313  #endif
314 
315  if (pActiveButton)
316  {
317  ColorRadioButtonClicked(m_pColorButtonGroup->id(pActiveButton));
318  }
319 }
320 
321 void CQTColorParameterWindow::SaveColorParametersButtonClicked()
322 {
323 
324  #if QT_VERSION >= 0x040000
325  m_pColorParameterSet->SaveToFile(m_pLineEditColorParameterFile->text().toLatin1().constData());
326  #else
327  m_pColorParameterSet->SaveToFile(m_pLineEditColorParameterFile->text().latin1());
328  #endif
329 }
330 
331 void CQTColorParameterWindow::PrintColorParametersButtonClicked()
332 {
333  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());
334 }
335 
336 void CQTColorParameterWindow::ColorParameterFileBrowseButtonClicked()
337 {
338  #if QT_VERSION >= 0x040000
339  QString s = QFileDialog::getOpenFileName(this,tr("Choose a file"),"",tr("Text (*.txt)") );
340  #else
341  QString s = QFileDialog::getOpenFileName("", "Text (*.txt)", this, "Open File Dialog", "Choose a file");
342  #endif
343 
344  if (s.length() > 0)
345  m_pLineEditColorParameterFile->setText(s);
346 }
347 
348 void CQTColorParameterWindow::SliderValueChanged(int nValue)
349 {
350  if (!m_bUpdateParameterSet)
351  return;
352 
353  if (m_color == eNone)
354  {
355  custom_par1 = m_pSliderColorH->value();
356  custom_par2 = m_pSliderColorHT->value();
357  custom_par3 = m_pSliderColorMinS->value();
358  custom_par4 = m_pSliderColorMaxS->value();
359  custom_par5 = m_pSliderColorMinV->value();
360  custom_par6 = m_pSliderColorMaxV->value();
361  }
362  else
363  {
364  m_pColorParameterSet->SetColorParameters(m_color,
365  m_pSliderColorH->value(),
366  m_pSliderColorHT->value(),
367  m_pSliderColorMinS->value(),
368  m_pSliderColorMaxS->value(),
369  m_pSliderColorMinV->value(),
370  m_pSliderColorMaxV->value()
371  );
372  }
373 }
374 
375 void CQTColorParameterWindow::ColorRadioButtonClicked(int id)
376 {
377  m_bUpdateParameterSet = false;
378 
379  if (id >= 0 && id <= 5)
380  {
381  switch (id)
382  {
383  case 0: m_color = eSkin; break;
384  case 1: m_color = eYellow; break;
385  case 2: m_color = eOrange; break;
386  case 3: m_color = eRed; break;
387  case 4: m_color = eBlue; break;
388  case 5: m_color = eGreen; break;
389  }
390 
391  m_pSliderColorH->setValue(m_pColorParameterSet->GetColorParameters(m_color)[0]);
392  m_pSliderColorHT->setValue(m_pColorParameterSet->GetColorParameters(m_color)[1]);
393  m_pSliderColorMinS->setValue(m_pColorParameterSet->GetColorParameters(m_color)[2]);
394  m_pSliderColorMaxS->setValue(m_pColorParameterSet->GetColorParameters(m_color)[3]);
395  m_pSliderColorMinV->setValue(m_pColorParameterSet->GetColorParameters(m_color)[4]);
396  m_pSliderColorMaxV->setValue(m_pColorParameterSet->GetColorParameters(m_color)[5]);
397  }
398  else if (id == 6)
399  {
400  m_pSliderColorH->setValue(custom_par1);
401  m_pSliderColorHT->setValue(custom_par2);
402  m_pSliderColorMinS->setValue(custom_par3);
403  m_pSliderColorMaxS->setValue(custom_par4);
404  m_pSliderColorMinV->setValue(custom_par5);
405  m_pSliderColorMaxV->setValue(custom_par6);
406  m_color = eNone;
407  }
408 
409  m_bUpdateParameterSet = true;
410 }
411 
412 void CQTColorParameterWindow::Update(const CByteImage * const *ppInputImages, int nImages)
413 {
414  const int nIndex = (nImages == 1 || !m_pCheckBoxShowRightImage->isChecked()) ? 0 : 1;
415 
416  CByteImage hsv_image(ppInputImages[nIndex]);
417  CByteImage result_image(ppInputImages[nIndex]->width, ppInputImages[nIndex]->height, CByteImage::eGrayScale);
418 
419  ImageProcessor::CalculateHSVImage(ppInputImages[nIndex], &hsv_image);
420  ImageProcessor::FilterHSV(&hsv_image, &result_image, m_pSliderColorH->value(), m_pSliderColorHT->value(), m_pSliderColorMinS->value(), m_pSliderColorMaxS->value(), m_pSliderColorMinV->value(), m_pSliderColorMaxV->value());
421  ImageProcessor::Erode(&result_image, &result_image);
422  ImageProcessor::Dilate(&result_image, &result_image);
423 
424  if (m_pCheckBoxShowSegmentedImage->isChecked())
425  DrawImage(&result_image);
426  else
427  DrawImage(ppInputImages[nIndex]);
428 }
429 
431 {
432  return m_pColorParameterSet;
433 }
void Update(const CByteImage *const *ppInputImages, int nImages)
const CColorParameterSet * GetColorParameterSet() const
void DrawImage(const CByteImage *pImage, int x=0, int y=0)
Definition: QTWindow.cpp:129
bool LoadFromFile(const char *pFileName)
bool SaveToFile(const char *pFileName)
bool Erode(const CByteImage *pInputImage, CByteImage *pOutputImage, int nMaskSize=3, const MyRegion *pROI=0)
Applies a morphological erode operation to a binary CByteImage and writes the result to a binary CByt...
bool FilterHSV(const CByteImage *pInputImage, CByteImage *pOutputImage, unsigned char hue, unsigned char tol_hue, unsigned char min_sat, unsigned char max_sat, unsigned char min_v, unsigned char max_v, const MyRegion *pROI=0)
Performs color filtering with binarization for an HSV CByteImage and writes the result to a grayscale...
bool CalculateHSVImage(const CByteImage *pInputImage, CByteImage *pOutputImage, const MyRegion *pROI=0)
Computes the HSV image for a RGB CByteImage and writes the result to a CByteImage.
const int * GetColorParameters(ObjectColor color) const
CQTColorParameterWindow(int width, int height)
void SetColorParameters(ObjectColor color, int par1, int par2, int par3, int par4, int par5, int par6)
bool Dilate(const CByteImage *pInputImage, CByteImage *pOutputImage, int nMaskSize=3, const MyRegion *pROI=0)
Applies a morphological dilate operation to a binary CByteImage and writes the result to a binary CBy...
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...
Definition: ByteImage.h:80