BasicGeometricDrawing.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import org.opencv.core.*;
  2. import org.opencv.core.Point;
  3. import org.opencv.highgui.HighGui;
  4. import org.opencv.imgproc.Imgproc;
  5. import java.util.*;
  6. import java.util.List;
  7. class GeometricDrawingRun{
  8. private static final int W = 400;
  9. public void run(){
  10. //! [create_images]
  11. /// Windows names
  12. String atom_window = "Drawing 1: Atom";
  13. String rook_window = "Drawing 2: Rook";
  14. /// Create black empty images
  15. Mat atom_image = Mat.zeros( W, W, CvType.CV_8UC3 );
  16. Mat rook_image = Mat.zeros( W, W, CvType.CV_8UC3 );
  17. //! [create_images]
  18. //! [draw_atom]
  19. /// 1. Draw a simple atom:
  20. /// -----------------------
  21. MyEllipse( atom_image, 90.0 );
  22. MyEllipse( atom_image, 0.0 );
  23. MyEllipse( atom_image, 45.0 );
  24. MyEllipse( atom_image, -45.0 );
  25. /// 1.b. Creating circles
  26. MyFilledCircle( atom_image, new Point( W/2, W/2) );
  27. //! [draw_atom]
  28. //! [draw_rook]
  29. /// 2. Draw a rook
  30. /// ------------------
  31. /// 2.a. Create a convex polygon
  32. MyPolygon( rook_image );
  33. //! [rectangle]
  34. /// 2.b. Creating rectangles
  35. Imgproc.rectangle( rook_image,
  36. new Point( 0, 7*W/8 ),
  37. new Point( W, W),
  38. new Scalar( 0, 255, 255 ),
  39. -1,
  40. 8,
  41. 0 );
  42. //! [rectangle]
  43. /// 2.c. Create a few lines
  44. MyLine( rook_image, new Point( 0, 15*W/16 ), new Point( W, 15*W/16 ) );
  45. MyLine( rook_image, new Point( W/4, 7*W/8 ), new Point( W/4, W ) );
  46. MyLine( rook_image, new Point( W/2, 7*W/8 ), new Point( W/2, W ) );
  47. MyLine( rook_image, new Point( 3*W/4, 7*W/8 ), new Point( 3*W/4, W ) );
  48. //! [draw_rook]
  49. /// 3. Display your stuff!
  50. HighGui.imshow( atom_window, atom_image );
  51. HighGui.moveWindow( atom_window, 0, 200 );
  52. HighGui.imshow( rook_window, rook_image );
  53. HighGui.moveWindow( rook_window, W, 200 );
  54. HighGui.waitKey( 0 );
  55. System.exit(0);
  56. }
  57. /// Function Declaration
  58. /**
  59. * @function MyEllipse
  60. * @brief Draw a fixed-size ellipse with different angles
  61. */
  62. //! [my_ellipse]
  63. private void MyEllipse( Mat img, double angle ) {
  64. int thickness = 2;
  65. int lineType = 8;
  66. int shift = 0;
  67. Imgproc.ellipse( img,
  68. new Point( W/2, W/2 ),
  69. new Size( W/4, W/16 ),
  70. angle,
  71. 0.0,
  72. 360.0,
  73. new Scalar( 255, 0, 0 ),
  74. thickness,
  75. lineType,
  76. shift );
  77. }
  78. //! [my_ellipse]
  79. /**
  80. * @function MyFilledCircle
  81. * @brief Draw a fixed-size filled circle
  82. */
  83. //! [my_filled_circle]
  84. private void MyFilledCircle( Mat img, Point center ) {
  85. int thickness = -1;
  86. int lineType = 8;
  87. int shift = 0;
  88. Imgproc.circle( img,
  89. center,
  90. W/32,
  91. new Scalar( 0, 0, 255 ),
  92. thickness,
  93. lineType,
  94. shift );
  95. }
  96. //! [my_filled_circle]
  97. /**
  98. * @function MyPolygon
  99. * @function Draw a simple concave polygon (rook)
  100. */
  101. //! [my_polygon]
  102. private void MyPolygon( Mat img ) {
  103. int lineType = 8;
  104. int shift = 0;
  105. /** Create some points */
  106. Point[] rook_points = new Point[20];
  107. rook_points[0] = new Point( W/4, 7*W/8 );
  108. rook_points[1] = new Point( 3*W/4, 7*W/8 );
  109. rook_points[2] = new Point( 3*W/4, 13*W/16 );
  110. rook_points[3] = new Point( 11*W/16, 13*W/16 );
  111. rook_points[4] = new Point( 19*W/32, 3*W/8 );
  112. rook_points[5] = new Point( 3*W/4, 3*W/8 );
  113. rook_points[6] = new Point( 3*W/4, W/8 );
  114. rook_points[7] = new Point( 26*W/40, W/8 );
  115. rook_points[8] = new Point( 26*W/40, W/4 );
  116. rook_points[9] = new Point( 22*W/40, W/4 );
  117. rook_points[10] = new Point( 22*W/40, W/8 );
  118. rook_points[11] = new Point( 18*W/40, W/8 );
  119. rook_points[12] = new Point( 18*W/40, W/4 );
  120. rook_points[13] = new Point( 14*W/40, W/4 );
  121. rook_points[14] = new Point( 14*W/40, W/8 );
  122. rook_points[15] = new Point( W/4, W/8 );
  123. rook_points[16] = new Point( W/4, 3*W/8 );
  124. rook_points[17] = new Point( 13*W/32, 3*W/8 );
  125. rook_points[18] = new Point( 5*W/16, 13*W/16 );
  126. rook_points[19] = new Point( W/4, 13*W/16 );
  127. MatOfPoint matPt = new MatOfPoint();
  128. matPt.fromArray(rook_points);
  129. List<MatOfPoint> ppt = new ArrayList<MatOfPoint>();
  130. ppt.add(matPt);
  131. Imgproc.fillPoly(img,
  132. ppt,
  133. new Scalar( 255, 255, 255 ),
  134. lineType,
  135. shift,
  136. new Point(0,0) );
  137. }
  138. //! [my_polygon]
  139. /**
  140. * @function MyLine
  141. * @brief Draw a simple line
  142. */
  143. //! [my_line]
  144. private void MyLine( Mat img, Point start, Point end ) {
  145. int thickness = 2;
  146. int lineType = 8;
  147. int shift = 0;
  148. Imgproc.line( img,
  149. start,
  150. end,
  151. new Scalar( 0, 0, 0 ),
  152. thickness,
  153. lineType,
  154. shift );
  155. }
  156. //! [my_line]
  157. }
  158. public class BasicGeometricDrawing {
  159. public static void main(String[] args) {
  160. // Load the native library.
  161. System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  162. new GeometricDrawingRun().run();
  163. }
  164. }