show | version | enable_checker |
---|---|---|
step |
1.0 |
true |
- 上次研究了图像的各种类型的变型方法
- 旋转
- 翻转
- 透视
- 倾斜
- 可以自己从零开始画些什么吗?
- 如果用传统方法设置像素的话
- 应该如何描述这个线段呢?
import numpy as np
import cv2
canvas = np.zeros((300, 300,3), np.uint8)
canvas = cv2.line(canvas,(50,50),(200,200),(255,0,0),5)
cv2.imshow("Line",canvas)
cv2.waitKey()
cv2.destroyAllWindows()
- 使用cv2.line方法
import numpy as np
import cv2
canvas = np.zeros((300, 300,3), np.uint8)
canvas = cv2.rectangle(canvas,(50,50),(200,200),(255,255,0),25)
cv2.imshow("rectangle",canvas)
cv2.waitKey()
cv2.destroyAllWindows()
- 效果
- 如果用传统方法设置像素的话
- 该如何描述这个矩形呢?
import numpy as np
import cv2
canvas = np.zeros((300, 300,3), np.uint8)
canvas = cv2.rectangle(canvas,(50,50),(200,200),(255,255,0),-1)
cv2.imshow("Line",canvas)
cv2.waitKey()
cv2.destroyAllWindows()
- 结果
- 如何用传统方法描述这个矩形?
- 可以遍历整个矩阵
- 也可以设置区域颜色
import numpy as np
import cv2
canvas = np.zeros((300, 300,3), np.uint8)
canvas = cv2.ellipse(canvas, (150, 100), (150, 50), 0, 0, 360, (0, 255, 0), 2, cv2.LINE_8, 0)
cv2.imshow("Line",canvas)
cv2.waitKey()
cv2.destroyAllWindows()
- 绘制效果
- 参数具体什么意思呢?
# -*- coding:utf-8 -*-
import cv2
import numpy as np
#创建黑色图像
img = np.zeros((256,256,3), np.uint8)
#绘制椭圆
#椭圆中心(120,100) 长轴和短轴为(100,50)
#偏转角度为20
#圆弧起始角的角度0 圆弧终结角的角度360
#颜色(255,0,255) 线条粗细2
cv2.ellipse(img, (120, 100), (100, 50), 20, 0, 360, (255, 0, 255), 2)
#显示图像
cv2.imshow("ellipse", img)
#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()
- 结果
- 可以填充吗?
import cv2
import numpy as np
#创建黑色图像
img = np.zeros((256,256,3), np.uint8)
#绘制椭圆
#椭圆中心(120,100) 长轴和短轴为(100,50)
#偏转角度为20
#圆弧起始角的角度0 圆弧终结角的角度360
#颜色(255,0,255) 线条粗细2
cv2.ellipse(img, (120, 100), (100, 50), 30, 0, 360, (255, 0, 255), -1)
#显示图像
cv2.imshow("ellipse", img)
#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()
- 效果
- 可以只要一段圆弧吗?
- 应该可以设置各种角度
- 从30度 到 150度 总共120度
import numpy as np
import cv2
canvas = np.zeros((300, 300,3), np.uint8)
canvas = cv2.ellipse(canvas, (150, 100), (150, 50), 0, 30, 150, (0, 255, 0), 2, cv2.LINE_8, 0)
cv2.imshow("Line",canvas)
cv2.waitKey()
cv2.destroyAllWindows()
- 结果
- 在此基础上再旋转45度
import numpy as np
import cv2
canvas = np.zeros((300, 300,3), np.uint8)
canvas = cv2.ellipse(canvas, (150, 100), (150, 50), 0, 30, 150, (0, 255, 0), 2, cv2.LINE_8, 0)
cv2.imshow("Line",canvas)
cv2.waitKey()
cv2.destroyAllWindows()
- 效果
- 圆弧 是 非常实用的 图形
import numpy as np
import cv2
canvas = np.zeros((300, 300,3), np.uint8)
canvas = cv2.circle(canvas,(50,50),40,(255,255,0),25)
canvas = cv2.circle(canvas,(150,150),40,(255,255,0),-1)
cv2.imshow("circle",canvas)
cv2.waitKey()
cv2.destroyAllWindows()
- 效果
- 如果圆形半径为-1的话
- 圆形为实心圆
import numpy as np
import cv2
canvas = np.zeros((300, 300,3), np.uint8)
for r in range(0,150,30):
canvas = cv2.circle(canvas,(150,150),r,(255,255,0),5)
cv2.imshow("circle",canvas)
cv2.waitKey()
cv2.destroyAllWindows()
- 效果
import numpy as np
import cv2
frame = np.zeros((800, 600,3), np.uint8)
width = 5
cv2.circle(frame, (150, 150), 50, (255, 0, 0), width, 4)
cv2.circle(frame, (260, 150), 50, (100, 100, 100), width, 4)
cv2.circle(frame, (370, 150), 50, (0, 0, 255), width, 4)
cv2.circle(frame, (205, 220), 50, (0, 255, 255), width, 4) # ^M
cv2.circle(frame, (315, 220), 50, (0, 255, 0), width, 4) #
cv2.imshow("circle",frame)
cv2.waitKey()
cv2.destroyAllWindows()
- 效果
import numpy as np
import cv2
canvas = np.zeros((480, 640,3), np.uint8)
canvas = cv2.rectangle(canvas,(200,100),(400,400),(0,255,255),-1)
canvas = cv2.circle(canvas,(250,200),40,(255,255,255),-1)
canvas = cv2.circle(canvas,(350,200),40,(255,255,255),-1)
canvas = cv2.circle(canvas,(250,200),10,(0,0,0),-1)
canvas = cv2.circle(canvas,(350,200),10,(0,0,0),-1)
cv2.imshow("man",canvas)
cv2.waitKey()
cv2.destroyAllWindows()
- 人物效果
- 这次我们学习了基本绘制
- 线段
- 圆形
- 矩形
- 但是能够将图形绘制和之前的概念关联起来吗?
- 图像
- 滚动条
- 文字
- 如何关联呢?🤔
- 下次再说👋