boofcv.abst.scene.WrapFeatureToSceneRecognition Maven / Gradle / Ivy
/*
* 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.abst.scene;
import boofcv.abst.feature.detdesc.DetectDescribePoint;
import boofcv.misc.BoofLambdas;
import boofcv.struct.feature.TupleDesc;
import boofcv.struct.image.ImageBase;
import boofcv.struct.image.ImageType;
import georegression.struct.point.Point2D_F64;
import lombok.Getter;
import lombok.Setter;
import org.ddogleg.struct.DogArray;
import org.jetbrains.annotations.Nullable;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
/**
* Converts {@link FeatureSceneRecognition} into {@link SceneRecognition}.
*
* @author Peter Abeles
*/
@SuppressWarnings({"NullAway.Init"})
public class WrapFeatureToSceneRecognition, TD extends TupleDesc>
implements SceneRecognition {
/** Detects image features */
protected @Getter @Setter DetectDescribePoint detector;
// Used to ensure the image is at the expected scale
protected BoofLambdas.Transform downSample;
/** The {@link FeatureSceneRecognition} */
protected @Setter FeatureSceneRecognition recognizer;
/** Optional reference to a config. Useful when saving to disk */
public @Getter @Setter ConfigFeatureToSceneRecognition config;
// Used to plug in detected image features to the scene recognition algorithm
FeatureSceneRecognition.Features wrappedDetector = wrap();
public WrapFeatureToSceneRecognition( DetectDescribePoint detector,
BoofLambdas.Transform downSample,
FeatureSceneRecognition recognizer ) {
this.detector = detector;
this.downSample = downSample;
this.recognizer = recognizer;
}
/**
* Wrap the image iterator by adding detection to it
*/
@Override public void learnModel( Iterator images ) {
recognizer.learnModel(new Iterator<>() {
@Override public boolean hasNext() {return images.hasNext();}
@Override public FeatureSceneRecognition.Features next() {
detector.detect(images.next());
return wrappedDetector;
}
});
}
@Override public void clearDatabase() {
recognizer.clearDatabase();
}
@Override public void addImage( String id, Image image ) {
detector.detect(image);
recognizer.addImage(id, wrappedDetector);
}
@Override
public boolean query( Image queryImage, @Nullable BoofLambdas.Filter filter, int limit, DogArray matches ) {
detector.detect(queryImage);
return recognizer.query(wrappedDetector, filter, limit, matches);
}
@Override public List getImageIds( @Nullable List storage ) {
return recognizer.getImageIds(storage);
}
@Override public ImageType getImageType() {
return detector.getInputType();
}
@Override public void setVerbose( @Nullable PrintStream out, @Nullable Set options ) {
recognizer.setVerbose(out, options);
}
// @formatter:off
private FeatureSceneRecognition.Features wrap() {
return new FeatureSceneRecognition.Features<>() {
@Override public Point2D_F64 getPixel( int index ) {return detector.getLocation(index);}
@Override public TD getDescription( int index ) {return detector.getDescription(index);}
@Override public int size() {return detector.getNumberOfFeatures();}
};
}
// @formatter:on
public > T getRecognizer() {
return (T)recognizer;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy