Example usage for org.opencv.core MatOfKeyPoint toList

List of usage examples for org.opencv.core MatOfKeyPoint toList

Introduction

In this page you can find the example usage for org.opencv.core MatOfKeyPoint toList.

Prototype

public List<KeyPoint> toList() 

Source Link

Usage

From source file:OctoEye.java

License:Open Source License

private void detectPupil() {
    // min and max pupil radius
    int r_min = 2;
    int r_max = 45;

    // min and max pupil diameter
    int d_min = 2 * r_min;
    int d_max = 2 * r_max;

    // min and max pupil area
    double area;/*from  w  ww . j a  va2 s  .  com*/
    double a_min = Math.PI * r_min * r_min;
    double a_max = Math.PI * r_max * r_max;

    // histogram stuff
    List<Mat> images;
    MatOfInt channels;
    Mat mask;
    Mat hist;
    MatOfInt mHistSize;
    MatOfFloat mRanges;

    // contour and circle stuff
    Rect rect = null;
    Rect rectMin;
    Rect rectMax;
    List<MatOfPoint> contours;
    MatOfPoint3 circles;

    // pupil center
    Point p;

    // ellipse test points
    Point v;
    Point r;
    Point s;

    // rect points
    Point tl;
    Point br;

    // pupil edge detection
    Vector<Point> pointsTest;
    Vector<Point> pointsEllipse;
    Vector<Point> pointsRemoved;

    // temporary variables
    double distance;
    double rad;
    double length;
    int x;
    int y;
    int tmp;
    byte buff[];

    // -------------------------------------------------------------------------------------------------------------
    // step 1
    // blur the image to reduce noise

    Imgproc.medianBlur(src, tmp1, 25);

    // -------------------------------------------------------------------------------------------------------------
    // step 2
    // locate the pupil with feature detection and compute a histogram for each,
    // the best feature will be used as rough pupil location (rectMin)

    int score = 0;
    int winner = 0;

    // feature detection
    MatOfKeyPoint matOfKeyPoints = new MatOfKeyPoint();
    FeatureDetector blobDetector = FeatureDetector.create(FeatureDetector.MSER); // Maximal Stable Extremal Regions
    blobDetector.detect(tmp1, matOfKeyPoints);
    List<KeyPoint> keyPoints = matOfKeyPoints.toList();

    // histogram calculation
    for (int i = 0; i < keyPoints.size(); i++) {
        x = (int) keyPoints.get(i).pt.x;
        y = (int) keyPoints.get(i).pt.y;
        tl = new Point(x - 5 >= 0 ? x - 5 : 0, y - 5 >= 0 ? y - 5 : 0);
        br = new Point(x + 5 < WIDTH ? x + 5 : WIDTH - 1, y + 5 < HEIGHT ? y + 5 : HEIGHT - 1);

        images = new ArrayList<Mat>();
        images.add(tmp1.submat(new Rect(tl, br)));
        channels = new MatOfInt(0);
        mask = new Mat();
        hist = new Mat();
        mHistSize = new MatOfInt(256);
        mRanges = new MatOfFloat(0f, 256f);
        Imgproc.calcHist(images, channels, mask, hist, mHistSize, mRanges);

        tmp = 0;
        for (int j = 0; j < 256 / 3; j++) {
            tmp += (256 / 3 - j) * (int) hist.get(j, 0)[0];
        }
        if (tmp >= score) {
            score = tmp;
            winner = i;
            rect = new Rect(tl, br);
        }

        if (debug) {
            // show features (orange)
            Core.circle(dbg, new Point(x, y), 3, ORANGE);
        }
    }
    if (rect == null) {
        return;
    }
    rectMin = rect.clone();

    if (debug) {
        // show rectMin (red)
        Core.rectangle(dbg, rectMin.tl(), rect.br(), RED, 1);
    }

    // -------------------------------------------------------------------------------------------------------------
    // step 3
    // compute a rectMax (blue) which is larger than the pupil

    int margin = 32;

    rect.x = rect.x - margin;
    rect.y = rect.y - margin;
    rect.width = rect.width + 2 * margin;
    rect.height = rect.height + 2 * margin;

    rectMax = rect.clone();

    if (debug) {
        // show features (orange)
        Core.rectangle(dbg, rectMax.tl(), rectMax.br(), BLUE);
    }

    // -------------------------------------------------------------------------------------------------------------
    // step 4
    // blur the image again

    Imgproc.medianBlur(src, tmp1, 7);
    Imgproc.medianBlur(tmp1, tmp1, 3);
    Imgproc.medianBlur(tmp1, tmp1, 3);
    Imgproc.medianBlur(tmp1, tmp1, 3);

    // -------------------------------------------------------------------------------------------------------------
    // step 5
    // detect edges

    Imgproc.Canny(tmp1, tmp2, 40, 50);

    // -------------------------------------------------------------------------------------------------------------
    // step 6
    // from pupil center to maxRect borders, find all edge points, compute a first ellipse

    p = new Point(rectMin.x + rectMin.width / 2, rectMin.y + rectMin.height / 2);
    pointsTest = new Vector<Point>();
    pointsEllipse = new Vector<Point>();
    pointsRemoved = new Vector<Point>();
    buff = new byte[tmp2.rows() * tmp2.cols()];
    tmp2.get(0, 0, buff);

    length = Math.min(p.x - rectMax.x - 3, p.y - rectMax.y - 3);
    length = Math.sqrt(2 * Math.pow(length, 2));
    Point z = new Point(p.x, p.y - length);
    for (int i = 0; i < 360; i += 15) {
        rad = Math.toRadians(i);
        x = (int) (p.x + Math.cos(rad) * (z.x - p.x) - Math.sin(rad) * (z.y - p.y));
        y = (int) (p.y + Math.sin(rad) * (z.x - p.x) - Math.cos(rad) * (z.y - p.y));
        pointsTest.add(new Point(x, y));
    }

    if (debug) {
        for (int i = 0; i < pointsTest.size(); i++) {
            Core.line(dbg, p, pointsTest.get(i), GRAY, 1);
            Core.rectangle(dbg, rectMin.tl(), rectMin.br(), GREEN, 1);
            Core.rectangle(dbg, rectMax.tl(), rectMax.br(), BLUE, 1);
        }
        Core.rectangle(dbg, rectMin.tl(), rectMin.br(), BLACK, -1);
        Core.rectangle(dbg, rectMin.tl(), rectMin.br(), RED, 1);
        Core.rectangle(dbg, rectMax.tl(), rectMax.br(), BLUE);
    }

    // p: Ursprung ("Mittelpunkt" der Ellipse)
    // v: Zielpunkt (Testpunkt)
    // r: Richtungsvektor PV
    for (int i = 0; i < pointsTest.size(); i++) {
        v = new Point(pointsTest.get(i).x, pointsTest.get(i).y);
        r = new Point(v.x - p.x, v.y - p.y);
        length = Math.sqrt(Math.pow(p.x - v.x, 2) + Math.pow(p.y - v.y, 2));
        boolean found = false;
        for (int j = 0; j < Math.round(length); j++) {
            s = new Point(Math.rint(p.x + (double) j / length * r.x),
                    Math.rint(p.y + (double) j / length * r.y));
            s.x = Math.max(1, Math.min(s.x, WIDTH - 2));
            s.y = Math.max(1, Math.min(s.y, HEIGHT - 2));
            tl = new Point(s.x - 1, s.y - 1);
            br = new Point(s.x + 1, s.y + 1);
            buff = new byte[3 * 3];
            rect = new Rect(tl, br);
            try {
                (tmp2.submat(rect)).get(0, 0, buff);
                for (int k = 0; k < 3 * 3; k++) {
                    if (Math.abs(buff[k]) == 1) {
                        pointsEllipse.add(s);
                        found = true;
                        break;
                    }
                }
            } catch (Exception e) {
                break;
            }
            if (found) {
                break;
            }
        }
    }

    double e_min = Double.POSITIVE_INFINITY;
    double e_max = 0;
    double e_med = 0;
    for (int i = 0; i < pointsEllipse.size(); i++) {
        v = pointsEllipse.get(i);
        length = Math.sqrt(Math.pow(p.x - v.x, 2) + Math.pow(p.y - v.y, 2));
        e_min = (length < e_min) ? length : e_min;
        e_max = (length > e_max) ? length : e_max;
        e_med = e_med + length;
    }
    e_med = e_med / pointsEllipse.size();
    if (pointsEllipse.size() >= 5) {
        Point[] points1 = new Point[pointsEllipse.size()];
        for (int i = 0; i < pointsEllipse.size(); i++) {
            points1[i] = pointsEllipse.get(i);
        }
        MatOfPoint2f points2 = new MatOfPoint2f();
        points2.fromArray(points1);
        pupil = Imgproc.fitEllipse(points2);
    }
    if (pupil.center.x == 0 && pupil.center.y == 0) {
        // something went wrong, return null
        reset();
        return;
    }

    if (debug) {
        Core.ellipse(dbg, pupil, PURPLE, 2);
    }

    // -------------------------------------------------------------------------------------------------------------
    // step 7
    // remove some outlier points and compute the ellipse again

    try {
        for (int i = 1; i <= 4; i++) {
            distance = 0;
            int remove = 0;
            for (int j = pointsEllipse.size() - 1; j >= 0; j--) {
                v = pointsEllipse.get(j);
                length = Math.sqrt(Math.pow(v.x - pupil.center.x, 2) + Math.pow(v.y - pupil.center.y, 2));
                if (length > distance) {
                    distance = length;
                    remove = j;
                }
            }
            v = pointsEllipse.get(remove);
            pointsEllipse.removeElementAt(remove);
            pointsRemoved.add(v);
        }
    } catch (Exception e) {
        // something went wrong, return null
        reset();
        return;
    }
    if (pointsEllipse.size() >= 5) {
        Point[] points1 = new Point[pointsEllipse.size()];
        for (int i = 0; i < pointsEllipse.size(); i++) {
            points1[i] = pointsEllipse.get(i);
        }
        MatOfPoint2f points2 = new MatOfPoint2f();
        points2.fromArray(points1);
        pupil = Imgproc.fitEllipse(points2);

        Point[] vertices = new Point[4];
        pupil.points(vertices);
        double d1 = Math
                .sqrt(Math.pow(vertices[1].x - vertices[0].x, 2) + Math.pow(vertices[1].y - vertices[0].y, 2));
        double d2 = Math
                .sqrt(Math.pow(vertices[2].x - vertices[1].x, 2) + Math.pow(vertices[2].y - vertices[1].y, 2));

        if (d1 >= d2) {
            pupilMajorAxis = (int) (d1 / 2);
            pupilMinorAxis = (int) (d2 / 2);
            axisA = new Point(vertices[1].x + (vertices[2].x - vertices[1].x) / 2,
                    vertices[1].y + (vertices[2].y - vertices[1].y) / 2);
            axisB = new Point(vertices[0].x + (vertices[1].x - vertices[0].x) / 2,
                    vertices[0].y + (vertices[1].y - vertices[0].y) / 2);
        } else {
            pupilMajorAxis = (int) (d2 / 2);
            pupilMinorAxis = (int) (d1 / 2);
            axisB = new Point(vertices[1].x + (vertices[2].x - vertices[1].x) / 2,
                    vertices[1].y + (vertices[2].y - vertices[1].y) / 2);
            axisA = new Point(vertices[0].x + (vertices[1].x - vertices[0].x) / 2,
                    vertices[0].y + (vertices[1].y - vertices[0].y) / 2);
        }
    }

    double ratio = (double) pupilMinorAxis / (double) pupilMajorAxis;
    if (ratio < 0.75 || 2 * pupilMinorAxis <= d_min || 2 * pupilMajorAxis >= d_max) {
        // something went wrong, return null
        reset();
        return;
    }

    // pupil found
    if (debug) {
        Core.ellipse(dbg, pupil, GREEN, 2);
        Core.line(dbg, pupil.center, axisA, RED, 2);
        Core.line(dbg, pupil.center, axisB, BLUE, 2);
        Core.circle(dbg, pupil.center, 1, GREEN, 0);

        x = 5;
        y = 5;
        Core.rectangle(dbg, new Point(x, y), new Point(x + 80 + 4, y + 10), BLACK, -1);
        Core.rectangle(dbg, new Point(x + 2, y + 2), new Point(x + 2 + pupilMajorAxis, y + 4), RED, -1);
        Core.rectangle(dbg, new Point(x + 2, y + 6), new Point(x + 2 + pupilMinorAxis, y + 8), BLUE, -1);

        for (int i = pointsEllipse.size() - 1; i >= 0; i--) {
            Core.circle(dbg, pointsEllipse.get(i), 2, ORANGE, -1);
        }
        for (int i = pointsRemoved.size() - 1; i >= 0; i--) {
            Core.circle(dbg, pointsRemoved.get(i), 2, PURPLE, -1);
        }
    }
    Core.ellipse(dst, pupil, GREEN, 2);
    Core.circle(dst, pupil.center, 1, GREEN, 0);
}

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// w ww .  j av  a2  s .com
 * @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:net.semanticmetadata.lire.imageanalysis.features.local.opencvfeatures.CvSiftExtractor.java

License:Open Source License

@Override
public void extract(BufferedImage img) {
    MatOfKeyPoint keypoints = new MatOfKeyPoint();
    Mat descriptors = new Mat();
    List<KeyPoint> myKeys;//  w  ww  .j a v a 2 s. c  om
    //        Mat img_object = Highgui.imread(image, 0); //0 = CV_LOAD_IMAGE_GRAYSCALE
    //        detector.detect(img_object, keypoints);
    byte[] data = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
    Mat matRGB = new Mat(img.getHeight(), img.getWidth(), CvType.CV_8UC3);
    matRGB.put(0, 0, data);
    Mat matGray = new Mat(img.getHeight(), img.getWidth(), CvType.CV_8UC1);
    Imgproc.cvtColor(matRGB, matGray, Imgproc.COLOR_BGR2GRAY); //TODO: RGB or BGR?
    byte[] dataGray = new byte[matGray.rows() * matGray.cols() * (int) (matGray.elemSize())];
    matGray.get(0, 0, dataGray);

    detector.detect(matGray, keypoints);
    extractor.compute(matGray, keypoints, descriptors);
    myKeys = keypoints.toList();

    features = new LinkedList<CvSiftFeature>();
    KeyPoint key;
    CvSiftFeature feat;
    double[] desc;
    int cols, rows = myKeys.size();
    for (int i = 0; i < rows; i++) {
        cols = (descriptors.row(i)).cols();
        desc = new double[cols];
        key = myKeys.get(i);
        for (int j = 0; j < cols; j++) {
            desc[j] = descriptors.get(i, j)[0];
        }
        feat = new CvSiftFeature(key.pt.x, key.pt.y, key.size, desc);
        features.add(feat);
    }
}

From source file:net.semanticmetadata.lire.imageanalysis.features.local.opencvfeatures.CvSiftExtractor.java

License:Open Source License

public LinkedList<CvSiftFeature> computeSiftKeypoints(BufferedImage img) {
    MatOfKeyPoint keypoints = new MatOfKeyPoint();
    List<KeyPoint> myKeys;//from  w w w  .  j  av a2  s .c  o m
    //        Mat img_object = Highgui.imread(image, 0); //0 = CV_LOAD_IMAGE_GRAYSCALE
    //        detector.detect(img_object, keypoints);
    byte[] data = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
    Mat matRGB = new Mat(img.getHeight(), img.getWidth(), CvType.CV_8UC3);
    matRGB.put(0, 0, data);
    Mat matGray = new Mat(img.getHeight(), img.getWidth(), CvType.CV_8UC1);
    Imgproc.cvtColor(matRGB, matGray, Imgproc.COLOR_BGR2GRAY); //TODO: RGB or BGR?
    byte[] dataGray = new byte[matGray.rows() * matGray.cols() * (int) (matGray.elemSize())];
    matGray.get(0, 0, dataGray);

    detector.detect(matGray, keypoints);
    myKeys = keypoints.toList();

    LinkedList<CvSiftFeature> myKeypoints = new LinkedList<CvSiftFeature>();
    KeyPoint key;
    CvSiftFeature feat;
    for (Iterator<KeyPoint> iterator = myKeys.iterator(); iterator.hasNext();) {
        key = iterator.next();
        feat = new CvSiftFeature(key.pt.x, key.pt.y, key.size, null);
        myKeypoints.add(feat);
    }

    return myKeypoints;
}

From source file:net.semanticmetadata.lire.imageanalysis.features.local.opencvfeatures.CvSurfExtractor.java

License:Open Source License

@Override
public void extract(BufferedImage img) {
    MatOfKeyPoint keypoints = new MatOfKeyPoint();
    Mat descriptors = new Mat();
    List<KeyPoint> myKeys;//from   w ww . jav  a2s . co m
    //        Mat img_object = Highgui.imread(image, 0); //0 = CV_LOAD_IMAGE_GRAYSCALE
    //        detector.detect(img_object, keypoints);
    byte[] data = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
    Mat matRGB = new Mat(img.getHeight(), img.getWidth(), CvType.CV_8UC3);
    matRGB.put(0, 0, data);
    Mat matGray = new Mat(img.getHeight(), img.getWidth(), CvType.CV_8UC1);
    Imgproc.cvtColor(matRGB, matGray, Imgproc.COLOR_BGR2GRAY); //TODO: RGB or BGR?
    byte[] dataGray = new byte[matGray.rows() * matGray.cols() * (int) (matGray.elemSize())];
    matGray.get(0, 0, dataGray);

    detector.detect(matGray, keypoints);
    extractor.compute(matGray, keypoints, descriptors);
    myKeys = keypoints.toList();

    features = new LinkedList<CvSurfFeature>();
    KeyPoint key;
    CvSurfFeature feat;
    double[] desc;
    int cols, rows = myKeys.size();
    for (int i = 0; i < rows; i++) {
        cols = (descriptors.row(i)).cols();
        desc = new double[cols];
        key = myKeys.get(i);
        for (int j = 0; j < cols; j++) {
            desc[j] = descriptors.get(i, j)[0];
        }
        feat = new CvSurfFeature(key.pt.x, key.pt.y, key.size, desc);
        features.add(feat);
    }
}

From source file:net.semanticmetadata.lire.imageanalysis.features.local.opencvfeatures.CvSurfExtractor.java

License:Open Source License

public LinkedList<CvSurfFeature> computeSurfKeypoints(BufferedImage img) {
    MatOfKeyPoint keypoints = new MatOfKeyPoint();
    List<KeyPoint> myKeys;/*w w  w.  j a va 2s . c  o m*/
    //        Mat img_object = Highgui.imread(image, 0); //0 = CV_LOAD_IMAGE_GRAYSCALE
    //        detector.detect(img_object, keypoints);
    byte[] data = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
    Mat matRGB = new Mat(img.getHeight(), img.getWidth(), CvType.CV_8UC3);
    matRGB.put(0, 0, data);
    Mat matGray = new Mat(img.getHeight(), img.getWidth(), CvType.CV_8UC1);
    Imgproc.cvtColor(matRGB, matGray, Imgproc.COLOR_BGR2GRAY); //TODO: RGB or BGR?
    byte[] dataGray = new byte[matGray.rows() * matGray.cols() * (int) (matGray.elemSize())];
    matGray.get(0, 0, dataGray);

    detector.detect(matGray, keypoints);
    myKeys = keypoints.toList();

    LinkedList<CvSurfFeature> myKeypoints = new LinkedList<CvSurfFeature>();
    KeyPoint key;
    CvSurfFeature feat;
    for (Iterator<KeyPoint> iterator = myKeys.iterator(); iterator.hasNext();) {
        key = iterator.next();
        feat = new CvSurfFeature(key.pt.x, key.pt.y, key.size, null);
        myKeypoints.add(feat);
    }

    return myKeypoints;
}

From source file:overwatchteampicker.OverwatchTeamPicker.java

public static ReturnValues findImage(String template, String source, int flag) {
    File lib = null;/* w w w. ja v  a 2 s.  c  om*/
    BufferedImage image = null;
    try {
        image = ImageIO.read(new File(source));
    } catch (Exception e) {
        e.printStackTrace();
    }

    String os = System.getProperty("os.name");
    String bitness = System.getProperty("sun.arch.data.model");

    if (os.toUpperCase().contains("WINDOWS")) {
        if (bitness.endsWith("64")) {
            lib = new File("C:\\Users\\POWERUSER\\Downloads\\opencv\\build\\java\\x64\\"
                    + System.mapLibraryName("opencv_java2413"));
        } else {
            lib = new File("libs//x86//" + System.mapLibraryName("opencv_java2413"));
        }
    }
    System.load(lib.getAbsolutePath());
    String tempObject = "images\\hero_templates\\" + template + ".png";
    String source_pic = source;
    Mat objectImage = Highgui.imread(tempObject, Highgui.CV_LOAD_IMAGE_GRAYSCALE);
    Mat sceneImage = Highgui.imread(source_pic, Highgui.CV_LOAD_IMAGE_GRAYSCALE);

    MatOfKeyPoint objectKeyPoints = new MatOfKeyPoint();
    FeatureDetector featureDetector = FeatureDetector.create(FeatureDetector.SURF);
    featureDetector.detect(objectImage, objectKeyPoints);
    KeyPoint[] keypoints = objectKeyPoints.toArray();
    MatOfKeyPoint objectDescriptors = new MatOfKeyPoint();
    DescriptorExtractor descriptorExtractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
    descriptorExtractor.compute(objectImage, objectKeyPoints, objectDescriptors);

    // Create the matrix for output image.
    Mat outputImage = new Mat(objectImage.rows(), objectImage.cols(), Highgui.CV_LOAD_IMAGE_COLOR);
    Scalar newKeypointColor = new Scalar(255, 0, 0);
    Features2d.drawKeypoints(objectImage, objectKeyPoints, outputImage, newKeypointColor, 0);

    // Match object image with the scene image
    MatOfKeyPoint sceneKeyPoints = new MatOfKeyPoint();
    MatOfKeyPoint sceneDescriptors = new MatOfKeyPoint();
    featureDetector.detect(sceneImage, sceneKeyPoints);
    descriptorExtractor.compute(sceneImage, sceneKeyPoints, sceneDescriptors);

    Mat matchoutput = new Mat(sceneImage.rows() * 2, sceneImage.cols() * 2, Highgui.CV_LOAD_IMAGE_COLOR);
    Scalar matchestColor = new Scalar(0, 255, 25);

    List<MatOfDMatch> matches = new LinkedList<MatOfDMatch>();
    DescriptorMatcher descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
    descriptorMatcher.knnMatch(objectDescriptors, sceneDescriptors, matches, 2);

    LinkedList<DMatch> goodMatchesList = new LinkedList<DMatch>();

    float nndrRatio = .78f;

    for (int i = 0; i < matches.size(); i++) {
        MatOfDMatch matofDMatch = matches.get(i);
        DMatch[] dmatcharray = matofDMatch.toArray();
        DMatch m1 = dmatcharray[0];
        DMatch m2 = dmatcharray[1];

        if (m1.distance <= m2.distance * nndrRatio) {
            goodMatchesList.addLast(m1);

        }
    }

    if (goodMatchesList.size() >= 4) {

        List<KeyPoint> objKeypointlist = objectKeyPoints.toList();
        List<KeyPoint> scnKeypointlist = sceneKeyPoints.toList();

        LinkedList<Point> objectPoints = new LinkedList<>();
        LinkedList<Point> scenePoints = new LinkedList<>();

        for (int i = 0; i < goodMatchesList.size(); i++) {
            objectPoints.addLast(objKeypointlist.get(goodMatchesList.get(i).queryIdx).pt);
            scenePoints.addLast(scnKeypointlist.get(goodMatchesList.get(i).trainIdx).pt);
        }

        MatOfPoint2f objMatOfPoint2f = new MatOfPoint2f();
        objMatOfPoint2f.fromList(objectPoints);
        MatOfPoint2f scnMatOfPoint2f = new MatOfPoint2f();
        scnMatOfPoint2f.fromList(scenePoints);

        Mat homography = Calib3d.findHomography(objMatOfPoint2f, scnMatOfPoint2f, Calib3d.RANSAC, 3);

        Mat obj_corners = new Mat(4, 1, CvType.CV_32FC2);
        Mat scene_corners = new Mat(4, 1, CvType.CV_32FC2);

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

        Core.perspectiveTransform(obj_corners, scene_corners, homography);

        Mat img = Highgui.imread(source_pic, Highgui.CV_LOAD_IMAGE_COLOR);

        Core.line(img, new Point(scene_corners.get(0, 0)), new Point(scene_corners.get(1, 0)),
                new Scalar(0, 255, 255), 4);
        Core.line(img, new Point(scene_corners.get(1, 0)), new Point(scene_corners.get(2, 0)),
                new Scalar(255, 255, 0), 4);
        Core.line(img, new Point(scene_corners.get(2, 0)), new Point(scene_corners.get(3, 0)),
                new Scalar(0, 255, 0), 4);
        Core.line(img, new Point(scene_corners.get(3, 0)), new Point(scene_corners.get(0, 0)),
                new Scalar(0, 255, 0), 4);

        MatOfDMatch goodMatches = new MatOfDMatch();
        goodMatches.fromList(goodMatchesList);

        Features2d.drawMatches(objectImage, objectKeyPoints, sceneImage, sceneKeyPoints, goodMatches,
                matchoutput, matchestColor, newKeypointColor, new MatOfByte(), 2);
        if (new Point(scene_corners.get(0, 0)).x < new Point(scene_corners.get(1, 0)).x
                && new Point(scene_corners.get(0, 0)).y < new Point(scene_corners.get(2, 0)).y) {
            System.out.println("found " + template);
            Highgui.imwrite("points.jpg", outputImage);
            Highgui.imwrite("matches.jpg", matchoutput);
            Highgui.imwrite("final.jpg", img);

            if (flag == 0) {
                ReturnValues retVal = null;
                int y = (int) new Point(scene_corners.get(3, 0)).y;
                int yHeight = (int) new Point(scene_corners.get(3, 0)).y
                        - (int) new Point(scene_corners.get(2, 0)).y;
                if (y < image.getHeight() * .6) { //if found hero is in upper half of image then return point 3,0
                    retVal = new ReturnValues(y + (int) (image.getHeight() * .01), yHeight);
                } else { //if found hero is in lower half of image then return point 2,0
                    y = (int) new Point(scene_corners.get(2, 0)).y;
                    retVal = new ReturnValues(y + (int) (image.getHeight() * .3), yHeight);
                }
                return retVal;
            } else if (flag == 1) {
                int[] xPoints = new int[4];
                int[] yPoints = new int[4];

                xPoints[0] = (int) (new Point(scene_corners.get(0, 0)).x);
                xPoints[1] = (int) (new Point(scene_corners.get(1, 0)).x);
                xPoints[2] = (int) (new Point(scene_corners.get(2, 0)).x);
                xPoints[3] = (int) (new Point(scene_corners.get(3, 0)).x);

                yPoints[0] = (int) (new Point(scene_corners.get(0, 0)).y);
                yPoints[1] = (int) (new Point(scene_corners.get(1, 0)).y);
                yPoints[2] = (int) (new Point(scene_corners.get(2, 0)).y);
                yPoints[3] = (int) (new Point(scene_corners.get(3, 0)).y);

                ReturnValues retVal = new ReturnValues(xPoints, yPoints);
                return retVal;

            }
        }
    }
    return null;

}

From source file:View.Signature.java

public static int sift(String routeVal, String route, String n_img1, String n_img2, String extension) {

    String bookObject = routeVal + n_img2 + extension;
    String bookScene = route + n_img1 + extension;

    //System.out.println("Iniciando SIFT");
    //java.lang.System.out.print("Abriendo imagenes | ");
    Mat objectImage = Highgui.imread(bookObject, Highgui.CV_LOAD_IMAGE_COLOR);
    Mat sceneImage = Highgui.imread(bookScene, Highgui.CV_LOAD_IMAGE_COLOR);

    MatOfKeyPoint objectKeyPoints = new MatOfKeyPoint();
    FeatureDetector featureDetector = FeatureDetector.create(FeatureDetector.SIFT);
    //java.lang.System.out.print("Encontrar keypoints con SIFT | ");  
    featureDetector.detect(objectImage, objectKeyPoints);
    KeyPoint[] keypoints = objectKeyPoints.toArray();

    MatOfKeyPoint objectDescriptors = new MatOfKeyPoint();
    DescriptorExtractor descriptorExtractor = DescriptorExtractor.create(DescriptorExtractor.SIFT);
    //java.lang.System.out.print("Computando descriptores | ");  
    descriptorExtractor.compute(objectImage, objectKeyPoints, objectDescriptors);

    // Create the matrix for output image.   
    Mat outputImage = new Mat(objectImage.rows(), objectImage.cols(), Highgui.CV_LOAD_IMAGE_COLOR);
    Scalar newKeypointColor = new Scalar(255, 0, 0);

    //java.lang.System.out.print("Dibujando keypoints en imagen base | ");  
    Features2d.drawKeypoints(objectImage, objectKeyPoints, outputImage, newKeypointColor, 0);

    // Match object image with the scene image  
    MatOfKeyPoint sceneKeyPoints = new MatOfKeyPoint();
    MatOfKeyPoint sceneDescriptors = new MatOfKeyPoint();
    //java.lang.System.out.print("Detectando keypoints en imagen base | ");
    featureDetector.detect(sceneImage, sceneKeyPoints);
    //java.lang.System.out.print("Computando descriptores en imagen base | ");
    descriptorExtractor.compute(sceneImage, sceneKeyPoints, sceneDescriptors);

    Mat matchoutput = new Mat(sceneImage.rows() * 2, sceneImage.cols() * 2, Highgui.CV_LOAD_IMAGE_COLOR);
    Scalar matchestColor = new Scalar(0, 255, 0);

    List<MatOfDMatch> matches = new LinkedList<MatOfDMatch>();
    DescriptorMatcher descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
    //java.lang.System.out.print("Encontrando matches entre imagenes | ");  
    descriptorMatcher.knnMatch(objectDescriptors, sceneDescriptors, matches, 2);

    //java.lang.System.out.println("Calculando buenos matches");
    LinkedList<DMatch> goodMatchesList = new LinkedList<DMatch>();

    float nndrRatio = 0.7f;
    java.lang.System.out.println(matches.size());
    for (int i = 0; i < matches.size(); i++) {
        MatOfDMatch matofDMatch = matches.get(i);
        DMatch[] dmatcharray = matofDMatch.toArray();
        DMatch m1 = dmatcharray[0];//from   w  ww  .ja  va2 s. co m
        DMatch m2 = dmatcharray[1];

        if (m1.distance <= m2.distance * nndrRatio) {
            goodMatchesList.addLast(m1);

        }
    }

    if (goodMatchesList.size() >= 7) {
        //java.lang.System.out.println("Match enontrado!!! Matches: "+goodMatchesList.size());
        //if(goodMatchesList.size()>max){

        //cambio = 1;
        //}    

        List<KeyPoint> objKeypointlist = objectKeyPoints.toList();
        List<KeyPoint> scnKeypointlist = sceneKeyPoints.toList();

        LinkedList<Point> objectPoints = new LinkedList<>();
        LinkedList<Point> scenePoints = new LinkedList<>();

        for (int i = 0; i < goodMatchesList.size(); i++) {
            objectPoints.addLast(objKeypointlist.get(goodMatchesList.get(i).queryIdx).pt);
            scenePoints.addLast(scnKeypointlist.get(goodMatchesList.get(i).trainIdx).pt);
        }

        MatOfPoint2f objMatOfPoint2f = new MatOfPoint2f();
        objMatOfPoint2f.fromList(objectPoints);
        MatOfPoint2f scnMatOfPoint2f = new MatOfPoint2f();
        scnMatOfPoint2f.fromList(scenePoints);

        Mat homography = Calib3d.findHomography(objMatOfPoint2f, scnMatOfPoint2f, Calib3d.RANSAC, 3);

        Mat obj_corners = new Mat(4, 1, CvType.CV_32FC2);
        Mat scene_corners = new Mat(4, 1, CvType.CV_32FC2);

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

        //System.out.println("Transforming object corners to scene corners...");  
        Core.perspectiveTransform(obj_corners, scene_corners, homography);

        Mat img = Highgui.imread(bookScene, Highgui.CV_LOAD_IMAGE_COLOR);

        Core.line(img, new Point(scene_corners.get(0, 0)), new Point(scene_corners.get(1, 0)),
                new Scalar(0, 255, 0), 4);
        Core.line(img, new Point(scene_corners.get(1, 0)), new Point(scene_corners.get(2, 0)),
                new Scalar(0, 255, 0), 4);
        Core.line(img, new Point(scene_corners.get(2, 0)), new Point(scene_corners.get(3, 0)),
                new Scalar(0, 255, 0), 4);
        Core.line(img, new Point(scene_corners.get(3, 0)), new Point(scene_corners.get(0, 0)),
                new Scalar(0, 255, 0), 4);

        //java.lang.System.out.println("Dibujando imagen de coincidencias");
        MatOfDMatch goodMatches = new MatOfDMatch();
        goodMatches.fromList(goodMatchesList);

        Features2d.drawMatches(objectImage, objectKeyPoints, sceneImage, sceneKeyPoints, goodMatches,
                matchoutput, matchestColor, newKeypointColor, new MatOfByte(), 2);
        String n_outputImage = route + "results\\" + n_img2 + "_outputImage_sift" + extension;
        String n_matchoutput = route + "results\\" + n_img2 + "_matchoutput_sift" + extension;
        String n_img = route + "results\\" + n_img2 + "_sift" + extension;
        Highgui.imwrite(n_outputImage, outputImage);
        Highgui.imwrite(n_matchoutput, matchoutput);
        //Highgui.imwrite(n_img, img);  
        java.lang.System.out.println(goodMatches.size().height);
        double result = goodMatches.size().height * 100 / matches.size();

        java.lang.System.out.println((int) result);
        //double result =goodMatches.size().height;
        if (result > 100) {
            return 100;
        } else if (result <= 100 && result > 85) {
            return 85;
        } else if (result <= 85 && result > 50) {
            return 50;
        } else if (result <= 50 && result > 25) {
            return 25;
        } else {
            return 0;
        }
    } else {
        //java.lang.System.out.println("Firma no encontrada");  
    }
    return 0;
    //System.out.println("Terminando SIFT");  
}

From source file:View.SignatureLib.java

public static int sift(String routeRNV, String routeAdherent) {

    String bookObject = routeAdherent;
    String bookScene = routeRNV;//  w ww . j  a  va2 s  . c o  m

    //System.out.println("Iniciando SIFT");
    //java.lang.System.out.print("Abriendo imagenes | ");
    Mat objectImage = Highgui.imread(bookObject, Highgui.CV_LOAD_IMAGE_COLOR);
    Mat sceneImage = Highgui.imread(bookScene, Highgui.CV_LOAD_IMAGE_COLOR);

    MatOfKeyPoint objectKeyPoints = new MatOfKeyPoint();
    FeatureDetector featureDetector = FeatureDetector.create(FeatureDetector.SIFT);
    //java.lang.System.out.print("Encontrar keypoints con SIFT | ");  
    featureDetector.detect(objectImage, objectKeyPoints);
    KeyPoint[] keypoints = objectKeyPoints.toArray();

    MatOfKeyPoint objectDescriptors = new MatOfKeyPoint();
    DescriptorExtractor descriptorExtractor = DescriptorExtractor.create(DescriptorExtractor.SIFT);
    //java.lang.System.out.print("Computando descriptores | ");  
    descriptorExtractor.compute(objectImage, objectKeyPoints, objectDescriptors);

    // Create the matrix for output image.   
    Mat outputImage = new Mat(objectImage.rows(), objectImage.cols(), Highgui.CV_LOAD_IMAGE_COLOR);
    Scalar newKeypointColor = new Scalar(255, 0, 0);

    //java.lang.System.out.print("Dibujando keypoints en imagen base | ");  
    Features2d.drawKeypoints(objectImage, objectKeyPoints, outputImage, newKeypointColor, 0);

    // Match object image with the scene image  
    MatOfKeyPoint sceneKeyPoints = new MatOfKeyPoint();
    MatOfKeyPoint sceneDescriptors = new MatOfKeyPoint();
    //java.lang.System.out.print("Detectando keypoints en imagen base | ");
    featureDetector.detect(sceneImage, sceneKeyPoints);
    //java.lang.System.out.print("Computando descriptores en imagen base | ");
    descriptorExtractor.compute(sceneImage, sceneKeyPoints, sceneDescriptors);

    Mat matchoutput = new Mat(sceneImage.rows() * 2, sceneImage.cols() * 2, Highgui.CV_LOAD_IMAGE_COLOR);
    Scalar matchestColor = new Scalar(0, 255, 0);

    List<MatOfDMatch> matches = new LinkedList<MatOfDMatch>();
    DescriptorMatcher descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
    //java.lang.System.out.println(sceneDescriptors);  

    if (sceneDescriptors.empty()) {
        java.lang.System.out.println("Objeto no encontrado");
        return 0;
    }

    descriptorMatcher.knnMatch(objectDescriptors, sceneDescriptors, matches, 2);

    //java.lang.System.out.println("Calculando buenos matches");
    LinkedList<DMatch> goodMatchesList = new LinkedList<DMatch>();

    float nndrRatio = 0.7f;

    for (int i = 0; i < matches.size(); i++) {
        MatOfDMatch matofDMatch = matches.get(i);
        DMatch[] dmatcharray = matofDMatch.toArray();
        DMatch m1 = dmatcharray[0];
        DMatch m2 = dmatcharray[1];

        if (m1.distance <= m2.distance * nndrRatio) {
            goodMatchesList.addLast(m1);

        }
    }

    if (goodMatchesList.size() >= 7) {
        max = goodMatchesList.size();

        List<KeyPoint> objKeypointlist = objectKeyPoints.toList();
        List<KeyPoint> scnKeypointlist = sceneKeyPoints.toList();

        LinkedList<Point> objectPoints = new LinkedList<>();
        LinkedList<Point> scenePoints = new LinkedList<>();

        for (int i = 0; i < goodMatchesList.size(); i++) {
            objectPoints.addLast(objKeypointlist.get(goodMatchesList.get(i).queryIdx).pt);
            scenePoints.addLast(scnKeypointlist.get(goodMatchesList.get(i).trainIdx).pt);
        }

        MatOfPoint2f objMatOfPoint2f = new MatOfPoint2f();
        objMatOfPoint2f.fromList(objectPoints);
        MatOfPoint2f scnMatOfPoint2f = new MatOfPoint2f();
        scnMatOfPoint2f.fromList(scenePoints);

        Mat homography = Calib3d.findHomography(objMatOfPoint2f, scnMatOfPoint2f, Calib3d.RANSAC, 3);

        Mat obj_corners = new Mat(4, 1, CvType.CV_32FC2);
        Mat scene_corners = new Mat(4, 1, CvType.CV_32FC2);

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

        //System.out.println("Transforming object corners to scene corners...");  
        Core.perspectiveTransform(obj_corners, scene_corners, homography);

        Mat img = Highgui.imread(bookScene, Highgui.CV_LOAD_IMAGE_COLOR);

        Core.line(img, new Point(scene_corners.get(0, 0)), new Point(scene_corners.get(1, 0)),
                new Scalar(0, 255, 0), 4);
        Core.line(img, new Point(scene_corners.get(1, 0)), new Point(scene_corners.get(2, 0)),
                new Scalar(0, 255, 0), 4);
        Core.line(img, new Point(scene_corners.get(2, 0)), new Point(scene_corners.get(3, 0)),
                new Scalar(0, 255, 0), 4);
        Core.line(img, new Point(scene_corners.get(3, 0)), new Point(scene_corners.get(0, 0)),
                new Scalar(0, 255, 0), 4);

        //java.lang.System.out.println("Dibujando imagen de coincidencias");
        MatOfDMatch goodMatches = new MatOfDMatch();
        goodMatches.fromList(goodMatchesList);

        Features2d.drawMatches(objectImage, objectKeyPoints, sceneImage, sceneKeyPoints, goodMatches,
                matchoutput, matchestColor, newKeypointColor, new MatOfByte(), 2);

        String n_outputImage = "../pre/outputImage_sift.jpg";
        String n_matchoutput = "../pre/matchoutput_sift.jpg";
        String n_img = "../pre/sift.jpg";
        Highgui.imwrite(n_outputImage, outputImage);
        Highgui.imwrite(n_matchoutput, matchoutput);
        Highgui.imwrite(n_img, img);
        java.lang.System.out.println(goodMatches.size().height);
        double result = goodMatches.size().height;//*100/matches.size();
        int score = 0;
        if (result > 26) {
            score = 100;
        } else if (result <= 26 && result > 22) {
            score = 85;
        } else if (result <= 22 && result > 17) {
            score = 50;
        } else if (result <= 17 && result > 11) {
            score = 25;
        } else {
            score = 0;
        }
        java.lang.System.out.println("Score: " + score);
        return score;
    } else {
        java.lang.System.out.println("Objeto no encontrado");
        return 0;
    }
    //System.out.println("Terminando SIFT");  
}

From source file:vinylsleevedetection.Analyze.java

public void Check() {
    count = 1;//w  w  w  .  j a  v  a 2s  .  c  om
    //load openCV library
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    //for loop to compare source images to user image
    for (int j = 1; j < 4; j++) {
        //source image location (record sleeve)
        String Object = "E:\\Users\\Jamie\\Documents\\NetBeansProjects\\VinylSleeveDetection\\Source\\" + j
                + ".jpg";
        //user image location
        String Scene = "E:\\Users\\Jamie\\Documents\\NetBeansProjects\\VinylSleeveDetection\\Output\\camera.jpg";
        //load images
        Mat objectImage = Imgcodecs.imread(Object, Imgcodecs.CV_LOAD_IMAGE_COLOR);
        Mat sceneImage = Imgcodecs.imread(Scene, Imgcodecs.CV_LOAD_IMAGE_COLOR);
        //use BRISK feature detection
        MatOfKeyPoint objectKeyPoints = new MatOfKeyPoint();
        FeatureDetector featureDetector = FeatureDetector.create(FeatureDetector.BRISK);
        //perform feature detection on source image
        featureDetector.detect(objectImage, objectKeyPoints);
        KeyPoint[] keypoints = objectKeyPoints.toArray();
        //use descriptor extractor
        MatOfKeyPoint objectDescriptors = new MatOfKeyPoint();
        DescriptorExtractor descriptorExtractor = DescriptorExtractor.create(DescriptorExtractor.BRISK);
        descriptorExtractor.compute(objectImage, objectKeyPoints, objectDescriptors);

        Mat outputImage = new Mat(objectImage.rows(), objectImage.cols(), Imgcodecs.CV_LOAD_IMAGE_COLOR);
        Scalar newKeypointColor = new Scalar(255, 0, 0);

        Features2d.drawKeypoints(objectImage, objectKeyPoints, outputImage, newKeypointColor, 0);

        MatOfKeyPoint sceneKeyPoints = new MatOfKeyPoint();
        MatOfKeyPoint sceneDescriptors = new MatOfKeyPoint();
        featureDetector.detect(sceneImage, sceneKeyPoints);
        descriptorExtractor.compute(sceneImage, sceneKeyPoints, sceneDescriptors);

        Mat matchoutput = new Mat(sceneImage.rows() * 2, sceneImage.cols() * 2, Imgcodecs.CV_LOAD_IMAGE_COLOR);
        Scalar matchestColor = new Scalar(0, 255, 0);

        List<MatOfDMatch> matches = new LinkedList<>();
        DescriptorMatcher descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE);
        descriptorMatcher.knnMatch(objectDescriptors, sceneDescriptors, matches, 2);

        LinkedList<DMatch> goodMatchesList = new LinkedList<DMatch>();

        float nndrRatio = 0.7f;

        for (int i = 0; i < matches.size(); i++) {
            MatOfDMatch matofDMatch = matches.get(i);
            DMatch[] dmatcharray = matofDMatch.toArray();
            DMatch m1 = dmatcharray[0];
            DMatch m2 = dmatcharray[1];

            if (m1.distance <= m2.distance * nndrRatio) {
                goodMatchesList.addLast(m1);

            }
        }
        //if the number of good mathces is more than 150 a match is found
        if (goodMatchesList.size() > 150) {
            System.out.println("Object Found");

            List<KeyPoint> objKeypointlist = objectKeyPoints.toList();
            List<KeyPoint> scnKeypointlist = sceneKeyPoints.toList();

            LinkedList<Point> objectPoints = new LinkedList<>();
            LinkedList<Point> scenePoints = new LinkedList<>();

            for (int i = 0; i < goodMatchesList.size(); i++) {
                objectPoints.addLast(objKeypointlist.get(goodMatchesList.get(i).queryIdx).pt);
                scenePoints.addLast(scnKeypointlist.get(goodMatchesList.get(i).trainIdx).pt);
            }

            MatOfPoint2f objMatOfPoint2f = new MatOfPoint2f();
            objMatOfPoint2f.fromList(objectPoints);
            MatOfPoint2f scnMatOfPoint2f = new MatOfPoint2f();
            scnMatOfPoint2f.fromList(scenePoints);

            Mat homography = Calib3d.findHomography(objMatOfPoint2f, scnMatOfPoint2f, Calib3d.RANSAC, 3);

            Mat obj_corners = new Mat(4, 1, CvType.CV_32FC2);
            Mat scene_corners = new Mat(4, 1, CvType.CV_32FC2);

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

            Core.perspectiveTransform(obj_corners, scene_corners, homography);

            Mat img = Imgcodecs.imread(Scene, Imgcodecs.CV_LOAD_IMAGE_COLOR);
            //draw a green square around the matched object
            Imgproc.line(img, new Point(scene_corners.get(0, 0)), new Point(scene_corners.get(1, 0)),
                    new Scalar(0, 255, 0), 10);
            Imgproc.line(img, new Point(scene_corners.get(1, 0)), new Point(scene_corners.get(2, 0)),
                    new Scalar(0, 255, 0), 10);
            Imgproc.line(img, new Point(scene_corners.get(2, 0)), new Point(scene_corners.get(3, 0)),
                    new Scalar(0, 255, 0), 10);
            Imgproc.line(img, new Point(scene_corners.get(3, 0)), new Point(scene_corners.get(0, 0)),
                    new Scalar(0, 255, 0), 10);

            MatOfDMatch goodMatches = new MatOfDMatch();
            goodMatches.fromList(goodMatchesList);

            Features2d.drawMatches(objectImage, objectKeyPoints, sceneImage, sceneKeyPoints, goodMatches,
                    matchoutput, matchestColor, newKeypointColor, new MatOfByte(), 2);
            //output image with match, image of the match locations and keypoints image
            String folder = "E:\\Users\\Jamie\\Documents\\NetBeansProjects\\VinylSleeveDetection\\Output\\";
            Imgcodecs.imwrite(folder + "outputImage.jpg", outputImage);
            Imgcodecs.imwrite(folder + "matchoutput.jpg", matchoutput);
            Imgcodecs.imwrite(folder + "found.jpg", img);
            count = j;
            break;
        } else {
            System.out.println("Object Not Found");
            count = 0;
        }

    }

}