drawing.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #!/usr/bin/env python
  2. '''
  3. This program demonstrates OpenCV drawing and text output functions by drawing different shapes and text strings
  4. Usage :
  5. python3 drawing.py
  6. Press any button to exit
  7. '''
  8. # Python 2/3 compatibility
  9. from __future__ import print_function
  10. import numpy as np
  11. import cv2 as cv
  12. # Drawing Lines
  13. def lines():
  14. for i in range(NUMBER*2):
  15. pt1, pt2 = [], []
  16. pt1.append(np.random.randint(x1, x2))
  17. pt1.append(np.random.randint(y1, y2))
  18. pt2.append(np.random.randint(x1, x2))
  19. pt2.append(np.random.randint(y1, y2))
  20. color = "%06x" % np.random.randint(0, 0xFFFFFF)
  21. color = tuple(int(color[i:i+2], 16) for i in (0, 2 ,4))
  22. arrowed = np.random.randint(0, 6)
  23. if (arrowed<3):
  24. cv.line(image, tuple(pt1), tuple(pt2), color, np.random.randint(1, 10), lineType)
  25. else:
  26. cv.arrowedLine(image, tuple(pt1), tuple(pt2), color, np.random.randint(1, 10), lineType)
  27. cv.imshow(wndname, image)
  28. if cv.waitKey(DELAY)>=0:
  29. return
  30. # Drawing Rectangle
  31. def rectangle():
  32. for i in range(NUMBER*2):
  33. pt1, pt2 = [], []
  34. pt1.append(np.random.randint(x1, x2))
  35. pt1.append(np.random.randint(y1, y2))
  36. pt2.append(np.random.randint(x1, x2))
  37. pt2.append(np.random.randint(y1, y2))
  38. color = "%06x" % np.random.randint(0, 0xFFFFFF)
  39. color = tuple(int(color[i:i+2], 16) for i in (0, 2 ,4))
  40. thickness = np.random.randint(-3, 10)
  41. marker = np.random.randint(0, 10)
  42. marker_size = np.random.randint(30, 80)
  43. if (marker > 5):
  44. cv.rectangle(image, tuple(pt1), tuple(pt2), color, max(thickness, -1), lineType)
  45. else:
  46. cv.drawMarker(image, tuple(pt1), color, marker, marker_size)
  47. cv.imshow(wndname, image)
  48. if cv.waitKey(DELAY)>=0:
  49. return
  50. # Drawing ellipse
  51. def ellipse():
  52. for i in range(NUMBER*2):
  53. center = []
  54. center.append(np.random.randint(x1, x2))
  55. center.append(np.random.randint(x1, x2))
  56. axes = []
  57. axes.append(np.random.randint(0, 200))
  58. axes.append(np.random.randint(0, 200))
  59. angle = np.random.randint(0, 180)
  60. color = "%06x" % np.random.randint(0, 0xFFFFFF)
  61. color = tuple(int(color[i:i+2], 16) for i in (0, 2 ,4))
  62. thickness = np.random.randint(-1, 9)
  63. cv.ellipse(image, tuple(center), tuple(axes), angle, angle-100, angle + 200, color, thickness, lineType)
  64. cv.imshow(wndname, image)
  65. if cv.waitKey(DELAY)>=0:
  66. return
  67. # Drawing Polygonal Curves
  68. def polygonal():
  69. for i in range(NUMBER):
  70. pt = [(0, 0)]*6
  71. pt = np.resize(pt, (2, 3, 2))
  72. pt[0][0][0] = np.random.randint(x1, x2)
  73. pt[0][0][1] = np.random.randint(y1, y2)
  74. pt[0][1][0] = np.random.randint(x1, x2)
  75. pt[0][1][1] = np.random.randint(y1, y2)
  76. pt[0][2][0] = np.random.randint(x1, x2)
  77. pt[0][2][1] = np.random.randint(y1, y2)
  78. pt[1][0][0] = np.random.randint(x1, x2)
  79. pt[1][0][1] = np.random.randint(y1, y2)
  80. pt[1][1][0] = np.random.randint(x1, x2)
  81. pt[1][1][1] = np.random.randint(y1, y2)
  82. pt[1][2][0] = np.random.randint(x1, x2)
  83. pt[1][2][1] = np.random.randint(y1, y2)
  84. color = "%06x" % np.random.randint(0, 0xFFFFFF)
  85. color = tuple(int(color[i:i+2], 16) for i in (0, 2 ,4))
  86. alist = []
  87. for k in pt[0]:
  88. alist.append(k)
  89. for k in pt[1]:
  90. alist.append(k)
  91. ppt = np.array(alist)
  92. cv.polylines(image, [ppt], True, color, thickness = np.random.randint(1, 10), lineType = lineType)
  93. cv.imshow(wndname, image)
  94. if cv.waitKey(DELAY) >= 0:
  95. return
  96. # fills an area bounded by several polygonal contours
  97. def fill():
  98. for i in range(NUMBER):
  99. pt = [(0, 0)]*6
  100. pt = np.resize(pt, (2, 3, 2))
  101. pt[0][0][0] = np.random.randint(x1, x2)
  102. pt[0][0][1] = np.random.randint(y1, y2)
  103. pt[0][1][0] = np.random.randint(x1, x2)
  104. pt[0][1][1] = np.random.randint(y1, y2)
  105. pt[0][2][0] = np.random.randint(x1, x2)
  106. pt[0][2][1] = np.random.randint(y1, y2)
  107. pt[1][0][0] = np.random.randint(x1, x2)
  108. pt[1][0][1] = np.random.randint(y1, y2)
  109. pt[1][1][0] = np.random.randint(x1, x2)
  110. pt[1][1][1] = np.random.randint(y1, y2)
  111. pt[1][2][0] = np.random.randint(x1, x2)
  112. pt[1][2][1] = np.random.randint(y1, y2)
  113. color = "%06x" % np.random.randint(0, 0xFFFFFF)
  114. color = tuple(int(color[i:i+2], 16) for i in (0, 2 ,4))
  115. alist = []
  116. for k in pt[0]:
  117. alist.append(k)
  118. for k in pt[1]:
  119. alist.append(k)
  120. ppt = np.array(alist)
  121. cv.fillPoly(image, [ppt], color, lineType)
  122. cv.imshow(wndname, image)
  123. if cv.waitKey(DELAY) >= 0:
  124. return
  125. # Drawing Circles
  126. def circles():
  127. for i in range(NUMBER):
  128. center = []
  129. center.append(np.random.randint(x1, x2))
  130. center.append(np.random.randint(x1, x2))
  131. color = "%06x" % np.random.randint(0, 0xFFFFFF)
  132. color = tuple(int(color[i:i+2], 16) for i in (0, 2 ,4))
  133. cv.circle(image, tuple(center), np.random.randint(0, 300), color, np.random.randint(-1, 9), lineType)
  134. cv.imshow(wndname, image)
  135. if cv.waitKey(DELAY) >= 0:
  136. return
  137. # Draws a text string
  138. def string():
  139. for i in range(NUMBER):
  140. org = []
  141. org.append(np.random.randint(x1, x2))
  142. org.append(np.random.randint(x1, x2))
  143. color = "%06x" % np.random.randint(0, 0xFFFFFF)
  144. color = tuple(int(color[i:i+2], 16) for i in (0, 2 ,4))
  145. cv.putText(image, "Testing text rendering", tuple(org), np.random.randint(0, 8), np.random.randint(0, 100)*0.05+0.1, color, np.random.randint(1, 10), lineType)
  146. cv.imshow(wndname, image)
  147. if cv.waitKey(DELAY) >= 0:
  148. return
  149. def string1():
  150. textsize = cv.getTextSize("OpenCV forever!", cv.FONT_HERSHEY_COMPLEX, 3, 5)
  151. org = (int((width - textsize[0][0])/2), int((height - textsize[0][1])/2))
  152. for i in range(0, 255, 2):
  153. image2 = np.array(image) - i
  154. cv.putText(image2, "OpenCV forever!", org, cv.FONT_HERSHEY_COMPLEX, 3, (i, i, 255), 5, lineType)
  155. cv.imshow(wndname, image2)
  156. if cv.waitKey(DELAY) >= 0:
  157. return
  158. if __name__ == '__main__':
  159. print(__doc__)
  160. wndname = "Drawing Demo"
  161. NUMBER = 100
  162. DELAY = 5
  163. width, height = 1000, 700
  164. lineType = cv.LINE_AA # change it to LINE_8 to see non-antialiased graphics
  165. x1, x2, y1, y2 = -width/2, width*3/2, -height/2, height*3/2
  166. image = np.zeros((height, width, 3), dtype = np.uint8)
  167. cv.imshow(wndname, image)
  168. cv.waitKey(DELAY)
  169. lines()
  170. rectangle()
  171. ellipse()
  172. polygonal()
  173. fill()
  174. circles()
  175. string()
  176. string1()
  177. cv.waitKey(0)
  178. cv.destroyAllWindows()