Path: blob/master/samples/java/tutorial_code/ImgProc/BasicGeometricDrawing/BasicGeometricDrawing.java
16344 views
import org.opencv.core.*;1import org.opencv.core.Point;2import org.opencv.highgui.HighGui;3import org.opencv.imgproc.Imgproc;45import java.util.*;6import java.util.List;78class GeometricDrawingRun{910private static final int W = 400;1112public void run(){13//! [create_images]14/// Windows names15String atom_window = "Drawing 1: Atom";16String rook_window = "Drawing 2: Rook";1718/// Create black empty images19Mat atom_image = Mat.zeros( W, W, CvType.CV_8UC3 );20Mat rook_image = Mat.zeros( W, W, CvType.CV_8UC3 );21//! [create_images]2223//! [draw_atom]24/// 1. Draw a simple atom:25/// -----------------------26MyEllipse( atom_image, 90.0 );27MyEllipse( atom_image, 0.0 );28MyEllipse( atom_image, 45.0 );29MyEllipse( atom_image, -45.0 );3031/// 1.b. Creating circles32MyFilledCircle( atom_image, new Point( W/2, W/2) );33//! [draw_atom]3435//! [draw_rook]36/// 2. Draw a rook37/// ------------------38/// 2.a. Create a convex polygon39MyPolygon( rook_image );4041//! [rectangle]42/// 2.b. Creating rectangles43Imgproc.rectangle( rook_image,44new Point( 0, 7*W/8 ),45new Point( W, W),46new Scalar( 0, 255, 255 ),47-1,488,490 );50//! [rectangle]5152/// 2.c. Create a few lines53MyLine( rook_image, new Point( 0, 15*W/16 ), new Point( W, 15*W/16 ) );54MyLine( rook_image, new Point( W/4, 7*W/8 ), new Point( W/4, W ) );55MyLine( rook_image, new Point( W/2, 7*W/8 ), new Point( W/2, W ) );56MyLine( rook_image, new Point( 3*W/4, 7*W/8 ), new Point( 3*W/4, W ) );57//! [draw_rook]5859/// 3. Display your stuff!60HighGui.imshow( atom_window, atom_image );61HighGui.moveWindow( atom_window, 0, 200 );62HighGui.imshow( rook_window, rook_image );63HighGui.moveWindow( rook_window, W, 200 );6465HighGui.waitKey( 0 );66System.exit(0);67}6869/// Function Declaration7071/**72* @function MyEllipse73* @brief Draw a fixed-size ellipse with different angles74*/75//! [my_ellipse]76private void MyEllipse( Mat img, double angle ) {77int thickness = 2;78int lineType = 8;79int shift = 0;8081Imgproc.ellipse( img,82new Point( W/2, W/2 ),83new Size( W/4, W/16 ),84angle,850.0,86360.0,87new Scalar( 255, 0, 0 ),88thickness,89lineType,90shift );91}92//! [my_ellipse]93/**94* @function MyFilledCircle95* @brief Draw a fixed-size filled circle96*/97//! [my_filled_circle]98private void MyFilledCircle( Mat img, Point center ) {99int thickness = -1;100int lineType = 8;101int shift = 0;102103Imgproc.circle( img,104center,105W/32,106new Scalar( 0, 0, 255 ),107thickness,108lineType,109shift );110}111//! [my_filled_circle]112/**113* @function MyPolygon114* @function Draw a simple concave polygon (rook)115*/116//! [my_polygon]117private void MyPolygon( Mat img ) {118int lineType = 8;119int shift = 0;120121/** Create some points */122Point[] rook_points = new Point[20];123rook_points[0] = new Point( W/4, 7*W/8 );124rook_points[1] = new Point( 3*W/4, 7*W/8 );125rook_points[2] = new Point( 3*W/4, 13*W/16 );126rook_points[3] = new Point( 11*W/16, 13*W/16 );127rook_points[4] = new Point( 19*W/32, 3*W/8 );128rook_points[5] = new Point( 3*W/4, 3*W/8 );129rook_points[6] = new Point( 3*W/4, W/8 );130rook_points[7] = new Point( 26*W/40, W/8 );131rook_points[8] = new Point( 26*W/40, W/4 );132rook_points[9] = new Point( 22*W/40, W/4 );133rook_points[10] = new Point( 22*W/40, W/8 );134rook_points[11] = new Point( 18*W/40, W/8 );135rook_points[12] = new Point( 18*W/40, W/4 );136rook_points[13] = new Point( 14*W/40, W/4 );137rook_points[14] = new Point( 14*W/40, W/8 );138rook_points[15] = new Point( W/4, W/8 );139rook_points[16] = new Point( W/4, 3*W/8 );140rook_points[17] = new Point( 13*W/32, 3*W/8 );141rook_points[18] = new Point( 5*W/16, 13*W/16 );142rook_points[19] = new Point( W/4, 13*W/16 );143144MatOfPoint matPt = new MatOfPoint();145matPt.fromArray(rook_points);146147List<MatOfPoint> ppt = new ArrayList<MatOfPoint>();148ppt.add(matPt);149150Imgproc.fillPoly(img,151ppt,152new Scalar( 255, 255, 255 ),153lineType,154shift,155new Point(0,0) );156}157//! [my_polygon]158/**159* @function MyLine160* @brief Draw a simple line161*/162//! [my_line]163private void MyLine( Mat img, Point start, Point end ) {164int thickness = 2;165int lineType = 8;166int shift = 0;167168Imgproc.line( img,169start,170end,171new Scalar( 0, 0, 0 ),172thickness,173lineType,174shift );175}176//! [my_line]177}178179public class BasicGeometricDrawing {180public static void main(String[] args) {181// Load the native library.182System.loadLibrary(Core.NATIVE_LIBRARY_NAME);183new GeometricDrawingRun().run();184}185}186187188