Example usage for org.opencv.core MatOfPoint size

List of usage examples for org.opencv.core MatOfPoint size

Introduction

In this page you can find the example usage for org.opencv.core MatOfPoint size.

Prototype

public Size size() 

Source Link

Usage

From source file:dfmDrone.examples.fitEllipseExample.java

private static Mat findAndDrawEllipse(Mat sourceImg) {
    Mat grayScaleImg = new Mat();
    Mat hsvImg = new Mat();
    Imgproc.cvtColor(sourceImg, hsvImg, Imgproc.COLOR_BGR2HSV);
    Mat lower_hue_range = new Mat();
    Mat upper_hue_range = new Mat();
    Core.inRange(hsvImg, new Scalar(0, 100, 45), new Scalar(15, 255, 255), lower_hue_range);
    Core.inRange(hsvImg, new Scalar(160, 100, 45), new Scalar(180, 255, 255), upper_hue_range);
    Mat red_hue_image = new Mat();
    Core.addWeighted(lower_hue_range, 1.0, upper_hue_range, 1.0, 0, red_hue_image);
    Mat dilateElement = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(24, 24));
    Mat erodeElement = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10, 10));

    Imgproc.blur(red_hue_image, red_hue_image, new Size(11, 11));
    // init//from  www  .  j  a  v  a  2  s .  co m
    List<MatOfPoint> contours = new ArrayList<>();
    Mat hierarchy = new Mat();

    // find contours
    Imgproc.findContours(red_hue_image, contours, hierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE);
    System.out.println("After findcontours");
    // if any contour exist...
    if (hierarchy.size().height > 0 && hierarchy.size().width > 0) {
        // for each contour, display it in blue
        for (int idx = 0; idx >= 0; idx = (int) hierarchy.get(0, idx)[0]) {
            System.out.println(idx);
            //   Imgproc.drawContours(frame, contours, idx, new Scalar(250, 0, 0), 3);

        }
    }
    MatOfPoint2f approxCurve = new MatOfPoint2f();

    //For each contour found
    MatOfPoint2f contour2f = null;
    RotatedRect rotatedrect = null;
    for (MatOfPoint contour : contours) {
        //Convert contours(i) from MatOfPoint to MatOfPoint2f
        if (contour2f == null)
            contour2f = new MatOfPoint2f(contour.toArray());
        if (contour.size().area() > contour2f.size().area()) {
            contour2f = new MatOfPoint2f(contour.toArray());
        }
    }
    try {
        Imgproc.fitEllipse(contour2f);
        rotatedrect = Imgproc.fitEllipse(contour2f);

        double approxDistance = Imgproc.arcLength(contour2f, true) * 0.02;
        Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);

        //Convert back to MatOfPoint
        MatOfPoint points = new MatOfPoint(approxCurve.toArray());

        // Get bounding rect of contour
        Rect rect = Imgproc.boundingRect(points);

        // draw enclosing rectangle (all same color, but you could use variable i to make them unique)
        Imgproc.rectangle(sourceImg, rect.tl(), rect.br(), new Scalar(255, 0, 0), 1, 8, 0);
        Imgproc.ellipse(sourceImg, rotatedrect, new Scalar(255, 192, 203), 4, 8);
    } catch (CvException e) {
        e.printStackTrace();
        System.out.println("Ingen ellipse fundet");
    }
    return sourceImg;
}

From source file:logic.featurepointextractor.MouthFPE.java

/**
 * Extract feature points from contour of mouth
 * Algorithm:   For all points find most left and most right
 *              Find 2 pair up and down center points between most left and most right points
 * @param contour//from  w w w . j  a v a 2s .  co m
 * @return 6 feature points
 */
private Point[] extractFeaturePoints(MatOfPoint contour) {
    Point[] dst = new Point[6];

    int size = (int) contour.size().height;

    Point[] cPoints = contour.toArray();

    int leftInd = 0;
    double leftVal = cPoints[leftInd].x;
    int rightInd = 0;
    double rightVal = cPoints[rightInd].x;
    for (int i = 1; i < size; ++i) {
        double xVal = cPoints[i].x;
        if (leftVal > xVal) {
            leftVal = xVal;
            leftInd = i;
        }

        if (rightVal < xVal) {
            rightVal = xVal;
            rightInd = i;
        }
    }

    dst[0] = cPoints[leftInd];
    dst[1] = cPoints[rightInd];

    int indDiff = rightInd - leftInd;

    return dst;
}

From source file:qupath.opencv.tools.WandToolCV.java

License:Open Source License

@Override
protected Shape createShape(double x, double y, boolean useTiles) {

    if (mat == null)
        mat = new Mat(w, w, CvType.CV_8UC3);
    if (matMask == null)
        matMask = new Mat(w + 2, w + 2, CvType.CV_8U);

    if (pLast != null && pLast.distanceSq(x, y) < 4)
        return new Path2D.Float();

    long startTime = System.currentTimeMillis();

    QuPathViewer viewer = getViewer();/*from   w  ww . jav a2 s  .  co m*/
    if (viewer == null)
        return new Path2D.Float();

    double downsample = viewer.getDownsampleFactor();

    DefaultImageRegionStore regionStore = viewer.getImageRegionStore();

    // Paint the image as it is currently being viewed
    Graphics2D g2d = imgTemp.createGraphics();
    g2d.setColor(Color.BLACK);
    g2d.fillRect(0, 0, w, w);
    bounds.setFrame(x - w * downsample * .5, y - w * downsample * .5, w * downsample, w * downsample);
    g2d.scale(1.0 / downsample, 1.0 / downsample);
    g2d.translate(-bounds.getX(), -bounds.getY());
    regionStore.paintRegionCompletely(viewer.getServer(), g2d, bounds, viewer.getZPosition(),
            viewer.getTPosition(), viewer.getDownsampleFactor(), null, viewer.getImageDisplay(), 250);
    g2d.dispose();

    // Put pixels into an OpenCV image
    byte[] buffer = ((DataBufferByte) imgTemp.getRaster().getDataBuffer()).getData();
    mat.put(0, 0, buffer);

    //      Imgproc.cvtColor(mat, mat, Imgproc.COLOR_BGR2Lab);
    //      blurSigma = 4;

    double blurSigma = Math.max(0.5, getWandSigmaPixels());
    double size = Math.ceil(blurSigma * 2) * 2 + 1;
    blurSize.width = size;
    blurSize.height = size;

    // Smooth a little
    Imgproc.GaussianBlur(mat, mat, blurSize, blurSigma);

    //      Imgproc.cvtColor(mat, mat, Imgproc.COLOR_RGB2Lab);

    MatOfDouble mean = new MatOfDouble();
    MatOfDouble stddev = new MatOfDouble();
    Core.meanStdDev(mat, mean, stddev);
    //      logger.trace(stddev.dump());

    double[] stddev2 = stddev.toArray();
    double scale = .4;
    for (int i = 0; i < stddev2.length; i++)
        stddev2[i] = stddev2[i] * scale;
    threshold.set(stddev2);

    mean.release();
    stddev.release();

    matMask.setTo(zero);
    Imgproc.circle(matMask, seed, w / 2, one);
    Imgproc.floodFill(mat, matMask, seed, one, null, threshold, threshold,
            4 | (2 << 8) | Imgproc.FLOODFILL_MASK_ONLY | Imgproc.FLOODFILL_FIXED_RANGE);
    Core.subtract(matMask, one, matMask);

    if (strel == null)
        strel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5, 5));
    Imgproc.morphologyEx(matMask, matMask, Imgproc.MORPH_CLOSE, strel);
    ////      Imgproc.morphologyEx(matMask, matMask, Imgproc.MORPH_OPEN, Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, size));
    //      
    ////      threshold = new Scalar(10, 10, 10);
    //      double[] stddev2 = stddev.toArray();
    //      double scale = .5;
    //      threshold = new Scalar(stddev2[0]*scale, stddev2[1]*scale, stddev2[2]*scale);

    contours.clear();
    if (contourHierarchy == null)
        contourHierarchy = new Mat();
    Imgproc.findContours(matMask, contours, contourHierarchy, Imgproc.RETR_EXTERNAL,
            Imgproc.CHAIN_APPROX_SIMPLE);
    //      logger.trace("Contours: " + contours.size());

    Path2D path = new Path2D.Float();
    boolean isOpen = false;
    for (MatOfPoint contour : contours) {

        // Discard single pixels / lines
        if (contour.size().height <= 2)
            continue;

        // Create a polygon ROI
        boolean firstPoint = true;
        for (Point p : contour.toArray()) {
            double xx = (p.x - w / 2 - 1) * downsample + x;
            double yy = (p.y - w / 2 - 1) * downsample + y;
            if (firstPoint) {
                path.moveTo(xx, yy);
                firstPoint = false;
                isOpen = true;
            } else
                path.lineTo(xx, yy);
        }
    }
    if (isOpen)
        path.closePath();

    long endTime = System.currentTimeMillis();
    logger.trace(getClass().getSimpleName() + " time: " + (endTime - startTime));

    if (pLast == null)
        pLast = new Point2D.Double(x, y);
    else
        pLast.setLocation(x, y);

    return path;

}