ObjectFinder.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: ObjectFinder.cpp
37 // Author: Pedram Azad
38 // Date: 02.12.2007
39 // ****************************************************************************
40 
41 
42 // ****************************************************************************
43 // Includes
44 // ****************************************************************************
45 
46 #include <new> // for explicitly using correct new/delete operators on VC DSPs
47 
48 #include "ObjectFinder.h"
49 
50 #include "ObjectColorSegmenter.h"
51 #include "Image/ImageProcessor.h"
52 #include "Image/PrimitivesDrawer.h"
53 #include "Image/ByteImage.h"
56 #include "Helpers/helpers.h"
57 
58 #include <stdio.h>
59 #include <math.h>
60 
61 
62 
63 // ****************************************************************************
64 // Constructor / Destructor
65 // ****************************************************************************
66 
68 {
70 
72  m_pRegionFilter = 0;
73  m_nIDCounter = 0;
74  m_bUseROI = false;
75 }
76 
78 {
80 
82  delete m_pSegmentedImage;
83 }
84 
85 
86 // ****************************************************************************
87 // Methods
88 // ****************************************************************************
89 
91 {
92  m_pObjectColorSegmenter->SetColorParameterSet(pColorParameterSet);
93 }
94 
95 void CObjectFinder::PrepareImages(const CByteImage *pImage, float fROIFactor, bool bCalculateHSVImage)
96 {
97  if (m_pSegmentedImage && (m_pSegmentedImage->width != pImage->width || m_pSegmentedImage->height != pImage->height))
98  {
99  delete m_pSegmentedImage;
100  m_pSegmentedImage = 0;
101  }
102 
103  if (!m_pSegmentedImage)
105 
106  m_bUseROI = fROIFactor != -1;
107  m_ROIList.clear();
108 
109  if (m_bUseROI)
110  {
111  for (int i = 0; i < (int) m_objectList.size(); i++)
112  {
113  // create copy
114  Object2DEntry entry = m_objectList.at(i);
115 
116  const float w = floor((entry.region.max_x - entry.region.min_x + 1) * fROIFactor * 0.5f);
117  const float h = floor((entry.region.max_y - entry.region.min_y + 1) * fROIFactor * 0.5f);
118 
119  entry.region.min_x = int(entry.region.centroid.x - w - 0.5f);
120  entry.region.min_y = int(entry.region.centroid.y - h - 0.5f);
121  entry.region.max_x = int(entry.region.centroid.x + w + 0.5f);
122  entry.region.max_y = int(entry.region.centroid.y + h + 0.5f);
123 
124  m_ROIList.push_back(entry);
125  }
126  }
127 
128  if (bCalculateHSVImage)
130 
131  m_objectList.clear();
132 }
133 
134 void CObjectFinder::FindObjects(const CByteImage *pImage, CByteImage *pResultImage, ObjectColor color, int nMinPointsPerRegion, bool bShowSegmentedImage)
135 {
136  RegionList regionList;
137 
138  // do color segmentation and perform region growing
139  switch (color)
140  {
141  case eColored:
142  m_pObjectColorSegmenter->FindColoredRegions(m_pSegmentedImage, regionList, nMinPointsPerRegion);
143 
144  default:
145  m_pObjectColorSegmenter->FindRegionsOfGivenColor(m_pSegmentedImage, color, regionList, nMinPointsPerRegion);
146  }
147 
148  if (bShowSegmentedImage && pResultImage)
150 
151  // run object detectors
152  CheckRegionsForObjects(pImage, m_pSegmentedImage, pResultImage, regionList, color);
153 }
154 
155 void CObjectFinder::FindObjects(const CByteImage *pImage, CByteImage *pResultImage, ObjectColor color, int nMinPointsPerRegion, CByteImage *pResultSegmentedImage)
156 {
157  RegionList regionList;
158 
159  // do color segmentation and perform region growing
160  switch (color)
161  {
162  case eColored:
163  m_pObjectColorSegmenter->FindColoredRegions(pResultSegmentedImage, regionList, nMinPointsPerRegion);
164 
165  default:
166  m_pObjectColorSegmenter->FindRegionsOfGivenColor(pResultSegmentedImage, color, regionList, nMinPointsPerRegion);
167  }
168 
169  // run object detectors
170  CheckRegionsForObjects(pImage, pResultSegmentedImage, pResultImage, regionList, color);
171 }
172 
173 void CObjectFinder::FindObjects(const CByteImage *pImage, CByteImage *pResultImage, ObjectColor color, int hue, int hue_tol, int min_sat, int max_sat, int min_v, int max_v, int nMinPointsPerRegion, bool bShowSegmentedImage)
174 {
175  RegionList regionList;
176 
177  // do color segmentation and perform region growing
178  m_pObjectColorSegmenter->FindRegionsOfGivenColor(m_pSegmentedImage, color, hue, hue_tol, min_sat, max_sat, min_v, max_v, regionList, nMinPointsPerRegion);
179 
180  if (bShowSegmentedImage)
182 
183  // run object detectors
184  CheckRegionsForObjects(pImage, m_pSegmentedImage, pResultImage, regionList, color);
185 }
186 
187 void CObjectFinder::FindObjectsInSegmentedImage(const CByteImage *pSegmentedImage, CByteImage *pResultImage, ObjectColor color, int nMinPointsPerRegion, bool bShowSegmentedImage)
188 {
189  RegionList regionList;
190 
191  // perform region growing
192  ImageProcessor::FindRegions(pSegmentedImage, regionList, nMinPointsPerRegion, 0, true, true);
193 
194  if (bShowSegmentedImage)
195  ImageProcessor::ConvertImage(pSegmentedImage, pResultImage);
196 
197  // run object detectors
198  CheckRegionsForObjects(0, pSegmentedImage, pResultImage, regionList, color);
199 }
200 
201 void CObjectFinder::CheckRegionsForObjects(const CByteImage *pColorImage, const CByteImage *pSegmentedImage, CByteImage *pResultImage, RegionList &regionList, ObjectColor color)
202 {
203  const int nRegions = (int) regionList.size();
204 
205  for (int i = 0; i < nRegions; i++)
206  {
207  const MyRegion &region = regionList.at(i);
208 
209  if (!m_pRegionFilter || m_pRegionFilter->CheckRegion(pColorImage, pSegmentedImage, region))
210  {
211  Object2DEntry objectEntry;
212  objectEntry.color = color;
213  objectEntry.region = region;
214  objectEntry.type = eCompactObject;
215  objectEntry.sName = "CompactObject";
216 
217  float min = 100.0f;
218  int best_id = -1;
219 
220  for (int j = 0; j < (int) m_ROIList.size(); j++)
221  {
222  const Object2DEntry &entry = m_ROIList.at(j);
223  const float fRatio = entry.region.nPixels < region.nPixels ? float(entry.region.nPixels) / region.nPixels : float(region.nPixels) / entry.region.nPixels;
224  const float fDistance = Math2d::Distance(entry.region.centroid, region.centroid);
225 
226  if (entry.color == color && fRatio > 0.75f && fDistance < min)
227  {
228  min = fDistance;
229  best_id = entry.id;
230  }
231  }
232 
233  if (best_id == -1)
234  objectEntry.id = m_nIDCounter++;
235  else
236  objectEntry.id = best_id;
237 
238  m_objectList.push_back(objectEntry);
239 
240  if (pResultImage)
241  PrimitivesDrawer::DrawRegion(pResultImage, region, 255, 0, 0, 2);
242  }
243  }
244 }
245 
247 {
248  return (int) m_objectList.size();
249 }
250 
252 {
253  m_objectList.clear();
254 }
255 
257 {
258  m_objectList.push_back(entry);
259 }
CRegionFilterInterface * m_pRegionFilter
Definition: ObjectFinder.h:116
Vec2d centroid
Definition: Structs.h:331
int min_x
Definition: Structs.h:333
virtual bool CheckRegion(const CByteImage *pColorImage, const CByteImage *pSegmentedImage, const MyRegion &region)=0
void SetColorParameterSet(const CColorParameterSet *pColorParameterSet)
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.
float y
Definition: Math2d.h:84
void CheckRegionsForObjects(const CByteImage *pColorImage, const CByteImage *pSegmentedImage, CByteImage *pResultImage, RegionList &regionList, ObjectColor color)
Object2DList m_ROIList
Definition: ObjectFinder.h:120
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
ObjectColor color
bool ConvertImage(const CByteImage *pInputImage, CByteImage *pOutputImage, bool bFast=false, const MyRegion *pROI=0)
Converts a grayscale CByteImage to an RGB CByteImage image and vice versa.
Object2DList m_objectList
Definition: ObjectFinder.h:123
float Distance(const Vec2d &vector1, const Vec2d &vector2)
Definition: Math2d.cpp:181
float x
Definition: Math2d.h:84
std::vector< MyRegion > RegionList
Definition: Structs.h:346
void SetImage(const CByteImage *pImage, const Object2DList *pROIList=0)
int min_y
Definition: Structs.h:334
int height
The height of the image in pixels.
Definition: ByteImage.h:264
CByteImage * m_pSegmentedImage
Definition: ObjectFinder.h:114
int nPixels
Definition: Structs.h:327
int max_x
Definition: Structs.h:335
void ClearObjectList()
void AddObject(const Object2DEntry &entry)
void FindObjectsInSegmentedImage(const CByteImage *pSegmentedImage, CByteImage *pResultImage, ObjectColor color, int nMinPointsPerRegion, bool bShowSegmentedImage)
CObjectColorSegmenter * m_pObjectColorSegmenter
Definition: ObjectFinder.h:113
void FindColoredRegions(CByteImage *pResultImage, RegionList &regionList, int nMinPointsPerRegion)
void DrawRegion(CByteImage *pImage, const MyRegion &region, int r=255, int g=255, int b=255, int thickness=1)
Draws a rectangle into a CByteImage.
void FindObjects(const CByteImage *pImage, CByteImage *pResultImage, ObjectColor color, int nMinPointsPerRegion, bool bShowSegmentedImage)
void PrepareImages(const CByteImage *pImage, float fROIFactor=-1, bool bCalculateHSVImage=true)
int max_y
Definition: Structs.h:336
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:3571
ObjectColor
std::string sName


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