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
29 #include <Color/ColorParameterSet.h>
30 
31 
35 
37 
39 
42 
43 
44 #include "HsvImageSegmentation.h"
45 
46 
47 namespace armarx
48 {
49 
50  /**
51  \page VisionX-GuiPlugins-HsvColorSegmentation HsvColorSegmentation
52  \brief The HsvColorSegmentation allows image segmentation by HSV color and fast parameter selection.
53 
54  \image html HsvColorSegmentation.png
55  The user can connect to any image provider for segmentation.
56  Once connected, the providers image stream is shown in the GUI.
57  The user can select colors from the image to be included in the segmented image.
58  Segmentation parameters can be automatically generated from the selected colors,
59  or manually refined.
60  The segmented image is shown live inside the GUI, and is provided for usage by
61  other image processors.
62 
63  API Documentation \ref HsvColorSegmentationWidgetController
64 
65  \see HsvColorSegmentationGuiPlugin
66  */
67 
68  /**
69  * \class HsvColorSegmentationWidgetController
70  * \brief HsvColorSegmentationWidgetController brief one line description
71  *
72  * Detailed description
73  */
76  public armarx::ArmarXComponentWidgetControllerTemplate < HsvColorSegmentationWidgetController >,
78  {
79  Q_OBJECT
80 
81  public:
82  /**
83  * Controller Constructor
84  */
86 
87  /**
88  * Controller destructor
89  */
91 
92  /**
93  * @see ArmarXWidgetController::loadSettings()
94  */
95  void loadSettings(QSettings* settings) override;
96 
97  /**
98  * @see ArmarXWidgetController::saveSettings()
99  */
100  void saveSettings(QSettings* settings) override;
101 
102  /**
103  * Returns the Widget name displayed in the ArmarXGui to create an
104  * instance of this class.
105  */
106  static QString GetWidgetName()
107  {
108  return "VisionX.HsvColorSegmentation";
109  }
110 
111  protected:
112 
113  // ImageProcessor interface
114  virtual void onInitImageProcessor() override;
115  virtual void onConnectImageProcessor() override;
116  virtual void onDisconnectImageProcessor() override;
117  virtual void onExitImageProcessor() override;
118 
119  virtual void process() override;
120 
121  public slots:
122  /* QT slot declarations */
123 
124  // provider settings
125  void clickedButtonConnect(bool toggled);
126  void clickedButtonDisconnect(bool toggled);
127  void clickedButtonPlayPause(bool toggled);
128 
129  // selection
130  void addColorSelection(int imageIndex, float x, float y);
131  void clearColorSelection();
132 
133 
134  // color settings
135 
136  void onColorSettingsChanged();
137 
138  void onColorIndexChanged(int value);
139 
140  void setManualHueMid(int value);
141  void setManualHueTol(int value);
142  void setManualSatMin(int value);
143  void setManualSatMax(int value);
144  void setManualValMin(int value);
145  void setManualValMax(int value);
146 
147  void onToggledCheckBoxAutoAll(bool enabled);
148  void onToggledCheckBoxAuto(bool enabled);
149  void onChangedAddTolerance(int value);
150 
151  void resetCurrentColorSettings();
152 
153  // color parameter set
154 
155  /// Loads the color parameter set from file and update the gui
156  void loadColorParameterSetFromFile(bool showMsgBoxes = true);
157  void saveColorParameterSetToFile();
158 
159  void searchColorParameterFile();
160  void resetColorParameterFile();
161 
162  // UI updates
163  void updateImageMonitorUI();
164 
165  signals:
166  /* QT signal declarations */
167  void imageProviderConnected(bool connected);
168 
169  private:
170 
171  enum Param
172  {
173  HUE_MID, HUE_TOL, SAT_MIN, SAT_MAX, VAL_MIN, VAL_MAX, 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 
256  class SelectedPoint
257  {
258  public:
259 
260  SelectedPoint(int imageIndex, float xRel, float yRel, int red, int green, int blue) :
261  imageIndex(imageIndex), xRel(xRel), yRel(yRel),
262  red(red), green(green), blue(blue)
263  {
264  rgbToHsv(red, green, blue, hue, sat, val);
265  }
266 
267  int imageIndex;
268  float xRel, yRel;
269  int red, green, blue;
270  int hue, sat, val;
271  };
272 
273  std::vector<SelectedPoint> selectedPoints;
274  std::vector<int> autoValues;
275 
276 
277  int currentColorIndex = 0;
278  std::set<int> dirtyColors;
279  CColorParameterSet colorParameterSet;
280 
281 
282 
283 
284  std::vector<QCheckBox*> guiAutoCheckBoxes;
285  std::vector<QSpinBox*> guiValueSpinBoxes;
286  std::vector<QSlider*> guiValueSliders;
287 
288  };
289 }
290 
291 
visionx::ImageProviderInfo::numberImages
int numberImages
Number of images.
Definition: ImageProcessor.h:506
visionx::ImageProcessor
The ImageProcessor class provides an interface for access to ImageProviders via Ice and shared memory...
Definition: ImageProcessor.h:87
armarx::HsvColorSegmentationWidgetController::GetWidgetName
static QString GetWidgetName()
Returns the Widget name displayed in the ArmarXGui to create an instance of this class.
Definition: HsvColorSegmentationWidgetController.h:106
HsvImageSegmentation.h
visionx::ImageProviderInfo::imageFormat
ImageFormatInfo imageFormat
Image format struct that contains all necessary image information.
Definition: ImageProcessor.h:496
visionx::ImageViewerArea
Definition: ImageViewerArea.h:38
armarx::ArmarXComponentWidgetControllerTemplate
Definition: ArmarXComponentWidgetController.h:69
visionx::ImageProviderInfo
Definition: ImageProcessor.h:466
ArmarXGuiPlugin.h
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
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:76
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:472
armarx::HsvColorSegmentationWidgetController
HsvColorSegmentationWidgetController brief one line description.
Definition: HsvColorSegmentationWidgetController.h:74
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:28
armarx::green
QColor green()
Definition: StyleSheets.h:72
ImportExportComponent.h