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


obj_rec_gui
Author(s): AGAS/agas@uni-koblenz.de
autogenerated on Mon Oct 6 2014 02:53:43