Example usage for org.opencv.core MatOfPoint2f toArray

List of usage examples for org.opencv.core MatOfPoint2f toArray

Introduction

In this page you can find the example usage for org.opencv.core MatOfPoint2f toArray.

Prototype

public Point[] toArray() 

Source Link

Usage

From source file:ac.robinson.ticqr.TickBoxImageParserTask.java

License:Apache License

@Override
protected ArrayList<PointF> doInBackground(Void... unused) {
    Log.d(TAG, "Searching for tick boxes of " + mBoxSize + " size");

    // we look for *un-ticked* boxes, rather than ticked, as they are uniform in appearance (and hence easier to
    // detect) - they show up as a box within a box
    ArrayList<PointF> centrePoints = new ArrayList<>();
    int minimumOuterBoxArea = (int) Math.round(Math.pow(mBoxSize, 2));
    int maximumOuterBoxArea = (int) Math.round(Math.pow(mBoxSize * 1.35f, 2));
    int minimumInnerBoxArea = (int) Math.round(Math.pow(mBoxSize * 0.5f, 2));

    // image adjustment - blurSize, blurSTDev and adaptiveThresholdSize must not be even numbers
    int blurSize = 9;
    int blurSTDev = 3;
    int adaptiveThresholdSize = Math.round(mBoxSize * 3); // (oddness ensured below)
    int adaptiveThresholdC = 4; // value to add to the mean (can be negative or zero)
    adaptiveThresholdSize = adaptiveThresholdSize % 2 == 0 ? adaptiveThresholdSize + 1 : adaptiveThresholdSize;

    // how similar the recognised polygon must be to its actual contour - lower is more similar
    float outerPolygonSimilarity = 0.045f;
    float innerPolygonSimilarity = 0.075f; // don't require as much accuracy for the inner part of the tick box

    // how large the maximum internal angle can be (e.g., for checking square shape)
    float maxOuterAngleCos = 0.3f;
    float maxInnerAngleCos = 0.4f;

    // use OpenCV to recognise boxes that have a box inside them - i.e. an un-ticked tick box
    // see: http://stackoverflow.com/a/11427501
    // Bitmap newBitmap = mBitmap.copy(Bitmap.Config.RGB_565, true); // not needed
    Mat bitMat = new Mat();
    Utils.bitmapToMat(mBitmap, bitMat);//w w w  . java  2s  .c  o  m

    // blur and convert to grey
    // alternative (less flexible): Imgproc.medianBlur(bitMat, bitMat, blurSize);
    Imgproc.GaussianBlur(bitMat, bitMat, new Size(blurSize, blurSize), blurSTDev, blurSTDev);
    Imgproc.cvtColor(bitMat, bitMat, Imgproc.COLOR_RGB2GRAY); // need 8uC1 (1 channel, unsigned char) image type

    // perform adaptive thresholding to detect edges
    // alternative (slower): Imgproc.Canny(bitMat, bitMat, 10, 20, 3, false);
    Imgproc.adaptiveThreshold(bitMat, bitMat, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY,
            adaptiveThresholdSize, adaptiveThresholdC);

    // get the contours in the image, and their hierarchy
    Mat hierarchyMat = new Mat();
    List<MatOfPoint> contours = new ArrayList<>();
    Imgproc.findContours(bitMat, contours, hierarchyMat, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
    if (DEBUG) {
        Imgproc.drawContours(bitMat, contours, -1, new Scalar(30, 255, 255), 1);
    }

    // parse the contours and look for a box containing another box, with similar enough sizes
    int numContours = contours.size();
    ArrayList<Integer> searchedContours = new ArrayList<>();
    Log.d(TAG, "Found " + numContours + " possible tick box areas");
    if (numContours > 0 && !hierarchyMat.empty()) {
        for (int i = 0; i < numContours; i++) {

            // the original detected contour
            MatOfPoint boxPoints = contours.get(i);

            // hierarchy key: 0 = next sibling num, 1 = previous sibling num, 2 = first child num, 3 = parent num
            int childBox = (int) hierarchyMat.get(0, i)[2]; // usually the largest child (as we're doing RETR_TREE)
            if (childBox == -1) { // we only want elements that have children
                continue;
            } else {
                if (searchedContours.contains(childBox)) {
                    if (DEBUG) {
                        Log.d(TAG, "Ignoring duplicate box at first stage: " + childBox);
                    }
                    continue;
                } else {
                    searchedContours.add(childBox);
                }
            }

            // discard smaller (i.e. noise) outer box areas as soon as possible for speed
            // used to do Imgproc.isContourConvex(outerPoints) later, but the angle check covers this, so no need
            double originalArea = Math.abs(Imgproc.contourArea(boxPoints));
            if (originalArea < minimumOuterBoxArea) {
                // if (DEBUG) {
                // drawPoints(bitMat, boxPoints, new Scalar(255, 255, 255), 1);
                // Log.d(TAG, "Outer box too small");
                // }
                continue;
            }
            if (originalArea > maximumOuterBoxArea) {
                // if (DEBUG) {
                // drawPoints(bitMat, boxPoints, new Scalar(255, 255, 255), 1);
                // Log.d(TAG, "Outer box too big");
                // }
                continue;
            }

            // simplify the contours of the outer box - we want to detect four-sided shapes only
            MatOfPoint2f boxPoints2f = new MatOfPoint2f(boxPoints.toArray()); // Point2f for approxPolyDP
            Imgproc.approxPolyDP(boxPoints2f, boxPoints2f,
                    outerPolygonSimilarity * Imgproc.arcLength(boxPoints2f, true), true); // simplify the contour
            if (boxPoints2f.height() != 4) { // height is number of points
                if (DEBUG) {
                    // drawPoints(bitMat, new MatOfPoint(boxPoints2f.toArray()), new Scalar(255, 255, 255), 1);
                    Log.d(TAG, "Outer box not 4 points");
                }
                continue;
            }

            // check that the simplified outer box is approximately a square, angle-wise
            org.opencv.core.Point[] boxPointsArray = boxPoints2f.toArray();
            double maxCosine = 0;
            for (int j = 0; j < 4; j++) {
                org.opencv.core.Point pL = boxPointsArray[j];
                org.opencv.core.Point pIntersect = boxPointsArray[(j + 1) % 4];
                org.opencv.core.Point pR = boxPointsArray[(j + 2) % 4];
                getLineAngle(pL, pIntersect, pR);
                maxCosine = Math.max(maxCosine, getLineAngle(pL, pIntersect, pR));
            }
            if (maxCosine > maxOuterAngleCos) {
                if (DEBUG) {
                    // drawPoints(bitMat, new MatOfPoint(boxPoints2f.toArray()), new Scalar(255, 255, 255), 1);
                    Log.d(TAG, "Outer angles not square enough");
                }
                continue;
            }

            // check that the simplified outer box is approximately a square, line length-wise
            double minLine = Double.MAX_VALUE;
            double maxLine = 0;
            for (int p = 1; p < 4; p++) {
                org.opencv.core.Point p1 = boxPointsArray[p - 1];
                org.opencv.core.Point p2 = boxPointsArray[p];
                double xd = p1.x - p2.x;
                double yd = p1.y - p2.y;
                double lineLength = Math.sqrt((xd * xd) + (yd * yd));
                minLine = Math.min(minLine, lineLength);
                maxLine = Math.max(maxLine, lineLength);
            }
            if (maxLine - minLine > minLine) {
                if (DEBUG) {
                    // drawPoints(bitMat, new MatOfPoint(boxPoints2f.toArray()), new Scalar(255, 255, 255), 1);
                    Log.d(TAG, "Outer lines not square enough");
                }
                continue;
            }

            // draw the outer box if debugging
            if (DEBUG) {
                MatOfPoint debugBoxPoints = new MatOfPoint(boxPointsArray);
                Log.d(TAG,
                        "Potential tick box: " + boxPoints2f.size() + ", " + "area: "
                                + Math.abs(Imgproc.contourArea(debugBoxPoints)) + " (min:" + minimumOuterBoxArea
                                + ", max:" + maximumOuterBoxArea + ")");
                drawPoints(bitMat, debugBoxPoints, new Scalar(50, 255, 255), 2);
            }

            // loop through the children - they should be in descending size order, but sometimes this is wrong
            boolean wrongBox = false;
            while (true) {
                if (DEBUG) {
                    Log.d(TAG, "Looping with box: " + childBox);
                }

                // we've previously tried a child - try the next one
                // key: 0 = next sibling num, 1 = previous sibling num, 2 = first child num, 3 = parent num
                if (wrongBox) {
                    childBox = (int) hierarchyMat.get(0, childBox)[0];
                    if (childBox == -1) {
                        break;
                    }
                    if (searchedContours.contains(childBox)) {
                        if (DEBUG) {
                            Log.d(TAG, "Ignoring duplicate box at loop stage: " + childBox);
                        }
                        break;
                    } else {
                        searchedContours.add(childBox);
                    }
                    //noinspection UnusedAssignment
                    wrongBox = false;
                }

                // perhaps this is the outer box - check its child has no children itself
                // (removed so tiny children (i.e. noise) don't mean we mis-detect an un-ticked box as ticked)
                // if (hierarchyMat.get(0, childBox)[2] != -1) {
                // continue;
                // }

                // check the size of the child box is large enough
                boxPoints = contours.get(childBox);
                originalArea = Math.abs(Imgproc.contourArea(boxPoints));
                if (originalArea < minimumInnerBoxArea) {
                    if (DEBUG) {
                        // drawPoints(bitMat, boxPoints, new Scalar(255, 255, 255), 1);
                        Log.d(TAG, "Inner box too small");
                    }
                    wrongBox = true;
                    continue;
                }

                // simplify the contours of the inner box - again, we want four-sided shapes only
                boxPoints2f = new MatOfPoint2f(boxPoints.toArray());
                Imgproc.approxPolyDP(boxPoints2f, boxPoints2f,
                        innerPolygonSimilarity * Imgproc.arcLength(boxPoints2f, true), true);
                if (boxPoints2f.height() != 4) { // height is number of points
                    // if (DEBUG) {
                    // drawPoints(bitMat, boxPoints, new Scalar(255, 255, 255), 1);
                    // }
                    Log.d(TAG, "Inner box fewer than 4 points"); // TODO: allow > 4 for low quality images?
                    wrongBox = true;
                    continue;
                }

                // check that the simplified inner box is approximately a square, angle-wise
                // higher tolerance because noise means if we get several inners, the box may not be quite square
                boxPointsArray = boxPoints2f.toArray();
                maxCosine = 0;
                for (int j = 0; j < 4; j++) {
                    org.opencv.core.Point pL = boxPointsArray[j];
                    org.opencv.core.Point pIntersect = boxPointsArray[(j + 1) % 4];
                    org.opencv.core.Point pR = boxPointsArray[(j + 2) % 4];
                    getLineAngle(pL, pIntersect, pR);
                    maxCosine = Math.max(maxCosine, getLineAngle(pL, pIntersect, pR));
                }
                if (maxCosine > maxInnerAngleCos) {
                    Log.d(TAG, "Inner angles not square enough");
                    wrongBox = true;
                    continue;
                }

                // this is probably an inner box - log if debugging
                if (DEBUG) {
                    Log.d(TAG,
                            "Un-ticked inner box: " + boxPoints2f.size() + ", " + "area: "
                                    + Math.abs(Imgproc.contourArea(new MatOfPoint2f(boxPointsArray)))
                                    + " (min: " + minimumInnerBoxArea + ")");
                }

                // find the inner box centre
                double centreX = (boxPointsArray[0].x + boxPointsArray[1].x + boxPointsArray[2].x
                        + boxPointsArray[3].x) / 4f;
                double centreY = (boxPointsArray[0].y + boxPointsArray[1].y + boxPointsArray[2].y
                        + boxPointsArray[3].y) / 4f;

                // draw the inner box if debugging
                if (DEBUG) {
                    drawPoints(bitMat, new MatOfPoint(boxPointsArray), new Scalar(255, 255, 255), 1);
                    Core.circle(bitMat, new org.opencv.core.Point(centreX, centreY), 3,
                            new Scalar(255, 255, 255));
                }

                // add to the list of boxes to check
                centrePoints.add(new PointF((float) centreX, (float) centreY));
                break;
            }
        }
    }

    Log.d(TAG, "Found " + centrePoints.size() + " un-ticked boxes");
    return centrePoints;
}

From source file:com.trandi.opentld.tld.LKTracker.java

License:Apache License

/**
 * @return Pair of new, FILTERED, last and current POINTS, or null if it hasn't managed to track anything.
 *//*from w  w  w  . ja v  a 2 s .co m*/
Pair<Point[], Point[]> track(final Mat lastImg, final Mat currentImg, Point[] lastPoints) {
    final int size = lastPoints.length;
    final MatOfPoint2f currentPointsMat = new MatOfPoint2f();
    final MatOfPoint2f pointsFBMat = new MatOfPoint2f();
    final MatOfByte statusMat = new MatOfByte();
    final MatOfFloat errSimilarityMat = new MatOfFloat();
    final MatOfByte statusFBMat = new MatOfByte();
    final MatOfFloat errSimilarityFBMat = new MatOfFloat();

    //Forward-Backward tracking
    Video.calcOpticalFlowPyrLK(lastImg, currentImg, new MatOfPoint2f(lastPoints), currentPointsMat, statusMat,
            errSimilarityMat, WINDOW_SIZE, MAX_LEVEL, termCriteria, 0, LAMBDA);
    Video.calcOpticalFlowPyrLK(currentImg, lastImg, currentPointsMat, pointsFBMat, statusFBMat,
            errSimilarityFBMat, WINDOW_SIZE, MAX_LEVEL, termCriteria, 0, LAMBDA);

    final byte[] status = statusMat.toArray();
    float[] errSimilarity = new float[lastPoints.length];
    //final byte[] statusFB = statusFBMat.toArray();
    final float[] errSimilarityFB = errSimilarityFBMat.toArray();

    // compute the real FB error (relative to LAST points not the current ones...
    final Point[] pointsFB = pointsFBMat.toArray();
    for (int i = 0; i < size; i++) {
        errSimilarityFB[i] = Util.norm(pointsFB[i], lastPoints[i]);
    }

    final Point[] currPoints = currentPointsMat.toArray();
    // compute real similarity error
    errSimilarity = normCrossCorrelation(lastImg, currentImg, lastPoints, currPoints, status);

    //TODO  errSimilarityFB has problem != from C++
    // filter out points with fwd-back error > the median AND points with similarity error > median
    return filterPts(lastPoints, currPoints, errSimilarity, errSimilarityFB, status);
}

From source file:detectiontest.Particle.java

public static Rect calcBoundingBox(MatOfPoint contour) {
    MatOfPoint2f curve = new MatOfPoint2f(contour.toArray());
    MatOfPoint2f curveApprox = new MatOfPoint2f();
    Imgproc.approxPolyDP(curve, curveApprox, 3, true);
    return Imgproc.boundingRect(new MatOfPoint(curveApprox.toArray()));
}

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/*www .j  ava2s.  c  o 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:edu.fiu.cate.breader.BaseSegmentation.java

/**
 * Finds the bounding box for the book on the stand using 
 * the depth average image.// w w  w. j  av  a 2  s  . c  o m
 * @param src- The Depth average image
 * @return Rectangle delineating the book
 */
public Rect lowResDist(Mat src) {
    Mat dst = src.clone();

    Imgproc.blur(src, dst, new Size(5, 5), new Point(-1, -1), Core.BORDER_REPLICATE);
    //      Imgproc.threshold(dst, dst, 0,255,Imgproc.THRESH_BINARY_INV+Imgproc.THRESH_OTSU);
    Imgproc.Canny(dst, dst, 50, 200, 3, false);
    //      Canny(src, dst, 20, 60, 3);

    List<MatOfPoint> contours = new LinkedList<>();
    Mat hierarchy = new Mat();
    /// Find contours
    Imgproc.findContours(dst, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE,
            new Point(0, 0));

    Mat color = new Mat();
    Imgproc.cvtColor(src, color, Imgproc.COLOR_GRAY2BGR);
    for (int k = 0; k < contours.size(); k++) {
        byte[] vals = ITools.getHeatMapColor((float) k / (float) contours.size());
        Imgproc.drawContours(color, contours, k, new Scalar(vals[0], vals[1], vals[2]), 1);
    }
    new IViewer("LowRes Contours ", BReaderTools.bufferedImageFromMat(color));

    for (int k = 0; k < contours.size(); k++) {
        MatOfPoint2f tMat = new MatOfPoint2f();
        Imgproc.approxPolyDP(new MatOfPoint2f(contours.get(k).toArray()), tMat, 5, true);
        contours.set(k, new MatOfPoint(tMat.toArray()));
    }

    List<Point> points = new LinkedList<Point>();
    for (int i = 0; i < contours.size(); i++) {
        points.addAll(contours.get(i).toList());
    }

    MatOfInt tHull = new MatOfInt();
    Imgproc.convexHull(new MatOfPoint(points.toArray(new Point[points.size()])), tHull);

    //get bounding box
    Point[] tHullPoints = new Point[tHull.rows()];
    for (int i = 0; i < tHull.rows(); i++) {
        int pIndex = (int) tHull.get(i, 0)[0];
        tHullPoints[i] = points.get(pIndex);
    }
    Rect out = Imgproc.boundingRect(new MatOfPoint(tHullPoints));
    return out;
}

From source file:gab.opencv.OpenCV.java

License:Open Source License

public static ArrayList<PVector> matToPVectors(MatOfPoint2f mat) {
    ArrayList<PVector> result = new ArrayList<PVector>();
    Point[] points = mat.toArray();
    for (int i = 0; i < points.length; i++) {
        result.add(new PVector((float) points[i].x, (float) points[i].y));
    }//  ww  w .j ava2  s .  c o m

    return result;
}

From source file:logic.featurepointextractor.MouthFPE.java

/**
 * Detect mouth feature points//from www  . j  a v  a  2  s .  c o  m
 * Algorithm:           Equalize histogram of mouth rect
 *                      Implement Sobel horizontal filter
 *                      Find corners
 *                      Invert color + Binarization
 *                      Find lip up and down points
 * @param mc
 * @return 
 */
@Override
public Point[] detect(MatContainer mc) {
    /**Algorithm
     *                  find pix(i) = (R-G)/R
     *                  normalize: 2arctan(pix(i))/pi
     */

    //find pix(i) = (R-G)/R
    Mat mouthRGBMat = mc.origFrame.submat(mc.mouthRect);
    List mouthSplitChannelsList = new ArrayList<Mat>();
    Core.split(mouthRGBMat, mouthSplitChannelsList);
    //extract R-channel
    Mat mouthR = (Mat) mouthSplitChannelsList.get(2);
    mouthR.convertTo(mouthR, CvType.CV_64FC1);
    //extract G-channel
    Mat mouthG = (Mat) mouthSplitChannelsList.get(1);
    mouthG.convertTo(mouthG, CvType.CV_64FC1);
    //calculate (R-G)/R
    Mat dst = new Mat(mouthR.rows(), mouthR.cols(), CvType.CV_64FC1);
    mc.mouthProcessedMat = new Mat(mouthR.rows(), mouthR.cols(), CvType.CV_64FC1);

    Core.absdiff(mouthR, mouthG, dst);
    //        Core.divide(dst, mouthR, mc.mouthProcessedMat);
    mc.mouthProcessedMat = dst;
    mc.mouthProcessedMat.convertTo(mc.mouthProcessedMat, CvType.CV_8UC1);
    Imgproc.equalizeHist(mc.mouthProcessedMat, mc.mouthProcessedMat);
    //       Imgproc.blur(mc.mouthProcessedMat, mc.mouthProcessedMat, new Size(4,4));
    //        Imgproc.morphologyEx(mc.mouthProcessedMat, mc.mouthProcessedMat, Imgproc.MORPH_OPEN, Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(4,4)));
    Imgproc.threshold(mc.mouthProcessedMat, mc.mouthProcessedMat, 230, 255, THRESH_BINARY);

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Imgproc.findContours(mc.mouthProcessedMat, contours, new Mat(), Imgproc.RETR_TREE,
            Imgproc.CHAIN_APPROX_SIMPLE);

    //find the biggest contour
    int maxSize = -1;
    int tmpSize = -1;
    int index = -1;

    Rect centMouthRect = new Rect(mc.mouthRect.x + mc.mouthRect.width / 4,
            mc.mouthRect.y + mc.mouthRect.height / 4, mc.mouthRect.width / 2, mc.mouthRect.height / 2);
    if (contours.size() != 0) {
        maxSize = contours.get(0).toArray().length;
        tmpSize = 0;
        index = 0;
    }

    //find max contour
    for (int j = 0; j < contours.size(); ++j) {
        //if contour is vertical, exclude it 
        Rect boundRect = Imgproc.boundingRect(contours.get(j));
        int centX = mc.mouthRect.x + boundRect.x + boundRect.width / 2;
        int centY = mc.mouthRect.y + boundRect.y + boundRect.height / 2;
        //                LOG.info("Center = " + centX + "; " + centY);
        //                LOG.info("Rect = " + centMouthRect.x + "; " + centMouthRect.y);
        if (!centMouthRect.contains(new Point(centX, centY)))
            continue;

        tmpSize = contours.get(j).toArray().length;

        LOG.info("Contour " + j + "; size = " + tmpSize);

        if (tmpSize > maxSize) {
            maxSize = tmpSize;
            index = j;
        }
    }

    //appproximate curve
    Point[] p1 = contours.get(index).toArray();
    MatOfPoint2f p2 = new MatOfPoint2f(p1);
    MatOfPoint2f p3 = new MatOfPoint2f();
    Imgproc.approxPolyDP(p2, p3, 1, true);

    p1 = p3.toArray();

    MatOfInt tmpMatOfPoint = new MatOfInt();
    Imgproc.convexHull(new MatOfPoint(p1), tmpMatOfPoint);

    Rect boundRect = Imgproc.boundingRect(new MatOfPoint(p1));
    if (boundRect.area() / mc.mouthRect.area() > 0.3)
        return null;

    int size = (int) tmpMatOfPoint.size().height;
    Point[] _p1 = new Point[size];
    int[] a = tmpMatOfPoint.toArray();

    _p1[0] = new Point(p1[a[0]].x + mc.mouthRect.x, p1[a[0]].y + mc.mouthRect.y);
    Core.circle(mc.origFrame, _p1[0], 3, new Scalar(0, 0, 255), -1);
    for (int i = 1; i < size; i++) {
        _p1[i] = new Point(p1[a[i]].x + mc.mouthRect.x, p1[a[i]].y + mc.mouthRect.y);
        Core.circle(mc.origFrame, _p1[i], 3, new Scalar(0, 0, 255), -1);
        Core.line(mc.origFrame, _p1[i - 1], _p1[i], new Scalar(255, 0, 0), 2);
    }
    Core.line(mc.origFrame, _p1[size - 1], _p1[0], new Scalar(255, 0, 0), 2);

    /*        contours.set(index, new MatOfPoint(_p1));
            
            mc.mouthProcessedMat.setTo(new Scalar(0));
                    
            Imgproc.drawContours(mc.mouthProcessedMat, contours, index, new Scalar(255), -1);
                    
    */ mc.mouthMatOfPoint = _p1;

    MatOfPoint matOfPoint = new MatOfPoint(_p1);
    mc.mouthBoundRect = Imgproc.boundingRect(matOfPoint);
    mc.features.mouthBoundRect = mc.mouthBoundRect;

    /**extract feature points:  1 most left
     *                          2 most right
     *                          3,4 up
     *                          5,6 down
     */

    //        mc.mouthMatOfPoint = extractFeaturePoints(contours.get(index));

    return null;
}

From source file:org.lasarobotics.vision.detection.ObjectDetection.java

License:Open Source License

/**
 * Draw the object's location/*from  ww  w  .  j a v  a2s .c o  m*/
 *
 * @param output         Image to draw on
 * @param objectAnalysis Object analysis information
 * @param sceneAnalysis  Scene analysis information
 */
public static void drawObjectLocation(Mat output, ObjectAnalysis objectAnalysis, SceneAnalysis sceneAnalysis) {
    List<Point> ptsObject = new ArrayList<>();
    List<Point> ptsScene = new ArrayList<>();

    KeyPoint[] keypointsObject = objectAnalysis.keypoints.toArray();
    KeyPoint[] keypointsScene = sceneAnalysis.keypoints.toArray();

    DMatch[] matches = sceneAnalysis.matches.toArray();

    for (DMatch matche : matches) {
        //Get the keypoints from these matches
        ptsObject.add(keypointsObject[matche.queryIdx].pt);
        ptsScene.add(keypointsScene[matche.trainIdx].pt);
    }

    MatOfPoint2f matObject = new MatOfPoint2f();
    matObject.fromList(ptsObject);

    MatOfPoint2f matScene = new MatOfPoint2f();
    matScene.fromList(ptsScene);

    //Calculate homography of object in scene
    Mat homography = Calib3d.findHomography(matObject, matScene, Calib3d.RANSAC, 5.0f);

    //Create the unscaled array of corners, representing the object size
    Point cornersObject[] = new Point[4];
    cornersObject[0] = new Point(0, 0);
    cornersObject[1] = new Point(objectAnalysis.object.cols(), 0);
    cornersObject[2] = new Point(objectAnalysis.object.cols(), objectAnalysis.object.rows());
    cornersObject[3] = new Point(0, objectAnalysis.object.rows());

    Point[] cornersSceneTemp = new Point[0];

    MatOfPoint2f cornersSceneMatrix = new MatOfPoint2f(cornersSceneTemp);
    MatOfPoint2f cornersObjectMatrix = new MatOfPoint2f(cornersObject);

    //Transform the object coordinates to the scene coordinates by the homography matrix
    Core.perspectiveTransform(cornersObjectMatrix, cornersSceneMatrix, homography);

    //Mat transform = Imgproc.getAffineTransform(cornersObjectMatrix, cornersSceneMatrix);

    //Draw the lines of the object on the scene
    Point[] cornersScene = cornersSceneMatrix.toArray();
    final ColorRGBA lineColor = new ColorRGBA("#00ff00");
    Drawing.drawLine(output, new Point(cornersScene[0].x + objectAnalysis.object.cols(), cornersScene[0].y),
            new Point(cornersScene[1].x + objectAnalysis.object.cols(), cornersScene[1].y), lineColor, 5);
    Drawing.drawLine(output, new Point(cornersScene[1].x + objectAnalysis.object.cols(), cornersScene[1].y),
            new Point(cornersScene[2].x + objectAnalysis.object.cols(), cornersScene[2].y), lineColor, 5);
    Drawing.drawLine(output, new Point(cornersScene[2].x + objectAnalysis.object.cols(), cornersScene[2].y),
            new Point(cornersScene[3].x + objectAnalysis.object.cols(), cornersScene[3].y), lineColor, 5);
    Drawing.drawLine(output, new Point(cornersScene[3].x + objectAnalysis.object.cols(), cornersScene[3].y),
            new Point(cornersScene[0].x + objectAnalysis.object.cols(), cornersScene[0].y), lineColor, 5);
}

From source file:org.lasarobotics.vision.detection.objects.Contour.java

License:Open Source License

/**
 * Instantiate a contour from an OpenCV matrix of points (double)
 *
 * @param data OpenCV matrix of points/*from   w ww .ja  v a 2  s .  c o m*/
 */
public Contour(MatOfPoint2f data) {
    this.mat = new MatOfPoint(data.toArray());
}

From source file:org.lasarobotics.vision.detection.PrimitiveDetection.java

License:Open Source License

/**
 * Locate rectangles in an image//from   w  w  w  .  ja v  a 2s . c om
 *
 * @param grayImage Grayscale image
 * @return Rectangle locations
 */
public RectangleLocationResult locateRectangles(Mat grayImage) {
    Mat gray = grayImage.clone();

    //Filter out some noise by halving then doubling size
    Filter.downsample(gray, 2);
    Filter.upsample(gray, 2);

    //Mat is short for Matrix, and here is used to store an image.
    //it is n-dimensional, but as an image, is two-dimensional
    Mat cacheHierarchy = new Mat();
    Mat grayTemp = new Mat();
    List<Rectangle> rectangles = new ArrayList<>();
    List<Contour> contours = new ArrayList<>();

    //This finds the edges using a Canny Edge Detector
    //It is sent the grayscale Image, a temp Mat, the lower detection threshold for an edge,
    //the higher detection threshold, the Aperture (blurring) of the image - higher is better
    //for long, smooth edges, and whether a more accurate version (but time-expensive) version
    //should be used (true = more accurate)
    //Note: the edges are stored in "grayTemp", which is an image where everything
    //is black except for gray-scale lines delineating the edges.
    Imgproc.Canny(gray, grayTemp, 0, THRESHOLD_CANNY, APERTURE_CANNY, true);
    //make the white lines twice as big, while leaving the image size constant
    Filter.dilate(gray, 2);

    List<MatOfPoint> contoursTemp = new ArrayList<>();
    //Find contours - the parameters here are very important to compression and retention
    //grayTemp is the image from which the contours are found,
    //contoursTemp is where the resultant contours are stored (note: color is not retained),
    //cacheHierarchy is the parent-child relationship between the contours (e.g. a contour
    //inside of another is its child),
    //Imgproc.CV_RETR_LIST disables the hierarchical relationships being returned,
    //Imgproc.CHAIN_APPROX_SIMPLE means that the contour is compressed from a massive chain of
    //paired coordinates to just the endpoints of each segment (e.g. an up-right rectangular
    //contour is encoded with 4 points.)
    Imgproc.findContours(grayTemp, contoursTemp, cacheHierarchy, Imgproc.CV_RETR_LIST,
            Imgproc.CHAIN_APPROX_SIMPLE);
    //MatOfPoint2f means that is a MatofPoint (Matrix of Points) represented by floats instead of ints
    MatOfPoint2f approx = new MatOfPoint2f();
    //For each contour, test whether the contour is a rectangle
    //List<Contour> contours = new ArrayList<>()
    for (MatOfPoint co : contoursTemp) {
        //converting the MatOfPoint to MatOfPoint2f
        MatOfPoint2f matOfPoint2f = new MatOfPoint2f(co.toArray());
        //converting the matrix to a Contour
        Contour c = new Contour(co);

        //Attempt to fit the contour to the best polygon
        //input: matOfPoint2f, which is the contour found earlier
        //output: approx, which is the MatOfPoint2f that holds the new polygon that has less vertices
        //basically, it smooths out the edges using the third parameter as its approximation accuracy
        //final parameter determines whether the new approximation must be closed (true=closed)
        Imgproc.approxPolyDP(matOfPoint2f, approx, c.arcLength(true) * EPLISON_APPROX_TOLERANCE_FACTOR, true);

        //converting the MatOfPoint2f to a contour
        Contour approxContour = new Contour(approx);

        //Make sure the contour is big enough, CLOSED (convex), and has exactly 4 points
        if (approx.toArray().length == 4 && Math.abs(approxContour.area()) > 1000 && approxContour.isClosed()) {

            //TODO contours and rectangles array may not match up, but why would they?
            contours.add(approxContour);

            //Check each angle to be approximately 90 degrees
            //Done by comparing the three points constituting the angle of each corner
            double maxCosine = 0;
            for (int j = 2; j < 5; j++) {
                double cosine = Math.abs(MathUtil.angle(approx.toArray()[j % 4], approx.toArray()[j - 2],
                        approx.toArray()[j - 1]));
                maxCosine = Math.max(maxCosine, cosine);
            }

            if (maxCosine < MAX_COSINE_VALUE) {
                //Convert the points to a rectangle instance
                rectangles.add(new Rectangle(approx.toArray()));
            }
        }
    }

    return new RectangleLocationResult(contours, rectangles);
}