命令行安装方式 - mac
brew install [email protected]
pip3 install numpy matplotlib opencv_python
// numpy - 矩阵操作
// matplotlib - 显示
// 测试是否安装成功(import 无报错信息 表示安装成功)
MacintoshdeMacBook-Pro:~ Jartin$ python3
Python 3.9.1 (default, Jan 8 2021, 17:15:36)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> import matplotlib
>>> import cv2
import cv2;
# 创建窗口
cv2.namedWindow('new', cv2.WINDOW_NORMAL);
# WINDOW_AUTOSIZE 不可以resize
# WINDOW_NORMAL 可以resize
# 设定窗口大小
cv2.resizeWindow('new', 1920, 1080)
# 展示窗口
cv2.imshow('new', 0);
# 键盘和鼠标的监听 实现
key = cv2.waitKey(0)
if(key == 'q'):
exit()
# 销毁所有窗口
cv2.destroyAllWindows();
import cv2;
# 图片加载
cv2.namedWindow('img', cv2.WINDOW_NORMAL);
img = cv2.imread('./jobs.jpeg');
cv2.imshow('img', img)
key = cv2.waitKey(0)
if (key & 0xFF == ord('q')):
cv2.destroyAllWindows();
import cv2;
# 使用opencv 的 videocapture采集视频数据
# 创建窗口
cv2.namedWindow('video', cv2.WINDOW_NORMAL);
cv2.resizeWindow('video', 640, 480);
# 获取视频设备
cap = cv2.VideoCapture(0);
while True:
# 从摄像头读取视频帧
ret, frame = cap.read();
# 将视频帧在窗口中显示
cv2.imshow('video', frame);
# 等待键盘事件,如果q,退出 waitKey(1)帧率越小 视频越流畅
key = cv2.waitKey(1);
if (key & 0xFF == ord('q')):
break;
# 释放VideoCapture
cap.release();
cv2.destroyAllWindows();
import cv2;
# 读取视频文件
# 创建窗口
cv2.namedWindow('video', cv2.WINDOW_NORMAL);
cv2.resizeWindow('video', 640, 480);
# 获取视频设备 / 从视频文件中读取视频帧
cap = cv2.VideoCapture(0);
cap = cv2.VideoCapture('/Users/v_yangyinglong/Desktop/opencv/zl.mp4');
while True:
# 从摄像头读取视频帧
ret, frame = cap.read();
# 将视频帧在窗口中显示
cv2.imshow('video', frame);
# 等待键盘事件 如果q退出
key = cv2.waitKey(1);
if (key & 0xFF == ord('q')):
break;
# 释放rediocap
cap.release();
cv2.destroyAllWindows();
import cv2;
# 视频录制
# 创建videowrite为写多媒体文件
fourcc = cv2.VideoWriter_fourcc(*'MJPG');
# 路径、 forcc、帧率、分辨率
vw = cv2.VideoWriter('./out.mp4', fourcc, 25, (1280, 720))
# 创建窗口
cv2.namedWindow('video', cv2.WINDOW_NORMAL);
cv2.resizeWindow('video', 640, 360);
# 获取视频设备
cap = cv2.VideoCapture(0);
while True:
# 从摄像头读取数据
ret, frame = cap.read();
# 将视频帧在窗口显示
cv2.imshow('video', frame);
# 写数据到多媒体文件
vw.write(frame);
# 等待键盘事件 如果为q退出
key = cv2.waitKey(1);
if (key & 0xFF == ord('q')):
break;
# 释放videocapture
cap.release();
# 释放videowrite资源
vw.release();
cv2.destroyAllWindows();
import cv2;
import numpy as np;
# 控制鼠标 鼠标回调函数
def mouse_callback(event, x, y, flags, userdata):
print(event, x, y, flags, userdata);
# 创建窗口
cv2.namedWindow('mouse', cv2.WINDOW_NORMAL);
cv2.resizeWindow('mouse', 640, 360);
# 设置鼠标回调
cv2.setMouseCallback('mouse', mouse_callback, '123');
# 显示窗口和背景
# numpy图片组件、np.zeros显示图片、(高, 宽, bgr3种数组)、np.uint8像素类型
img = np.zeros((360, 640, 3), np.uint8);
while True:
cv2.imshow('mouse', img);
key = cv2.waitKey(1);
if key & 0xFF == ord('q'):
break;
cv2.destroyAllWindows();
import cv2;
import numpy as np;
def callback():
pass;
# 创建窗口
cv2.namedWindow('trackbar', cv2.WINDOW_NORMAL);
# 创建trackbar 名字、窗口名字、默认当前值、最大值、回调方法
cv2.createTrackbar('R', 'trackbar', 0, 255, callback);
cv2.createTrackbar('G', 'trackbar', 0, 255, callback);
cv2.createTrackbar('B', 'trackbar', 0, 255, callback);
# 创建图片
img = np.zeros((480, 640, 3), np.uint8);
while True:
# 读取值
r = cv2.getTrackbarPos('R', 'trackbar');
g = cv2.getTrackbarPos('G', 'trackbar');
b = cv2.getTrackbarPos('B', 'trackbar');
img[:] = [b, g, r];
cv2.imshow('trackbar', img);
key = cv2.waitKey(10);
if key & 0xFF == ord('q'):
break;
cv2.destroyAllWindows();
import cv2;
import numpy as np;
def callback():
pass;
# 创建窗口
cv2.namedWindow('trackbar', cv2.WINDOW_NORMAL);
# 创建trackbar 名字、窗口名字、默认当前值、最大值、回调方法
cv2.createTrackbar('R', 'trackbar', 0, 255, callback);
cv2.createTrackbar('G', 'trackbar', 0, 255, callback);
cv2.createTrackbar('B', 'trackbar', 0, 255, callback);
# 创建图片
img = np.zeros((480, 640, 3), np.uint8);
while True:
# 读取值
r = cv2.getTrackbarPos('R', 'trackbar');
g = cv2.getTrackbarPos('G', 'trackbar');
b = cv2.getTrackbarPos('B', 'trackbar');
img[:] = [b, g, r];
cv2.imshow('trackbar', img);
key = cv2.waitKey(10);
if key & 0xFF == ord('q'):
break;
cv2.destroyAllWindows();
HSV Hue:色相、红色、蓝色、绿色
Saturation:饱和度,颜色的纯度
Value:明度
YUV 视频中常用
对像素的描述 Y数据 UV彩色信息
import cv2;
def callback():
pass;
cv2.namedWindow('color', cv2.WINDOW_NORMAL);
img = cv2.imread('./jobs.jpeg');
colorspaces = [cv2.COLOR_BGR2RGBA, cv2.COLOR_BGR2BGRA, cv2.COLOR_BGR2GRAY, cv2.COLOR_BGR2HSV, cv2.COLOR_BGR2YUV];
cv2.createTrackbar('curcolor', 'color', 0, 4, callback);
while True:
index = cv2.getTrackbarPos('curcolor', 'color');
# 颜色空间转换API
cvt_img = cv2.cvtColor(img, colorspaces[index]);
cv2.imshow('color', img)
key = cv2.waitKey(10);
if key & 0xFF == ord('q'):
break;
cv2.destroyAllWindows();
import numpy as np
# np.zeros((行的个数, 列的个数, 通道数/层数), 矩阵中的数据类型);
c = np.zeros((480, 640, 3), np.uint8);
print(c)
矩阵的检索与赋值
ROI [y1:y2,x1:x2]
Mat是什么
是矩阵,可以理解为一张图 有head 与 body组成
import cv2
import numpy as np
img = cv2.imread('./1.jpeg');
# 浅拷贝
img2 = img;
# 深拷贝
img3 = img.copy();
img[10:100, 10:100] = [0, 0, 255]
cv2.imshow('img', img)
cv2.imshow('img2', img2)
cv2.imshow('img3', img3)
cv2.waitKey(0);
import cv2;
import numpy as numpy
img = cv2.imread('./1.jpeg')
# (1200, 1920, 3) 高度、长度、通道数
print(img.shape)
# 6912000 高度 * 长度 * 3
print(img.size)
# uint8 图像中每个元素的位深
print(img.dtype)
import cv2;
import numpy as np
# 通道的分离与合并
img = np.zeros((480, 640, 3), np.uint8)
b,g,r = cv2.split(img);
b[10:100, 10:100] = 255;
g[10:100, 10:100] = 255;
img2 = cv2.merge((b, g, r))
cv2.imshow('img', img);
cv2.imshow('b', b);
cv2.imshow('g', g);
cv2.imshow('img2', img2);
cv2.waitKey(0);
import cv2;
import numpy as np;
# 绘制直线
img = cv2.imread('./1.jpeg')
cv2.line(img, (10, 20), (10, 900), (0,0,255), 20, 4);
cv2.line(img, (100, 12), (400, 100), (0,0,255), 20, 14);
cv2.imshow('img', img);
key = cv2.waitKey(0);
import cv2;
import numpy as np;
img = cv2.imread('./1.jpeg');
# cv2.ellipse(img, (500, 500), )
# 画圆
cv2.circle(img, (520, 440), 100, (0, 0, 255), 10)
cv2.circle(img, (520, 440), 5, (0, 0, 255), 10)
# 画椭圆
# 度是按顺时针计算的
cv2.ellipse(img, (520, 440), (100, 50), 15, 0, 360, (0, 0, 255), -1)
# 画多边形
pts = np.array(((320, 10), (150, 100), (450, 100)), np.int32)
cv2.polylines(img, [pts], True, (0, 0, 255))
# 填充
cv2.fillPoly(img, [pts], (255, 255, 0))
# 绘制文本
cv2.putText(img, 'Yulanlan', (800, 400), cv2.FONT_HERSHEY_PLAIN, 9, (255, 0, 0))
cv2.imshow('draw', img);
cv2.waitKey(0)