List of usage examples for org.opencv.imgproc Imgproc getAffineTransform
public static Mat getAffineTransform(MatOfPoint2f src, MatOfPoint2f dst)
From source file:OCV_GetAffineTransform.java
License:Open Source License
@Override public void run(ImageProcessor ip) { MatOfPoint2f matPt_src = new MatOfPoint2f(); MatOfPoint2f matPt_dst = new MatOfPoint2f(); matPt_src.fromList(lstPt_src);/*from ww w .jav a 2s.c o m*/ matPt_dst.fromList(lstPt_dst); Mat mat = Imgproc.getAffineTransform(matPt_src, matPt_dst); if (mat == null || mat.rows() <= 0 || mat.cols() <= 0) { IJ.showMessage("Output is null or error"); return; } ResultsTable rt = OCV__LoadLibrary.GetResultsTable(true); for (int i = 0; i < 2; i++) { rt.incrementCounter(); rt.addValue("Column01", String.valueOf(mat.get(i, 0)[0])); rt.addValue("Column02", String.valueOf(mat.get(i, 1)[0])); rt.addValue("Column03", String.valueOf(mat.get(i, 2)[0])); } rt.show("Results"); }
From source file:samples.LWF.java
private static void affine(Mat mat, double[][] from, double[][] to, double[][] coeficients, Mat lienzo, double escala, double gap) { // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. // http://stackoverflow.com/questions/10100715/opencv-warping-from-one-triangle-to-another // https://www.learnopencv.com/warp-one-triangle-to-another-using-opencv-c-python/ // http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/warp_affine/warp_affine.html MatOfPoint2f src_pf = new MatOfPoint2f(new Point(from[0][0], from[0][1]), new Point(from[1][0], from[1][1]), new Point(from[2][0], from[2][1])); MatOfPoint2f dst_pf = new MatOfPoint2f(new Point(to[0][0], to[0][1]), new Point(to[1][0], to[1][1]), new Point(to[2][0], to[2][1])); // https://www.learnopencv.com/warp-one-triangle-to-another-using-opencv-c-python/#download //how do I set up the position numbers in MatOfPoint2f here? // Mat perspective_matrix = Imgproc.getAffineTransform(src_pf, dst_pf); Rect r1 = Imgproc.boundingRect(new MatOfPoint(new Point(from[0][0], from[0][1]), new Point(from[1][0], from[1][1]), new Point(from[2][0], from[2][1]))); Rect r2 = Imgproc.boundingRect(new MatOfPoint(new Point(to[0][0], to[0][1]), new Point(to[1][0], to[1][1]), new Point(to[2][0], to[2][1]))); MatOfPoint2f tri1Cropped = new MatOfPoint2f(new Point(from[0][0] - r1.x, from[0][1] - r1.y), new Point(from[1][0] - r1.x, from[1][1] - r1.y), new Point(from[2][0] - r1.x, from[2][1] - r1.y)); MatOfPoint tri2CroppedInt = new MatOfPoint(new Point(to[0][0] - r2.x, to[0][1] - r2.y), new Point(to[1][0] - r2.x, to[1][1] - r2.y), new Point(to[2][0] - r2.x, to[2][1] - r2.y)); MatOfPoint2f tri2Cropped = new MatOfPoint2f(new Point((to[0][0] - r2.x), (to[0][1] - r2.y)), new Point((to[1][0] - r2.x), (to[1][1] - r2.y)), new Point((to[2][0] - r2.x), (to[2][1] - r2.y))); // for (int i = 0; i < 3; i++) { // // tri1Cropped.push_back(new MatOfPoint(new Point(from[i][0] - r1.x, from[i][1] - r1.y))); // new Point( from[i][0] - r1.x, from[i][1]- r1.y) ); // //tri2Cropped.push_back(new MatOfPoint(new Point(to[i][0] - r2.x, to[i][1] - r2.y))); ////from w w w .j a va2 s . c o m // // fillConvexPoly needs a vector of Point and not Point2f // // tri2CroppedInt.push_back(new MatOfPoint2f(new Point((int) (to[i][0] - r2.x), (int) (to[i][1] - r2.y)))); // // } // Apply warpImage to small rectangular patches Mat img1Cropped = mat.submat(r1); //img1(r1).copyTo(img1Cropped); // Given a pair of triangles, find the affine transform. Mat warpMat = Imgproc.getAffineTransform(tri1Cropped, tri2Cropped); // Mat bbb = warpMat.mul(tri1Cropped); // // System.out.println( warpMat.dump() ); // System.out.println( tri2Cropped.dump() ); // System.out.println( bbb.dump() ); // Apply the Affine Transform just found to the src image Mat img2Cropped = Mat.zeros(r2.height, r2.width, img1Cropped.type()); Imgproc.warpAffine(img1Cropped, img2Cropped, warpMat, img2Cropped.size(), 0, Imgproc.INTER_LINEAR, new Scalar(Core.BORDER_TRANSPARENT)); //, 0, Imgproc.INTER_LINEAR, new Scalar(Core.BORDER_REFLECT_101)); // Get mask by filling triangle Mat mask = Mat.zeros(r2.height, r2.width, CvType.CV_8UC3); ///CV_8U CV_32FC3 Imgproc.fillConvexPoly(mask, tri2CroppedInt, new Scalar(1.0, 1.0, 1.0), 16, 0); // Copy triangular region of the rectangular patch to the output image // Core.multiply(img2Cropped,mask, img2Cropped); // // Core.multiply(mask, new Scalar(-1), mask); // Core.(mask,new Scalar(gap), mask); //Core.multiply(lienzo.submat(r2), (new Scalar(1.0,1.0,1.0)). - Core.multiply(mask,), lienzo.submat(r2)); // img2(r2) = img2(r2) + img2Cropped; // Core.subtract(Mat.ones(mask.height(), mask.width(), CvType.CV_8UC3), mask, mask); // Mat ff = ; // este Core.multiply(img2Cropped, mask, img2Cropped); //Core.multiply(lienzo.submat(r2), mask , lienzo.submat(r2)); Core.add(lienzo.submat(r2), img2Cropped, lienzo.submat(r2)); /* Mat bb = new Mat(mat, r2); bb.setTo(new Scalar(rnd.nextInt(),rnd.nextInt(),rnd.nextInt())); Core.multiply(bb,mask, bb); Core.multiply(lienzo.submat(r2), mask , lienzo.submat(r2)); Core.add(lienzo.submat(r2), bb, lienzo.submat(r2)); */ // lienzo.submat(r2).setTo(new Scalar(rnd.nextInt(),rnd.nextInt(),rnd.nextInt())); // // Imgproc.fillConvexPoly(lienzo, new MatOfPoint( // new Point(to[0][0] , to[0][1]), // new Point(to[1][0] , to[1][1]), // new Point(to[2][0] , to[2][1] )), new Scalar(1,1,1)); // img2Cropped.copyTo(lienzo); // return; // http://stackoverflow.com/questions/14111716/how-to-set-a-mask-image-for-grabcut-in-opencv // Imgproc.warpAffine(mat, lienzo, perspective_matrix, lienzo.size()); // Imgproc.getAffineTransform(null, null); /* // Find bounding rectangle for each triangle Rect r1 = boundingRect(tri1); Rect r2 = boundingRect(tri2); // Offset points by left top corner of the respective rectangles vector<Point2f> tri1Cropped, tri2Cropped; vector<Point> tri2CroppedInt; for(int i = 0; i < 3; i++) { tri1Cropped.push_back( Point2f( tri1[i].x - r1.x, tri1[i].y - r1.y) ); tri2Cropped.push_back( Point2f( tri2[i].x - r2.x, tri2[i].y - r2.y) ); // fillConvexPoly needs a vector of Point and not Point2f tri2CroppedInt.push_back( Point((int)(tri2[i].x - r2.x), (int)(tri2[i].y - r2.y)) ); } // Apply warpImage to small rectangular patches Mat img1Cropped; img1(r1).copyTo(img1Cropped); // Given a pair of triangles, find the affine transform. Mat warpMat = getAffineTransform( tri1Cropped, tri2Cropped ); // Apply the Affine Transform just found to the src image Mat img2Cropped = Mat::zeros(r2.height, r2.width, img1Cropped.type()); warpAffine( img1Cropped, img2Cropped, warpMat, img2Cropped.size(), INTER_LINEAR, BORDER_REFLECT_101); // Get mask by filling triangle Mat mask = Mat::zeros(r2.height, r2.width, CV_32FC3); fillConvexPoly(mask, tri2CroppedInt, Scalar(1.0, 1.0, 1.0), 16, 0); // Copy triangular region of the rectangular patch to the output image multiply(img2Cropped,mask, img2Cropped); multiply(img2(r2), Scalar(1.0,1.0,1.0) - mask, img2(r2)); img2(r2) = img2(r2) + img2Cropped;*/ }