今天研究了一下opencv中的FLANN库,踩坑一下午,遇到问题:OpenCV Error: Unsupported format or combination of formats (type=5)
在stackoverflow下也没人解决,所以特意记录下。
在库中使用Index类,完成knn的查找。其构造函数为:
Index(InputArray features, const IndexParams& params, cvflann::flann_distance_t distType=cvflann::FLANN_DIST_L2);
其参数有一些特殊要求:
1.对于params的取值为 AutotunedIndexParams、LinearIndexParams、KDTreeIndexParams时需要使用float型的features
2.当param为LshIndexParams时,features是uchar的Mat
原因是Index的构造函数其实调用了其build方法:
void Index::build(InputArray _data, const IndexParams& params, flann_distance_t _distType)
{
release();
algo = getParam
if( algo == FLANN_INDEX_SAVED )
{
load(_data, getParam
return;
}
Mat data = _data.getMat();
index = 0;
featureType = data.type();
distType = _distType;
if ( algo == FLANN_INDEX_LSH)
{
distType = FLANN_DIST_HAMMING;
}
switch( distType )
{
case FLANN_DIST_HAMMING:
buildIndex< HammingDistance >(index, data, params);
break;
case FLANN_DIST_L2:
buildIndex< ::cvflann::L2
break;
case FLANN_DIST_L1:
buildIndex< ::cvflann::L1
break;
#if MINIFLANN_SUPPORT_EXOTIC_DISTANCE_TYPES
case FLANN_DIST_MAX:
buildIndex< ::cvflann::MaxDistance
break;
case FLANN_DIST_HIST_INTERSECT:
buildIndex< ::cvflann::HistIntersectionDistance
break;
case FLANN_DIST_HELLINGER:
buildIndex< ::cvflann::HellingerDistance
break;
case FLANN_DIST_CHI_SQUARE:
buildIndex< ::cvflann::ChiSquareDistance
break;
case FLANN_DIST_KL:
buildIndex< ::cvflann::KL_Divergence
break;
#endif
default:
CV_Error(Error::StsBadArg, "Unknown/unsupported distance type");
}
}
从中可以看出,除FLANN_DIST_HAMMING外,其余数据类型都为float型
对于FLANN_DIST_HAMMING其中:
if ( algo == FLANN_INDEX_LSH)
{
distType = FLANN_DIST_HAMMING;
}
switch( distType )
{
case FLANN_DIST_HAMMING:
buildIndex< HammingDistance >(index, data, params);
break;
HammingDistance的定义为:typedef ::cvflann::Hamming
因此,使用LshIndexParams时,features的类型要为uchar。