ObjectFinder.cpp
Go to the documentation of this file.
00001 // ****************************************************************************
00002 // This file is part of the Integrating Vision Toolkit (IVT).
00003 //
00004 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
00005 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
00006 //
00007 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
00008 // All rights reserved.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are met:
00012 //
00013 // 1. Redistributions of source code must retain the above copyright
00014 //    notice, this list of conditions and the following disclaimer.
00015 //
00016 // 2. Redistributions in binary form must reproduce the above copyright
00017 //    notice, this list of conditions and the following disclaimer in the
00018 //    documentation and/or other materials provided with the distribution.
00019 //
00020 // 3. Neither the name of the KIT nor the names of its contributors may be
00021 //    used to endorse or promote products derived from this software
00022 //    without specific prior written permission.
00023 //
00024 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
00025 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00026 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
00028 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00031 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00033 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 // ****************************************************************************
00035 // ****************************************************************************
00036 // Filename:  ObjectFinder.cpp
00037 // Author:    Pedram Azad
00038 // Date:      02.12.2007
00039 // ****************************************************************************
00040 
00041 
00042 // ****************************************************************************
00043 // Includes
00044 // ****************************************************************************
00045 
00046 #include <new> // for explicitly using correct new/delete operators on VC DSPs
00047 
00048 #include "ObjectFinder.h"
00049 
00050 #include "ObjectColorSegmenter.h"
00051 #include "Image/ImageProcessor.h"
00052 #include "Image/PrimitivesDrawer.h"
00053 #include "Image/ByteImage.h"
00054 #include "Interfaces/RegionFilterInterface.h"
00055 #include "Interfaces/ObjectClassifierInterface.h"
00056 #include "Helpers/helpers.h"
00057 
00058 #include <stdio.h>
00059 #include <math.h>
00060 
00061 
00062 
00063 // ****************************************************************************
00064 // Constructor / Destructor
00065 // ****************************************************************************
00066 
00067 CObjectFinder::CObjectFinder()
00068 {
00069         m_pObjectColorSegmenter = new CObjectColorSegmenter();
00070         
00071         m_pSegmentedImage = 0;
00072         m_pRegionFilter = 0;
00073         m_nIDCounter = 0;
00074         m_bUseROI = false;
00075 }
00076 
00077 CObjectFinder::~CObjectFinder()
00078 {
00079         delete m_pObjectColorSegmenter;
00080                 
00081         if (m_pSegmentedImage)
00082                 delete m_pSegmentedImage;
00083 }
00084 
00085 
00086 // ****************************************************************************
00087 // Methods
00088 // ****************************************************************************
00089 
00090 void CObjectFinder::SetColorParameterSet(const CColorParameterSet *pColorParameterSet)
00091 {
00092         m_pObjectColorSegmenter->SetColorParameterSet(pColorParameterSet);
00093 }
00094 
00095 void CObjectFinder::PrepareImages(const CByteImage *pImage, float fROIFactor, bool bCalculateHSVImage)
00096 {
00097         if (m_pSegmentedImage && (m_pSegmentedImage->width != pImage->width || m_pSegmentedImage->height != pImage->height))
00098         {
00099                 delete m_pSegmentedImage;
00100                 m_pSegmentedImage = 0;
00101         }
00102         
00103         if (!m_pSegmentedImage)
00104                 m_pSegmentedImage = new CByteImage(pImage->width, pImage->height, CByteImage::eGrayScale);
00105                 
00106         m_bUseROI = fROIFactor != -1;
00107         m_ROIList.clear();
00108 
00109         if (m_bUseROI)
00110         {
00111                 for (int i = 0; i < (int) m_objectList.size(); i++)
00112                 {
00113                         // create copy
00114                         Object2DEntry entry = m_objectList.at(i);
00115 
00116                         const float w = floor((entry.region.max_x - entry.region.min_x + 1) * fROIFactor * 0.5f);
00117                         const float h = floor((entry.region.max_y - entry.region.min_y + 1) * fROIFactor * 0.5f);
00118 
00119                         entry.region.min_x = int(entry.region.centroid.x - w - 0.5f);
00120                         entry.region.min_y = int(entry.region.centroid.y - h - 0.5f);
00121                         entry.region.max_x = int(entry.region.centroid.x + w + 0.5f);
00122                         entry.region.max_y = int(entry.region.centroid.y + h + 0.5f);
00123 
00124                         m_ROIList.push_back(entry);
00125                 }
00126         }
00127         
00128         if (bCalculateHSVImage)
00129                 m_pObjectColorSegmenter->SetImage(pImage, &m_ROIList);
00130         
00131         m_objectList.clear();
00132 }
00133 
00134 void CObjectFinder::FindObjects(const CByteImage *pImage, CByteImage *pResultImage, ObjectColor color, int nMinPointsPerRegion, bool bShowSegmentedImage)
00135 {
00136         RegionList regionList;
00137         
00138         // do color segmentation and perform region growing
00139         switch (color)
00140         {
00141                 case eColored:
00142                         m_pObjectColorSegmenter->FindColoredRegions(m_pSegmentedImage, regionList, nMinPointsPerRegion);
00143                 
00144                 default:
00145                         m_pObjectColorSegmenter->FindRegionsOfGivenColor(m_pSegmentedImage, color, regionList, nMinPointsPerRegion);
00146         }
00147         
00148         if (bShowSegmentedImage && pResultImage)
00149                 ImageProcessor::ConvertImage(m_pSegmentedImage, pResultImage);
00150         
00151         // run object detectors
00152         CheckRegionsForObjects(pImage, m_pSegmentedImage, pResultImage, regionList, color);
00153 }
00154 
00155 void CObjectFinder::FindObjects(const CByteImage *pImage, CByteImage *pResultImage, ObjectColor color, int nMinPointsPerRegion, CByteImage *pResultSegmentedImage)
00156 {
00157         RegionList regionList;
00158         
00159         // do color segmentation and perform region growing
00160         switch (color)
00161         {
00162                 case eColored:
00163                         m_pObjectColorSegmenter->FindColoredRegions(pResultSegmentedImage, regionList, nMinPointsPerRegion);
00164                 
00165                 default:
00166                         m_pObjectColorSegmenter->FindRegionsOfGivenColor(pResultSegmentedImage, color, regionList, nMinPointsPerRegion);
00167         }
00168         
00169         // run object detectors
00170         CheckRegionsForObjects(pImage, pResultSegmentedImage, pResultImage, regionList, color);
00171 }
00172 
00173 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)
00174 {
00175         RegionList regionList;
00176 
00177         // do color segmentation and perform region growing
00178         m_pObjectColorSegmenter->FindRegionsOfGivenColor(m_pSegmentedImage, color, hue, hue_tol, min_sat, max_sat, min_v, max_v, regionList, nMinPointsPerRegion);
00179 
00180         if (bShowSegmentedImage)
00181                 ImageProcessor::ConvertImage(m_pSegmentedImage, pResultImage);
00182 
00183         // run object detectors
00184         CheckRegionsForObjects(pImage, m_pSegmentedImage, pResultImage, regionList, color);
00185 }
00186 
00187 void CObjectFinder::FindObjectsInSegmentedImage(const CByteImage *pSegmentedImage, CByteImage *pResultImage, ObjectColor color, int nMinPointsPerRegion, bool bShowSegmentedImage)
00188 {
00189         RegionList regionList;
00190         
00191         // perform region growing
00192         ImageProcessor::FindRegions(pSegmentedImage, regionList, nMinPointsPerRegion, 0, true, true);
00193 
00194         if (bShowSegmentedImage)
00195                 ImageProcessor::ConvertImage(pSegmentedImage, pResultImage);
00196         
00197         // run object detectors
00198         CheckRegionsForObjects(0, pSegmentedImage, pResultImage, regionList, color);
00199 }
00200 
00201 void CObjectFinder::CheckRegionsForObjects(const CByteImage *pColorImage, const CByteImage *pSegmentedImage, CByteImage *pResultImage, RegionList &regionList, ObjectColor color)
00202 {
00203         const int nRegions = (int) regionList.size();
00204 
00205         for (int i = 0; i < nRegions; i++)
00206         {
00207                 const MyRegion &region = regionList.at(i);
00208                 
00209                 if (!m_pRegionFilter || m_pRegionFilter->CheckRegion(pColorImage, pSegmentedImage, region))
00210                 {
00211                         Object2DEntry objectEntry;
00212                         objectEntry.color = color;
00213                         objectEntry.region = region;
00214                         objectEntry.type = eCompactObject;
00215                         objectEntry.sName = "CompactObject";
00216                         
00217                         float min = 100.0f;
00218                         int best_id = -1;
00219                         
00220                         for (int j = 0; j < (int) m_ROIList.size(); j++)
00221                         {
00222                                 const Object2DEntry &entry = m_ROIList.at(j);
00223                                 const float fRatio = entry.region.nPixels < region.nPixels ? float(entry.region.nPixels) / region.nPixels : float(region.nPixels) / entry.region.nPixels;
00224                                 const float fDistance = Math2d::Distance(entry.region.centroid, region.centroid);
00225                                 
00226                                 if (entry.color == color && fRatio > 0.75f && fDistance < min)
00227                                 {
00228                                         min = fDistance;
00229                                         best_id = entry.id;
00230                                 }
00231                         }
00232                         
00233                         if (best_id == -1)
00234                                 objectEntry.id = m_nIDCounter++;
00235                         else
00236                                 objectEntry.id = best_id;
00237 
00238                         m_objectList.push_back(objectEntry);
00239                         
00240                         if (pResultImage)
00241                                 PrimitivesDrawer::DrawRegion(pResultImage, region, 255, 0, 0, 2);
00242                 }
00243         }
00244 }
00245 
00246 int CObjectFinder::Finalize()
00247 {
00248         return (int) m_objectList.size();
00249 }
00250 
00251 void CObjectFinder::ClearObjectList()
00252 {
00253         m_objectList.clear();
00254 }
00255 
00256 void CObjectFinder::AddObject(const Object2DEntry &entry)
00257 {
00258         m_objectList.push_back(entry);
00259 }


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Thu Jun 6 2019 21:46:57