OpenCV视觉分析之目标跟踪(7)目标跟踪器类TrackerVit的使用

  • 操作系统:ubuntu22.04
  • OpenCV版本:OpenCV4.9
  • IDE:Visual Studio Code
  • 编程语言:C++11

算法描述

VIT 跟踪器由于特殊的模型结构而变得更快且极其轻量级,模型文件大约为 767KB。模型下载链接:https://github.com/opencv/opencv_zoo/tree/main/models/object_tracking_vittrack 作者:PengyuLiu,邮箱:1872918507@qq.com

cv::TrackerVit 是 OpenCV 中的一个视觉跟踪器类,它是基于 ViT(Vision Transformer)架构的一种实现。ViT 是一种用于图像识别的深度学习模型,近年来在计算机视觉领域取得了显著的效果。

代码示例

cpp 复制代码
#include <iostream>
#include <opencv2/opencv.hpp>

int main()
{
    // 读取视频文件
    cv::VideoCapture cap( 0 );
    if ( !cap.isOpened() )
    {
        std::cout << "Error opening video file" << std::endl;
        return -1;
    }

    // 读取第一帧
    cv::Mat frame;
    cap >> frame;
    if ( frame.empty() )
    {
        std::cout << "Error reading first frame" << std::endl;
        return -1;
    }

    // 选择目标区域
    cv::Rect bbox = cv::selectROI( "Select ROI", frame, false, false );
    if ( bbox.width <= 0 || bbox.height <= 0 )
    {
        std::cout << "No ROI selected" << std::endl;
        return -1;
    }

    // 创建 TrackerVit 对象
    cv::Ptr< cv::Tracker > tracker = cv::TrackerVit::create();

    // 初始化跟踪器
    tracker->init( frame, bbox );
   

    // 跟踪目标
    while ( true )
    {
        cap >> frame;
        if ( frame.empty() )
        {
            break;
        }

        // 更新跟踪结果
        cv::Rect newBox;
        bool ok = tracker->update( frame, newBox );

        // 绘制边界框
        if ( ok )
        {
            cv::rectangle( frame, newBox, cv::Scalar( 0, 255, 0 ), 2, 1 );
        }
        else
        {
            cv::rectangle( frame, newBox, cv::Scalar( 0, 0, 255 ), 2, 1 );
        }

        // 显示结果
        cv::imshow( "Tracking", frame );
        if ( cv::waitKey( 1 ) >= 0 )
        {
            break;
        }
    }

    return 0;
}

运行结果

代码所需的onnx文件下载地址:
https://download.csdn.net/download/jndingxin/89938956

相关推荐
人工智能技术咨询.5 小时前
具身智能中的世界模型训练
人工智能
LaughingZhu5 小时前
Product Hunt 每日热榜 | 2026-10-06
人工智能·深度学习·神经网络·搜索引擎·百度
AOI小白新手上路6 小时前
AOI 缺陷检测复现实操指南:Anomalib + MVTec AD(glass)与 YOLOv8 + NEU-DET 两条路线
人工智能·深度学习·yolo
henrylin99996 小时前
RD-AGENT 第一讲 · AI 因子工厂是怎么运转的
人工智能
高洁016 小时前
具身智能中的世界模型训练
人工智能·python·深度学习·机器学习·transformer
无线通信科研笔记6 小时前
IEEE TVT 2026 论文精读与完整复现|相位误差如何重塑近场 RIS 的幅相响应
论文阅读·人工智能·python·算法·论文笔记
Devlive 开源社区6 小时前
AuthX 正式更名 GrantForge:我们重新做了一遍权限管理系统
大数据·人工智能·架构
朝朝辞暮i7 小时前
VLA 系统学习第 1 课:VLA 到底在干什么?
人工智能·python·计算机视觉·vla
鲲穹AI种草7 小时前
AI 壁纸生成工具怎么选?鲲穹 AI 壁纸工具功能实测与横向对比
人工智能·壁纸生成工具
科技林总7 小时前
向量与重排模型
人工智能