在OpenImaj库中使用BagOfVisualWord类

时间:2013-03-11 15:32:11

标签: java generics openimaj

所以在我的情况下我应该向computeQuantisedFeatures方法提供两个参数,第二个是类型

        List<LocalFeature<Location, ? extends ArrayFeatureVector<byte[]>>>

我尝试传递

类型的imagekeypoints列表
    LocalFeatureList<Keypoint>

以及

    List<LocalFeature<KeypointLocation, ByteFV>> features = null;
    for (java.util.Iterator<Keypoint> iter = imageKeypoints.iterator(); iter.hasNext();)
    {features.add((LocalFeature<KeypointLocation, ByteFV>)iter.next());}

但我总是得到着名的错误

    The method computeQuantisedFeatures(HardAssigner<T,?,?>, List<LocalFeature<L,? 
    extends ArrayFeatureVector<T>>>) in the type BagOfVisualWords is not applicable for
    the arguments (HardAssigner<byte[],capture#3-of ?,capture#4-of ?>, 
    List<LocalFeature<KeypointLocation,ByteFV>>)

2 个答案:

答案 0 :(得分:1)

注意:我的答案基于Keypoint来源here

这个问题源于泛型aren't covariant。该方法需要List<LocalFeature<L, ? extends ArrayFeatureVector<T>>>,但您提供List<Keypoint>。如果该方法改为使用List<? extends LocalFeature<L, ? extends ArrayFeatureVector<T>>>,那将是合法的电话。

最简单的解决方案是将imagekeypoints的内容复制到新创建的List<LocalFeature<KeypointLocation, ByteFV>>中,然后将其传递给调用。

答案 1 :(得分:1)

正如Paul Bellora所指出的,这实际上是该方法的泛型中的一个错误。我刚刚提交了一个修复程序,很快就会在1.1.1-SNAPSHOT版本中提供。临时解决方案是在类中实现computeQuantisedFeatures方法的正确版本,如下所示:

public static <L extends Location, T> List<QuantisedLocalFeature<L>> computeQuantisedFeatures(
    HardAssigner<T, ?, ?> assigner,
    List<? extends LocalFeature<L, ? extends ArrayFeatureVector<T>>> features)
{
    final List<QuantisedLocalFeature<L>> out = new ArrayList<QuantisedLocalFeature<L>>(features.size());

    for (final LocalFeature<L, ? extends ArrayFeatureVector<T>> f : features) {
        final int idx = assigner.assign(f.getFeatureVector().values);
        out.add(new QuantisedLocalFeature<L>(f.getLocation(), idx));
    }

    return out;
}