bikecalibration.ROIDetection.java Source code

Java tutorial

Introduction

Here is the source code for bikecalibration.ROIDetection.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bikecalibration;

import java.util.ArrayList;
import java.util.List;
import org.opencv.core.DMatch;
import org.opencv.core.Mat;
import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.DescriptorMatcher;
import org.opencv.features2d.FeatureDetector;
import org.opencv.imgproc.Imgproc;

/**
 *
 * @author vasja
 */
public class ROIDetection {

    private List<Mat> ROIs;

    public ROIDetection() {
        ROIs = null;
    }

    public ROIDetection(int numberOfRois) {
        ROIs = new ArrayList(numberOfRois);
    }

    public ROIDetection(Mat[] rois) {
        setROIs(rois);
    }

    /**
     * Set the array of region of interest (ROI) images
     *
     * @param rois
     */
    public final void setROIs(Mat[] rois) {
        ROIs = new ArrayList(rois.length);
        for (int i = 0; i < rois.length; ++i) {
            Imgproc.cvtColor(rois[i], ROIs.get(i), Imgproc.COLOR_BGR2GRAY);
        }
    }

    /**
     * This function processes the image that contains the ROIs. It returns the
     * array of nodes found in the image.
     *
     * @param image
     * @return Array of nodes
     */
    public Node[] processImage(Mat image) {
        Node[] outputNodes = new Node[ROIs.size()];

        // convert the scene mat to gray scale
        Mat grayImage = new Mat();
        Imgproc.cvtColor(image, grayImage, Imgproc.COLOR_BGR2GRAY);

        // create a feature detector
        FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);

        List<MatOfKeyPoint> keypoints_objects = new ArrayList<>();
        MatOfKeyPoint keypoints_scene = new MatOfKeyPoint();

        detector.detect(ROIs, keypoints_objects);
        detector.detect(grayImage, keypoints_scene);

        // create a descriptor extractor
        DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);

        List<Mat> descriptor_objects = new ArrayList<>();
        Mat descriptor_scene = new Mat();

        extractor.compute(ROIs, keypoints_objects, descriptor_objects);
        extractor.compute(grayImage, keypoints_scene, descriptor_scene);

        // create a descriptor matcher
        DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
        List<MatOfDMatch> matches = new ArrayList<>();

        descriptor_objects.stream().map((descriptor_object) -> {
            MatOfDMatch match = new MatOfDMatch();
            matcher.match(descriptor_object, descriptor_scene, match);
            return match;
        }).forEach((match) -> {
            matches.add(match);
        });

        ArrayList<ArrayList<DMatch>> matchesList = new ArrayList<>();
        matches.stream().forEach((match) -> {
            matchesList.add((ArrayList<DMatch>) match.toList());
        });

        double max_dist = 100;
        double min_dist = 0;

        return null;
    }
}