All Downloads are FREE. Search and download functionalities are using the official Maven repository.

boofcv.alg.bow.ClusterVisualWords Maven / Gradle / Ivy

Go to download

BoofCV is an open source Java library for real-time computer vision and robotics applications.

The newest version!
/*
 * Copyright (c) 2021, Peter Abeles. All Rights Reserved.
 *
 * This file is part of BoofCV (http://boofcv.org).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package boofcv.alg.bow;

import boofcv.struct.feature.TupleDesc_F64;
import org.ddogleg.clustering.AssignCluster;
import org.ddogleg.clustering.ComputeClusters;
import org.ddogleg.clustering.misc.ListAccessor;
import org.ddogleg.struct.LArrayAccessor;

import java.util.ArrayList;
import java.util.List;

/**
 * Finds clusters of {@link TupleDesc_F64} which can be used to identify frequent features, a.k.a words.
 * Internally it uses {@link org.ddogleg.clustering.ComputeClusters} and simply extracts the inner array
 * from the tuple.
 *
 * @author Peter Abeles
 */
// TODO make completely generic?  Not F64 specific?
public class ClusterVisualWords {

	// cluster finding algorithm
	ComputeClusters computeClusters;

	// inner arrays extracted from the input features
	List tuples = new ArrayList<>();
	LArrayAccessor accessor = new ListAccessor<>(tuples,
			( a, b ) -> System.arraycopy(a, 0, b, 0, a.length), double[].class);

	/**
	 * Constructor which configures the cluster finder.
	 *
	 * @param computeClusters Cluster finding algorithm
	 * @param randomSeed Seed for random number generator
	 */
	public ClusterVisualWords( ComputeClusters computeClusters, long randomSeed ) {
		this.computeClusters = computeClusters;
		computeClusters.initialize(randomSeed);
	}

	/**
	 * Add a feature to the list.
	 *
	 * @param feature image feature. Reference to inner array is saved.
	 */
	public void addReference( TupleDesc_F64 feature ) {
		tuples.add(feature.getData());
	}

	/**
	 * Clusters the list of features into the specified number of words
	 *
	 * @param numberOfWords Number of words/clusters it should find
	 */
	public void process( int numberOfWords ) {
		computeClusters.process(accessor, numberOfWords);
	}

	/**
	 * Returns a transform from point to cluster.
	 */
	public AssignCluster getAssignment() {
		return computeClusters.getAssignment();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy