自己是一个菜鸡,希望通过写博客的方式提升自己,最近正好接触到了opencv,想把学习路程以博客的形式记录下来,也算是学习opencv的一种动力吧,好吧,话不多说,干就完了!
基本上参数形式都是固定的:
img: 指明要在哪个图片上画
pt1,pt2,center是三个点,都是tuple类型,radius是正整数
color: 指明什么颜色
thickness: 线的厚度,单位是像素
lineType: 线(曲线)的类型
import cv2
import numpy as np
import os
our_image = np.zeros([300, 300, 3], dtype='uint8') # 构造的是一个初始化为全黑的RGB图片噢
# 两条直线
cv2.line(our_image, (0, 0), (299, 299), (255, 255, 255), 3)
cv2.line(our_image, (0, 299), (299, 0), (255, 255, 255), 3)
# 两个矩形
cv2.rectangle(our_image, (5, 125), (35, 175), (0, 255, 0), 3)
cv2.rectangle(our_image, (264, 125), (294, 175), (0, 255, 0), -1) # thickness是负数的时候表示实心
# 四个圆
h, w = our_image.shape[:2] #获取图像高,宽
center = (w // 2, h // 2)
for radius in range(0, 101, 25):
cv2.circle(our_image, center, radius, (0, 0, 255), 3)
cv2.imwrite('operation.jpg', our_image)
our_image = np.zeros((300, 300, 3), dtype='uint8')
# 圆心,半径,颜色全部随机~画25个圆。
for num in range(25):
radius = np.random.randint(5, 200) # 圆心在[5, 199]之间
# 不知道为什么tuple()不行,只能tolist()...
color = np.random.randint(0, 256, size=(3,)).tolist() # 颜色三个数的数值都在[0, 255]上
centers = np.random.randint(0, 300, size=(2, )) # 圆心两个数的数值都在[0, 299]上
cv2.circle(our_image, tuple(centers), radius, color, -1)
cv2.imwrite('operation.jpg', our_image)