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

The following examples show how to use org.opencv.core.MatOfPoint2f#fromArray() . 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: PerspectiveTransformation.java    From AndroidDocumentScanner with MIT License 6 votes vote down vote up
private MatOfPoint2f sortCorners(MatOfPoint2f corners) {
    Point center = getMassCenter(corners);
    List<Point> points = corners.toList();
    List<Point> topPoints = new ArrayList<Point>();
    List<Point> bottomPoints = new ArrayList<Point>();

    for (Point point : points) {
        if (point.y < center.y) {
            topPoints.add(point);
        } else {
            bottomPoints.add(point);
        }
    }

    Point topLeft = topPoints.get(0).x > topPoints.get(1).x ? topPoints.get(1) : topPoints.get(0);
    Point topRight = topPoints.get(0).x > topPoints.get(1).x ? topPoints.get(0) : topPoints.get(1);
    Point bottomLeft = bottomPoints.get(0).x > bottomPoints.get(1).x ? bottomPoints.get(1) : bottomPoints.get(0);
    Point bottomRight = bottomPoints.get(0).x > bottomPoints.get(1).x ? bottomPoints.get(0) : bottomPoints.get(1);

    MatOfPoint2f result = new MatOfPoint2f();
    Point[] sortedPoints = {topLeft, topRight, bottomRight, bottomLeft};
    result.fromArray(sortedPoints);

    return result;
}
 
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: AutoCalibrationManager.java    From ShootOFF with GNU General Public License v3.0 6 votes vote down vote up
private MatOfPoint2f calcBoardRectFromCorners(MatOfPoint2f corners) {
	final MatOfPoint2f result = new MatOfPoint2f();
	result.alloc(4);

	final Point topLeft = new Point(corners.get(0, 0)[0], corners.get(0, 0)[1]);
	final Point topRight = new Point(corners.get(PATTERN_WIDTH - 1, 0)[0], corners.get(PATTERN_WIDTH - 1, 0)[1]);
	final Point bottomRight = new Point(corners.get(PATTERN_WIDTH * PATTERN_HEIGHT - 1, 0)[0],
			corners.get(PATTERN_WIDTH * PATTERN_HEIGHT - 1, 0)[1]);
	final Point bottomLeft = new Point(corners.get(PATTERN_WIDTH * (PATTERN_HEIGHT - 1), 0)[0],
			corners.get(PATTERN_WIDTH * (PATTERN_HEIGHT - 1), 0)[1]);

	final Point[] unsorted = { topLeft, topRight, bottomLeft, bottomRight };
	final Point[] sorted = sortCorners(unsorted);

	result.fromArray(sorted);

	// result.put(0, 0, topLeft.x, topLeft.y, topRight.x, topRight.y,
	// bottomRight.x, bottomRight.y, bottomLeft.x,
	// bottomLeft.y);

	return result;
}
 
Example 4
Source File: NativeClass.java    From AndroidDocumentScanner with MIT License 5 votes vote down vote up
public Bitmap getScannedBitmap(Bitmap bitmap, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) {
    PerspectiveTransformation perspective = new PerspectiveTransformation();
    MatOfPoint2f rectangle = new MatOfPoint2f();
    rectangle.fromArray(new Point(x1, y1), new Point(x2, y2), new Point(x3, y3), new Point(x4, y4));
    Mat dstMat = perspective.transform(ImageUtils.bitmapToMat(bitmap), rectangle);
    return ImageUtils.matToBitmap(dstMat);
}
 
Example 5
Source File: PerspectiveTransformation.java    From AndroidDocumentScanner with MIT License 5 votes vote down vote up
private MatOfPoint2f getOutline(Mat image) {
    Point topLeft = new Point(0, 0);
    Point topRight = new Point(image.cols(), 0);
    Point bottomRight = new Point(image.cols(), image.rows());
    Point bottomLeft = new Point(0, image.rows());
    Point[] points = {topLeft, topRight, bottomRight, bottomLeft};

    MatOfPoint2f result = new MatOfPoint2f();
    result.fromArray(points);

    return result;
}