MATLAB中进行视觉检测入门教程

在MATLAB中进行视觉检测主要依赖于Computer Vision Toolbox(计算机视觉工具箱),该工具箱提供了丰富的函数和预训练模型,可实现目标检测、特征提取、图像分割、运动分析等任务。以下是MATLAB视觉检测的核心技术和实践示例:

一、核心技术与工具

  1. 图像预处理

    包括灰度化、滤波(去噪)、阈值分割等,为后续检测提供高质量图像。

    关键函数:rgb2gray(灰度化)、imgaussfilt(高斯滤波)、imbinarize(阈值分割)。

  2. 特征检测与匹配

    用于识别图像中的关键特征(如角点、边缘),常用于目标定位或图像配准。

    常用算法:SURF、ORB、Harris角点检测等,对应函数detectSURFFeatures、detectORBFeatures。

  3. 目标检测

    • 传统方法:基于 Haar 特征的级联分类器(trainCascadeObjectDetector)。
    • 深度学习方法:支持预训练模型(如YOLO、Faster R-CNN),可直接检测常见目标(人、车、物体等),也可训练自定义模型。
  4. 目标跟踪

    对视频中的目标进行持续追踪,函数如vision.KalmanFilter(卡尔曼滤波跟踪)、trackerLK(光流跟踪)。

二、实践示例

示例1:基于预训练模型的目标检测(检测图像中的车辆、行人等)

MATLAB的Computer Vision Toolbox提供了预训练的目标检测模型(如YOLO v2),可直接调用:

matlab 复制代码
% 1. 加载预训练的YOLO v2模型(检测20类常见目标)
detector = yolov2ObjectDetector('yolov2-tiny-coco');

% 2. 读取图像
I = imread('street_scene.jpg');  % 替换为你的图像路径
figure, imshow(I), title('原始图像');

% 3. 运行目标检测
[bboxes, scores, labels] = detect(detector, I);

% 4. 筛选高置信度结果(置信度>0.5)
idx = scores > 0.5;
bboxes = bboxes(idx,:);
scores = scores(idx);
labels = labels(idx);

% 5. 绘制检测结果
I_detected = insertObjectAnnotation(I, 'rectangle', bboxes, ...
    cellstr([labels, ": " + string(round(scores*100,1)) + "%"]));
figure, imshow(I_detected), title('目标检测结果');
示例2:自定义目标检测(训练模型检测特定物体,如瓶盖)
  1. 准备数据集 :收集含目标的图像,用Image Labeler工具标注目标位置(生成边界框)。
  2. 训练模型 :使用trainYOLOv2ObjectDetector训练自定义YOLO模型:
matlab 复制代码
% 1. 加载标注数据(由Image Labeler导出的.mat文件)
load('cap_annotations.mat');  % 包含图像路径和边界框信息

% 2. 配置YOLOv2网络
inputSize = [224 224 3];  % 输入图像尺寸
numClasses = 1;  % 仅检测"瓶盖"一类目标
anchorBoxes = estimateAnchorBoxes(annotations, inputSize);  % 估计锚框

% 3. 训练模型
options = trainingOptions('sgdm', ...
    'InitialLearnRate', 1e-3, ...
    'MaxEpochs', 20, ...
    'MiniBatchSize', 16);

detector = trainYOLOv2ObjectDetector(annotations, anchorBoxes, numClasses, inputSize, options);

% 4. 测试模型
testImage = imread('test_cap.jpg');
[bboxes, scores] = detect(detector, testImage);
I_result = insertObjectAnnotation(testImage, 'rectangle', bboxes, ...
    "瓶盖: " + string(round(scores*100,1)) + "%");
figure, imshow(I_result);
示例3:特征检测与匹配(定位图像中的已知目标)

利用SURF特征匹配实现目标定位:

matlab 复制代码
% 1. 读取目标模板和待检测图像
template = rgb2gray(imread('template.jpg'));  % 目标模板
scene = rgb2gray(imread('scene.jpg'));        % 含目标的场景图

% 2. 检测SURF特征
pointsTemplate = detectSURFFeatures(template);
pointsScene = detectSURFFeatures(scene);

% 3. 提取特征描述子
[featuresTemplate, pointsTemplate] = extractFeatures(template, pointsTemplate);
[featuresScene, pointsScene] = extractFeatures(scene, pointsScene);

% 4. 匹配特征
indexPairs = matchFeatures(featuresTemplate, featuresScene);
matchedTemplatePoints = pointsTemplate(indexPairs(:,1), :);
matchedScenePoints = pointsScene(indexPairs(:,2), :);

% 5. 绘制匹配结果
figure, showMatchedFeatures(template, scene, matchedTemplatePoints, ...
    matchedScenePoints, 'montage');
title('特征匹配结果');

% 6. 计算变换矩阵并定位目标
[tform, inliers] = estimateGeometricTransform2D(...
    matchedTemplatePoints, matchedScenePoints, 'affine');

% 7. 绘制目标边界框
box = [1, 1, size(template,2), size(template,1)];  % 模板边界框
newBox = transformPointsForward(tform, box(:,1), box(:,2));
sceneWithBox = insertShape(scene, 'Polygon', newBox, 'LineWidth', 2);
figure, imshow(sceneWithBox), title('目标定位结果');

三、应用场景

  • 工业检测:产品缺陷检测(如裂纹、划痕)。
  • 智能监控:行人/车辆检测与跟踪。
  • 机器人视觉:目标抓取、场景导航。
  • 医学影像:病灶检测、细胞计数。

四、注意事项

  1. 需安装Computer Vision Toolbox 和Deep Learning Toolbox(深度学习相关功能)。
  2. 自定义检测时,数据集质量(数量、多样性)直接影响模型性能。
  3. 对于实时检测(如摄像头输入),可结合videoinput或vision.VideoFileReader处理视频流。

通过上述工具和方法,MATLAB可高效实现从简单特征检测到复杂深度学习目标检测的全流程视觉任务。

相关推荐
chenbingjie_c5 小时前
C++ 进阶核心知识点总结:模板、内存分区、new/delete、内存池
开发语言·c++
Escalating_xu6 小时前
【C 语言】深入理解指针(1·下):指针运算、野指针、assert 与传址实战
java·c语言·开发语言
实心儿儿6 小时前
Qt — Qt 事件
开发语言·qt
周杰偷奶茶6 小时前
【Java】数据类型与变量
java·开发语言
code斗6 小时前
Java数据结构:堆详解
java·开发语言·数据结构
重生之小比特7 小时前
【C++进阶】map和set
java·开发语言·c++
lie..7 小时前
30天从零开始学AI应用开发(Day 12):像产品一样稳:流式输出、报错重试、花钱控制
开发语言·人工智能·php
傻啦嘿哟7 小时前
Python的默认参数把我坑惨了,原来写[]和写None的区别这么大
开发语言·python·机器学习
DeepAgent7 小时前
AI Agent 工程实践(49):一次真实优化——从 Agent v1 到 v2
开发语言·人工智能·agent
知识分享小能手8 小时前
C++ 学习教程,从入门到精通,C++对象和类 — 详细知识点总结(10)
开发语言·c++·学习