Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
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
00153 if(m_MaskedImageUV)
00154 delete m_MaskedImageUV;
00155
00156 m_MaskedImageUV = new cv::Mat( newHeight, newWidth, CV_8UC3);
00157
00158 for ( int y=0; y<height; y++ )
00159 {
00160 for ( int x=0; x<width; x++ )
00161 {
00162 m_MaskedImageY->at<unsigned char>(m_Border+y, m_Border+x) = m_ImageY->at<unsigned char>(y,x);
00163 }
00164 }
00165
00166 for ( int y=0; y<height; y++ )
00167 {
00168 for ( int x=0; x<width; x++ )
00169 {
00170 m_MaskedImageUV->at<cv::Vec3b>(m_Border+y, m_Border+x) = m_ImageUV->at<cv::Vec3b>(y,x);
00171 }
00172 }
00173
00174 m_ImageMaskWithBorder = new ImageMaskCV( newWidth, newHeight );
00175 m_ImageMaskWithBorder->fill( ImageMaskCV::MASKED );
00176 unsigned char* maskData = m_ImageMask->getData();
00177 unsigned char* maskDataNew = m_ImageMaskWithBorder->getData();
00178
00179 int i=0;
00180 for ( int y=m_Border; y<m_Border+height; y++ )
00181 {
00182 for ( int x=m_Border; x<m_Border+width; x++ )
00183 {
00184 maskDataNew[x+newWidth*y] = maskData[i];
00185 i++;
00186 }
00187 }
00188
00189 m_Center = m_ImageMaskWithBorder->getGravCenter();
00190 }
00191 else
00192 {
00193 m_MaskedImageY = new cv::Mat( *m_ImageY );
00194 m_MaskedImageUV = new cv::Mat( *m_ImageUV );
00195
00196 m_Center = Point2D( m_ImageY->cols/2, m_ImageY->rows/2 );
00197 }
00198 }
00199
00200 void THIS::extractKeyPoints()
00201 {
00202 if ( m_KeyPoints )
00203 {
00204 return;
00205 }
00206
00207 applyMask();
00208
00209 KeyPointExtractor* extractor = DefaultExtractor::createInstance();
00210
00211 m_KeyPoints = new std::vector<KeyPoint>();
00212
00213 extractor->setImage( *m_MaskedImageY );
00214 extractor->getKeyPoints( *m_KeyPoints );
00215
00216 if ( m_ImageMask )
00217 {
00218 KeyPointHelper::maskFilter( *m_KeyPoints, *m_KeyPoints, *m_ImageMaskWithBorder );
00219 }
00220
00221 delete extractor;
00222 }
00223
00224 void THIS::traceOutline()
00225 {
00226 if ( m_Outline )
00227 {
00228 return;
00229 }
00230 m_Outline = new std::vector<Point2D>();
00231
00232 if ( m_ImageMask )
00233 {
00234 ImageMaskCV maskOutline = *m_ImageMask;
00235 maskOutline.dilate(1);
00236 maskOutline.findBorders();
00237 unsigned char *m_BorderData=maskOutline.getData();
00238 unsigned rowStart=0;
00239 unsigned w=maskOutline.getWidth();
00240 unsigned h=maskOutline.getHeight();
00241
00242 float minDist = ( m_ImageY->cols + m_ImageY->rows ) * 0.002;
00243
00244 m_Outline->reserve ( 2000 );
00245
00246 for ( unsigned row=0; row<h; row++ )
00247 {
00248 rowStart=row*w;
00249 for ( unsigned i=rowStart; i<rowStart+w; i++ )
00250 {
00251 if ( m_BorderData[i] == ImageMaskCV::MASKED )
00252 {
00253
00254
00255 unsigned x=i-rowStart;
00256 unsigned y=row;
00257
00258 m_BorderData[i] = ImageMaskCV::VISIBLE;
00259 m_Outline->push_back ( Point2D ( x+m_Border,y+m_Border ) );
00260
00261 bool pixelFound=true;
00262 while ( pixelFound )
00263 {
00264
00265 if ( Point2D ( x+m_Border,y+m_Border ).distance( m_Outline->back() ) > minDist ) {
00266 m_Outline->push_back ( Point2D ( x+m_Border,y+m_Border ) );
00267 }
00268
00269
00270
00271
00272
00273
00274 int xpos[8]={-1,+1, 0, 0,-1,+1,-1,+1};
00275 int ypos[8]={ 0, 0,-1,+1,-1,+1,+1,-1};
00276
00277 pixelFound=false;
00278 for ( int j=7; j>=0; j-- )
00279 {
00280 int tmp_x=x+xpos[j];
00281 int tmp_y=y+ypos[j];
00282 if ( ( tmp_x < 0 ) || ( tmp_y < 0 ) || ( tmp_x >= (int)m_ImageMask->getWidth() ) || ( tmp_y >= (int)m_ImageMask->getHeight() ) ) {
00283 continue;
00284 }
00285 if ( m_BorderData[ ( tmp_y ) *w + tmp_x ] == ImageMaskCV::MASKED )
00286 {
00287 x=tmp_x;
00288 y=tmp_y;
00289
00290 m_BorderData[ y*w + x ] = ImageMaskCV::VISIBLE;
00291 pixelFound=true;
00292 break;
00293 }
00294 }
00295
00296 }
00297 m_Outline->push_back ( Point2D ( x+m_Border,y+m_Border ) );
00298 m_Outline->push_back( Point2D::invalidPoint() );
00299 }
00300 }
00301 }
00302 }
00303 }
00304
00305 std::vector<Point2D> THIS::getBoundingBox() const
00306 {
00307 std::vector<Point2D> bBox;
00308 bBox.reserve( 5 );
00309 bBox.push_back( Point2D( 0, 0 ) );
00310 bBox.push_back( Point2D( m_ImageMask->getWidth()-1+2*m_Border, 0 ) );
00311 bBox.push_back( Point2D( m_ImageMask->getWidth()-1+2*m_Border, m_ImageMask->getHeight()-1+2*m_Border ) );
00312 bBox.push_back( Point2D( 0, m_ImageMask->getHeight()-1+2*m_Border ) );
00313 bBox.push_back( Point2D( 0, 0 ) );
00314 return bBox;
00315 }
00316
00317
00318
00319 #undef THIS
00320