1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369 |
- #ifndef __OPENCV_DESCRIPTOR_HPP__
- #define __OPENCV_DESCRIPTOR_HPP__
- #include <map>
- #include <vector>
- #include <list>
- #if defined _MSC_VER && _MSC_VER <= 1700
- #include <stdint.h>
- #else
- #include <inttypes.h>
- #endif
- #include <stdio.h>
- #include <iostream>
- #include "opencv2/core/utility.hpp"
- #include <opencv2/imgproc.hpp>
- #include "opencv2/core.hpp"
- typedef uint64_t UINT64;
- typedef uint32_t UINT32;
- typedef uint16_t UINT16;
- typedef uint8_t UINT8;
- #define UINT64_1 ((UINT64)0x01)
- #define UINT32_1 ((UINT32)0x01)
- namespace cv
- {
- namespace line_descriptor
- {
- struct CV_EXPORTS KeyLine
- {
- public:
-
- float angle;
-
- int class_id;
-
- int octave;
-
- Point2f pt;
-
- float response;
-
- float size;
-
- float startPointX;
- float startPointY;
- float endPointX;
- float endPointY;
-
- float sPointInOctaveX;
- float sPointInOctaveY;
- float ePointInOctaveX;
- float ePointInOctaveY;
-
- float lineLength;
-
- int numOfPixels;
-
- Point2f getStartPoint() const
- {
- return Point2f(startPointX, startPointY);
- }
-
- Point2f getEndPoint() const
- {
- return Point2f(endPointX, endPointY);
- }
-
- Point2f getStartPointInOctave() const
- {
- return Point2f(sPointInOctaveX, sPointInOctaveY);
- }
-
- Point2f getEndPointInOctave() const
- {
- return Point2f(ePointInOctaveX, ePointInOctaveY);
- }
-
- KeyLine()
- {
- }
- };
- class CV_EXPORTS BinaryDescriptor : public Algorithm
- {
- public:
-
- struct CV_EXPORTS Params
- {
-
- Params();
-
- int numOfOctave_;
-
- int widthOfBand_;
-
- int reductionRatio;
- int ksize_;
-
- void read( const FileNode& fn );
-
- void write( FileStorage& fs ) const;
- };
-
- BinaryDescriptor( const BinaryDescriptor::Params ¶meters = BinaryDescriptor::Params() );
-
- static Ptr<BinaryDescriptor> createBinaryDescriptor();
- static Ptr<BinaryDescriptor> createBinaryDescriptor( Params parameters );
-
- ~BinaryDescriptor();
-
- int getNumOfOctaves();
-
- void setNumOfOctaves( int octaves );
-
- int getWidthOfBand();
-
- void setWidthOfBand( int width );
-
- int getReductionRatio();
-
- void setReductionRatio( int rRatio );
-
- virtual void read( const cv::FileNode& fn );
-
- virtual void write( cv::FileStorage& fs ) const;
-
- void detect( const Mat& image, CV_OUT std::vector<KeyLine>& keypoints, const Mat& mask = Mat() );
-
- void detect( const std::vector<Mat>& images, std::vector<std::vector<KeyLine> >& keylines, const std::vector<Mat>& masks =
- std::vector<Mat>() ) const;
-
- void compute( const Mat& image, CV_OUT CV_IN_OUT std::vector<KeyLine>& keylines, CV_OUT Mat& descriptors, bool returnFloatDescr = false ) const;
-
- void compute( const std::vector<Mat>& images, std::vector<std::vector<KeyLine> >& keylines, std::vector<Mat>& descriptors, bool returnFloatDescr =
- false ) const;
-
- int descriptorSize() const;
-
- int descriptorType() const;
-
-
- int defaultNorm() const;
-
- virtual void operator()( InputArray image, InputArray mask, CV_OUT std::vector<KeyLine>& keylines, OutputArray descriptors,
- bool useProvidedKeyLines = false, bool returnFloatDescr = false ) const;
- protected:
-
- virtual void detectImpl( const Mat& imageSrc, std::vector<KeyLine>& keylines, const Mat& mask = Mat() ) const;
-
- virtual void computeImpl( const Mat& imageSrc, std::vector<KeyLine>& keylines, Mat& descriptors, bool returnFloatDescr,
- bool useDetectionData ) const;
- private:
-
- struct OctaveLine
- {
- unsigned int octaveCount;
- unsigned int lineIDInOctave;
- unsigned int lineIDInScaleLineVec;
- float lineLength;
- };
-
- struct SingleLine
- {
-
- double rho;
- double theta;
- double linePointX;
- double linePointY;
-
- double startPointX;
- double startPointY;
- double endPointX;
- double endPointY;
-
- double direction;
-
- double gradientMagnitude;
-
- double darkSideGrayValue;
-
- double lightSideGrayValue;
-
- double lineLength;
-
- double width;
-
- int numOfPixels;
-
- std::vector<double> descriptor;
- };
-
- typedef std::vector<SingleLine> Lines_list;
- struct OctaveSingleLine
- {
-
- float startPointX;
- float startPointY;
- float endPointX;
- float endPointY;
-
- float sPointInOctaveX;
- float sPointInOctaveY;
- float ePointInOctaveX;
- float ePointInOctaveY;
-
- float direction;
-
- float salience;
-
- float lineLength;
-
- unsigned int numOfPixels;
-
- unsigned int octaveCount;
-
- std::vector<float> descriptor;
- OctaveSingleLine() : startPointX(0), startPointY(0), endPointX(0), endPointY(0),
- sPointInOctaveX(0), sPointInOctaveY(0), ePointInOctaveX(0), ePointInOctaveY(0),
- direction(0), salience(0), lineLength(0), numOfPixels(0), octaveCount(0),
- descriptor(std::vector<float>())
- {}
- };
- struct Pixel
- {
- unsigned int x;
- unsigned int y;
- };
- struct EdgeChains
- {
- std::vector<unsigned int> xCors;
- std::vector<unsigned int> yCors;
- std::vector<unsigned int> sId;
- unsigned int numOfEdges;
- };
- struct LineChains
- {
- std::vector<unsigned int> xCors;
- std::vector<unsigned int> yCors;
- std::vector<unsigned int> sId;
- unsigned int numOfLines;
- };
- typedef std::list<Pixel> PixelChain;
- struct EDLineParam
- {
- int ksize;
- float sigma;
- float gradientThreshold;
- float anchorThreshold;
- int scanIntervals;
- int minLineLen;
- double lineFitErrThreshold;
- };
- #define RELATIVE_ERROR_FACTOR 100.0
- #define MLN10 2.30258509299404568402
- #define log_gamma(x) ((x)>15.0?log_gamma_windschitl(x):log_gamma_lanczos(x))
-
- class EDLineDetector
- {
- public:
- EDLineDetector();
- EDLineDetector( EDLineParam param );
- ~EDLineDetector();
-
- int EdgeDrawing( cv::Mat &image, EdgeChains &edgeChains );
-
- int EDline( cv::Mat &image, LineChains &lines );
-
- int EDline( cv::Mat &image );
- cv::Mat dxImg_;
- cv::Mat dyImg_;
- cv::Mat gImgWO_;
- LineChains lines_;
-
- std::vector<std::vector<double> > lineEquations_;
-
- std::vector<std::vector<float> > lineEndpoints_;
-
- std::vector<float> lineDirection_;
-
- std::vector<float> lineSalience_;
-
- unsigned int imageWidth;
- unsigned int imageHeight;
-
- double lineFitErrThreshold_;
-
- short gradienThreshold_;
-
- unsigned char anchorThreshold_;
-
- unsigned int scanIntervals_;
- int minLineLen_;
- private:
- void InitEDLine_();
-
- double LeastSquaresLineFit_( unsigned int *xCors, unsigned int *yCors, unsigned int offsetS, std::vector<double> &lineEquation );
-
- double LeastSquaresLineFit_( unsigned int *xCors, unsigned int *yCors, unsigned int offsetS, unsigned int newOffsetS, unsigned int offsetE,
- std::vector<double> &lineEquation );
-
- bool LineValidation_( unsigned int *xCors, unsigned int *yCors, unsigned int offsetS, unsigned int offsetE, std::vector<double> &lineEquation,
- float &direction );
- bool bValidate_;
- int ksize_;
- float sigma_;
-
-
- unsigned int *pFirstPartEdgeX_;
-
- unsigned int *pFirstPartEdgeY_;
-
- unsigned int *pFirstPartEdgeS_;
-
- unsigned int *pSecondPartEdgeX_;
-
- unsigned int *pSecondPartEdgeY_;
-
- unsigned int *pSecondPartEdgeS_;
-
- unsigned int *pAnchorX_;
-
- unsigned int *pAnchorY_;
-
- cv::Mat edgeImage_;
- cv::Mat gImg_;
- cv::Mat dirImg_;
- double logNT_;
- cv::Mat_<float> ATA;
- cv::Mat_<float> ATV;
- cv::Mat_<float> fitMatT;
- cv::Mat_<float> fitVec;
- cv::Mat_<float> tempMatLineFit;
- cv::Mat_<float> tempVecLineFit;
-
- static int double_equal( double a, double b )
- {
- double abs_diff, aa, bb, abs_max;
-
- if( a == b )
- return true;
- abs_diff = fabs( a - b );
- aa = fabs( a );
- bb = fabs( b );
- abs_max = aa > bb ? aa : bb;
-
- if( abs_max < DBL_MIN )
- abs_max = DBL_MIN;
-
- return ( abs_diff / abs_max ) <= ( RELATIVE_ERROR_FACTOR * DBL_EPSILON );
- }
-
- static double log_gamma_lanczos( double x )
- {
- static double q[7] =
- { 75122.6331530, 80916.6278952, 36308.2951477, 8687.24529705, 1168.92649479, 83.8676043424, 2.50662827511 };
- double a = ( x + 0.5 ) * log( x + 5.5 ) - ( x + 5.5 );
- double b = 0.0;
- int n;
- for ( n = 0; n < 7; n++ )
- {
- a -= log( x + (double) n );
- b += q[n] * pow( x, (double) n );
- }
- return a + log( b );
- }
-
- static double log_gamma_windschitl( double x )
- {
- return 0.918938533204673 + ( x - 0.5 ) * log( x ) - x + 0.5 * x * log( x * sinh( 1 / x ) + 1 / ( 810.0 * pow( x, 6.0 ) ) );
- }
-
- static double nfa( int n, int k, double p, double logNT )
- {
- double tolerance = 0.1;
- double log1term, term, bin_term, mult_term, bin_tail, err, p_term;
- int i;
-
- if( n < 0 || k < 0 || k > n || p <= 0.0 || p >= 1.0 )
- CV_Error(Error::StsBadArg, "nfa: wrong n, k or p values.\n");
-
- if( n == 0 || k == 0 )
- return -logNT;
- if( n == k )
- return -logNT - (double) n * log10( p );
-
- p_term = p / ( 1.0 - p );
-
-
- log1term = log_gamma( (double) n + 1.0 )- log_gamma( (double ) k + 1.0 )- log_gamma( (double ) ( n - k ) + 1.0 )
- + (double) k * log( p )
- + (double) ( n - k ) * log( 1.0 - p );
- term = exp( log1term );
- if( double_equal( term, 0.0 ) )
- {
- if( (double) k > (double) n * p )
- return -log1term / MLN10 - logNT;
- else
- return -logNT;
- }
- bin_tail = term;
- for ( i = k + 1; i <= n; i++ )
- {
-
- bin_term = (double) ( n - i + 1 ) / (double) i;
- mult_term = bin_term * p_term;
- term *= mult_term;
- bin_tail += term;
- if( bin_term < 1.0 )
- {
-
- err = term * ( ( 1.0 - pow( mult_term, (double) ( n - i + 1 ) ) ) / ( 1.0 - mult_term ) - 1.0 );
-
- if( err < tolerance * fabs( -log10( bin_tail ) - logNT ) * bin_tail )
- break;
- }
- }
- return -log10( bin_tail ) - logNT;
- }
- };
-
- typedef std::vector<OctaveSingleLine> LinesVec;
- typedef std::vector<LinesVec> ScaleLines;
- void computeGaussianPyramid( const Mat& image, const int numOctaves );
- void computeSobel( const Mat& image, const int numOctaves );
- unsigned char binaryConversion( float* f1, float* f2 );
- int computeLBD( ScaleLines &keyLines, bool useDetectionData = false );
- int OctaveKeyLines( cv::Mat& image, ScaleLines &keyLines );
- std::vector<double> gaussCoefL_;
- std::vector<double> gaussCoefG_;
- Params params;
- std::vector<cv::Size> images_sizes;
- std::vector<Ptr<EDLineDetector> > edLineVec_;
- std::vector<cv::Mat> dxImg_vector, dyImg_vector;
- std::vector<cv::Mat> octaveImages;
- };
- class CV_EXPORTS LSDDetector : public Algorithm
- {
- public:
- LSDDetector()
- {
- }
- ;
- static Ptr<LSDDetector> createLSDDetector();
- void detect( const Mat& image, CV_OUT std::vector<KeyLine>& keypoints, int scale, int numOctaves, const Mat& mask = Mat() );
- void detect( const std::vector<Mat>& images, std::vector<std::vector<KeyLine> >& keylines, int scale, int numOctaves,
- const std::vector<Mat>& masks = std::vector<Mat>() ) const;
- private:
- void computeGaussianPyramid( const Mat& image, int numOctaves, int scale );
- void detectImpl( const Mat& imageSrc, std::vector<KeyLine>& keylines, int numOctaves, int scale, const Mat& mask ) const;
- std::vector<cv::Mat> gaussianPyrs;
- };
- class CV_EXPORTS BinaryDescriptorMatcher : public Algorithm
- {
- public:
- void match( const Mat& queryDescriptors, const Mat& trainDescriptors, std::vector<DMatch>& matches, const Mat& mask = Mat() ) const;
- void match( const Mat& queryDescriptors, std::vector<DMatch>& matches, const std::vector<Mat>& masks = std::vector<Mat>() );
- void knnMatch( const Mat& queryDescriptors, const Mat& trainDescriptors, std::vector<std::vector<DMatch> >& matches, int k, const Mat& mask = Mat(),
- bool compactResult = false ) const;
- void knnMatch( const Mat& queryDescriptors, std::vector<std::vector<DMatch> >& matches, int k, const std::vector<Mat>& masks = std::vector<Mat>(),
- bool compactResult = false );
- void radiusMatch( const Mat& queryDescriptors, const Mat& trainDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
- const Mat& mask = Mat(), bool compactResult = false ) const;
- void radiusMatch( const Mat& queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance, const std::vector<Mat>& masks =
- std::vector<Mat>(),
- bool compactResult = false );
- void add( const std::vector<Mat>& descriptors );
- void train();
- static Ptr<BinaryDescriptorMatcher> createBinaryDescriptorMatcher();
- void clear();
- BinaryDescriptorMatcher();
- ~BinaryDescriptorMatcher()
- {
- }
- private:
- class BucketGroup
- {
- public:
- BucketGroup(bool needAllocateGroup = true);
- ~BucketGroup();
- void insert( int subindex, UINT32 data );
- UINT32* query( int subindex, int *size );
- void insert_value( std::vector<uint32_t>& vec, int index, UINT32 data );
- void push_value( std::vector<uint32_t>& vec, UINT32 Data );
- UINT32 empty;
- std::vector<uint32_t> group;
- };
- class SparseHashtable
- {
- private:
- static const int MAX_B;
- std::vector<BucketGroup> table;
- public:
- SparseHashtable();
- ~SparseHashtable();
- int init( int _b );
- void insert( UINT64 index, UINT32 data );
- UINT32* query( UINT64 index, int* size );
- int b;
- UINT64 size;
- };
- class bitarray
- {
- public:
- UINT32 *arr;
- UINT32 length;
- bitarray()
- {
- arr = NULL;
- length = 0;
- }
- bitarray( UINT64 _bits )
- {
- arr = NULL;
- init( _bits );
- }
- void init( UINT64 _bits )
- {
- if( arr )
- delete[] arr;
- length = (UINT32) ceil( _bits / 32.00 );
- arr = new UINT32[length];
- erase();
- }
- ~bitarray()
- {
- if( arr )
- delete[] arr;
- }
- inline void flip( UINT64 index )
- {
- arr[index >> 5] ^= ( (UINT32) 0x01 ) << ( index % 32 );
- }
- inline void set( UINT64 index )
- {
- arr[index >> 5] |= ( (UINT32) 0x01 ) << ( index % 32 );
- }
- inline UINT8 get( UINT64 index )
- {
- return ( arr[index >> 5] & ( ( (UINT32) 0x01 ) << ( index % 32 ) ) ) != 0;
- }
- inline void erase()
- {
- memset( arr, 0, sizeof(UINT32) * length );
- }
- };
- class Mihasher
- {
- public:
- int B;
- int B_over_8;
- int b;
- int m;
- int mplus;
- int D;
- int d;
- int K;
- UINT64 N;
- cv::Mat codes;
- Ptr<bitarray> counter;
- std::vector<SparseHashtable> H;
- std::vector<UINT32> xornum;
- int power[100];
- Mihasher();
- ~Mihasher();
- Mihasher( int B, int m );
- void setK( int K );
- void populate( cv::Mat & codes, UINT32 N, int dim1codes );
- void batchquery( UINT32 * results, UINT32 *numres, const cv::Mat & q, UINT32 numq, int dim1queries );
- private:
- void query( UINT32 * results, UINT32* numres, UINT8 *q, UINT64 * chunks, UINT32 * res );
- };
- void checkKDistances( UINT32 * numres, int k, std::vector<int>& k_distances, int row, int string_length ) const;
- Mat descriptorsMat;
- std::map<int, int> indexesMap;
- Ptr<Mihasher> dataset;
- int nextAddedIndex;
- int numImages;
- int descrInDS;
- };
- struct CV_EXPORTS DrawLinesMatchesFlags
- {
- enum
- {
- DEFAULT = 0,
-
-
-
- DRAW_OVER_OUTIMG = 1,
- NOT_DRAW_SINGLE_LINES = 2
- };
- };
- CV_EXPORTS void drawLineMatches( const Mat& img1, const std::vector<KeyLine>& keylines1, const Mat& img2, const std::vector<KeyLine>& keylines2,
- const std::vector<DMatch>& matches1to2, Mat& outImg, const Scalar& matchColor = Scalar::all( -1 ),
- const Scalar& singleLineColor = Scalar::all( -1 ), const std::vector<char>& matchesMask = std::vector<char>(),
- int flags = DrawLinesMatchesFlags::DEFAULT );
- CV_EXPORTS void drawKeylines( const Mat& image, const std::vector<KeyLine>& keylines, Mat& outImage, const Scalar& color = Scalar::all( -1 ),
- int flags = DrawLinesMatchesFlags::DEFAULT );
- }
- }
- #endif
|