3.opencv绘图

12 篇文章 0 订阅
8 篇文章 1 订阅

在本教程的第一部分中,我们将简要回顾OpenCV的绘图功能。

然后,我们将配置我们的开发环境并查看我们的项目目录结构。

完成审查后,我们将继续实现两个Python脚本:

basic_drawing.py
image_drawing.py
这些脚本将帮助您了解如何使用OpenCV执行基本的绘图功能。

在本指南的最后,您将了解如何使用OpenCV绘制线,圆和矩形。

openCV中的绘图功能

OpenCV具有许多可用于绘制各种形状的绘制函数,包括不规则形状的多边形,但是您将看到的三个最常见的

openCV绘制函数是:

cv2.line:在图像上画一条线,从指定的(x,y)坐标开始,到另一个(x,y)坐标结束
cv2.circle:在由中心(x,y)-坐标和提供的半径指定的图像上绘制圆
cv2.rectangle:绘制由指定的图像上的矩形的左上角和右下角(X,Y)坐标-
今天我们将介绍这三个绘图功能。

但是,值得注意的是,存在更高级的OpenCV绘图功能,包括:

cv2.ellipse:在图像上绘制椭圆
cv2.polylines:绘制由一组(x,y) -coordinates指定的多边形的轮廓
cv2.fillPoly:绘制多边形,但不绘制轮廓,而是填充多边形
cv2.arrowedLine:绘制从起点(x,y)坐标到终点(x,y)坐标的箭头

代码结构

在这里插入图片描述
basic_drawing.py:初始化一个空的NumPy数组,并利用OpenCV绘制线,圆和矩形
image_drawing.py:负载 adrian.png 从磁盘上绘制,然后在图像上绘制(而不是一个空/空白的NumPy数组画布)。

以下代码示例运行在Google的Colab云端中:可作为学习参考,本项目代码见结尾代码下载和运行!

# import the necessary packages
from matplotlib import pyplot as plt
import numpy as np
import cv2

"""### Function to display images in Jupyter Notebooks and Google Colab"""

def plt_imshow(title, image):
	# convert the image frame BGR to RGB color space and display it
	image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
	plt.imshow(image)
	plt.title(title)
	plt.grid(False)
	plt.show()

"""### Implementing basic drawing functions with OpenCV"""

# 将画布初始化为具有3个通道的300x300像素的黑色背景图像
# 使用np.zeros300行300列的方法,

canvas = np.zeros((300, 300, 3), dtype="uint8")

# 产生300 x 300像素的图像。我们还为3个通道分配了空间-每个分别分配给红色,绿色和蓝色。
# 顾名思义,np.zeros 方法用初始值零填充数组中的每个元素。

# 其次,重要的是要引起您的注意 np.zeros 方法:数据类型, dtype。
# 由于我们将图像表示为像素范围为[0,255]的RGB图像,因此必须使用8位无符号整数,
# 或者uint8。我们可以使用许多其他数据类型(常见的数据类型包括32位整数以及32位和64位浮点数,
# 但我们将主要使用uint8

# draw a green line from the top-left corner of our canvas to the
# bottom-right
green = (0, 255, 0)
cv2.line(canvas, (0, 0), (300, 300), green)
plt_imshow("Canvas", canvas)

# draw a 3 pixel thick red line from the top-right corner to the
# bottom-left
red = (0, 0, 255)
cv2.line(canvas, (300, 0), (0, 300), red, 3)
plt_imshow("Canvas", canvas)

# cv2.line(image/canvas,(x0,y0),(x,y),color)
# 此方法的第一个参数是要绘制的图像。在这种情况下,这是我们的帆布。
# 第二个参数是该行的起点。我们选择开始我们从线左上角的图像角,在点(0,0)
# 我们还需要提供直线的终点(第三个参数)。我们定义我们的终点是(300,300) ,在右下的图像角。
# 最后一个参数是我们行的颜色(在本例中为绿色)。

# draw a green 50x50 pixel square, starting at 10x10 and ending at 60x60
# 画矩形
cv2.rectangle(canvas, (10, 10), (60, 60), green)
plt_imshow("Canvas", canvas)

# draw another rectangle, this one red with 5 pixel thickness
cv2.rectangle(canvas, (50, 200), (200, 225), red, 5)#参数5是线段粗细
plt_imshow("Canvas", canvas)

# draw a final rectangle (blue and filled in )
blue = (255, 0, 0)
cv2.rectangle(canvas, (200, 50), (225, 125), blue, -1)
plt_imshow("Canvas", canvas)

# 第一个参数是我们要在其上绘制矩形的图像。我们想利用我们的帆布,因此我们将其传递给方法。
# 第二个参数是矩形的起始(x,y)位置-在这里,我们从(10,10)点开始矩形。
# 然后,我们必须为矩形提供一个终点(x,y)。我们决定在(60,60)处结束矩形,定义一个50 x 50像素的区域
# 最后,最后一个参数是我们要绘制的矩形的颜色

# re-initialize our canvas as an empty array, then compute the
# center (x, y)-coordinates of the canvas
canvas = np.zeros((300, 300, 3), dtype="uint8")

#(centerX, centerY) = (canvas.shape[1] // 2, canvas.shape[0] // 2)
#或这样求图像中心点坐标
(h,w) = canvas.shape[:2]
(centerX, centerY) = (w // 2,h // 2)

white = (255, 255, 255)

# 我们从以下位置开始遍历几个半径值0 并结束于 150,
# 递增 25在每一步。注意这范围功能是排他性的,及不包括175; 因此,我们指定的停止值为175 而不是 150
for r in range(0, 175, 25):
	# draw a white circle with the current radius size
	cv2.circle(canvas, (centerX, centerY), r, white)
# 第一个参数是我们的 画布或已知图像,我们要在其上绘制圆的图像。
# 然后,我们需要提供要画圆的点。我们传入一个元组(centerX,centerY ) 这样我们的圈子将以图片的中心为中心。
# 第三个参数是半径R,我们希望绘制的圆。
# 最后,我们传入圆的颜色:在这种情况下,为白色。
# show our work of art
plt_imshow("Canvas", canvas)

# re-initialize our canvas once again
canvas = np.zeros((300, 300, 3), dtype="uint8")

# let's draw 25 random circles
for i in range(0, 25):
	# 随机产生5到200之间的半径大小,产生一个
	# 随机颜色,然后在画布上选择一个随机点
	# 圆圈将被绘制
	radius = np.random.randint(5, high=200)
  # 随机产生的颜色 
	color = np.random.randint(0, high=256, size=(3,)).tolist()
	# 随机产生的圆心点
  pt = np.random.randint(0, high=300, size=(2,))

	# draw our random circle on the canvas
	cv2.circle(canvas, tuple(pt), radius, color, -1)

# display our masterpiece to our screen
plt_imshow("Canvas", canvas)

"""### Drawing on images with OpenCV"""

# construct the argument parser and parse the arguments
#ap = argparse.ArgumentParser()
#ap.add_argument("-i", "--image", type=str, default="adrian.png",
#	help="path to the input image")
#args = vars(ap.parse_args())

# since we are using Jupyter Notebooks we can replace our argument
# parsing code with *hard coded* arguments and values
args = {
	"image": "adrian.png"
}

# load the input image from disk
image = cv2.imread(args["image"])

# draw a circle around my face, two filled in circles covering my
# eyes, and a rectangle over top of my mouth
cv2.circle(image, (168, 188), 90, (0, 0, 255), 2)
cv2.circle(image, (150, 164), 10, (0, 0, 255), -1)
cv2.circle(image, (192, 174), 10, (0, 0, 255), -1)
cv2.rectangle(image, (134, 200), (186, 218), (0, 0, 255), -1)

# show the output image
plt_imshow("Output", image)

代码下载和运行

在这里插入图片描述

链接: 代码下载

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

·菜鸟看世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值