美文网首页
基本绘图 — OpenCV& Python

基本绘图 — OpenCV& Python

作者: WZChan | 来源:发表于2017-10-10 09:42 被阅读0次
import numpy as np
import cv2

canvas = np.zeros((400, 600, 3), dtype=np.uint8) + 255
cv2.line(canvas, (300, 0), (300, 399), (0, 0, 0), 2)
cv2.line(canvas, (300, 149), (599, 149), (0, 0, 0), 2)
cv2.circle(canvas, (200, 300), 75, (0, 0, 255), 5)
cv2.rectangle(canvas, (20, 240), (100, 360), (255, 0, 0), thickness=3)

triangles = np.array([
    [(200, 240), (145, 333), (255, 333)],
    [(60, 180), (20, 237), (100, 237)]
])
cv2.fillPoly(canvas, triangles, (0, 255, 0))

phi = 4 * np.pi / 5
rotations = [
    [[np.cos(i * phi), -np.sin(i * phi)] , [i * np.sin(phi), np.cos(i * phi)]] for i in range(1,5)
]
pentagram = np.array([[[[0, -1]] + [np.dot(m, (0, -1)) for m in rotations]]], dtype=np.float)
pentagram = np.round(pentagram * 80 + np.array([160, 120])).astype((np.int))

cv2.polylines(canvas, pentagram, True, (0, 255, 255), 9)

for x in range(302, 600):
    color_pixel = np.array([[[round(180*float(x-302) / 298), 255, 255]]], dtype=np.uint8)
    line_color = [int(c) for c in cv2.cvtColor(color_pixel, cv2.COLOR_HSV2BGR)[0][0]]
    cv2.line(canvas, (x, 0), (x, 147), line_color)

np.random.seed(42)
n_pts = 30
pts_x = np.random.randint(310, 590, n_pts)
pts_y = np.random.randint(160, 390, n_pts)
pts = zip(pts_x, pts_y)

for pt in pts:
    pt_color = [int(c) for c in np.random.randint(0, 255, 3)]
    cv2.circle(canvas, pt, 3, pt_color, 5)
cv2.putText(canvas,
            'Python-OpenCV Drawing Example',
            (5, 15),
            cv2.FONT_HERSHEY_SIMPLEX,
            0.5,
            (0, 0, 0),
            1)
cv2.imshow('Example of basic drawing functions', canvas)
cv2.waitKey()
Example of basic drawing fucntions.jpg

相关文章

网友评论

      本文标题:基本绘图 — OpenCV& Python

      本文链接:https://www.haomeiwen.com/subject/iktvyxtx.html