Java Code Examples for org.opencv.core.MatOfPoint2f#toArray()

The following examples show how to use org.opencv.core.MatOfPoint2f#toArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CVProcessor.java    From CVScanner with GNU General Public License v3.0 6 votes vote down vote up
static public Quadrilateral getQuadrilateral(List<MatOfPoint> contours, Size srcSize){
    double ratio = getScaleRatio(srcSize);
    int height = Double.valueOf(srcSize.height / ratio).intValue();
    int width = Double.valueOf(srcSize.width / ratio).intValue();
    Size size = new Size(width,height);

    for ( MatOfPoint c: contours ) {
        MatOfPoint2f c2f = new MatOfPoint2f(c.toArray());
        double peri = Imgproc.arcLength(c2f, true);
        MatOfPoint2f approx = new MatOfPoint2f();
        Imgproc.approxPolyDP(c2f, approx, 0.02 * peri, true);

        Point[] points = approx.toArray();
        Log.d("SCANNER", "approx size: " + points.length);

        // select biggest 4 angles polygon
        if (points.length == 4) {
            Point[] foundPoints = sortPoints(points);

            if (isInside(foundPoints, size) && isLargeEnough(foundPoints, size, 0.25)) {
                return new Quadrilateral( c , foundPoints );
            }
            else{
                //showToast(context, "Try getting closer to the ID");
                Log.d("SCANNER", "Not inside defined area");
            }
        }
    }

    //showToast(context, "Make sure the ID is on a contrasting background");
    return null;
}
 
Example 2
Source File: AutoCalibrationManager.java    From ShootOFF with GNU General Public License v3.0 6 votes vote down vote up
private MatOfPoint2f sortPointsForWarpPerspective(final MatOfPoint2f boardRect, final Point[] corners) {
	final Point[] cornerArray = new Point[4];
	final Double[] cornerED = new Double[4];
	final Point[] boardRectArray = boardRect.toArray();
	for (int i = 0; i < 4; i++)
		cornerED[i] = -1.0;
	for (final Point cpt : corners) {
		for (int i = 0; i < 4; i++) {

			final double tempED = euclideanDistance(cpt, boardRectArray[i]);
			if (cornerED[i] == -1.0 || tempED < cornerED[i]) {
				cornerArray[i] = cpt;
				cornerED[i] = tempED;
			}
		}
	}

	final MatOfPoint2f corners2f = new MatOfPoint2f();
	corners2f.fromArray(cornerArray);
	return corners2f;
}
 
Example 3
Source File: NativeClass.java    From AndroidDocumentScanner with MIT License 5 votes vote down vote up
private boolean isRectangle(MatOfPoint2f polygon, int srcArea) {
    MatOfPoint polygonInt = MathUtils.toMatOfPointInt(polygon);

    if (polygon.rows() != 4) {
        return false;
    }

    double area = Math.abs(Imgproc.contourArea(polygon));
    if (area < srcArea * AREA_LOWER_THRESHOLD || area > srcArea * AREA_UPPER_THRESHOLD) {
        return false;
    }

    if (!Imgproc.isContourConvex(polygonInt)) {
        return false;
    }

    // Check if the all angles are more than 72.54 degrees (cos 0.3).
    double maxCosine = 0;
    Point[] approxPoints = polygon.toArray();

    for (int i = 2; i < 5; i++) {
        double cosine = Math.abs(MathUtils.angle(approxPoints[i % 4], approxPoints[i - 2], approxPoints[i - 1]));
        maxCosine = Math.max(cosine, maxCosine);
    }

    return !(maxCosine >= 0.3);
}
 
Example 4
Source File: PerspectiveTransformation.java    From AndroidDocumentScanner with MIT License 5 votes vote down vote up
private Size getRectangleSize(MatOfPoint2f rectangle) {
    Point[] corners = rectangle.toArray();

    double top = getDistance(corners[0], corners[1]);
    double right = getDistance(corners[1], corners[2]);
    double bottom = getDistance(corners[2], corners[3]);
    double left = getDistance(corners[3], corners[0]);

    double averageWidth = (top + bottom) / 2f;
    double averageHeight = (right + left) / 2f;

    return new Size(new Point(averageWidth, averageHeight));
}
 
Example 5
Source File: LKTracker.java    From OpenTLDAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * @return Pair of new, FILTERED, last and current POINTS, or null if it hasn't managed to track anything.
 */
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);
}
 
Example 6
Source File: PrimitiveDetection.java    From FTCVision with MIT License 4 votes vote down vote up
/**
 * Locate rectangles in an image
 *
 * @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);
}
 
Example 7
Source File: ContoursUtils.java    From super-cloudops with Apache License 2.0 3 votes vote down vote up
/**
 * 利用函数approxPolyDP来对指定的点集进行逼近 精确度设置好,效果还是比较好的
 *
 * @param cannyMat
 * @param threshold
 *            阀值(精确度)
 * @return
 */
public static Point[] useApproxPolyDPFindPoints(Mat cannyMat, double threshold) {

	MatOfPoint maxContour = findMaxContour(cannyMat);

	MatOfPoint2f approxCurve = new MatOfPoint2f();
	MatOfPoint2f matOfPoint2f = new MatOfPoint2f(maxContour.toArray());

	// 原始曲线与近似曲线之间的最大距离设置为0.01,true表示是闭合的曲线
	Imgproc.approxPolyDP(matOfPoint2f, approxCurve, threshold, true);

	Point[] points = approxCurve.toArray();

	return points;
}
 
Example 8
Source File: Contour.java    From FTCVision with MIT License 2 votes vote down vote up
/**
 * Instantiate a contour from an OpenCV matrix of points (double)
 *
 * @param data OpenCV matrix of points
 */
public Contour(MatOfPoint2f data) {
    this.mat = new MatOfPoint(data.toArray());
}