OpenCV-Day-015:几何形状绘制

代码

import cv2 as cv
import numpy as np

src = np.zeros((512, 512, 3), dtype=np.uint8)
# 线宽为2
cv.rectangle(src, (100, 100), (300, 300), (255, 0, 0), 2, cv.LINE_8, 0)
cv.circle(src, (255, 255), 50, (0, 0, 255), 2, cv.LINE_8, 0)
cv.ellipse(src, (256, 256), (150, 50), 360, 0, 360, (0, 255, 0), 2, cv.LINE_8, 0)
cv.imshow('black', src)
# 线宽为-1,也即填充满
cv.rectangle(src, (100, 100), (300, 300), (255, 0, 0), -1, cv.LINE_8, 0)
cv.circle(src, (255, 255), 50, (0, 0, 255), -1, cv.LINE_8, 0)
cv.ellipse(src, (256, 256), (150, 50), 360, 0, 360, (0, 255, 0), -1, cv.LINE_8, 0)
cv.imshow('FILLED', src)
# 画线条
src[:, :, :] = 0
for i in range(100):
    x1 = np.random.rand() * 512
    y1 = np.random.rand() * 512
    x2 = np.random.rand() * 512
    y2 = np.random.rand() * 512

    b = np.random.randint(0, 256)
    g = np.random.randint(0, 256)
    r = np.random.randint(0, 256)
    cv.line(src, (np.int(x1), np.int(y1)), (np.int(x2), np.int(y2)), (b, g, r), 4, cv.LINE_8, 0)
    cv.imshow('multi_line', src)
    c = cv.waitKey(20)
    if c == 27:
        break
# 画方框
src[:, :, :] = 0
for i in range(100):
    x1 = np.random.rand() * 512
    y1 = np.random.rand() * 512
    x2 = np.random.rand() * 512
    y2 = np.random.rand() * 512

    b = np.random.randint(0, 256)
    g = np.random.randint(0, 256)
    r = np.random.randint(0, 256)
    cv.rectangle(src, (np.int(x1), np.int(y1)), (np.int(x2), np.int(y2)), (b, g, r), 4, cv.LINE_8, 0)
    cv.imshow('multi_rectangle', src)
    c = cv.waitKey(20)
    if c == 27:
        break
cv.waitKey(0)
cv.destroyAllWindows()

效果

在这里插入图片描述
在这里插入图片描述

解释

cv.rectangle(src, (100, 100), (300, 300), (255, 0, 0), 2, cv.LINE_8, 0)
cv.circle(src, (255, 255), 50, (0, 0, 255), 2, cv.LINE_8, 0)
cv.ellipse(src, (256, 256), (150, 50), 360, 0, 360, (0, 255, 0), 2, cv.LINE_8, 0)
参数顺序为:
原图,位置,颜色,线宽,cv.LINE_8,0

发布了197 篇原创文章 · 获赞 35 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/PoGeN1/article/details/90815369