ObjectColorSegmenter.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:  ObjectColorSegmenter.cpp
00037 // Author:    Pedram Azad
00038 // Date:      2005
00039 // ****************************************************************************
00040 
00041 
00042 // ****************************************************************************
00043 // Includes
00044 // ****************************************************************************
00045 
00046 #include <new> // for explicitly using correct new/delete operators on VC DSPs
00047 
00048 #include "ObjectColorSegmenter.h"
00049 
00050 #include "Image/ImageProcessor.h"
00051 #include "Image/ByteImage.h"
00052 #include "Color/ColorParameterSet.h"
00053 
00054 
00055 
00056 // ****************************************************************************
00057 // Constructor / Destructor
00058 // ****************************************************************************
00059 
00060 CObjectColorSegmenter::CObjectColorSegmenter()
00061 {
00062         m_pColorParameterSet = 0;
00063         
00064         m_pTempImage = 0;
00065         m_pHSVImage = 0;
00066         m_pRGBImage = 0;
00067         
00068         m_pROIList = 0;
00069 }
00070 
00071 CObjectColorSegmenter::~CObjectColorSegmenter()
00072 {
00073         if (m_pHSVImage)
00074                 delete m_pHSVImage;
00075         
00076         if (m_pTempImage)
00077                 delete m_pTempImage;
00078 }
00079 
00080 
00081 // ****************************************************************************
00082 // Methods
00083 // ****************************************************************************
00084 
00085 void CObjectColorSegmenter::SetImage(const CByteImage *pImage, const Object2DList *pROIList)
00086 {
00087         m_pRGBImage = pImage;
00088         
00089         if (m_pTempImage && (m_pTempImage->width != pImage->width || m_pTempImage->height != pImage->height))
00090         {
00091                 delete m_pTempImage;
00092                 m_pTempImage = 0;
00093         }
00094         
00095         if (!m_pTempImage)
00096                 m_pTempImage = new CByteImage(pImage->width, pImage->height, CByteImage::eGrayScale);
00097                 
00098         if (m_pHSVImage && !m_pHSVImage->IsCompatible(pImage))
00099         {
00100                 delete m_pHSVImage;
00101                 m_pHSVImage = 0;
00102         }
00103         
00104         if (!m_pHSVImage)
00105                 m_pHSVImage = new CByteImage(pImage);
00106         
00107         m_pROIList = pROIList;
00108         
00109         if (m_pROIList && m_pROIList->size() != 0)
00110         {
00111                 const int nSize = (int) m_pROIList->size();
00112                 for (int i = 0; i < nSize; i++)
00113                         ImageProcessor::CalculateHSVImage(pImage, m_pHSVImage, &m_pROIList->at(i).region);
00114         }
00115         else
00116                 ImageProcessor::CalculateHSVImage(pImage, m_pHSVImage);
00117 }
00118 
00119 void CObjectColorSegmenter::SetColorParameterSet(const CColorParameterSet *pColorParameterSet)
00120 {
00121         m_pColorParameterSet = pColorParameterSet;
00122 }
00123 
00124 void CObjectColorSegmenter::FindColoredRegions(CByteImage *pResultImage, RegionList &regionList, int nMinPointsPerRegion)
00125 {
00126         if (!m_pHSVImage)
00127         {
00128                 printf("error: call CObjectColorSegmenter::SetImage first and do not use optimizer\n");
00129                 return;
00130         }
00131         
00132         ImageProcessor::CalculateSaturationImage(m_pHSVImage, pResultImage);
00133         ImageProcessor::ThresholdBinarize(pResultImage, pResultImage, 120);
00134         
00135         ImageProcessor::Erode(pResultImage, m_pTempImage);
00136         ImageProcessor::Dilate(m_pTempImage, pResultImage);
00137         
00138         ImageProcessor::FindRegions(pResultImage, regionList, nMinPointsPerRegion, 0, true, true);
00139 }
00140 
00141 void CObjectColorSegmenter::CalculateSegmentedImage(CByteImage *pResultImage, ObjectColor color)
00142 {
00143         if (!m_pColorParameterSet)
00144         {
00145                 printf("error: color parameter set has not been set\n");
00146                 return;
00147         }
00148                 
00149         if (!m_pHSVImage)
00150         {
00151                 printf("error: call CObjectColorSegmenter::SetImage first\n");
00152                 return;
00153         }
00154         
00155         const int *pParameters = m_pColorParameterSet->GetColorParameters(color);
00156         
00157         if (!pParameters)
00158         {
00159                 printf("error: could not load parameters for segmenting color\n");
00160                 return;
00161         }
00162         
00163         if (m_pROIList && m_pROIList->size() != 0)
00164         {
00165                 ImageProcessor::Zero(pResultImage);
00166                 
00167                 const int nSize = (int) m_pROIList->size();
00168                 for (int i = 0; i < nSize; i++)
00169                 {
00170                         const Object2DEntry &entry = m_pROIList->at(i);
00171                         
00172                         if (entry.color == color)
00173                                 ImageProcessor::FilterHSV(m_pHSVImage, pResultImage, pParameters[0], pParameters[1], pParameters[2], pParameters[3], pParameters[4], pParameters[5], &entry.region);
00174                 }
00175         }
00176         else
00177         {
00178                 ImageProcessor::FilterHSV(m_pHSVImage, pResultImage, pParameters[0], pParameters[1], pParameters[2], pParameters[3], pParameters[4], pParameters[5]);
00179         }
00180 }
00181 
00182 
00183 void CObjectColorSegmenter::FindRegionsOfGivenColor(CByteImage *pResultImage, ObjectColor color, RegionList &regionList, int nMinPointsPerRegion)
00184 {
00185         CalculateSegmentedImage(pResultImage, color);
00186         
00187         if (m_pROIList && m_pROIList->size() != 0)
00188         {
00189                 const int nSize = (int) m_pROIList->size();
00190                 for (int i = 0; i < nSize; i++)
00191                 {
00192                         const Object2DEntry &entry = m_pROIList->at(i);
00193                         
00194                         if (entry.color == color)
00195                         {
00196                                 ImageProcessor::Erode(pResultImage, m_pTempImage, 3, &entry.region);
00197                                 ImageProcessor::Dilate(m_pTempImage, pResultImage, 3, &entry.region);
00198                         }
00199                 }
00200         }
00201         else
00202         {
00203                 ImageProcessor::Erode(pResultImage, m_pTempImage);
00204                 ImageProcessor::Dilate(m_pTempImage, pResultImage);
00205         }
00206         
00207         ImageProcessor::FindRegions(pResultImage, regionList, nMinPointsPerRegion, 0, true, true);
00208 }
00209 
00210 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)
00211 {
00212         if (!m_pHSVImage)
00213         {
00214                 printf("error: call CObjectColorSegmenter::SetImage first\n");
00215                 return;
00216         }
00217         
00218         if (m_pROIList && m_pROIList->size() != 0)
00219         {
00220                 ImageProcessor::Zero(pResultImage);
00221                 
00222                 const int nSize = (int) m_pROIList->size();
00223                 for (int i = 0; i < nSize; i++)
00224                 {
00225                         const Object2DEntry &entry = m_pROIList->at(i);
00226                         
00227                         if (entry.color == color)
00228                         {
00229                                 ImageProcessor::FilterHSV(m_pHSVImage, pResultImage, hue, hue_tol, min_sat, max_sat, min_v, max_v, &entry.region);
00230                                 ImageProcessor::Erode(pResultImage, m_pTempImage, 3, &entry.region);
00231                                 ImageProcessor::Dilate(m_pTempImage, pResultImage, 3, &entry.region);
00232                         }
00233                 }
00234         }
00235         else
00236         {
00237                 ImageProcessor::FilterHSV(m_pHSVImage, pResultImage, hue, hue_tol, min_sat, max_sat, min_v, max_v);
00238                 ImageProcessor::Erode(pResultImage, m_pTempImage);
00239                 ImageProcessor::Dilate(m_pTempImage, pResultImage);
00240         }
00241         
00242         
00243         ImageProcessor::FindRegions(pResultImage, regionList, nMinPointsPerRegion, 0, true, true);
00244 }


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