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