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

com.flash3388.flashlib.vision.cv.analysis.TemplateMatchingAnalyser Maven / Gradle / Ivy

package com.flash3388.flashlib.vision.cv.analysis;

import com.beans.util.function.Suppliers;
import com.flash3388.flashlib.vision.VisionException;
import com.flash3388.flashlib.vision.analysis.Analyser;
import com.flash3388.flashlib.vision.analysis.Analysis;
import com.flash3388.flashlib.vision.cv.CvImage;
import com.flash3388.flashlib.vision.cv.template.ScaledTemplateMatchingResult;
import com.flash3388.flashlib.vision.cv.template.SingleTemplateMatcher;

import java.util.Optional;
import java.util.function.DoubleSupplier;
import java.util.function.Function;

public class TemplateMatchingAnalyser implements Analyser {

    private final SingleTemplateMatcher mTemplateMatcher;
    private final DoubleSupplier mScaleFactor;
    private final Function mAnalysisFactory;

    public TemplateMatchingAnalyser(SingleTemplateMatcher templateMatcher, DoubleSupplier scaleFactor,
                                    Function analysisFactory) {
        mTemplateMatcher = templateMatcher;
        mScaleFactor = scaleFactor;
        mAnalysisFactory = analysisFactory;
    }

    public TemplateMatchingAnalyser(SingleTemplateMatcher templateMatcher, DoubleSupplier scaleFactor) {
        this(templateMatcher, scaleFactor, new DefaultAnalysisFactory());
    }

    public TemplateMatchingAnalyser(SingleTemplateMatcher templateMatcher, double scaleFactor,
                                    Function analysisFactory) {
        this(templateMatcher, Suppliers.of(scaleFactor), analysisFactory);
    }

    public TemplateMatchingAnalyser(SingleTemplateMatcher templateMatcher, double scaleFactor) {
        this(templateMatcher, Suppliers.of(scaleFactor));
    }

    @Override
    public Optional analyse(T original, CvImage input) throws VisionException {
        ScaledTemplateMatchingResult templateMatchingResult = mTemplateMatcher.matchWithScaling(input.getMat(), mScaleFactor.getAsDouble());
        Analysis analysis = mAnalysisFactory.apply(templateMatchingResult);
        return Optional.of(analysis);
    }

    private static class DefaultAnalysisFactory implements Function {

        @Override
        public Analysis apply(ScaledTemplateMatchingResult scaledTemplateMatchingResult) {
            return Analysis.builder()
                    .buildTarget()
                        .put("center.x", scaledTemplateMatchingResult.getCenterPoint().x)
                        .put("center.y", scaledTemplateMatchingResult.getCenterPoint().y)
                        .put("score", scaledTemplateMatchingResult.getScore())
                        .put("scaleFactor", scaledTemplateMatchingResult.getScaleFactor())
                        .build()
                    .build();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy