opencv-OpenCV中的绘图功能

参考:

1、http://docs.opencv.org/3.3.0/  官方文档api

2、http://docs.opencv.org/3.3.0/d6/d00/tutorial_py_root.html 官方英文教程

3、https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html

4、https://github.com/makelove/OpenCV-Python-Tutorial# 进阶教程

5、https://docs.opencv.org/3.3.0/index.html  官方英文教程

6、https://github.com/abidrahmank/OpenCV2-Python-Tutorials

7、https://www.learnopencv.com/

8、http://answers.opencv.org/questions/ OpenCV论坛


注:安装的版本 opencv_python-3.3.0-cp36-cp36m-win_amd64.whl



参考:https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html



提示:opencv 颜色默认采样BGR格式

OpenCV中的绘图功能

目标

  • Learn to draw different geometric shapes with OpenCV
  • You will learn these functions : cv2.line()cv2.circle() , cv2.rectangle()cv2.ellipse(),cv2.putText() etc.

代码

In all the above functions, you will see some common arguments as given below:

  • img : 你想要画出形状的图像
  • color : 形状的颜色. for BGR, pass it as a tuple, eg: (255,0,0) for blue. For grayscale, just pass the scalar value.
  • thickness : 厚度 Thickness of the line or circle etc. 对于封闭的形状,如圆,-1 将填充这个形状 ,默认厚度为 1
  • lineType : Type of line, whether 8-connected, anti-aliased line etc. By default, it is 8-connected. cv2.LINE_AA gives anti-aliased line which looks great for curves.

画线

画线,只需给点线的起始坐标。 We will create a black image and draw a blue line on it from top-left to bottom-right corners.


import numpy as np
import cv2

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Draw a diagonal blue line with thickness of 5 px
img = cv2.line(img,(0,0),(511,511),(0,0,255),5,cv2.LINE_AA) # opencv里默认是 BGR格式 ;(255,0,0) 代表 Blue

cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()


画矩形

画矩形,需要矩形的上-左角和下-右角坐标。在图像的上-右角画一个绿色的矩形


import numpy as np
import cv2

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Draw a diagonal blue line with thickness of 5 px
# img = cv2.line(img,(0,0),(511,511),(0,0,255),5,cv2.LINE_AA) # opencv里默认是 BGR格式 ;(255,0,0) 代表 Blue
img = cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()


画圆

画圆,需要给点圆心和半径。

import numpy as np
import cv2

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Draw a diagonal blue line with thickness of 5 px
# img = cv2.line(img,(0,0),(511,511),(0,0,255),5,cv2.LINE_AA) # opencv里默认是 BGR格式 ;(255,0,0) 代表 Blue
img = cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)
img = cv2.circle(img,(447,63), 63, (0,0,255), -1) # -1 表示填充
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()



画椭圆

画椭圆,中心位置(x,y),轴长(长半轴,短半轴),旋转角(顺时针旋转),起始角度,终止角度(按顺时针方向画)

import numpy as np
import cv2

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Draw a diagonal blue line with thickness of 5 px
# img = cv2.line(img,(0,0),(511,511),(0,0,255),5,cv2.LINE_AA) # opencv里默认是 BGR格式 ;(255,0,0) 代表 Blue
img = cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)
# img = cv2.circle(img,(447,63), 63, (0,0,255), -1) # -1 表示填充
img = cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1)  # 这里的255 相当于(255,0,0) 
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()



画多边形

先要给定各个顶点的坐标

import numpy as np
import cv2

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Here we draw a small polygon of with four vertices in yellow color.
pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
pts = pts.reshape((-1,1,2))
img = cv2.polylines(img,[pts],True,(0,255,255),3)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

cv2.polylines() can be used to draw multiple lines.  将第三个参数设置成FALSE


在图像上写文本:

To put texts in images, you need specify following things.
  • 想要写的文本
  • 放置的位置 (i.e. bottom-left corner where data starts).
  • 字体类型 (Check cv2.putText() docs for supported fonts)
  • 字体的比例(大小) (specifies the size of font)
  • regular things like color, thickness, lineType etc. For better look, lineType = cv2.LINE_AA is recommended.
import numpy as np
import cv2

# Create a black image
img = np.zeros((512,512,3), np.uint8)

font = cv2.FONT_HERSHEY_SIMPLEX # 字体
cv2.putText(img,'OpenCV',(10,256), font, 4,(255,255,255),2,cv2.LINE_AA)

cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()


Exercises

  1. Try to create the logo of OpenCV using drawing functions available in OpenCV
import numpy as np
import cv2

# Create a black image
img = np.zeros((512,512,3), np.uint8)


img = cv2.ellipse(img,(128,256),(50,50),0,0,270,(0,255,0),15)
img = cv2.ellipse(img,(256,256),(50,50),-90,0,270,(255,0,0),15)
img = cv2.ellipse(img,(191,166),(50,50),135,0,270,(0,0,255),15)

font = cv2.FONT_HERSHEY_SIMPLEX # 字体
cv2.putText(img,'OpenCV',(10,386), font, 4,(255,255,255),2,cv2.LINE_AA)

cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值