本章使用opencv创建图形 采用numpy创建图纸

    1. import cv2 as cv
    2. import numpy as np
    3. from matplotlib import pyplot as plot
    4. # 绘制opencv
    5. def example_opencv():
    6. img = np.ones((512, 512, 3), np.uint8) * 255
    7. # 画椭圆
    8. # 图片 (圆心) (短轴长,长轴长),旋转角度,旋转角度,开口大小角度,(颜色),(线条粗细,-1为实心)
    9. cv.ellipse(img, (255, 100), (65, 65), 125, 0, 290, (0, 0, 255), -1)
    10. cv.circle(img, (255, 100), 30, (255, 255, 255), -1)
    11. cv.ellipse(img, (180, 220), (65, 65), 16, 0, 290, (0, 255, 0), -1)
    12. cv.circle(img, (180, 220), 30, (255, 255, 255), -1)
    13. cv.ellipse(img, (330, 220), (65, 65), 300, 0, 290, (255, 0, 0), -1)
    14. cv.circle(img, (330, 220), 30, (255, 255, 255), -1)
    15. font = cv.FONT_HERSHEY_SIMPLEX
    16. cv.putText(img, "OpenCV", (100, 400), font, 3, (0, 0, 0), 15, cv.LINE_AA)
    17. cv.imshow('study', img)
    18. cv.waitKey(0)
    19. cv.destroyAllWindows()
    20. def writeTest():
    21. # 创建一个500行 500列 包含3个元素的矩阵
    22. img = np.zeros((500, 500, 3), np.uint8)
    23. # cv.line第一个参数表示绘制图像的位置
    24. # 第二个参数表示绘画的开始点为 第三个参数表示结束的位置 第五个参数表示图像颜色 第六个参数表示线条的厚度
    25. # 横向表示x 竖着的表示y (0,300)表示从x的0开始,y的300开始绘制(200,200) 到x=200,y=200结束
    26. cv.line(img, (0, 300), (200, 200), (255, 0, 0), 5)
    27. # 绘制一个圆圈
    28. cv.circle(img, (200, 200), 40, (0, 0, 255), 5)
    29. font = cv.FONT_HERSHEY_SIMPLEX
    30. cv.putText(img, 'OpenCV', (200, 250), font, 2, (255, 255, 255), 2, cv.LINE_AA)
    31. plot.imshow(img)
    32. plot.show()
    33. cv.waitKey(0)
    34. cv.destroyAllWindows()
    35. if __name__ == '__main__':
    36. example_opencv()
    37. writeTest()