123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- #include "Operations.h"
- using namespace OBJREC;
- using namespace std;
- using namespace NICE;
- Operation::Operation()
- {
- values = NULL;
- maxtypes = 1000;
- }
- void Operation::set ( int _x1, int _y1, int _x2, int _y2, int _channel1, int _channel2, ValueAccess *_values )
- {
- x1 = _x1;
- y1 = _y1;
- x2 = _x2;
- y2 = _y2;
- channel1 = _channel1;
- channel2 = _channel2;
- values = _values;
- }
- void Operation::setContext ( bool _context )
- {
- context = _context;
- }
- bool Operation::getContext()
- {
- return context;
- }
- void Operation::getXY ( const Features &feats, int &xsize, int &ysize )
- {
- xsize = feats.feats->width();
- ysize = feats.feats->height();
- }
- void Operation::store ( std::ostream & os )
- {
- os << x1 << " " << x2 << " " << y1 << " " << y2 << " " << channel1 << " " << channel2 << std::endl;
- if ( values == NULL )
- os << -1 << std::endl;
- else
- os << values->getType() << std::endl;
- }
- void Operation::restore ( std::istream &is )
- {
- is >> x1;
- is >> x2;
- is >> y1;
- is >> y2;
- is >> channel1;
- is >> channel2;
- int tmp;
- is >> tmp;
- if ( tmp >= 0 )
- {
- if ( tmp == RAWFEAT )
- {
- values = new MCImageAccess();
- }
- else if ( tmp == CONTEXT )
- {
- values = new ClassificationResultAccess();
- }
- else
- {
- throw ( "no valid ValueAccess" );
- }
- }
- else
- {
- values = NULL;
- }
- }
- std::string Operation::writeInfos()
- {
- std::stringstream ss;
- ss << " x1: " << x1 << " y1: " << y1 << " x2: " << x2 << " y2: " << y2 << " c1: " << channel1 << " c2: " << channel2;
- return ss.str();
- }
- double Equality::getVal ( const Features &feats, const int &x, const int &y )
- {
- int xsize, ysize;
- getXY ( feats, xsize, ysize );
- double v1 = values->getVal ( feats, BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 );
- double v2 = values->getVal ( feats, BOUND ( x + x2, 0, xsize - 1 ), BOUND ( y + y2, 0, ysize - 1 ), channel2 );
- return (double)(v1 == v2);
- }
- double Minus::getVal ( const Features &feats, const int &x, const int &y )
- {
- int xsize, ysize;
- getXY ( feats, xsize, ysize );
- double v1 = values->getVal ( feats, BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 );
- double v2 = values->getVal ( feats, BOUND ( x + x2, 0, xsize - 1 ), BOUND ( y + y2, 0, ysize - 1 ), channel2 );
- return v1 -v2;
- }
- double MinusAbs::getVal ( const Features &feats, const int &x, const int &y )
- {
- int xsize, ysize;
- getXY ( feats, xsize, ysize );
- double v1 = values->getVal ( feats, BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 );
- double v2 = values->getVal ( feats, BOUND ( x + x2, 0, xsize - 1 ), BOUND ( y + y2, 0, ysize - 1 ), channel2 );
- return abs ( v1 -v2 );
- }
- double Addition::getVal ( const Features &feats, const int &x, const int &y )
- {
- int xsize, ysize;
- getXY ( feats, xsize, ysize );
- double v1 = values->getVal ( feats, BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 );
- double v2 = values->getVal ( feats, BOUND ( x + x2, 0, xsize - 1 ), BOUND ( y + y2, 0, ysize -
- 1 ), channel2 );
- return v1 + v2;
- }
- double Only1::getVal ( const Features &feats, const int &x, const int &y )
- {
- int xsize, ysize;
- getXY ( feats, xsize, ysize );
- double v1 = values->getVal ( feats, BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 );
- return v1;
- }
- double RelativeXPosition::getVal ( const Features &feats, const int &x, const int &y )
- {
- int xsize, ysize;
- getXY ( feats, xsize, ysize );
- return ( double ) x / ( double ) xsize;
- }
- double RelativeYPosition::getVal ( const Features &feats, const int &x, const int &y )
- {
- int xsize, ysize;
- getXY ( feats, xsize, ysize );
- return ( double ) x / ( double ) xsize;
- }
- double IntegralOps::getVal ( const Features &feats, const int &x, const int &y )
- {
- int xsize, ysize;
- getXY ( feats, xsize, ysize );
- return computeMean ( *feats.feats, BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), BOUND ( x + x2, 0, xsize - 1 ), BOUND ( y + y2, 0, ysize - 1 ), channel1 );
- }
- double GlobalFeats::getVal ( const Features &feats, const int &x, const int &y )
- {
- int xsize, ysize;
- getXY ( feats, xsize, ysize );
- return computeMean ( *feats.feats, 0, 0, xsize - 1, ysize - 1, channel1 );
- }
- double IntegralCenteredOps::getVal ( const Features &feats, const int &x, const int &y )
- {
- int xsize, ysize;
- getXY ( feats, xsize, ysize );
- return computeMean ( *feats.feats, BOUND ( x - x1, 0, xsize - 1 ), BOUND ( y - y1, 0, ysize - 1 ), BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 );
- }
- double BiIntegralCenteredOps::getVal ( const Features &feats, const int &x, const int &y )
- {
- int xsize, ysize;
- getXY ( feats, xsize, ysize );
- return computeMean ( *feats.feats, BOUND ( x - x1, 0, xsize - 1 ), BOUND ( y - y1, 0, ysize - 1 ), BOUND ( x + x1, 0, xsize - 1 ), BOUND ( y + y1, 0, ysize - 1 ), channel1 ) - computeMean ( *feats.feats, BOUND ( x - x2, 0, xsize - 1 ), BOUND ( y - y2, 0, ysize - 1 ), BOUND ( x + x2, 0, xsize - 1 ), BOUND ( y + y2, 0, ysize - 1 ), channel1 );
- }
- double HaarHorizontal::getVal ( const Features &feats, const int &x, const int &y )
- {
- int xsize, ysize;
- getXY ( feats, xsize, ysize );
- int tlx = BOUND ( x - x1, 0, xsize - 1 );
- int tly = BOUND ( y - y1, 0, ysize - 1 );
- int lrx = BOUND ( x + x1, 0, xsize - 1 );
- int lry = BOUND ( y + y1, 0, ysize - 1 );
- return computeMean ( *feats.feats, tlx, tly, lrx, y, channel1 ) - computeMean ( *feats.feats, tlx, y, lrx, lry, channel1 );
- }
- double HaarVertical::getVal ( const Features &feats, const int &x, const int &y )
- {
- int xsize, ysize;
- getXY ( feats, xsize, ysize );
- int tlx = BOUND ( x - x1, 0, xsize - 1 );
- int tly = BOUND ( y - y1, 0, ysize - 1 );
- int lrx = BOUND ( x + x1, 0, xsize - 1 );
- int lry = BOUND ( y + y1, 0, ysize - 1 );
- return computeMean ( *feats.feats, tlx, tly, x, lry, channel1 ) - computeMean ( *feats.feats, x, tly, lrx, lry, channel1 );
- }
- double HaarDiag::getVal ( const Features &feats, const int &x, const int &y )
- {
- int xsize, ysize;
- getXY ( feats, xsize, ysize );
- int tlx = BOUND ( x - x1, 0, xsize - 1 );
- int tly = BOUND ( y - y1, 0, ysize - 1 );
- int lrx = BOUND ( x + x1, 0, xsize - 1 );
- int lry = BOUND ( y + y1, 0, ysize - 1 );
- return computeMean ( *feats.feats, tlx, tly, x, y, channel1 ) + computeMean ( *feats.feats, x, y, lrx, lry, channel1 ) - computeMean ( *feats.feats, tlx, y, x, lry, channel1 ) - computeMean ( *feats.feats, x, tly, lrx, y, channel1 );
- }
- double Haar3Horiz::getVal ( const Features &feats, const int &x, const int &y )
- {
- int xsize, ysize;
- getXY ( feats, xsize, ysize );
- int tlx = BOUND ( x - x2, 0, xsize - 1 );
- int tly = BOUND ( y - y2, 0, ysize - 1 );
- int mtly = BOUND ( y - y1, 0, ysize - 1 );
- int mlry = BOUND ( y + y1, 0, ysize - 1 );
- int lrx = BOUND ( x + x2, 0, xsize - 1 );
- int lry = BOUND ( y + y2, 0, ysize - 1 );
- return computeMean ( *feats.feats, tlx, tly, lrx, mtly, channel1 ) - computeMean ( *feats.feats, tlx, mtly, lrx, mlry, channel1 ) + computeMean ( *feats.feats, tlx, mlry, lrx, lry, channel1 );
- }
- double Haar3Vert::getVal ( const Features &feats, const int &x, const int &y )
- {
- int xsize, ysize;
- getXY ( feats, xsize, ysize );
- int tlx = BOUND ( x - x2, 0, xsize - 1 );
- int tly = BOUND ( y - y2, 0, ysize - 1 );
- int mtlx = BOUND ( x - x1, 0, xsize - 1 );
- int mlrx = BOUND ( x + x1, 0, xsize - 1 );
- int lrx = BOUND ( x + x2, 0, xsize - 1 );
- int lry = BOUND ( y + y2, 0, ysize - 1 );
- return computeMean ( *feats.feats, tlx, tly, mtlx, lry, channel1 ) - computeMean ( *feats.feats, mtlx, tly, mlrx, lry, channel1 ) + computeMean ( *feats.feats, mlrx, tly, lrx, lry, channel1 );
- }
- void IntegralOps::set ( int _x1, int _y1, int _x2, int _y2, int _channel1, int _channel2, ValueAccess *_values )
- {
- x1 = std::min ( _x1, _x2 );
- y1 = std::min ( _y1, _y2 );
- x2 = std::max ( _x1, _x2 );
- y2 = std::max ( _y1, _y2 );
- channel1 = _channel1;
- channel2 = _channel2;
- values = _values;
- }
- double IntegralOps::computeMean ( const NICE::MultiChannelImageT<double> &intImg, const int &uLx, const int &uLy, const int &lRx, const int &lRy, const int &chan )
- {
- double val1 = intImg.get ( uLx, uLy, chan );
- double val2 = intImg.get ( lRx, uLy, chan );
- double val3 = intImg.get ( uLx, lRy, chan );
- double val4 = intImg.get ( lRx, lRy, chan );
- double area = ( lRx - uLx ) * ( lRy - uLy );
- if ( area == 0 )
- return 0.0;
- return ( val1 + val4 - val2 - val3 ) / area;
- }
- void IntegralCenteredOps::set ( int _x1, int _y1, int _x2, int _y2, int _channel1, int _channel2, ValueAccess *_values )
- {
- x1 = abs ( _x1 );
- y1 = abs ( _y1 );
- x2 = abs ( _x2 );
- y2 = abs ( _y2 );
- channel1 = _channel1;
- channel2 = _channel2;
- values = _values;
- }
- void BiIntegralCenteredOps::set ( int _x1, int _y1, int _x2, int _y2, int _channel1, int _channel2, ValueAccess *_values )
- {
- x1 = std::min ( abs ( _x1 ), abs ( _x2 ) );
- y1 = std::min ( abs ( _y1 ), abs ( _y2 ) );
- x2 = std::max ( abs ( _x1 ), abs ( _x2 ) );
- y2 = std::max ( abs ( _y1 ), abs ( _y2 ) );
- channel1 = _channel1;
- channel2 = _channel2;
- values = _values;
- }
|