Example usage for org.opencv.features2d Features2d drawKeypoints

List of usage examples for org.opencv.features2d Features2d drawKeypoints

Introduction

In this page you can find the example usage for org.opencv.features2d Features2d drawKeypoints.

Prototype

public static void drawKeypoints(Mat image, MatOfKeyPoint keypoints, Mat outImage) 

Source Link

Usage

From source file:com.seleniumtests.util.imaging.ImageDetector.java

License:Apache License

/**
 * Compute the rectangle where the searched picture is and the rotation angle between both images
 * Throw {@link ImageSearchException} if picture is not found
 * @return/*from  w w  w .  java  2 s. c om*/
 * @Deprecated Kept here for information, but open CV 3 does not include SURF anymore for java build
 */
public void detectCorrespondingZone() {
    Mat objectImageMat = Imgcodecs.imread(objectImage.getAbsolutePath(), Imgcodecs.CV_LOAD_IMAGE_COLOR);
    Mat sceneImageMat = Imgcodecs.imread(sceneImage.getAbsolutePath(), Imgcodecs.CV_LOAD_IMAGE_COLOR);
    FeatureDetector surf = FeatureDetector.create(FeatureDetector.SURF);

    MatOfKeyPoint objectKeyPoints = new MatOfKeyPoint();
    MatOfKeyPoint sceneKeyPoints = new MatOfKeyPoint();

    surf.detect(objectImageMat, objectKeyPoints);
    surf.detect(sceneImageMat, sceneKeyPoints);

    DescriptorExtractor surfExtractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
    Mat objectDescriptor = new Mat();
    Mat sceneDescriptor = new Mat();
    surfExtractor.compute(objectImageMat, objectKeyPoints, objectDescriptor);
    surfExtractor.compute(sceneImageMat, sceneKeyPoints, sceneDescriptor);

    try {
        Mat outImage = new Mat();
        Features2d.drawKeypoints(objectImageMat, objectKeyPoints, outImage);
        String tempFile = File.createTempFile("img", ".png").getAbsolutePath();
        writeComparisonPictureToFile(tempFile, outImage);
    } catch (IOException e) {

    }

    // http://stackoverflow.com/questions/29828849/flann-for-opencv-java
    DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
    MatOfDMatch matches = new MatOfDMatch();

    if (objectKeyPoints.toList().isEmpty()) {
        throw new ImageSearchException("No keypoints in object to search, check it's not uniformly coloured: "
                + objectImage.getAbsolutePath());
    }
    if (sceneKeyPoints.toList().isEmpty()) {
        throw new ImageSearchException(
                "No keypoints in scene, check it's not uniformly coloured: " + sceneImage.getAbsolutePath());
    }
    if (objectDescriptor.type() != CvType.CV_32F) {
        objectDescriptor.convertTo(objectDescriptor, CvType.CV_32F);
    }
    if (sceneDescriptor.type() != CvType.CV_32F) {
        sceneDescriptor.convertTo(sceneDescriptor, CvType.CV_32F);
    }

    matcher.match(objectDescriptor, sceneDescriptor, matches);

    double maxDist = 0;
    double minDist = 10000;

    for (int i = 0; i < objectDescriptor.rows(); i++) {
        double dist = matches.toList().get(i).distance;
        if (dist < minDist) {
            minDist = dist;
        }
        if (dist > maxDist) {
            maxDist = dist;
        }
    }

    logger.debug("-- Max dist : " + maxDist);
    logger.debug("-- Min dist : " + minDist);

    LinkedList<DMatch> goodMatches = new LinkedList<>();
    MatOfDMatch gm = new MatOfDMatch();

    for (int i = 0; i < objectDescriptor.rows(); i++) {
        if (matches.toList().get(i).distance < detectionThreshold) {
            goodMatches.addLast(matches.toList().get(i));
        }
    }
    gm.fromList(goodMatches);

    Features2d.drawMatches(objectImageMat, objectKeyPoints, sceneImageMat, sceneKeyPoints, gm, imgMatch,
            Scalar.all(-1), Scalar.all(-1), new MatOfByte(), Features2d.NOT_DRAW_SINGLE_POINTS);

    if (goodMatches.isEmpty()) {
        throw new ImageSearchException("Cannot find matching zone");
    }

    LinkedList<Point> objList = new LinkedList<>();
    LinkedList<Point> sceneList = new LinkedList<>();

    List<KeyPoint> objectKeyPointsList = objectKeyPoints.toList();
    List<KeyPoint> sceneKeyPointsList = sceneKeyPoints.toList();

    for (int i = 0; i < goodMatches.size(); i++) {
        objList.addLast(objectKeyPointsList.get(goodMatches.get(i).queryIdx).pt);
        sceneList.addLast(sceneKeyPointsList.get(goodMatches.get(i).trainIdx).pt);
    }

    MatOfPoint2f obj = new MatOfPoint2f();
    obj.fromList(objList);

    MatOfPoint2f scene = new MatOfPoint2f();
    scene.fromList(sceneList);

    // Calib3d.RANSAC could be used instead of 0
    Mat hg = Calib3d.findHomography(obj, scene, 0, 5);

    Mat objectCorners = new Mat(4, 1, CvType.CV_32FC2);
    Mat sceneCorners = new Mat(4, 1, CvType.CV_32FC2);

    objectCorners.put(0, 0, new double[] { 0, 0 });
    objectCorners.put(1, 0, new double[] { objectImageMat.cols(), 0 });
    objectCorners.put(2, 0, new double[] { objectImageMat.cols(), objectImageMat.rows() });
    objectCorners.put(3, 0, new double[] { 0, objectImageMat.rows() });

    Core.perspectiveTransform(objectCorners, sceneCorners, hg);

    // points of object
    Point po1 = new Point(objectCorners.get(0, 0));
    Point po2 = new Point(objectCorners.get(1, 0));
    Point po3 = new Point(objectCorners.get(2, 0));
    Point po4 = new Point(objectCorners.get(3, 0));

    // point of object in scene
    Point p1 = new Point(sceneCorners.get(0, 0)); // top left
    Point p2 = new Point(sceneCorners.get(1, 0)); // top right
    Point p3 = new Point(sceneCorners.get(2, 0)); // bottom right
    Point p4 = new Point(sceneCorners.get(3, 0)); // bottom left

    logger.debug(po1);
    logger.debug(po2);
    logger.debug(po3);
    logger.debug(po4);
    logger.debug(p1); // top left
    logger.debug(p2); // top right
    logger.debug(p3); // bottom right
    logger.debug(p4); // bottom left

    if (debug) {
        try {
            // translate corners
            p1.set(new double[] { p1.x + objectImageMat.cols(), p1.y });
            p2.set(new double[] { p2.x + objectImageMat.cols(), p2.y });
            p3.set(new double[] { p3.x + objectImageMat.cols(), p3.y });
            p4.set(new double[] { p4.x + objectImageMat.cols(), p4.y });

            Imgproc.line(imgMatch, p1, p2, new Scalar(0, 255, 0), 1);
            Imgproc.line(imgMatch, p2, p3, new Scalar(0, 255, 0), 1);
            Imgproc.line(imgMatch, p3, p4, new Scalar(0, 255, 0), 1);
            Imgproc.line(imgMatch, p4, p1, new Scalar(0, 255, 0), 1);

            showResultingPicture(imgMatch);
        } catch (IOException e) {
        }
    }

    // check rotation angles
    checkRotationAngle(p1, p2, p3, p4, po1, po2, po3, po4);

    // rework on scene points as new, we are sure the object rotation is 0, 90, 180 or 270
    reworkOnScenePoints(p1, p2, p3, p4);

    // check that aspect ratio of the detected height and width are the same
    checkDetectionZoneAspectRatio(p1, p2, p4, po1, po2, po4);

    recordDetectedRectangle(p1, p2, p3, p4);
}

From source file:eu.fpetersen.robobrain.behavior.followobject.OrbObjectDetector.java

License:Open Source License

public void process(Mat image) {
    Mat tempImage = new Mat();
    Imgproc.cvtColor(image, tempImage, Imgproc.COLOR_RGBA2RGB);
    MatOfKeyPoint keypoints = detectInImage(tempImage);
    Mat descriptors = extractDescriptors(keypoints, tempImage);
    MatOfDMatch matches = new MatOfDMatch();
    matcher.match(descriptors, originalDescriptors, matches);

    KeyPoint[] keypointArray = keypoints.toArray();
    KeyPoint[] originalKeypointArray = originalKeypoints.toArray();

    float min = 40.0f;
    float max = 1000.0f;
    for (DMatch match : matches.toList()) {
        if (match.distance < min) {
            min = match.distance;//from   w  w w .  ja  va 2 s  . co m
        } else if (match.distance > max) {
            max = match.distance;
        }
    }

    float threshold = 1.5f * min;
    List<KeyPoint> matchedKeyPoints = new ArrayList<KeyPoint>();
    List<Point> matchedPoints = new ArrayList<Point>();
    List<Point> matchedOriginalPoints = new ArrayList<Point>();
    for (DMatch match : matches.toList()) {
        if (match.distance < threshold) {
            KeyPoint matchedKeypoint = keypointArray[match.queryIdx];
            matchedKeyPoints.add(matchedKeypoint);
            matchedPoints.add(matchedKeypoint.pt);

            KeyPoint matchedOriginalKeypoint = originalKeypointArray[match.trainIdx];
            matchedOriginalPoints.add(matchedOriginalKeypoint.pt);
        }
    }

    if (matchedKeyPoints.size() > 10) {

        Mat H = Calib3d.findHomography(
                new MatOfPoint2f(matchedOriginalPoints.toArray(new Point[matchedOriginalPoints.size()])),
                new MatOfPoint2f(matchedPoints.toArray(new Point[matchedPoints.size()])), Calib3d.RANSAC, 10);

        List<Point> originalCorners = new ArrayList<Point>();
        originalCorners.add(new Point(0, 0));
        originalCorners.add(new Point(originalImage.cols(), 0));
        originalCorners.add(new Point(originalImage.cols(), originalImage.rows()));
        originalCorners.add(new Point(0, originalImage.rows()));

        List<Point> corners = new ArrayList<Point>();
        for (int i = 0; i < 4; i++) {
            corners.add(new Point(0, 0));
        }
        Mat objectCorners = Converters.vector_Point2f_to_Mat(corners);

        Core.perspectiveTransform(Converters.vector_Point2f_to_Mat(originalCorners), objectCorners, H);
        corners.clear();
        Converters.Mat_to_vector_Point2f(objectCorners, corners);

        Core.line(tempImage, corners.get(0), corners.get(1), new Scalar(0, 255, 0), 4);
        Core.line(tempImage, corners.get(1), corners.get(2), new Scalar(0, 255, 0), 4);
        Core.line(tempImage, corners.get(2), corners.get(3), new Scalar(0, 255, 0), 4);
        Core.line(tempImage, corners.get(3), corners.get(0), new Scalar(0, 255, 0), 4);
    }

    Features2d.drawKeypoints(tempImage,
            new MatOfKeyPoint(matchedKeyPoints.toArray(new KeyPoint[matchedKeyPoints.size()])), tempImage);
    Imgproc.cvtColor(tempImage, image, Imgproc.COLOR_RGB2RGBA);

}