# demonstrate drawing into a numpy array with opencv
import numpy
import matplotlib.pyplot as plt
import cv2
# start with blank image
img = numpy.zeros((128, 128), numpy.uint8) + 255
# show image
plt.imshow(img, cmap='gray', vmin=0, vmax=255)
<matplotlib.image.AxesImage at 0x7f7a42e60ac8>
# start with blank image
img = numpy.zeros((128,128), numpy.uint8) + 255
# draw line
cv2.line(img, (30, 30), (90, 90), 0, 2, cv2.LINE_AA)
# show image
plt.imshow(img, cmap='gray', vmin=0, vmax=255)
<matplotlib.image.AxesImage at 0x7f7a405fcba8>
# start with blank image
img = numpy.zeros((128, 128), numpy.uint8) + 255
# draw unfilled circle
cv2.circle(img, (50,50), 20, 0, 2, cv2.LINE_AA)
# draw filled circle
cv2.circle(img, (80, 80), 20, 0, -1, cv2.LINE_AA)
# show image
plt.imshow(img, cmap='gray', vmin=0, vmax=255)
<matplotlib.image.AxesImage at 0x7f7a405669b0>
# start with blank image
img = numpy.zeros((128, 128, 3), numpy.uint8) + 255
# draw red line
cv2.line(img, (30, 30), (90, 90), (255, 0, 0), 2, cv2.LINE_AA)
# draw filled circle
cv2.circle(img, (80, 80), 20, (0, 0, 255), -1, cv2.LINE_AA)
# show image
plt.imshow(img)
<matplotlib.image.AxesImage at 0x7f7a404d00b8>
# start with blank image
img = numpy.zeros((128, 128, 3), numpy.uint8) + 255
for midy in range(60, 90, 10):
startx = 20
lengthx = 80
xvalues = numpy.linspace(0,lengthx,100)
yvalues = numpy.sin(xvalues/8) * 10
for (x,y) in zip(xvalues, yvalues):
x = startx + int(x)
y = midy - int(y)
cv2.circle(img, (x, y), 1, (0, 200, 0), -1)
pass
pass
# show image
plt.imshow(img)
<matplotlib.image.AxesImage at 0x7f7a3e80e160>
# start with blank image
img = numpy.zeros((128, 128, 3), numpy.uint8) + 255
angles = numpy.linspace(0,2*numpy.pi,10)
xvalues = numpy.cos(angles) * 20
yvalues = numpy.sin(angles) * 20
for (x,y) in zip(xvalues, yvalues):
x = int(x)
y = int(y)
cv2.line(img, (64, 64), (64 + x, 64 - y), (250, 250, 0), 2, cv2.LINE_AA)
pass
# show image
plt.imshow(img)
<matplotlib.image.AxesImage at 0x7f7a3e512780>