HsvImageSegmentation.cpp
Go to the documentation of this file.
1 #include "HsvImageSegmentation.h"
2 
4 
5 
6 using namespace armarx;
7 
9 {
10 }
11 
13  numImages(imageProviderInfo.numberImages),
14  width(imageProviderInfo.imageFormat.dimension.width),
15  height(imageProviderInfo.imageFormat.dimension.height)
16 {
17  if (numImages <= 0)
18  {
19  std::stringstream ss;
20  ss << "Tried to initialize HsvImageSegmentation with " << numImages
21  << " images, but need at least 1 image.";
22  throw std::invalid_argument(ss.str());
23  }
24 
25  allocate(imageProviderInfo);
26 }
27 
29 {
30  deallocate();
31 }
32 
34 {
35  moveFrom(other);
36 }
37 
40 {
41  if (this != &other)
42  {
43  deallocate();
44  moveFrom(other);
45  }
46  return *this;
47 }
48 
49 CByteImage**
51 {
52  return inputImagesRgb;
53 }
54 
55 CByteImage**
57 {
58  return inputImagesHsv;
59 }
60 
61 CByteImage**
63 {
64  return inputVisuImages;
65 }
66 
67 CByteImage**
69 {
70  return outputImagesGray;
71 }
72 
73 CByteImage**
75 {
76  return outputImagesRgb;
77 }
78 
79 int
81 {
82  return numImages;
83 }
84 
85 void
87  int hueTol,
88  int satMin,
89  int satMax,
90  int valMin,
91  int valMax)
92 {
93  // segment all input images
94  // #pragma omp for // seems to get ignored
95  for (int i = 0; i < numImages; ++i)
96  {
97  ::ImageProcessor::CopyImage(inputImagesRgb[i], inputVisuImages[i]);
98 
99  // convert to HSV
100  ::ImageProcessor::CalculateHSVImage(inputImagesRgb[i], inputImagesHsv[i]);
101 
102  // filter HSV (-> gray scale image)
103  ::ImageProcessor::FilterHSV(
104  inputImagesHsv[i], outputImagesGray[i], hue, hueTol, satMin, satMax, valMin, valMax);
105 
106  // convert gray scale to RGB
107  ::ImageProcessor::ConvertImage(outputImagesGray[i], outputImagesRgb[i], true);
108  }
109 }
110 
111 void
112 HsvImageSegmentation::allocate(const visionx::ImageProviderInfo& imageProviderInfo)
113 {
114  inputImagesRgb = new CByteImage*[numImages];
115  inputVisuImages = new CByteImage*[numImages];
116  inputImagesHsv = new CByteImage*[numImages];
117  outputImagesGray = new CByteImage*[numImages];
118  outputImagesRgb = new CByteImage*[numImages];
119 
120  for (int i = 0; i < numImages; ++i)
121  {
122  inputImagesRgb[i] = visionx::tools::createByteImage(imageProviderInfo);
123  inputVisuImages[i] = visionx::tools::createByteImage(imageProviderInfo);
124  inputImagesHsv[i] = visionx::tools::createByteImage(imageProviderInfo);
125  outputImagesGray[i] = new CByteImage(width, height, CByteImage::eGrayScale);
126  outputImagesRgb[i] = visionx::tools::createByteImage(imageProviderInfo);
127  }
128 }
129 
130 void
131 HsvImageSegmentation::deallocate()
132 {
133  if (numImages != 0)
134  {
135  for (int i = 0; i < numImages; ++i)
136  {
137  delete inputImagesRgb[i];
138  delete inputVisuImages[i];
139  delete inputImagesHsv[i];
140  delete outputImagesGray[i];
141  delete outputImagesRgb[i];
142  }
143 
144  delete[] inputImagesRgb;
145  delete[] inputVisuImages;
146  delete[] inputImagesHsv;
147  delete[] outputImagesGray;
148  delete[] outputImagesRgb;
149 
150  inputImagesRgb = nullptr;
151  inputVisuImages = nullptr;
152  inputImagesHsv = nullptr;
153  outputImagesGray = nullptr;
154  outputImagesRgb = nullptr;
155  }
156 }
157 
158 void
159 HsvImageSegmentation::moveFrom(HsvImageSegmentation& other)
160 {
161  this->numImages = other.numImages;
162  this->height = other.height;
163  this->width = other.width;
164 
165  // steal image buffers
166  this->inputImagesRgb = other.inputImagesRgb;
167  this->inputVisuImages = other.inputVisuImages;
168  this->inputImagesHsv = other.inputImagesHsv;
169  this->outputImagesGray = other.outputImagesGray;
170  this->outputImagesRgb = other.outputImagesRgb;
171 
172  // reset others state to default
173  other.numImages = other.height = other.width = 0;
174  other.inputImagesRgb = other.inputImagesHsv = other.inputVisuImages = nullptr;
175  other.outputImagesGray = other.outputImagesRgb = nullptr;
176 }
armarx::HsvImageSegmentation::processInputImages
void processInputImages(int hue, int hueTol, int satMin, int satMax, int valMin, int valMax)
Processes the current input images.
Definition: HsvImageSegmentation.cpp:86
armarx::HsvImageSegmentation::getInputImagesHsv
CByteImage ** getInputImagesHsv() const
Get the input images in HSV.
Definition: HsvImageSegmentation.cpp:56
armarx::HsvImageSegmentation::HsvImageSegmentation
HsvImageSegmentation()
No-initialization constructor.
Definition: HsvImageSegmentation.cpp:8
HsvImageSegmentation.h
armarx::HsvImageSegmentation
The HsvImageSegmentation class.
Definition: HsvImageSegmentation.h:24
visionx::tools::createByteImage
CByteImage * createByteImage(const ImageFormatInfo &imageFormat, const ImageType imageType)
Creates a ByteImage for the destination type specified in the given imageProviderInfo.
visionx::ImageProviderInfo
Definition: ImageProcessor.h:479
armarx::HsvImageSegmentation::getNumImages
int getNumImages() const
Definition: HsvImageSegmentation.cpp:80
armarx::HsvImageSegmentation::getOutputImagesRgb
CByteImage ** getOutputImagesRgb() const
Get the output images in RGB (from gray scale).
Definition: HsvImageSegmentation.cpp:74
armarx::HsvImageSegmentation::getInputVisuImages
CByteImage ** getInputVisuImages() const
Get input visualization images.
Definition: HsvImageSegmentation.cpp:62
ImageUtil.h
armarx::HsvImageSegmentation::getInputImagesRgb
CByteImage ** getInputImagesRgb() const
Get the input images (RGB) (buffer).
Definition: HsvImageSegmentation.cpp:50
armarx::HsvImageSegmentation::operator=
HsvImageSegmentation & operator=(HsvImageSegmentation &&other)
Definition: HsvImageSegmentation.cpp:39
armarx::HsvImageSegmentation::~HsvImageSegmentation
~HsvImageSegmentation()
Frees all allocated memory.
Definition: HsvImageSegmentation.cpp:28
armarx::HsvImageSegmentation::getOutputImagesGray
CByteImage ** getOutputImagesGray() const
Get the output images in gray scale.
Definition: HsvImageSegmentation.cpp:68
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27