opencv入门2:使用opencv简单画图

如opencv开发前的准备工作中所说,此系列文章是在学习Practical Python and OpenCV(点击下载)这本书的一些记录,发出来的文章跳过了第三章对RGB,以及numpy简单操作等介绍,请大家下载原书查看,在原书中对一下段落已进行翻译注释。文章系列完整展示代码点击下载

线和矩形

画线drawing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import numpy as np
import cv2
canvas = np.zeros((300, 300, 3), dtype = "uint8")
#使用np.zeros方法构造一个300,300,3通道的RGB的numpy array
#zeros方法用初始值为零填充数组中的每个元素。

#dtype 数据类型 8进制(
#Since we are representing our image as an RGB image with pixels in the range [0, 255],
#it’s important that we use an 8-bit unsigned integer, or uint8. There #are many
#other data types that we can use (common ones include 32-bit integers, and 32-bit
#or 64-bit floats), but we’ll mainly be using uint8 for the majority of the examples in this book)

green = (0,255,0) #定义一个表示绿色的BGR元组
cv2.line(canvas,(0,0),(300,300),green)
#line(canvas.point1,point2,color) 画布,起点,终点,颜色

cv2.imshow("Canvas", canvas)
cv2.waitKey(0)

red = (0,0,255)
cv2.line(canvas,(300,0),(0,300),red,3)
#line(canvas.point1,point2,color,thickness) 画布,起点,终点,颜色,线条宽度pixels

cv2.imshow("Canvas",canvas)
cv2.waitKey(0)

运行结果如下:

画矩形drawing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
cv2.rectangle(canvas,(10,10),(60,60),green)
#rectangle(canvas,point1,point2,color)画布,起点,终点,颜色
we are starting our rectangle at point (10, 10)
We decide to end our rectangle at(60, 60),
defining a region of 50 x 50 pixels

cv2.imshow("Canvas",canvas)
cv2.waitKey(0)

cv2.rectangle(canvas,(50,200),(200,225),red,5)
#rectangle(canvas,point1,point2,color,thickness) 画布,起点,终点,颜色,线条宽度pixels

cv2.imshow("Canvas",canvas)
cv2.waitKey(0)

以上我们只绘制了一个矩形的轮廓,如何绘制一个填充满的矩形?
  我们只需要通过设置厚度参数为:-1,如下

填充矩形drawing.py
1
2
3
4
blue = (255, 0 , 0)
cv2.rectangle(canvas,(200,50),(255,125),blue,-1)
cv2.imshow("Canvas",canvas)
cv2.waitKey(0)

运行结果如下:

画圆drawing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
canvas = np.zeros((300,300,3),dtype = "uint8")
(centerX,centerY) = (canvas.shape[1] // 2 ,canvas.shape[0] //2)
#图像canvas的高度可以在canvas.shape [0]和canvas.shape [1]的宽度中找到。shape [2]为图像channels
white = (255,255,255)

for r in range(0,175,25):
# range(start, stop[, step])
# 参数说明:
# start: 计数从 start 开始。默认是从 0 开始。例如range(5)等价于range(0, 5);
# end: 计数到 end 结束,但不包括 end。例如:range(0, 5) 是[0, 1, 2, 3, 4]没有5
# step:步长,默认为1。例如:range(0, 5) 等价于 range(0, 5, 1)
cv2.circle(canvas,(centerX,centerY),r,white)
# cv2.circle(canvas,(centerX,centerY),radius,color,thickness) 画布,起点,终点,颜色,线条宽度(为-1时填充整个图像)

运行结果如下:

画一个半径,中心点随机的填充多圆图

画圆drawing.py
1
2
3
4
5
6
7
8
for i in range(0, 25):
radius = np.random.randint(5, high =200)
使用np.random.randint 生成一个5200之间的随机数
color = np.random.randint(0, high =256,size=(3,)).tolist()
pt = np.random.randint(0, high=300, size = (2,))
cv2.circle(canvas,tuple(pt),radius,color,-1)
cv2.imshow("Canvas",canvas)
cv2.waitKey(0)

运行结果如下:

扩展

1
numpy.random.randint(low, high=None, size=None, dtype='l')

Return random integers from low (inclusive) to high (exclusive).
Return random integers from the “discrete uniform” distribution of the specified dtype in the “half-open” interval [low, high). If high is None (the default), then results are from [0, low).

Parameters:

  • low : intLowest (signed) integer to be drawn from the distribution (unless high=None, in which case this parameter is one above the highest such integer).
  • high : int, optionalIf provided, one above the largest (signed) integer to be drawn from the distribution (see above for behavior if high=None).
  • size : int or tuple of ints, optionalOutput shape. If the given shape is, e.g., (m, n, k), then m n k samples are drawn. Default is None, in which case a single value is returned.
  • dtype : dtype, optionalDesired dtype of the result. All dtypes are determined by their name, i.e., ‘int64’, ‘int’, etc, so byteorder is not available and a specific precision may have different C types depending on the platform. The default value is ‘np.int’.New in version 1.11.0.

Returns:

  • out : int or ndarray of intssize-shaped array of random integers from the appropriate distribution, or a single such random int if size not provided.
道虽迩,不行不至;事虽小,不为不成
坚持原创技术分享,您的支持将鼓励我继续创作!