OpenCV 使用教程

从入门到精通的全面学习指南,帮助您快速掌握OpenCV开发技能

教程分类

按照难度和主题分类的教程,选择适合您的学习路径

入门教程

适合初学者的基础教程,从零开始学习OpenCV的核心概念和基本操作。

  • 环境搭建与配置
  • 图像读取与显示
  • 基础绘图功能
  • 色彩空间转换
查看教程

进阶教程

适合有一定基础的开发者,深入学习图像处理和计算机视觉的核心算法。

  • 图像滤波与增强
  • 边缘检测与轮廓分析
  • 形态学操作
  • 特征检测与匹配
查看教程

高级教程

适合经验丰富的开发者,学习高级计算机视觉技术和深度学习应用。

  • 目标检测与跟踪
  • 人脸识别与分析
  • 深度学习与OpenCV
  • 3D重建与立体视觉
查看教程

入门教程

从零开始学习OpenCV的基础知识

1 OpenCV环境搭建与配置

本教程将指导您在不同操作系统上安装和配置OpenCV开发环境,包括Windows、Linux和macOS。

Windows安装步骤

  1. 下载OpenCV预编译库
  2. 解压到本地目录
  3. 配置环境变量
  4. 在Visual Studio中配置项目

Python安装步骤

  1. 确保已安装Python 3.7+
  2. 使用pip安装OpenCV:pip install opencv-python
  3. 安装扩展模块:pip install opencv-contrib-python
  4. 验证安装:import cv2

代码示例

# Python示例:验证OpenCV安装
import cv2

# 打印OpenCV版本
print(f"OpenCV版本: {cv2.__version__}")

# 读取并显示图像
img = cv2.imread('test.jpg')
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
预计学习时间:30分钟
阅读完整教程

2 图像读取与基本操作

学习如何使用OpenCV读取、显示、保存图像,以及进行基本的图像操作。

图像读取

OpenCV提供了多种方式读取图像:

  • 彩色图像:cv2.imread('image.jpg', cv2.IMREAD_COLOR)
  • 灰度图像:cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
  • 包含Alpha通道:cv2.imread('image.png', cv2.IMREAD_UNCHANGED)

图像显示

使用HighGUI模块显示图像:

  • 创建窗口:cv2.namedWindow('Window Name')
  • 显示图像:cv2.imshow('Window Name', image)
  • 等待按键:cv2.waitKey(0)
  • 销毁窗口:cv2.destroyAllWindows()

代码示例

# Python示例:图像基本操作
import cv2
import numpy as np

# 读取图像
img = cv2.imread('test.jpg', cv2.IMREAD_COLOR)

# 检查图像是否成功读取
if img is None:
    print("无法读取图像")
    exit()

# 显示图像信息
print(f"图像形状: {img.shape}")  # (高度, 宽度, 通道数)
print(f"图像数据类型: {img.dtype}")

# 转换为灰度图像
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 调整图像大小
resized_img = cv2.resize(img, (640, 480))

# 保存图像
cv2.imwrite('output.jpg', img)
cv2.imwrite('output_gray.jpg', gray_img)

# 显示图像
cv2.imshow('Original Image', img)
cv2.imshow('Grayscale Image', gray_img)
cv2.imshow('Resized Image', resized_img)

# 等待按键
cv2.waitKey(0)
cv2.destroyAllWindows()
预计学习时间:45分钟
阅读完整教程

3 基础绘图功能

学习如何在图像上绘制各种图形,如线条、矩形、圆形和文本。

基本图形绘制

  • 绘制线段:cv2.line()
  • 绘制矩形:cv2.rectangle()
  • 绘制圆形:cv2.circle()
  • 绘制椭圆:cv2.ellipse()

文本绘制

  • 绘制文本:cv2.putText()
  • 字体选择:cv2.FONT_HERSHEY_SIMPLEX
  • 文本颜色:BGR格式,如(255, 0, 0)表示蓝色
  • 文本粗细:整数,默认为1

代码示例

# Python示例:基础绘图功能
import cv2
import numpy as np

# 创建一个黑色图像
img = np.zeros((512, 512, 3), np.uint8)

# 绘制蓝色线段(参数:图像, 起点, 终点, 颜色, 线宽)
cv2.line(img, (0, 0), (511, 511), (255, 0, 0), 5)

# 绘制绿色矩形(参数:图像, 左上角, 右下角, 颜色, 线宽)
cv2.rectangle(img, (384, 0), (510, 128), (0, 255, 0), 3)

# 绘制红色圆形(参数:图像, 圆心, 半径, 颜色, 线宽)
# -1表示填充圆形
cv2.circle(img, (447, 63), 63, (0, 0, 255), -1)

# 绘制椭圆(参数:图像, 中心, (长轴长度, 短轴长度), 角度, 起始角度, 结束角度, 颜色, 线宽)
cv2.ellipse(img, (256, 256), (100, 50), 0, 0, 180, (255, 0, 0), -1)

# 绘制多边形
pts = np.array([[10, 5], [20, 30], [70, 20], [50, 10]], np.int32)
pts = pts.reshape((-1, 1, 2))
cv2.polylines(img, [pts], True, (0, 255, 255))

# 添加文本(参数:图像, 文本, 位置, 字体, 字体大小, 颜色, 线宽, 线型)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, 'OpenCV Drawing Demo', (10, 500), font, 1, (255, 255, 255), 2, cv2.LINE_AA)

# 显示图像
cv2.imshow('Drawing Demo', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
预计学习时间:40分钟
阅读完整教程

进阶教程

深入学习图像处理和计算机视觉的核心算法

1 图像滤波与增强

学习各种图像滤波技术,包括线性滤波和非线性滤波,以及图像增强方法。

线性滤波

  • 均值滤波:cv2.blur()
  • 高斯滤波:cv2.GaussianBlur()
  • 盒式滤波:cv2.boxFilter()
  • 双边滤波:cv2.bilateralFilter()

非线性滤波

  • 中值滤波:cv2.medianBlur()
  • 双边滤波:cv2.bilateralFilter()
  • 自适应中值滤波:自定义实现
  • 引导滤波:cv2.ximgproc.guidedFilter()

代码示例

# Python示例:图像滤波与增强
import cv2
import numpy as np

# 读取图像
img = cv2.imread('lena.jpg')

# 转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 添加高斯噪声
noise = np.random.normal(0, 25, gray.shape).astype(np.uint8)
noisy_gray = cv2.add(gray, noise)

# 1. 均值滤波
blur = cv2.blur(noisy_gray, (5, 5))

# 2. 高斯滤波
gaussian_blur = cv2.GaussianBlur(noisy_gray, (5, 5), 0)

# 3. 中值滤波
median_blur = cv2.medianBlur(noisy_gray, 5)

# 4. 双边滤波
bilateral = cv2.bilateralFilter(img, 9, 75, 75)

# 5. 图像增强 - 直方图均衡化
equalized = cv2.equalizeHist(gray)

# 6. 图像增强 - 对比度受限的自适应直方图均衡化(CLAHE)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
clahe_equalized = clahe.apply(gray)

# 显示所有结果
cv2.imshow('Original', gray)
cv2.imshow('Noisy Image', noisy_gray)
cv2.imshow('Mean Filter', blur)
cv2.imshow('Gaussian Filter', gaussian_blur)
cv2.imshow('Median Filter', median_blur)
cv2.imshow('Bilateral Filter (Color)', bilateral)
cv2.imshow('Histogram Equalization', equalized)
cv2.imshow('CLAHE', clahe_equalized)

cv2.waitKey(0)
cv2.destroyAllWindows()
预计学习时间:60分钟
阅读完整教程

2 边缘检测与轮廓分析

学习如何使用OpenCV进行边缘检测和轮廓分析,这是许多计算机视觉应用的基础。

边缘检测

  • Canny边缘检测:cv2.Canny()
  • Sobel算子:cv2.Sobel()
  • Laplacian算子:cv2.Laplacian()
  • Scharr算子:cv2.Scharr()

轮廓分析

  • 查找轮廓:cv2.findContours()
  • 绘制轮廓:cv2.drawContours()
  • 轮廓面积:cv2.contourArea()
  • 轮廓周长:cv2.arcLength()

代码示例

# Python示例:边缘检测与轮廓分析
import cv2
import numpy as np

# 读取图像
img = cv2.imread('shapes.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 高斯模糊降噪
blurred = cv2.GaussianBlur(gray, (5, 5), 0)

# 1. Canny边缘检测
edges = cv2.Canny(blurred, 50, 150)

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

# 创建一个副本用于绘制轮廓
contour_img = img.copy()

# 3. 绘制所有轮廓
cv2.drawContours(contour_img, contours, -1, (0, 255, 0), 2)

# 4. 轮廓分析
for i, contour in enumerate(contours):
    # 计算轮廓面积
    area = cv2.contourArea(contour)
    
    # 计算轮廓周长
    perimeter = cv2.arcLength(contour, True)
    
    # 近似多边形
    epsilon = 0.02 * perimeter
    approx = cv2.approxPolyDP(contour, epsilon, True)
    
    # 确定形状
    shape = ""
    if len(approx) == 3:
        shape = "Triangle"
    elif len(approx) == 4:
        # 检查是否为矩形
        x, y, w, h = cv2.boundingRect(approx)
        aspect_ratio = float(w) / h
        if 0.95 <= aspect_ratio <= 1.05:
            shape = "Square"
        else:
            shape = "Rectangle"
    elif len(approx) == 5:
        shape = "Pentagon"
    elif len(approx) == 6:
        shape = "Hexagon"
    else:
        # 计算轮廓与最小外接圆的匹配度
        (x, y), radius = cv2.minEnclosingCircle(contour)
        circle_area = np.pi * radius * radius
        area_ratio = area / circle_area
        if area_ratio > 0.8:
            shape = "Circle"
        else:
            shape = "Ellipse"
    
    # 获取轮廓的中心点
    M = cv2.moments(contour)
    if M["m00"] != 0:
        cX = int(M["m10"] / M["m00"])
        cY = int(M["m01"] / M["m00"])
    else:
        cX, cY = 0, 0
    
    # 在图像上绘制形状名称和中心点
    cv2.putText(contour_img, shape, (cX - 20, cY - 20),
                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
    cv2.circle(contour_img, (cX, cY), 5, (255, 0, 0), -1)
    
    # 打印轮廓信息
    print(f"轮廓 {i+1}: 形状={shape}, 面积={area:.2f}, 周长={perimeter:.2f}")

# 显示结果
cv2.imshow('Original Image', img)
cv2.imshow('Canny Edges', edges)
cv2.imshow('Contours', contour_img)

cv2.waitKey(0)
cv2.destroyAllWindows()
预计学习时间:75分钟
阅读完整教程

高级教程

学习高级计算机视觉技术和深度学习应用

1 目标检测与跟踪

学习如何使用OpenCV进行目标检测和跟踪,包括传统方法和深度学习方法。

传统目标检测

  • Haar级联分类器:cv2.CascadeClassifier()
  • HOG+SVM行人检测:cv2.HOGDescriptor()
  • 模板匹配:cv2.matchTemplate()
  • MeanShift跟踪:cv2.meanShift()

深度学习目标检测

  • YOLO模型:cv2.dnn.readNet()
  • SSD模型:cv2.dnn.readNetFromCaffe()
  • Faster R-CNN:通过ONNX导入
  • 跟踪API:cv2.TrackerKCF_create()

代码示例

# Python示例:使用YOLO进行实时目标检测
import cv2
import numpy as np

# 加载YOLO模型
def load_yolo():
    # 加载类别名称
    classes = []
    with open("coco.names", "r") as f:
        classes = [line.strip() for line in f.readlines()]
    
    # 加载YOLO权重和配置文件
    net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
    
    # 获取输出层名称
    layer_names = net.getLayerNames()
    output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]
    
    # 生成随机颜色用于绘制边界框
    colors = np.random.uniform(0, 255, size=(len(classes), 3))
    
    return net, classes, output_layers, colors

# 检测图像中的对象
def detect_objects(img, net, output_layers):
    # 创建blob
    blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
    net.setInput(blob)
    outs = net.forward(output_layers)
    
    # 初始化参数
    class_ids = []
    confidences = []
    boxes = []
    
    # 解析输出
    for out in outs:
        for detection in out:
            scores = detection[5:]
            class_id = np.argmax(scores)
            confidence = scores[class_id]
            
            if confidence > 0.5:
                # 对象检测
                center_x = int(detection[0] * img.shape[1])
                center_y = int(detection[1] * img.shape[0])
                w = int(detection[2] * img.shape[1])
                h = int(detection[3] * img.shape[0])
                
                # 矩形坐标
                x = int(center_x - w / 2)
                y = int(center_y - h / 2)
                
                boxes.append([x, y, w, h])
                confidences.append(float(confidence))
                class_ids.append(class_id)
    
    # 非最大抑制
    indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
    
    return boxes, confidences, class_ids, indexes

# 绘制边界框
def draw_labels(boxes, confidences, class_ids, classes, colors, img):
    font = cv2.FONT_HERSHEY_PLAIN
    for i in range(len(boxes)):
        if i in indexes:
            x, y, w, h = boxes[i]
            label = str(classes[class_ids[i]])
            confidence = confidences[i]
            color = colors[class_ids[i]]
            
            cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
            cv2.putText(img, f"{label} {confidence:.2f}", (x, y + 30), font, 3, color, 3)
    
    return img

# 主函数
def main():
    # 加载YOLO
    net, classes, output_layers, colors = load_yolo()
    
    # 打开网络摄像头
    cap = cv2.VideoCapture(0)
    
    while True:
        # 读取帧
        _, frame = cap.read()
        
        # 检测对象
        boxes, confidences, class_ids, indexes = detect_objects(frame, net, output_layers)
        
        # 绘制边界框
        result = draw_labels(boxes, confidences, class_ids, classes, colors, frame)
        
        # 显示结果
        cv2.imshow("Real-time Object Detection", result)
        
        # 按'q'退出
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
    # 释放资源
    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()
预计学习时间:90分钟
阅读完整教程

2 人脸识别与分析

学习如何使用OpenCV进行人脸识别、面部特征点检测和面部表情分析。

人脸识别

  • Haar级联人脸检测:cv2.CascadeClassifier()
  • LBPH人脸识别器:cv2.face.LBPHFaceRecognizer_create()
  • EigenFace人脸识别器:cv2.face.EigenFaceRecognizer_create()
  • FisherFace人脸识别器:cv2.face.FisherFaceRecognizer_create()

面部特征点检测

  • Dlib面部特征点:结合Dlib库使用
  • OpenCV DNN面部特征点:cv2.dnn.readNet()
  • 眨眼检测
  • 头部姿态估计

代码示例

# Python示例:实时人脸识别与面部特征点检测
import cv2
import numpy as np

# 加载人脸检测器和特征点检测器
def load_face_detectors():
    # 加载Haar级联人脸检测器
    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
    
    # 加载眼睛检测器
    eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
    
    # 加载预训练的面部特征点检测模型
    # 注意:需要下载相应的模型文件
    face_model = cv2.dnn.readNetFromTensorflow('opencv_face_detector_uint8.pb', 
                                             'opencv_face_detector.pbtxt')
    landmark_model = cv2.dnn.readNetFromTensorflow('landmark_model.pb')
    
    return face_cascade, eye_cascade, face_model, landmark_model

# 使用Haar级联检测人脸
def detect_faces_haar(img, face_cascade):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    return faces

# 使用DNN检测人脸
def detect_faces_dnn(img, face_model):
    h, w = img.shape[:2]
    blob = cv2.dnn.blobFromImage(img, 1.0, (300, 300), [104, 117, 123], False, False)
    face_model.setInput(blob)
    detections = face_model.forward()
    
    faces = []
    for i in range(detections.shape[2]):
        confidence = detections[0, 0, i, 2]
        if confidence > 0.7:
            x1 = int(detections[0, 0, i, 3] * w)
            y1 = int(detections[0, 0, i, 4] * h)
            x2 = int(detections[0, 0, i, 5] * w)
            y2 = int(detections[0, 0, i, 6] * h)
            faces.append([x1, y1, x2 - x1, y2 - y1])
    
    return faces

# 检测眼睛
def detect_eyes(img, eye_cascade, face_rect):
    x, y, w, h = face_rect
    roi_gray = cv2.cvtColor(img[y:y+h, x:x+w], cv2.COLOR_BGR2GRAY)
    eyes = eye_cascade.detectMultiScale(roi_gray)
    return eyes

# 检测面部特征点
def detect_landmarks(img, landmark_model, face_rect):
    x, y, w, h = face_rect
    face_roi = img[y:y+h, x:x+w]
    h_roi, w_roi = face_roi.shape[:2]
    
    blob = cv2.dnn.blobFromImage(face_roi, 1.0, (100, 100), [0, 0, 0], True, False)
    landmark_model.setInput(blob)
    landmarks = landmark_model.forward()
    
    # 假设模型输出68个特征点
    points = []
    for i in range(68):
        x_p = int(landmarks[0, i*2] * w_roi) + x
        y_p = int(landmarks[0, i*2+1] * h_roi) + y
        points.append((x_p, y_p))
    
    return points

# 绘制检测结果
def draw_detections(img, faces, eyes=None, landmarks=None):
    result = img.copy()
    
    # 绘制人脸边界框
    for (x, y, w, h) in faces:
        cv2.rectangle(result, (x, y), (x+w, y+h), (255, 0, 0), 2)
        
        # 绘制眼睛
        if eyes is not None:
            for (ex, ey, ew, eh) in eyes:
                cv2.rectangle(result, (x+ex, y+ey), (x+ex+ew, y+ey+eh), (0, 255, 0), 2)
    
    # 绘制特征点
    if landmarks is not None:
        for (x, y) in landmarks:
            cv2.circle(result, (x, y), 2, (0, 0, 255), -1)
    
    return result

# 主函数
def main():
    # 加载检测器
    face_cascade, eye_cascade, face_model, landmark_model = load_face_detectors()
    
    # 打开摄像头
    cap = cv2.VideoCapture(0)
    
    while True:
        # 读取帧
        ret, frame = cap.read()
        if not ret:
            break
        
        # 检测人脸(使用DNN方法)
        faces = detect_faces_dnn(frame, face_model)
        
        # 如果检测到人脸
        if len(faces) > 0:
            # 检测眼睛
            eyes = []
            for face in faces:
                face_eyes = detect_eyes(frame, eye_cascade, face)
                for (ex, ey, ew, eh) in face_eyes:
                    eyes.append((ex, ey, ew, eh))
            
            # 检测特征点(仅对第一个人脸)
            landmarks = detect_landmarks(frame, landmark_model, faces[0]) if len(faces) > 0 else None
            
            # 绘制检测结果
            result = draw_detections(frame, faces, eyes, landmarks)
        else:
            result = frame
        
        # 显示结果
        cv2.imshow('Face Detection and Landmarks', result)
        
        # 按'q'退出
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
    # 释放资源
    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()
预计学习时间:120分钟
阅读完整教程

推荐学习路径

按照以下路径学习,循序渐进掌握OpenCV

第1步:环境搭建与基础概念

安装OpenCV,了解基本概念和数据结构。

  • 安装OpenCV
  • 了解Mat数据结构
  • 图像读取与显示
1
2

第2步:基础图像处理

学习基本的图像处理操作和技术。

  • 图像变换(缩放、旋转等)
  • 色彩空间转换
  • 图像滤波与增强

第3步:边缘检测与轮廓分析

学习边缘检测和轮廓分析技术。

  • Canny边缘检测
  • 轮廓检测与绘制
  • 形状分析
3
4

第4步:特征检测与匹配

学习特征点检测和匹配技术。

  • SIFT/SURF/ORB特征
  • 特征匹配
  • 图像拼接

第5步:目标检测与跟踪

学习目标检测和跟踪技术。

  • Haar级联分类器
  • HOG行人检测
  • 目标跟踪
5
6

第6步:深度学习与高级应用

学习深度学习在计算机视觉中的应用。

  • DNN模块使用
  • 深度学习目标检测
  • 人脸识别与分析

开始您的OpenCV学习之旅

下载最新版本的OpenCV,跟随我们的教程,探索计算机视觉的无限可能

免费下载