opencv/modules/features2d/src/bagofwords.cpp

212 lines
6.5 KiB
C++
Raw Normal View History

2010-09-24 00:17:48 +08:00
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// Intel License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "precomp.hpp"
namespace cv
{
2014-02-23 18:59:06 +08:00
BOWTrainer::BOWTrainer() : size(0)
{}
BOWTrainer::~BOWTrainer()
{}
2010-09-30 22:21:22 +08:00
void BOWTrainer::add( const Mat& _descriptors )
{
CV_Assert( !_descriptors.empty() );
if( !descriptors.empty() )
{
CV_Assert( descriptors[0].cols == _descriptors.cols );
CV_Assert( descriptors[0].type() == _descriptors.type() );
size += _descriptors.rows;
}
else
{
size = _descriptors.rows;
}
descriptors.push_back(_descriptors);
}
const std::vector<Mat>& BOWTrainer::getDescriptors() const
{
return descriptors;
}
int BOWTrainer::descriptorsCount() const
{
return descriptors.empty() ? 0 : size;
}
2010-09-30 22:21:22 +08:00
void BOWTrainer::clear()
{
descriptors.clear();
}
2010-09-24 00:17:48 +08:00
BOWKMeansTrainer::BOWKMeansTrainer( int _clusterCount, const TermCriteria& _termcrit,
int _attempts, int _flags ) :
clusterCount(_clusterCount), termcrit(_termcrit), attempts(_attempts), flags(_flags)
{}
2010-09-30 22:21:22 +08:00
Mat BOWKMeansTrainer::cluster() const
2010-09-24 00:17:48 +08:00
{
2010-09-30 22:21:22 +08:00
CV_Assert( !descriptors.empty() );
int descCount = 0;
for( size_t i = 0; i < descriptors.size(); i++ )
descCount += descriptors[i].rows;
Mat mergedDescriptors( descCount, descriptors[0].cols, descriptors[0].type() );
for( size_t i = 0, start = 0; i < descriptors.size(); i++ )
{
2010-11-26 00:55:46 +08:00
Mat submut = mergedDescriptors.rowRange((int)start, (int)(start + descriptors[i].rows));
2010-09-30 22:21:22 +08:00
descriptors[i].copyTo(submut);
start += descriptors[i].rows;
}
return cluster( mergedDescriptors );
}
BOWKMeansTrainer::~BOWKMeansTrainer()
{}
2012-06-09 23:00:04 +08:00
Mat BOWKMeansTrainer::cluster( const Mat& _descriptors ) const
2010-09-30 22:21:22 +08:00
{
Mat labels, vocabulary;
2012-06-09 23:00:04 +08:00
kmeans( _descriptors, clusterCount, labels, termcrit, attempts, flags, vocabulary );
2010-09-30 22:21:22 +08:00
return vocabulary;
2010-09-24 00:17:48 +08:00
}
BOWImgDescriptorExtractor::BOWImgDescriptorExtractor( const Ptr<DescriptorExtractor>& _dextractor,
const Ptr<DescriptorMatcher>& _dmatcher ) :
dextractor(_dextractor), dmatcher(_dmatcher)
{}
2013-09-05 12:05:54 +08:00
BOWImgDescriptorExtractor::BOWImgDescriptorExtractor( const Ptr<DescriptorMatcher>& _dmatcher ) :
dmatcher(_dmatcher)
{}
BOWImgDescriptorExtractor::~BOWImgDescriptorExtractor()
{}
2010-09-30 22:21:22 +08:00
void BOWImgDescriptorExtractor::setVocabulary( const Mat& _vocabulary )
2010-09-24 00:17:48 +08:00
{
dmatcher->clear();
vocabulary = _vocabulary;
dmatcher->add( std::vector<Mat>(1, vocabulary) );
2010-09-24 00:17:48 +08:00
}
const Mat& BOWImgDescriptorExtractor::getVocabulary() const
{
return vocabulary;
}
2014-02-05 17:51:14 +08:00
void BOWImgDescriptorExtractor::compute( InputArray image, std::vector<KeyPoint>& keypoints, OutputArray imgDescriptor,
std::vector<std::vector<int> >* pointIdxsOfClusters, Mat* descriptors )
2010-09-24 00:17:48 +08:00
{
2010-09-30 22:21:22 +08:00
imgDescriptor.release();
if( keypoints.empty() )
return;
2010-09-24 00:17:48 +08:00
// Compute descriptors for the image.
2013-11-27 18:36:34 +08:00
Mat _descriptors;
dextractor->compute( image, keypoints, _descriptors );
2010-09-24 00:17:48 +08:00
2013-11-27 18:36:34 +08:00
compute( _descriptors, imgDescriptor, pointIdxsOfClusters );
2013-11-24 22:38:48 +08:00
// Add the descriptors of image keypoints
if (descriptors) {
2013-11-27 18:36:34 +08:00
*descriptors = _descriptors.clone();
}
2010-09-24 00:17:48 +08:00
}
int BOWImgDescriptorExtractor::descriptorSize() const
{
return vocabulary.empty() ? 0 : vocabulary.rows;
}
int BOWImgDescriptorExtractor::descriptorType() const
{
return CV_32FC1;
}
2014-02-05 17:51:14 +08:00
void BOWImgDescriptorExtractor::compute( InputArray keypointDescriptors, OutputArray _imgDescriptor, std::vector<std::vector<int> >* pointIdxsOfClusters )
{
CV_Assert( !vocabulary.empty() );
int clusterCount = descriptorSize(); // = vocabulary.rows
2010-09-24 00:17:48 +08:00
// Match keypoint descriptors to cluster center (to vocabulary)
std::vector<DMatch> matches;
dmatcher->match( keypointDescriptors, matches );
2010-09-24 00:17:48 +08:00
// Compute image descriptor
2010-09-30 22:21:22 +08:00
if( pointIdxsOfClusters )
{
pointIdxsOfClusters->clear();
pointIdxsOfClusters->resize(clusterCount);
}
2014-02-05 13:59:04 +08:00
_imgDescriptor.create(1, clusterCount, descriptorType());
_imgDescriptor.setTo(Scalar::all(0));
2014-02-04 20:34:18 +08:00
Mat imgDescriptor = _imgDescriptor.getMat();
float *dptr = imgDescriptor.ptr<float>();
2010-09-24 00:17:48 +08:00
for( size_t i = 0; i < matches.size(); i++ )
{
int queryIdx = matches[i].queryIdx;
int trainIdx = matches[i].trainIdx; // cluster index
2010-09-24 00:17:48 +08:00
CV_Assert( queryIdx == (int)i );
dptr[trainIdx] = dptr[trainIdx] + 1.f;
2010-09-30 22:21:22 +08:00
if( pointIdxsOfClusters )
(*pointIdxsOfClusters)[trainIdx].push_back( queryIdx );
2010-09-24 00:17:48 +08:00
}
// Normalize image descriptor.
2014-02-04 20:34:18 +08:00
imgDescriptor /= keypointDescriptors.size().height;
}
2010-09-24 00:17:48 +08:00
}