HsvColorSegmentationWidgetController.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package VisionX::gui-plugins::HsvColorSegmentationWidgetController
17  * @author Rainer Kartmann ( rainer dot kartmann at student dot kit dot edu )
18  * @date 2018
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 #pragma once
23 
24 
25 #include <VisionX/gui-plugins/HsvColorSegmentation/ui_HsvColorSegmentationWidget.h>
26 
27 
28 // ivt
30 
34 
38 
39 #include "HsvImageSegmentation.h"
40 #include <Color/ColorParameterSet.h>
41 
42 namespace armarx
43 {
44 
45  /**
46  \page VisionX-GuiPlugins-HsvColorSegmentation HsvColorSegmentation
47  \brief The HsvColorSegmentation allows image segmentation by HSV color and fast parameter selection.
48 
49  \image html HsvColorSegmentation.png
50  The user can connect to any image provider for segmentation.
51  Once connected, the providers image stream is shown in the GUI.
52  The user can select colors from the image to be included in the segmented image.
53  Segmentation parameters can be automatically generated from the selected colors,
54  or manually refined.
55  The segmented image is shown live inside the GUI, and is provided for usage by
56  other image processors.
57 
58  API Documentation \ref HsvColorSegmentationWidgetController
59 
60  \see HsvColorSegmentationGuiPlugin
61  */
62 
63  /**
64  * \class HsvColorSegmentationWidgetController
65  * \brief HsvColorSegmentationWidgetController brief one line description
66  *
67  * Detailed description
68  */
71  HsvColorSegmentationWidgetController>,
73  {
74  Q_OBJECT
75 
76  public:
77  /**
78  * Controller Constructor
79  */
81 
82  /**
83  * Controller destructor
84  */
86 
87  /**
88  * @see ArmarXWidgetController::loadSettings()
89  */
90  void loadSettings(QSettings* settings) override;
91 
92  /**
93  * @see ArmarXWidgetController::saveSettings()
94  */
95  void saveSettings(QSettings* settings) override;
96 
97  /**
98  * Returns the Widget name displayed in the ArmarXGui to create an
99  * instance of this class.
100  */
101  static QString
103  {
104  return "VisionX.HsvColorSegmentation";
105  }
106 
107  protected:
108  // ImageProcessor interface
109  virtual void onInitImageProcessor() override;
110  virtual void onConnectImageProcessor() override;
111  virtual void onDisconnectImageProcessor() override;
112  virtual void onExitImageProcessor() override;
113 
114  virtual void process() override;
115 
116  public slots:
117  /* QT slot declarations */
118 
119  // provider settings
120  void clickedButtonConnect(bool toggled);
121  void clickedButtonDisconnect(bool toggled);
122  void clickedButtonPlayPause(bool toggled);
123 
124  // selection
125  void addColorSelection(int imageIndex, float x, float y);
126  void clearColorSelection();
127 
128 
129  // color settings
130 
131  void onColorSettingsChanged();
132 
133  void onColorIndexChanged(int value);
134 
135  void setManualHueMid(int value);
136  void setManualHueTol(int value);
137  void setManualSatMin(int value);
138  void setManualSatMax(int value);
139  void setManualValMin(int value);
140  void setManualValMax(int value);
141 
142  void onToggledCheckBoxAutoAll(bool enabled);
143  void onToggledCheckBoxAuto(bool enabled);
144  void onChangedAddTolerance(int value);
145 
146  void resetCurrentColorSettings();
147 
148  // color parameter set
149 
150  /// Loads the color parameter set from file and update the gui
151  void loadColorParameterSetFromFile(bool showMsgBoxes = true);
152  void saveColorParameterSetToFile();
153 
154  void searchColorParameterFile();
155  void resetColorParameterFile();
156 
157  // UI updates
158  void updateImageMonitorUI();
159 
160  signals:
161  /* QT signal declarations */
162  void imageProviderConnected(bool connected);
163 
164  private:
165  enum Param
166  {
167  HUE_MID,
168  HUE_TOL,
169  SAT_MIN,
170  SAT_MAX,
171  VAL_MIN,
172  VAL_MAX,
173  NUM_PARAMS
174  };
175 
176  // utils
177  static void rgbToHsv(int r, int g, int b, int& h, int& s, int& v);
178 
179  static std::string getHomeDirectory();
180  static QString getDefaultColorParameterFile();
181 
182  const static QStringList COLOR_PARAMETER_NAMES;
183  /// Default color parameter file, relative to home directory.
184  const static std::string DEFAULT_COLOR_PARAMETER_FILE;
185 
186  // connenctivity
187  void reconnect();
188  void connectToProvider();
189  void disconnectFromProvider();
190 
191  // ui value getters
192  int value(int param);
193  int hueMin();
194  int hueMax();
195  int satMid();
196  int valMid();
197 
198  bool isAutoEnabled(int param);
199  int additionalTolerance();
200 
201  std::string colorParameterFile();
202 
203 
204  // procedures
205  void recomputeAutoValues();
206  void onSelectedPointsChanged();
207 
208  void drawSelectedPoints();
209  void runSegmentation();
210 
211 
212  /// Store the current settings to the local color parameter set.
213  /// Does not store anything to disc.
214  void saveCurrentColorParameters(int colorIndex);
215  /// Load the parameters of the currently selected color to the gui.
216  void loadCurrentColorParameters(int colorIndex);
217 
218  // ui updates
219  void updatePausePlayButtonText();
220 
221  void setUiParamValue(int param, int value);
222  void updateAutoValuesUI();
223 
224  void updateColorVisualization();
225 
226  void markCurrentColorDirty();
227  void markCurrentColorClean();
228  void markAllColorsClean();
229 
230 
231  /// Widget Form
232  Ui::HsvColorSegmentationWidget widget;
233 
234 
235  // provider
237 
238  std::string imageProviderName;
239  visionx::ImageProviderInfo imageProviderInfo;
240  visionx::ImageProviderInterfacePrx& imageProviderPrx = imageProviderInfo.proxy;
241  int& numImages = imageProviderInfo.numberImages;
242  int& imageWidth = imageProviderInfo.imageFormat.dimension.width;
243  int& imageHeight = imageProviderInfo.imageFormat.dimension.height;
244 
245  bool connected = false;
246  bool isPaused = true;
247 
248 
249  std::recursive_mutex imageMutex;
250  HsvImageSegmentation segmentation;
251 
252  SelectableImageViewer* imageViewerInput;
253  visionx::ImageViewerArea* imageViewerOutput;
254 
255  class SelectedPoint
256  {
257  public:
258  SelectedPoint(int imageIndex, float xRel, float yRel, int red, int green, int blue) :
259  imageIndex(imageIndex), xRel(xRel), yRel(yRel), red(red), green(green), blue(blue)
260  {
261  rgbToHsv(red, green, blue, hue, sat, val);
262  }
263 
264  int imageIndex;
265  float xRel, yRel;
266  int red, green, blue;
267  int hue, sat, val;
268  };
269 
270  std::vector<SelectedPoint> selectedPoints;
271  std::vector<int> autoValues;
272 
273 
274  int currentColorIndex = 0;
275  std::set<int> dirtyColors;
276  CColorParameterSet colorParameterSet;
277 
278 
279  std::vector<QCheckBox*> guiAutoCheckBoxes;
280  std::vector<QSpinBox*> guiValueSpinBoxes;
281  std::vector<QSlider*> guiValueSliders;
282  };
283 } // namespace armarx
visionx::ImageProviderInfo::numberImages
int numberImages
Number of images.
Definition: ImageProcessor.h:519
visionx::ImageProcessor
The ImageProcessor class provides an interface for access to ImageProviders via Ice and shared memory...
Definition: ImageProcessor.h:98
armarx::HsvColorSegmentationWidgetController::GetWidgetName
static QString GetWidgetName()
Returns the Widget name displayed in the ArmarXGui to create an instance of this class.
Definition: HsvColorSegmentationWidgetController.h:102
HsvImageSegmentation.h
visionx::ImageProviderInfo::imageFormat
ImageFormatInfo imageFormat
Image format struct that contains all necessary image information.
Definition: ImageProcessor.h:509
visionx::ImageViewerArea
Definition: ImageViewerArea.h:38
armarx::ArmarXComponentWidgetControllerTemplate
Definition: ArmarXComponentWidgetController.h:69
visionx::ImageProviderInfo
Definition: ImageProcessor.h:479
ArmarXGuiPlugin.h
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
ArmarXComponentWidgetController.h
ARMARXCOMPONENT_IMPORT_EXPORT
#define ARMARXCOMPONENT_IMPORT_EXPORT
Definition: ImportExportComponent.h:38
ImageProcessor.h
IceProxyFinder.h
enabled
std::atomic< bool > * enabled
Definition: RemoteGuiWidgetController.cpp:75
ImageViewerArea.h
armarx::red
QColor red()
Definition: StyleSheets.h:78
SelectableImageViewer.h
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
visionx::ImageProviderInfo::proxy
ImageProviderInterfacePrx proxy
proxy to image provider
Definition: ImageProcessor.h:485
armarx::HsvColorSegmentationWidgetController
HsvColorSegmentationWidgetController brief one line description.
Definition: HsvColorSegmentationWidgetController.h:69
armarx::IceProxyFinder< visionx::ImageProviderInterfacePrx >
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::green
QColor green()
Definition: StyleSheets.h:72
ImportExportComponent.h