ImagePropertiesCV.cpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *  ImagePropertiesCV.cpp
00003  *
00004  *  (C) 2007 AG Aktives Sehen <agas@uni-koblenz.de>
00005  *           Universitaet Koblenz-Landau
00006  *
00007  *  Additional information:
00008  *  $Id: $
00009  *******************************************************************************/
00010 
00011 #include "ImagePropertiesCV.h"
00012 #include "../KeyPointExtraction/DefaultExtractor.h"
00013 #include "../KeyPointExtraction/KeyPointHelper.h"
00014 
00015 #include "Architecture/Config/Config.h"
00016 
00017 #define THIS ImagePropertiesCV
00018 
00019 THIS::THIS()
00020 {
00021   clear();
00022 }
00023 
00024 THIS::THIS ( std::string name, cv::Mat* imageY, cv::Mat* imageUV, ImageMaskCV* imageMask )
00025 {
00026   clear();
00027 
00028   if ( !imageY || !imageUV )
00029   {
00030     ROS_ERROR_STREAM( "Received 0-pointer as source image." );
00031     return;
00032   }
00033 
00034   m_Name = name;
00035   m_ImageY = imageY;
00036   m_ImageUV = imageUV;
00037   m_ImageMask = imageMask;
00038 
00039   applyMask();
00040 }
00041 
00042 void THIS::clear()
00043 {
00044   m_ImageY = 0;
00045   m_ImageUV = 0;
00046   m_MaskedImageY = 0;
00047   m_MaskedImageUV = 0;
00048   m_KeyPoints = 0;
00049   m_Outline = 0;
00050   m_ImageMask = 0;
00051   m_ImageMaskWithBorder = 0;
00052 }
00053 
00054 
00055 THIS::~THIS()
00056 {
00057   deleteAll();
00058 }
00059 
00060 void THIS::deleteAll()
00061 {
00062   delete m_ImageY;
00063   delete m_ImageUV;
00064   delete m_ImageMask;
00065   delete m_ImageMaskWithBorder;
00066   delete m_MaskedImageY;
00067   delete m_MaskedImageUV;
00068   delete m_KeyPoints;
00069   delete m_Outline;}
00070 
00071 THIS& THIS::operator= ( const THIS& other )
00072 {
00073   deleteAll();
00074   clear();
00075 
00076   m_Name = other.m_Name;
00077   m_Center = other.m_Center;
00078   m_Border = other.m_Border;
00079   m_Center = other.m_Center;
00080 
00081   m_ImageY = new cv::Mat( * ( other.m_ImageY ) );
00082   m_ImageUV = new cv::Mat( * ( other.m_ImageUV ) );
00083 
00084   if ( other.m_ImageMask )
00085   {
00086     m_ImageMask = new ImageMaskCV( *(other.m_ImageMask) );
00087   }
00088 
00089   if ( other.m_ImageMaskWithBorder )
00090   {
00091     m_ImageMaskWithBorder = new ImageMaskCV( *(other.m_ImageMaskWithBorder) );
00092   }
00093 
00094   if ( other.m_MaskedImageY )
00095   {
00096     m_MaskedImageY = new cv::Mat( * ( other.m_MaskedImageY ) );
00097   }
00098 
00099   if ( other.m_MaskedImageUV )
00100   {
00101     m_MaskedImageUV = new cv::Mat( * ( other.m_MaskedImageUV ) );
00102   }
00103   if ( other.m_KeyPoints )
00104   {
00105     m_KeyPoints=new std::vector< KeyPoint > ( * ( other.m_KeyPoints ) );
00106   }
00107 
00108   if ( other.m_Outline )
00109   {
00110     m_Outline=new std::vector<Point2D>( * ( other.m_Outline ) );
00111   }
00112 
00113   return *this;
00114 }
00115 
00116 
00117 THIS::THIS ( const THIS& other )
00118 {
00119   clear();
00120   operator= ( other );
00121 }
00122 
00123 
00124 void THIS::calculateProperties()
00125 {
00126   applyMask();
00127   traceOutline();
00128   extractKeyPoints();
00129 }
00130 
00131 
00132 void THIS::applyMask()
00133 {
00134   if ( m_MaskedImageY )
00135   {
00136     return;
00137   }
00138 
00139   if ( m_ImageMask )
00140   {
00141     m_Border = 0;
00142 
00143     int width = m_ImageY->cols;
00144     int height = m_ImageY->rows;
00145     int newWidth = width + 2*m_Border;
00146     int newHeight = height + 2*m_Border;
00147 
00148     if(m_MaskedImageY)
00149         delete m_MaskedImageY;
00150 
00151     m_MaskedImageY = new cv::Mat( newHeight, newWidth, CV_8UC1);
00152     m_MaskedImageY->setTo(0);
00153 
00154     if(m_MaskedImageUV)
00155         delete m_MaskedImageUV;
00156 
00157     m_MaskedImageUV = new cv::Mat( newHeight, newWidth, CV_8UC3);
00158     m_MaskedImageUV->setTo(cv::Vec3b(0,0,0));
00159 
00160 
00161     for ( int y=0; y<height; y++ )
00162     {
00163       for ( int x=0; x<width; x++ )
00164       {
00165        //   if(m_ImageMask->getData()[x+m_ImageMask->getWidth()*y] != 0)
00166        //   {
00167                 m_MaskedImageY->at<unsigned char>(m_Border+y, m_Border+x) = m_ImageY->at<unsigned char>(y,x);
00168                 m_MaskedImageUV->at<cv::Vec3b>(m_Border+y, m_Border+x) = m_ImageUV->at<cv::Vec3b>(y,x);
00169        //   }
00170       }
00171     }
00172 
00173     for ( int y=0; y<height; y++ )
00174     {
00175       for ( int x=0; x<width; x++ )
00176       {
00177      //     if(m_ImageMask->at<unsigned char>(y,x) != 0)
00178      //     {
00179                 m_MaskedImageUV->at<cv::Vec3b>(m_Border+y, m_Border+x) = m_ImageUV->at<cv::Vec3b>(y,x);
00180      //     }
00181       }
00182     }
00183 
00184     m_ImageMaskWithBorder = new ImageMaskCV( newWidth, newHeight );
00185     m_ImageMaskWithBorder->fill( ImageMaskCV::MASKED );
00186     unsigned char* maskData = m_ImageMask->getData();
00187     unsigned char* maskDataNew = m_ImageMaskWithBorder->getData();
00188 
00189     int i=0;
00190     for ( int y=m_Border; y<m_Border+height; y++ )
00191     {
00192       for ( int x=m_Border; x<m_Border+width; x++ )
00193       {
00194         maskDataNew[x+newWidth*y] = maskData[i];
00195         i++;
00196       }
00197     }
00198 
00199     m_Center = m_ImageMaskWithBorder->getGravCenter();
00200   }
00201   else
00202   {
00203       ROS_INFO_STREAM("in imagePropertiesCV -- image mask is NOT valid");
00204     m_MaskedImageY = new cv::Mat( *m_ImageY );
00205     m_MaskedImageUV = new cv::Mat( *m_ImageUV );
00206 
00207     m_Center = Point2D( m_ImageY->cols/2, m_ImageY->rows/2 );
00208   }
00209 }
00210 
00211 void THIS::extractKeyPoints()
00212 {
00213   if ( m_KeyPoints )
00214   {
00215     return;
00216   }
00217 
00218   applyMask();
00219 
00220   KeyPointExtractor* extractor = DefaultExtractor::createInstance();
00221 
00222   m_KeyPoints = new std::vector<KeyPoint>();
00223 
00224   extractor->setImage( *m_MaskedImageY );
00225   extractor->getKeyPoints( *m_KeyPoints );
00226 
00227   if ( m_ImageMask )
00228   {
00229     KeyPointHelper::maskFilter( *m_KeyPoints, *m_KeyPoints, *m_ImageMaskWithBorder );
00230   }
00231 
00232   delete extractor;
00233 }
00234 
00235 void THIS::traceOutline()
00236 {
00237   if ( m_Outline )
00238   {
00239     return;
00240   }
00241   m_Outline = new std::vector<Point2D>();
00242   //trace object m_Borders (find longest m_Border)
00243   if ( m_ImageMask )
00244   {
00245     ImageMaskCV maskOutline = *m_ImageMask;
00246     maskOutline.dilate(1);
00247     maskOutline.findBorders();
00248     unsigned char *m_BorderData=maskOutline.getData();
00249     unsigned rowStart=0;
00250     unsigned w=maskOutline.getWidth();
00251     unsigned h=maskOutline.getHeight();
00252 
00253     float minDist = ( m_ImageY->cols + m_ImageY->rows ) * 0.002;
00254 
00255     m_Outline->reserve ( 2000 );
00256 
00257     for ( unsigned row=0; row<h; row++ )
00258     {
00259       rowStart=row*w;
00260       for ( unsigned i=rowStart; i<rowStart+w; i++ )
00261       {
00262         if ( m_BorderData[i] == ImageMaskCV::MASKED )
00263         {
00264           //cout << "found m_Borderpixel at " << i-rowStart << "," << row << endl;
00265           //found one m_Border pixel, trace this m_Border
00266           unsigned x=i-rowStart;
00267           unsigned y=row;
00268 
00269           m_BorderData[i] = ImageMaskCV::VISIBLE;
00270           m_Outline->push_back ( Point2D ( x+m_Border,y+m_Border ) );
00271 
00272           bool pixelFound=true;
00273           while ( pixelFound )
00274           {
00275             //cout << x << "," << y << "  ";
00276             if ( Point2D ( x+m_Border,y+m_Border ).distance( m_Outline->back() ) > minDist ) {
00277               m_Outline->push_back ( Point2D ( x+m_Border,y+m_Border ) );
00278             }
00279 
00280             //search neighbours
00281             //search order (inverted because stack pushing inverts the result):
00282             //   538
00283             //   1 2
00284             //   746
00285             int xpos[8]={-1,+1, 0, 0,-1,+1,-1,+1};
00286             int ypos[8]={ 0, 0,-1,+1,-1,+1,+1,-1};
00287 
00288             pixelFound=false;
00289             for ( int j=7; j>=0; j-- )
00290             {
00291               int tmp_x=x+xpos[j];
00292               int tmp_y=y+ypos[j];
00293               if ( ( tmp_x < 0 ) || ( tmp_y < 0 ) || ( tmp_x >= (int)m_ImageMask->getWidth() ) ||  ( tmp_y >= (int)m_ImageMask->getHeight() ) ) {
00294                 continue;
00295               }
00296               if ( m_BorderData[ ( tmp_y ) *w + tmp_x ] == ImageMaskCV::MASKED )
00297               {
00298                 x=tmp_x;
00299                 y=tmp_y;
00300                 //cout << tmp_x << "," << tmp_y << "  ";
00301                 m_BorderData[ y*w + x ] = ImageMaskCV::VISIBLE;
00302                 pixelFound=true;
00303                 break;
00304               }
00305             }
00306             //cout << " - ";
00307           }
00308           m_Outline->push_back ( Point2D ( x+m_Border,y+m_Border ) );
00309           m_Outline->push_back( Point2D::invalidPoint() );
00310         }
00311       }
00312     }
00313   }
00314 }
00315 
00316 std::vector<Point2D> THIS::getBoundingBox() const
00317 {
00318   std::vector<Point2D> bBox;
00319   bBox.reserve( 5 );
00320   bBox.push_back( Point2D( 0, 0 ) );
00321   bBox.push_back( Point2D( m_ImageMask->getWidth()-1+2*m_Border, 0 ) );
00322   bBox.push_back( Point2D( m_ImageMask->getWidth()-1+2*m_Border, m_ImageMask->getHeight()-1+2*m_Border ) );
00323   bBox.push_back( Point2D( 0, m_ImageMask->getHeight()-1+2*m_Border ) );
00324   bBox.push_back( Point2D( 0, 0 ) );
00325   return bBox;
00326 }
00327 
00328 
00329 
00330 #undef THIS
00331 


or_libs
Author(s): raphael
autogenerated on Mon Oct 6 2014 02:53:18