drawing.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "opencv2/core.hpp"
  2. #include "opencv2/imgproc.hpp"
  3. #include "opencv2/highgui.hpp"
  4. #include <stdio.h>
  5. using namespace cv;
  6. static void help(char** argv)
  7. {
  8. printf("\nThis program demonstrates OpenCV drawing and text output functions.\n"
  9. "Usage:\n"
  10. " %s\n", argv[0]);
  11. }
  12. static Scalar randomColor(RNG& rng)
  13. {
  14. int icolor = (unsigned)rng;
  15. return Scalar(icolor&255, (icolor>>8)&255, (icolor>>16)&255);
  16. }
  17. int main(int /* argc */, char** argv)
  18. {
  19. help(argv);
  20. char wndname[] = "Drawing Demo";
  21. const int NUMBER = 100;
  22. const int DELAY = 5;
  23. int lineType = LINE_AA; // change it to LINE_8 to see non-antialiased graphics
  24. int i, width = 1000, height = 700;
  25. int x1 = -width/2, x2 = width*3/2, y1 = -height/2, y2 = height*3/2;
  26. RNG rng(0xFFFFFFFF);
  27. Mat image = Mat::zeros(height, width, CV_8UC3);
  28. imshow(wndname, image);
  29. waitKey(DELAY);
  30. for (i = 0; i < NUMBER * 2; i++)
  31. {
  32. Point pt1, pt2;
  33. pt1.x = rng.uniform(x1, x2);
  34. pt1.y = rng.uniform(y1, y2);
  35. pt2.x = rng.uniform(x1, x2);
  36. pt2.y = rng.uniform(y1, y2);
  37. int arrowed = rng.uniform(0, 6);
  38. if( arrowed < 3 )
  39. line( image, pt1, pt2, randomColor(rng), rng.uniform(1,10), lineType );
  40. else
  41. arrowedLine(image, pt1, pt2, randomColor(rng), rng.uniform(1, 10), lineType);
  42. imshow(wndname, image);
  43. if(waitKey(DELAY) >= 0)
  44. return 0;
  45. }
  46. for (i = 0; i < NUMBER * 2; i++)
  47. {
  48. Point pt1, pt2;
  49. pt1.x = rng.uniform(x1, x2);
  50. pt1.y = rng.uniform(y1, y2);
  51. pt2.x = rng.uniform(x1, x2);
  52. pt2.y = rng.uniform(y1, y2);
  53. int thickness = rng.uniform(-3, 10);
  54. int marker = rng.uniform(0, 10);
  55. int marker_size = rng.uniform(30, 80);
  56. if (marker > 5)
  57. rectangle(image, pt1, pt2, randomColor(rng), MAX(thickness, -1), lineType);
  58. else
  59. drawMarker(image, pt1, randomColor(rng), marker, marker_size );
  60. imshow(wndname, image);
  61. if(waitKey(DELAY) >= 0)
  62. return 0;
  63. }
  64. for (i = 0; i < NUMBER; i++)
  65. {
  66. Point center;
  67. center.x = rng.uniform(x1, x2);
  68. center.y = rng.uniform(y1, y2);
  69. Size axes;
  70. axes.width = rng.uniform(0, 200);
  71. axes.height = rng.uniform(0, 200);
  72. double angle = rng.uniform(0, 180);
  73. ellipse( image, center, axes, angle, angle - 100, angle + 200,
  74. randomColor(rng), rng.uniform(-1,9), lineType );
  75. imshow(wndname, image);
  76. if(waitKey(DELAY) >= 0)
  77. return 0;
  78. }
  79. for (i = 0; i< NUMBER; i++)
  80. {
  81. Point pt[2][3];
  82. pt[0][0].x = rng.uniform(x1, x2);
  83. pt[0][0].y = rng.uniform(y1, y2);
  84. pt[0][1].x = rng.uniform(x1, x2);
  85. pt[0][1].y = rng.uniform(y1, y2);
  86. pt[0][2].x = rng.uniform(x1, x2);
  87. pt[0][2].y = rng.uniform(y1, y2);
  88. pt[1][0].x = rng.uniform(x1, x2);
  89. pt[1][0].y = rng.uniform(y1, y2);
  90. pt[1][1].x = rng.uniform(x1, x2);
  91. pt[1][1].y = rng.uniform(y1, y2);
  92. pt[1][2].x = rng.uniform(x1, x2);
  93. pt[1][2].y = rng.uniform(y1, y2);
  94. const Point* ppt[2] = {pt[0], pt[1]};
  95. int npt[] = {3, 3};
  96. polylines(image, ppt, npt, 2, true, randomColor(rng), rng.uniform(1,10), lineType);
  97. imshow(wndname, image);
  98. if(waitKey(DELAY) >= 0)
  99. return 0;
  100. }
  101. for (i = 0; i< NUMBER; i++)
  102. {
  103. Point pt[2][3];
  104. pt[0][0].x = rng.uniform(x1, x2);
  105. pt[0][0].y = rng.uniform(y1, y2);
  106. pt[0][1].x = rng.uniform(x1, x2);
  107. pt[0][1].y = rng.uniform(y1, y2);
  108. pt[0][2].x = rng.uniform(x1, x2);
  109. pt[0][2].y = rng.uniform(y1, y2);
  110. pt[1][0].x = rng.uniform(x1, x2);
  111. pt[1][0].y = rng.uniform(y1, y2);
  112. pt[1][1].x = rng.uniform(x1, x2);
  113. pt[1][1].y = rng.uniform(y1, y2);
  114. pt[1][2].x = rng.uniform(x1, x2);
  115. pt[1][2].y = rng.uniform(y1, y2);
  116. const Point* ppt[2] = {pt[0], pt[1]};
  117. int npt[] = {3, 3};
  118. fillPoly(image, ppt, npt, 2, randomColor(rng), lineType);
  119. imshow(wndname, image);
  120. if(waitKey(DELAY) >= 0)
  121. return 0;
  122. }
  123. for (i = 0; i < NUMBER; i++)
  124. {
  125. Point center;
  126. center.x = rng.uniform(x1, x2);
  127. center.y = rng.uniform(y1, y2);
  128. circle(image, center, rng.uniform(0, 300), randomColor(rng),
  129. rng.uniform(-1, 9), lineType);
  130. imshow(wndname, image);
  131. if(waitKey(DELAY) >= 0)
  132. return 0;
  133. }
  134. for (i = 1; i < NUMBER; i++)
  135. {
  136. Point org;
  137. org.x = rng.uniform(x1, x2);
  138. org.y = rng.uniform(y1, y2);
  139. putText(image, "Testing text rendering", org, rng.uniform(0,8),
  140. rng.uniform(0,100)*0.05+0.1, randomColor(rng), rng.uniform(1, 10), lineType);
  141. imshow(wndname, image);
  142. if(waitKey(DELAY) >= 0)
  143. return 0;
  144. }
  145. Size textsize = getTextSize("OpenCV forever!", FONT_HERSHEY_COMPLEX, 3, 5, 0);
  146. Point org((width - textsize.width)/2, (height - textsize.height)/2);
  147. Mat image2;
  148. for( i = 0; i < 255; i += 2 )
  149. {
  150. image2 = image - Scalar::all(i);
  151. putText(image2, "OpenCV forever!", org, FONT_HERSHEY_COMPLEX, 3,
  152. Scalar(i, i, 255), 5, lineType);
  153. imshow(wndname, image2);
  154. if(waitKey(DELAY) >= 0)
  155. return 0;
  156. }
  157. waitKey();
  158. return 0;
  159. }