Java Code Examples for org.opencv.imgproc.Imgproc#getPerspectiveTransform()
The following examples show how to use
org.opencv.imgproc.Imgproc#getPerspectiveTransform() .
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 |
public Mat transform(Mat src, MatOfPoint2f corners) { MatOfPoint2f sortedCorners = sortCorners(corners); Size size = getRectangleSize(sortedCorners); Mat result = Mat.zeros(size, src.type()); MatOfPoint2f imageOutline = getOutline(result); Mat transformation = Imgproc.getPerspectiveTransform(sortedCorners, imageOutline); Imgproc.warpPerspective(src, result, transformation, size); return result; }
Example 2
Source File: CVProcessor.java From CVScanner with GNU General Public License v3.0 | 6 votes |
/** * * @param src - actual image * @param pts - points scaled up with respect to actual image * @return */ public static Mat fourPointTransform( Mat src , Point[] pts ) { Point tl = pts[0]; Point tr = pts[1]; Point br = pts[2]; Point bl = pts[3]; double widthA = Math.sqrt(Math.pow(br.x - bl.x, 2) + Math.pow(br.y - bl.y, 2)); double widthB = Math.sqrt(Math.pow(tr.x - tl.x, 2) + Math.pow(tr.y - tl.y, 2)); double dw = Math.max(widthA, widthB); int maxWidth = Double.valueOf(dw).intValue(); double heightA = Math.sqrt(Math.pow(tr.x - br.x, 2) + Math.pow(tr.y - br.y, 2)); double heightB = Math.sqrt(Math.pow(tl.x - bl.x, 2) + Math.pow(tl.y - bl.y, 2)); double dh = Math.max(heightA, heightB); int maxHeight = Double.valueOf(dh).intValue(); Mat doc = new Mat(maxHeight, maxWidth, CvType.CV_8UC4); Mat src_mat = new Mat(4, 1, CvType.CV_32FC2); Mat dst_mat = new Mat(4, 1, CvType.CV_32FC2); src_mat.put(0, 0, tl.x, tl.y, tr.x, tr.y, br.x, br.y, bl.x, bl.y); dst_mat.put(0, 0, 0.0, 0.0, dw, 0.0, dw, dh, 0.0, dh); Mat m = Imgproc.getPerspectiveTransform(src_mat, dst_mat); Imgproc.warpPerspective(src, doc, m, doc.size()); return doc; }
Example 3
Source File: MainActivity.java From SimpleDocumentScanner-Android with MIT License | 5 votes |
/** * NOTE: * Based off of http://www.pyimagesearch.com/2014/08/25/4-point-opencv-getperspective-transform-example/ * * @param src * @param pts * @return */ private Mat fourPointTransform(Mat src, Point[] pts) { double ratio = src.size().height / (double) MAX_HEIGHT; Point ul = pts[0]; Point ur = pts[1]; Point lr = pts[2]; Point ll = pts[3]; double widthA = Math.sqrt(Math.pow(lr.x - ll.x, 2) + Math.pow(lr.y - ll.y, 2)); double widthB = Math.sqrt(Math.pow(ur.x - ul.x, 2) + Math.pow(ur.y - ul.y, 2)); double maxWidth = Math.max(widthA, widthB) * ratio; double heightA = Math.sqrt(Math.pow(ur.x - lr.x, 2) + Math.pow(ur.y - lr.y, 2)); double heightB = Math.sqrt(Math.pow(ul.x - ll.x, 2) + Math.pow(ul.y - ll.y, 2)); double maxHeight = Math.max(heightA, heightB) * ratio; Mat resultMat = new Mat(Double.valueOf(maxHeight).intValue(), Double.valueOf(maxWidth).intValue(), CvType.CV_8UC4); Mat srcMat = new Mat(4, 1, CvType.CV_32FC2); Mat dstMat = new Mat(4, 1, CvType.CV_32FC2); srcMat.put(0, 0, ul.x * ratio, ul.y * ratio, ur.x * ratio, ur.y * ratio, lr.x * ratio, lr.y * ratio, ll.x * ratio, ll.y * ratio); dstMat.put(0, 0, 0.0, 0.0, maxWidth, 0.0, maxWidth, maxHeight, 0.0, maxHeight); Mat M = Imgproc.getPerspectiveTransform(srcMat, dstMat); Imgproc.warpPerspective(src, resultMat, M, resultMat.size()); srcMat.release(); dstMat.release(); M.release(); return resultMat; }
Example 4
Source File: PerspectiveTransform.java From opencv-fun with GNU Affero General Public License v3.0 | 5 votes |
private static void doProjection (ImgWindow projWnd) { if(corners.size() == 4) { Mat cornersMat = Converters.vector_Point2f_to_Mat(corners); Mat targetMat = Converters.vector_Point2f_to_Mat(target); trans = Imgproc.getPerspectiveTransform(cornersMat, targetMat); invTrans = Imgproc.getPerspectiveTransform(targetMat, cornersMat); proj = new Mat(); Imgproc.warpPerspective(img, proj, trans, new Size(img.cols(), img.rows())); if(projWnd.isClicked()) { perspPoints.add(new Point(projWnd.mouseX, projWnd.mouseY)); } } }
Example 5
Source File: WarpPerspectiveUtils.java From super-cloudops with Apache License 2.0 | 4 votes |
/** * 透视变换 * * @param src * @param points * @return */ public static Mat warpPerspective(Mat src, Point[] points) { // 点的顺序[左上 ,右上 ,右下 ,左下] List<Point> listSrcs = Arrays.asList(points[0], points[1], points[2], points[3]); Mat srcPoints = Converters.vector_Point_to_Mat(listSrcs, CvType.CV_32F); List<Point> listDsts = Arrays.asList(new Point(0, 0), new Point(src.width(), 0), new Point(src.width(), src.height()), new Point(0, src.height())); Mat dstPoints = Converters.vector_Point_to_Mat(listDsts, CvType.CV_32F); Mat perspectiveMmat = Imgproc.getPerspectiveTransform(srcPoints, dstPoints); Mat dst = new Mat(); Imgproc.warpPerspective(src, dst, perspectiveMmat, src.size(), Imgproc.INTER_AREA, 1, new Scalar(0)); return dst; }