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

boofcv.factory.tracker.FactoryTrackerObjectQuad Maven / Gradle / Ivy

Go to download

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

There is a newer version: 1.1.6
Show 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.factory.tracker;

import boofcv.abst.filter.derivative.ImageGradient;
import boofcv.abst.tracker.*;
import boofcv.alg.filter.derivative.GImageDerivativeOps;
import boofcv.alg.interpolate.InterpolatePixelS;
import boofcv.alg.tracker.circulant.CirculantTracker;
import boofcv.alg.tracker.meanshift.PixelLikelihood;
import boofcv.alg.tracker.meanshift.TrackerMeanShiftComaniciu2003;
import boofcv.alg.tracker.meanshift.TrackerMeanShiftLikelihood;
import boofcv.alg.tracker.sfot.ConfigSfot;
import boofcv.alg.tracker.sfot.SparseFlowObjectTracker;
import boofcv.alg.tracker.tld.TldTracker;
import boofcv.factory.filter.derivative.FactoryDerivative;
import boofcv.factory.interpolate.FactoryInterpolation;
import boofcv.struct.border.BorderType;
import boofcv.struct.image.ImageBase;
import boofcv.struct.image.ImageGray;
import boofcv.struct.image.ImageType;
import org.jetbrains.annotations.Nullable;

/**
 * Factory for implementations of {@link TrackerObjectQuad}, a high level interface for tracking user specified
 * objects inside video sequences. As usual, the high level interface makes it easier to use these algorithms
 * at the expensive of algorithm specific features.
 *
 * @author Peter Abeles
 */
public class FactoryTrackerObjectQuad {

	/**
	 * Create an instance of {@link TldTracker  Tracking-Learning-Detection (TLD)} tracker for the
	 * {@link TrackerObjectQuad} interface.
	 *
	 * @param config Configuration for the tracker
	 * @param  Image input type
	 * @param  Image derivative type
	 * @return TrackerObjectQuad
	 */
	public static , D extends ImageGray>
	TrackerObjectQuad tld( ConfigTrackerTld config, Class imageType ) {
		if (config == null)
			config = new ConfigTrackerTld();

		Class derivType = GImageDerivativeOps.getDerivativeType(imageType);

		InterpolatePixelS interpolate = FactoryInterpolation.bilinearPixelS(imageType, BorderType.EXTENDED);
		ImageGradient gradient = FactoryDerivative.sobel(imageType, derivType);

		TldTracker tracker = new TldTracker<>(config.parameters, interpolate, gradient, imageType, derivType);

		return new Tld_to_TrackerObjectQuad<>(tracker, imageType);
	}

	/**
	 * Create an instance of {@link SparseFlowObjectTracker  Sparse Flow Object Tracker} for the
	 * {@link TrackerObjectQuad} interface.
	 *
	 * @param config Configuration for the tracker,  Null for default.
	 * @param  Image input type
	 * @param  Image derivative type. Null for default.
	 * @return TrackerObjectQuad
	 */
	public static , D extends ImageGray>
	TrackerObjectQuad sparseFlow( @Nullable ConfigSfot config, Class imageType, @Nullable Class derivType ) {

		if (derivType == null)
			derivType = GImageDerivativeOps.getDerivativeType(imageType);

		if (config == null)
			config = new ConfigSfot();

		ImageGradient gradient = FactoryDerivative.sobel(imageType, derivType);

		SparseFlowObjectTracker tracker = new SparseFlowObjectTracker<>(config, imageType, derivType, gradient);

		return new Sfot_to_TrackObjectQuad<>(tracker, imageType);
	}

	/**
	 * Very basic and very fast implementation of mean-shift which uses a fixed sized rectangle for its region.
	 * Works best when the target is composed of a single color.
	 *
	 * @param maxIterations Maximum number of mean-shift iterations. Try 30.
	 * @param numBins Number of bins in the histogram color model. Try 5.
	 * @param maxPixelValue Maximum number of pixel values. For 8-bit images this will be 256
	 * @param modelType Type of color model used.
	 * @param imageType Type of image
	 * @return TrackerObjectQuad based on {@link TrackerMeanShiftLikelihood}.
	 * @see TrackerMeanShiftLikelihood
	 */
	public static >
	TrackerObjectQuad meanShiftLikelihood( int maxIterations,
											  int numBins,
											  double maxPixelValue,
											  MeanShiftLikelihoodType modelType,
											  ImageType imageType ) {
		PixelLikelihood likelihood = switch (modelType) {
			case HISTOGRAM -> FactoryTrackerObjectAlgs.likelihoodHistogramCoupled(maxPixelValue, numBins, imageType);
			case HISTOGRAM_INDEPENDENT_RGB_to_HSV -> {
				if (imageType.getNumBands() != 3)
					throw new IllegalArgumentException("Expected RGB image as input with 3-bands");
				yield FactoryTrackerObjectAlgs.likelihoodHueSatHistIndependent(maxPixelValue, numBins, (ImageType)imageType);
			}
			case HISTOGRAM_RGB_to_HSV -> {
				if (imageType.getNumBands() != 3)
					throw new IllegalArgumentException("Expected RGB image as input with 3-bands");
				yield FactoryTrackerObjectAlgs.likelihoodHueSatHistCoupled(maxPixelValue, numBins, (ImageType)imageType);
			}
			default -> throw new IllegalArgumentException("Unknown likelihood model " + modelType);
		};

		var alg = new TrackerMeanShiftLikelihood<>(likelihood, maxIterations, 0.1f);

		return new Msl_to_TrackerObjectQuad<>(alg, likelihood, imageType);
	}

	/**
	 * Implementation of mean-shift which matches the histogram and can handle targets composed of multiple colors.
	 * The tracker can also be configured to estimate gradual changes in scale. The track region is
	 * composed of a rotated rectangle.
	 *
	 * @param config Tracker configuration
	 * @param  Image type
	 * @return TrackerObjectQuad based on Comaniciu2003
	 * @see TrackerMeanShiftComaniciu2003
	 */
	public static >
	TrackerObjectQuad meanShiftComaniciu2003( ConfigComaniciu2003 config, ImageType imageType ) {

		TrackerMeanShiftComaniciu2003 alg = FactoryTrackerObjectAlgs.meanShiftComaniciu2003(config, imageType);

		return new Comaniciu2003_to_TrackerObjectQuad<>(alg, imageType);
	}

	/**
	 * Creates the Circulant feature tracker. Texture based tracker which uses the theory of circulant matrices,
	 * Discrete Fourier Transform (DCF), and linear classifiers to track a target. Fixed sized rectangular target
	 * and only estimates translation. Can't detect when it loses track or re-aquire track.
	 *
	 * @param config Configuration
	 * @return CirculantTracker
	 * @see CirculantTracker
	 */
	public static >
	TrackerObjectQuad circulant( ConfigCirculantTracker config, Class imageType ) {

		CirculantTracker alg = FactoryTrackerObjectAlgs.circulant(config, imageType);

		return new Circulant_to_TrackerObjectQuad<>(alg, ImageType.single(imageType));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy