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