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

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

一、核心技术与工具

  1. 图像预处理

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

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

  2. 特征检测与匹配

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

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

  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 ToolboxDeep Learning Toolbox(深度学习相关功能)。
  2. 自定义检测时,数据集质量(数量、多样性)直接影响模型性能。
  3. 对于实时检测(如摄像头输入),可结合videoinputvision.VideoFileReader处理视频流。

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

相关推荐
不枯石26 分钟前
Matlab通过GUI实现点云的随机一致性(RANSAC)配准
开发语言·图像处理·算法·计算机视觉·matlab
牛马的人生30 分钟前
MATLAB模块库入门:提升你的工程分析效率
开发语言·其他·matlab
光电笑映2 小时前
C++list全解析
c语言·开发语言·数据结构·c++·list
恋猫de小郭3 小时前
Fluttercon EU 2025 :Let‘s go far with Flutter
android·开发语言·flutter·ios·golang
小龙报3 小时前
《构建模块化思维---函数(下)》
c语言·开发语言·c++·算法·visualstudio·学习方法
一只学java的小汉堡4 小时前
Spring Cloud RabbitMQ 详解:从基础概念到秒杀实战
开发语言·后端·ruby
952364 小时前
数据结构—双链表
c语言·开发语言·数据结构·学习
Y.9994 小时前
Python 题目练习 Day1.2
开发语言·python
祁同伟.4 小时前
【C++】继承
开发语言·c++
烈风4 小时前
011 Rust数组
开发语言·后端·rust