跳到主要内容

OpenCV 知识速查表

本页面提供 OpenCV 常用函数和代码片段的快速查阅。

安装与导入

# 安装
pip install opencv-python
pip install opencv-contrib-python # 包含扩展模块

# 导入
import cv2
import numpy as np

图像基本操作

读取、显示、保存

# 读取图像
image = cv2.imread('image.jpg') # 彩色
image = cv2.imread('image.jpg', 0) # 灰度
image = cv2.imread('image.jpg', -1) # 包含透明通道

# 显示图像
cv2.imshow('窗口名', image)
cv2.waitKey(0) # 无限等待
cv2.waitKey(1000) # 等待 1000ms
cv2.destroyAllWindows()

# 保存图像
cv2.imwrite('output.jpg', image)
cv2.imwrite('output.jpg', image, [cv2.IMWRITE_JPEG_QUALITY, 90])

图像属性

height, width, channels = image.shape    # 图像尺寸
dtype = image.dtype # 数据类型
total_pixels = image.size # 像素总数
dimensions = image.ndim # 维度数

# 访问像素
pixel = image[y, x] # BGR 值
blue = image[y, x, 0]
green = image[y, x, 1]
red = image[y, x, 2]

# 访问区域
roi = image[y1:y2, x1:x2]

颜色空间转换

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)

几何变换

# 缩放
resized = cv2.resize(image, (width, height))
resized = cv2.resize(image, None, fx=0.5, fy=0.5)

# 翻转
flipped_h = cv2.flip(image, 1) # 水平
flipped_v = cv2.flip(image, 0) # 垂直
flipped_b = cv2.flip(image, -1) # 双向

# 旋转
matrix = cv2.getRotationMatrix2D(center, angle, scale)
rotated = cv2.warpAffine(image, matrix, (width, height))

# 平移
matrix = np.float32([[1, 0, tx], [0, 1, ty]])
translated = cv2.warpAffine(image, matrix, (width, height))

# 仿射变换
matrix = cv2.getAffineTransform(pts1, pts2)
result = cv2.warpAffine(image, matrix, (width, height))

# 透视变换
matrix = cv2.getPerspectiveTransform(pts1, pts2)
result = cv2.warpPerspective(image, matrix, (width, height))

图像滤波

# 均值滤波
blurred = cv2.blur(image, (5, 5))

# 高斯滤波
blurred = cv2.GaussianBlur(image, (5, 5), 0)

# 中值滤波
blurred = cv2.medianBlur(image, 5)

# 双边滤波
blurred = cv2.bilateralFilter(image, 9, 75, 75)

# 自定义卷积
result = cv2.filter2D(image, -1, kernel)

边缘检测

# Canny
edges = cv2.Canny(image, threshold1, threshold2)

# Sobel
sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)

# Laplacian
edges = cv2.Laplacian(image, cv2.CV_64F)

阈值化

# 简单阈值
ret, binary = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)

# 自适应阈值
adaptive = cv2.adaptiveThreshold(image, 255,
cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)

# Otsu
ret, binary = cv2.threshold(image, 0, 255,
cv2.THRESH_BINARY + cv2.THRESH_OTSU)

形态学操作

kernel = np.ones((5, 5), np.uint8)

# 腐蚀
eroded = cv2.erode(image, kernel, iterations=1)

# 膨胀
dilated = cv2.dilate(image, kernel, iterations=1)

# 开运算
opened = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)

# 闭运算
closed = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)

# 形态学梯度
gradient = cv2.morphologyEx(image, cv2.MORPH_GRADIENT, kernel)

轮廓检测

# 查找轮廓
contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# 绘制轮廓
cv2.drawContours(image, contours, -1, (0, 255, 0), 2)

# 轮廓特征
area = cv2.contourArea(contour)
perimeter = cv2.arcLength(contour, True)
x, y, w, h = cv2.boundingRect(contour)
rect = cv2.minAreaRect(contour)
(cx, cy), radius = cv2.minEnclosingCircle(contour)

# 轮廓近似
epsilon = 0.02 * cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, epsilon, True)

视频操作

# 读取视频
cap = cv2.VideoCapture('video.mp4')
cap = cv2.VideoCapture(0) # 摄像头

# 获取属性
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

# 读取帧
ret, frame = cap.read()

# 写入视频
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('output.mp4', fourcc, fps, (width, height))
out.write(frame)

# 释放
cap.release()
out.release()

特征检测

# Harris 角点
dst = cv2.cornerHarris(gray, 2, 3, 0.04)

# Shi-Tomasi 角点
corners = cv2.goodFeaturesToTrack(gray, 100, 0.01, 10)

# SIFT
sift = cv2.SIFT_create()
kp, des = sift.detectAndCompute(gray, None)

# ORB
orb = cv2.ORB_create()
kp, des = orb.detectAndCompute(gray, None)

特征匹配

# 暴力匹配
bf = cv2.BFMatcher(cv2.NORM_L2, crossCheck=True)
matches = bf.match(des1, des2)

# KNN 匹配
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k=2)

# FLANN 匹配
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1, des2, k=2)

# 绘制匹配
result = cv2.drawMatches(img1, kp1, img2, kp2, matches, None)

目标检测

# Haar 级联
cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
faces = cascade.detectMultiScale(gray, 1.1, 5)

# HOG 行人检测
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
boxes, weights = hog.detectMultiScale(image)

# 模板匹配
result = cv2.matchTemplate(image, template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)

相机标定

# 查找棋盘格角点
ret, corners = cv2.findChessboardCorners(gray, pattern_size, None)

# 亚像素精确化
corners = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)

# 相机标定
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, image_size, None, None)

# 去畸变
undistorted = cv2.undistort(image, mtx, dist)

# 姿态估计
ret, rvec, tvec = cv2.solvePnP(objpoints, imgpoints, mtx, dist)

DNN 模块

# 加载模型
net = cv2.dnn.readNetFromCaffe('deploy.prototxt', 'model.caffemodel')
net = cv2.dnn.readNetFromONNX('model.onnx')
net = cv2.dnn.readNetFromTensorflow('model.pb')

# 创建 blob
blob = cv2.dnn.blobFromImage(image, 1.0, (224, 224), (104, 177, 123))

# 推理
net.setInput(blob)
output = net.forward()

绘图函数

# 直线
cv2.line(image, (x1, y1), (x2, y2), color, thickness)

# 矩形
cv2.rectangle(image, (x1, y1), (x2, y2), color, thickness)

# 圆
cv2.circle(image, center, radius, color, thickness)

# 椭圆
cv2.ellipse(image, center, axes, angle, startAngle, endAngle, color, thickness)

# 多边形
cv2.polylines(image, [pts], isClosed, color, thickness)

# 文字
cv2.putText(image, text, org, font, scale, color, thickness)

常用常量

# 读取模式
cv2.IMREAD_COLOR # 1
cv2.IMREAD_GRAYSCALE # 0
cv2.IMREAD_UNCHANGED # -1

# 阈值类型
cv2.THRESH_BINARY
cv2.THRESH_BINARY_INV
cv2.THRESH_TRUNC
cv2.THRESH_TOZERO

# 插值方法
cv2.INTER_NEAREST
cv2.INTER_LINEAR
cv2.INTER_CUBIC
cv2.INTER_AREA

# 边界填充
cv2.BORDER_CONSTANT
cv2.BORDER_REPLICATE
cv2.BORDER_REFLECT

参考链接