List of usage examples for org.opencv.imgproc Imgproc fillConvexPoly
public static void fillConvexPoly(Mat img, MatOfPoint points, Scalar color, int lineType, int shift)
From source file:org.akvo.caddisfly.sensor.colorimetry.strip.util.ResultUtil.java
License:Open Source License
@NonNull public static Mat createStripMat(@NonNull Mat mat, @NonNull Point centerPatch, boolean grouped, int maxWidth) { //done with lab schema, make rgb to show in image view // mat holds the strip image Imgproc.cvtColor(mat, mat, Imgproc.COLOR_Lab2RGB); int rightBorder = 0; double ratio; if (mat.width() < MIN_STRIP_WIDTH) { ratio = MAX_STRIP_HEIGHT / mat.height(); rightBorder = BORDER_SIZE;//w w w .jav a2s . com } else { ratio = (double) (maxWidth - 10) / (double) mat.width(); } Imgproc.resize(mat, mat, new Size(mat.width() * ratio, mat.height() * ratio)); Core.copyMakeBorder(mat, mat, BORDER_SIZE + ARROW_TRIANGLE_HEIGHT, BORDER_SIZE * 2, 0, rightBorder, Core.BORDER_CONSTANT, new Scalar(MAX_RGB_INT_VALUE, MAX_RGB_INT_VALUE, MAX_RGB_INT_VALUE, MAX_RGB_INT_VALUE)); // Draw an arrow to the patch // only draw if this is not a 'grouped' strip if (!grouped) { double x = centerPatch.x * ratio; double y = 0; MatOfPoint matOfPoint = new MatOfPoint(new Point((x - ARROW_TRIANGLE_LENGTH), y), new Point((x + ARROW_TRIANGLE_LENGTH), y), new Point(x, y + ARROW_TRIANGLE_HEIGHT), new Point((x - ARROW_TRIANGLE_LENGTH), y)); Imgproc.fillConvexPoly(mat, matOfPoint, DARK_GRAY, Core.LINE_AA, 0); } return mat; }
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))); ///* www . ja v a2 s .co 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;*/ }