ObjectColorSegmenter.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: ObjectColorSegmenter.cpp
37 // Author: Pedram Azad
38 // Date: 2005
39 // ****************************************************************************
40 
41 
42 // ****************************************************************************
43 // Includes
44 // ****************************************************************************
45 
46 #include <new> // for explicitly using correct new/delete operators on VC DSPs
47 
48 #include "ObjectColorSegmenter.h"
49 
50 #include "Image/ImageProcessor.h"
51 #include "Image/ByteImage.h"
53 
54 
55 
56 // ****************************************************************************
57 // Constructor / Destructor
58 // ****************************************************************************
59 
61 {
63 
64  m_pTempImage = 0;
65  m_pHSVImage = 0;
66  m_pRGBImage = 0;
67 
68  m_pROIList = 0;
69 }
70 
72 {
73  if (m_pHSVImage)
74  delete m_pHSVImage;
75 
76  if (m_pTempImage)
77  delete m_pTempImage;
78 }
79 
80 
81 // ****************************************************************************
82 // Methods
83 // ****************************************************************************
84 
85 void CObjectColorSegmenter::SetImage(const CByteImage *pImage, const Object2DList *pROIList)
86 {
87  m_pRGBImage = pImage;
88 
89  if (m_pTempImage && (m_pTempImage->width != pImage->width || m_pTempImage->height != pImage->height))
90  {
91  delete m_pTempImage;
92  m_pTempImage = 0;
93  }
94 
95  if (!m_pTempImage)
97 
98  if (m_pHSVImage && !m_pHSVImage->IsCompatible(pImage))
99  {
100  delete m_pHSVImage;
101  m_pHSVImage = 0;
102  }
103 
104  if (!m_pHSVImage)
105  m_pHSVImage = new CByteImage(pImage);
106 
107  m_pROIList = pROIList;
108 
109  if (m_pROIList && m_pROIList->size() != 0)
110  {
111  const int nSize = (int) m_pROIList->size();
112  for (int i = 0; i < nSize; i++)
114  }
115  else
117 }
118 
120 {
121  m_pColorParameterSet = pColorParameterSet;
122 }
123 
124 void CObjectColorSegmenter::FindColoredRegions(CByteImage *pResultImage, RegionList &regionList, int nMinPointsPerRegion)
125 {
126  if (!m_pHSVImage)
127  {
128  printf("error: call CObjectColorSegmenter::SetImage first and do not use optimizer\n");
129  return;
130  }
131 
133  ImageProcessor::ThresholdBinarize(pResultImage, pResultImage, 120);
134 
135  ImageProcessor::Erode(pResultImage, m_pTempImage);
136  ImageProcessor::Dilate(m_pTempImage, pResultImage);
137 
138  ImageProcessor::FindRegions(pResultImage, regionList, nMinPointsPerRegion, 0, true, true);
139 }
140 
142 {
144  {
145  printf("error: color parameter set has not been set\n");
146  return;
147  }
148 
149  if (!m_pHSVImage)
150  {
151  printf("error: call CObjectColorSegmenter::SetImage first\n");
152  return;
153  }
154 
155  const int *pParameters = m_pColorParameterSet->GetColorParameters(color);
156 
157  if (!pParameters)
158  {
159  printf("error: could not load parameters for segmenting color\n");
160  return;
161  }
162 
163  if (m_pROIList && m_pROIList->size() != 0)
164  {
165  ImageProcessor::Zero(pResultImage);
166 
167  const int nSize = (int) m_pROIList->size();
168  for (int i = 0; i < nSize; i++)
169  {
170  const Object2DEntry &entry = m_pROIList->at(i);
171 
172  if (entry.color == color)
173  ImageProcessor::FilterHSV(m_pHSVImage, pResultImage, pParameters[0], pParameters[1], pParameters[2], pParameters[3], pParameters[4], pParameters[5], &entry.region);
174  }
175  }
176  else
177  {
178  ImageProcessor::FilterHSV(m_pHSVImage, pResultImage, pParameters[0], pParameters[1], pParameters[2], pParameters[3], pParameters[4], pParameters[5]);
179  }
180 }
181 
182 
183 void CObjectColorSegmenter::FindRegionsOfGivenColor(CByteImage *pResultImage, ObjectColor color, RegionList &regionList, int nMinPointsPerRegion)
184 {
185  CalculateSegmentedImage(pResultImage, color);
186 
187  if (m_pROIList && m_pROIList->size() != 0)
188  {
189  const int nSize = (int) m_pROIList->size();
190  for (int i = 0; i < nSize; i++)
191  {
192  const Object2DEntry &entry = m_pROIList->at(i);
193 
194  if (entry.color == color)
195  {
196  ImageProcessor::Erode(pResultImage, m_pTempImage, 3, &entry.region);
197  ImageProcessor::Dilate(m_pTempImage, pResultImage, 3, &entry.region);
198  }
199  }
200  }
201  else
202  {
203  ImageProcessor::Erode(pResultImage, m_pTempImage);
204  ImageProcessor::Dilate(m_pTempImage, pResultImage);
205  }
206 
207  ImageProcessor::FindRegions(pResultImage, regionList, nMinPointsPerRegion, 0, true, true);
208 }
209 
210 void CObjectColorSegmenter::FindRegionsOfGivenColor(CByteImage *pResultImage, ObjectColor color, int hue, int hue_tol, int min_sat, int max_sat, int min_v, int max_v, RegionList &regionList, int nMinPointsPerRegion)
211 {
212  if (!m_pHSVImage)
213  {
214  printf("error: call CObjectColorSegmenter::SetImage first\n");
215  return;
216  }
217 
218  if (m_pROIList && m_pROIList->size() != 0)
219  {
220  ImageProcessor::Zero(pResultImage);
221 
222  const int nSize = (int) m_pROIList->size();
223  for (int i = 0; i < nSize; i++)
224  {
225  const Object2DEntry &entry = m_pROIList->at(i);
226 
227  if (entry.color == color)
228  {
229  ImageProcessor::FilterHSV(m_pHSVImage, pResultImage, hue, hue_tol, min_sat, max_sat, min_v, max_v, &entry.region);
230  ImageProcessor::Erode(pResultImage, m_pTempImage, 3, &entry.region);
231  ImageProcessor::Dilate(m_pTempImage, pResultImage, 3, &entry.region);
232  }
233  }
234  }
235  else
236  {
237  ImageProcessor::FilterHSV(m_pHSVImage, pResultImage, hue, hue_tol, min_sat, max_sat, min_v, max_v);
238  ImageProcessor::Erode(pResultImage, m_pTempImage);
239  ImageProcessor::Dilate(m_pTempImage, pResultImage);
240  }
241 
242 
243  ImageProcessor::FindRegions(pResultImage, regionList, nMinPointsPerRegion, 0, true, true);
244 }
const int * GetColorParameters(ObjectColor color) const
bool IsCompatible(const CByteImage *pImage) const
Checks whether two images are compatible or not.
Definition: ByteImage.cpp:212
bool FindRegions(const CByteImage *pImage, RegionList &regionList, int nMinimumPointsPerRegion=0, int nMaximumPointsPerRegion=0, bool bCalculateBoundingBox=true, bool bStorePixels=false)
Performs region growing on a binary CByteImage, segmenting all regions in the image.
bool CalculateSaturationImage(const CByteImage *pInputImage, CByteImage *pOutputImage)
Calculates the saturation image for an RGB CByteImage and writes the result to a CByteImage.
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...
int width
The width of the image in pixels.
Definition: ByteImage.h:257
void SetColorParameterSet(const CColorParameterSet *pColorParameterSet)
void FindRegionsOfGivenColor(CByteImage *pResultImage, ObjectColor color, RegionList &regionList, int nMinPointsPerRegion)
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...
Definition: ByteImage.h:80
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.
ObjectColor color
std::vector< MyRegion > RegionList
Definition: Structs.h:346
void SetImage(const CByteImage *pImage, const Object2DList *pROIList=0)
std::vector< Object2DEntry > Object2DList
void CalculateSegmentedImage(CByteImage *pResultImage, ObjectColor color)
int height
The height of the image in pixels.
Definition: ByteImage.h:264
bool ThresholdBinarize(const CByteImage *pInputImage, CByteImage *pOutputImage, unsigned char nThreshold)
Performs threshold binarization to a CByteImage and writes the result to a CByteImage.
const Object2DList * m_pROIList
const CColorParameterSet * m_pColorParameterSet
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...
void FindColoredRegions(CByteImage *pResultImage, RegionList &regionList, int nMinPointsPerRegion)
const CByteImage * m_pRGBImage
void Zero(CByteImage *pImage, const MyRegion *pROI=0)
Sets all values in a CByteImage to zero.
ObjectColor
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...


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Mon Dec 2 2019 03:47:28