【opencv】示例-application_trace.cpp 跟踪应用程序执行的宏

sql 复制代码
[ INFO:0@0.061] global videoio_registry.cpp:244 cv::`anonymous-namespace'::VideoBackendRegistry::VideoBackendRegistry VIDEOIO: Enabled backends(9, sorted by priority): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); MSMF(970); DSHOW(960); CV_IMAGES(950); CV_MJPEG(940); UEYE(930); OBSENSOR(920)
[ INFO:0@0.062] global backend_plugin.cpp:383 cv::impl::getPluginCandidates Found 2 plugin(s) for GSTREAMER
[ INFO:0@0.063] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load V:\learn\opencv\github\opencv\sources\test\x64\Debug\opencv_videoio_gstreamer490_64d.dll => FAILED
[ INFO:0@0.071] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load opencv_videoio_gstreamer490_64d.dll => FAILED
[ INFO:0@0.235] global cap_msmf.cpp:1031 CvCapture_MSMF::configureHW MSMF: Using D3D11 video acceleration on GPU device: NVIDIA GeForce GTX 1070 Ti
Video 0: width=640, height=480, nframes=-1
Start processing...
Press ESC key to terminate
[ INFO:0@5.295] global ocl.cpp:1185 cv::ocl::haveOpenCL Initialize OpenCL runtime...
[ INFO:0@5.306] global ocl.cpp:1191 cv::ocl::haveOpenCL OpenCL: found 1 platforms
[ INFO:0@5.306] global ocl.cpp:983 cv::ocl::OpenCLExecutionContext::Impl::getInitializedExecutionContext OpenCL: initializing thread execution context
[ INFO:0@5.306] global ocl.cpp:993 cv::ocl::OpenCLExecutionContext::Impl::getInitializedExecutionContext OpenCL: creating new execution context...
[ INFO:0@5.397] global ocl.cpp:1011 cv::ocl::OpenCLExecutionContext::Impl::getInitializedExecutionContext OpenCL: device=NVIDIA GeForce GTX 1070 Ti
[ INFO:0@5.397] global ocl.cpp:5369 cv::ocl::Context::Impl::__init_buffer_pools OpenCL: Initializing buffer pool for context@0 with max capacity: poolSize=0 poolSizeHostPtr=0
[ INFO:0@5.398] global registry_parallel.impl.hpp:96 cv::parallel::ParallelBackendRegistry::ParallelBackendRegistry core(parallel): Enabled backends(3, sorted by priority): ONETBB(1000); TBB(990); OPENMP(980)
[ INFO:0@5.399] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load V:\learn\opencv\github\opencv\sources\test\x64\Debug\opencv_core_parallel_onetbb490_64d.dll => FAILED
[ INFO:0@5.401] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load opencv_core_parallel_onetbb490_64d.dll => FAILED
[ INFO:0@5.401] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load V:\learn\opencv\github\opencv\sources\test\x64\Debug\opencv_core_parallel_tbb490_64d.dll => FAILED
[ INFO:0@5.403] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load opencv_core_parallel_tbb490_64d.dll => FAILED
[ INFO:0@5.403] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load V:\learn\opencv\github\opencv\sources\test\x64\Debug\opencv_core_parallel_openmp490_64d.dll => FAILED
[ INFO:0@5.405] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load opencv_core_parallel_openmp490_64d.dll => FAILED
[ INFO:0@5.407] global registry.impl.hpp:114 cv::highgui_backend::UIBackendRegistry::UIBackendRegistry UI: Enabled backends(4, sorted by priority): GTK(1000); GTK3(990); GTK2(980); WIN32(970) + BUILTIN(WIN32UI)
[ INFO:0@5.408] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load V:\learn\opencv\github\opencv\sources\test\x64\Debug\opencv_highgui_gtk490_64.dll => FAILED
[ INFO:0@5.410] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load opencv_highgui_gtk490_64.dll => FAILED
[ INFO:0@5.410] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load V:\learn\opencv\github\opencv\sources\test\x64\Debug\opencv_highgui_gtk3490_64.dll => FAILED
[ INFO:0@5.412] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load opencv_highgui_gtk3490_64.dll => FAILED
[ INFO:0@5.413] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load V:\learn\opencv\github\opencv\sources\test\x64\Debug\opencv_highgui_gtk2490_64.dll => FAILED
[ INFO:0@5.415] global plugin_loader.impl.hpp:67 cv::plugin::impl::DynamicLib::libraryLoad load opencv_highgui_gtk2490_64.dll => FAILED
[ INFO:0@5.415] global backend.cpp:90 cv::highgui_backend::createUIBackend UI: using backend: WIN32 (priority=970)
[ INFO:0@5.415] global window_w32.cpp:2993 cv::impl::Win32BackendUI::createWindow OpenCV/UI: Creating Win32UI window: Live (1)
[ INFO:0@5.563] global ocl.cpp:409 cv::ocl::OpenCLBinaryCacheConfigurator::OpenCLBinaryCacheConfigurator Successfully initialized OpenCL cache directory: C:\Users\cxy\AppData\Local\Temp\opencv\4.x\opencl_cache\
[ INFO:0@5.563] global ocl.cpp:433 cv::ocl::OpenCLBinaryCacheConfigurator::prepareCacheDirectoryForContext Preparing OpenCL cache configuration for context: NVIDIA_Corporation--NVIDIA_GeForce_GTX_1070_Ti--531_30
[ INFO:0@5.574] global window_w32.cpp:2993 cv::impl::Win32BackendUI::createWindow OpenCV/UI: Creating Win32UI window: Processed (1)
[ INFO:0@41.403] global cap_msmf.cpp:550 `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
cpp 复制代码
/* OpenCV 应用追踪支持演示. */
#include <iostream> // 引入输入输出流库,用于标准输入输出


#include <opencv2/core.hpp> // OpenCV 核心功能模块
#include <opencv2/imgproc.hpp> // OpenCV 图像处理相关功能
#include <opencv2/highgui.hpp> // OpenCV High-level GUI 和图像展示模块
#include <opencv2/videoio.hpp> // OpenCV 视频读写模块
#include <opencv2/core/utils/trace.hpp> // OpenCV 的追踪功能工具模块


using namespace cv; // 使用命名空间cv,避免每次调用OpenCV函数时都要加cv::
using namespace std; // 使用命名空间std,避免每次调用标准库函数时都要加std::


// 处理帧的函数定义
static void process_frame(const cv::UMat& frame)
{
    CV_TRACE_FUNCTION(); // OpenCV 追踪宏,用于追踪函数


    imshow("Live", frame); // 展示原始帧


    UMat gray, processed;
    cv::cvtColor(frame, gray, COLOR_BGR2GRAY); // BGR颜色转换为灰度
    Canny(gray, processed, 32, 64, 3); // Canny边缘检测
    imshow("Processed", processed); // 展示处理后的帧
}


int main(int argc, char** argv)
{
    CV_TRACE_FUNCTION(); // OpenCV 追踪宏,用于追踪main函数


    cv::CommandLineParser parser(argc, argv, // 命令行参数解析器
        "{help h ? |     | help message}" // 帮助信息
        "{n        | 100 | number of frames to process }" // 处理帧的数量
        "{@video   | 0   | video filename or cameraID }" // 视频文件名或摄像头ID
    );
    if (parser.has("help")) // 如果请求帮助信息
    {
        parser.printMessage(); // 打印帮助信息
        return 0; // 退出程序
    }


    VideoCapture capture; // 视频捕捉类实例
    std::string video = parser.get<string>("@video"); // 获取视频文件名或摄像头ID
    if (video.size() == 1 && isdigit(video[0]))
        capture.open(parser.get<int>("@video")); // 打开摄像头
    else
        capture.open(samples::findFileOrKeep(video));  // 保留GStreamer pipelines
    int nframes = 0;
    if (capture.isOpened()) // 如果成功打开视频捕捉
    {
        nframes = (int)capture.get(CAP_PROP_FRAME_COUNT); // 获取视频总帧数
        cout << "Video " << video <<
            ": width=" << capture.get(CAP_PROP_FRAME_WIDTH) << // 输出视频宽度
            ", height=" << capture.get(CAP_PROP_FRAME_HEIGHT) << // 输出视频高度
            ", nframes=" << nframes << endl; // 输出视频总帧数
    }
    else // 如果打开视频失败
    {
        cout << "Could not initialize video capturing...\n"; // 打印无法初始化视频捕捉信息
        return -1; // 返回错误代码-1退出程序
    }


    int N = parser.get<int>("n"); // 获取需要处理的帧数量
    if (nframes > 0 && N > nframes)
        N = nframes; // 如果处理的帧数量超过视频总帧数,将N修改为视频总帧数


    cout << "Start processing..." << endl // 开始处理消息
        << "Press ESC key to terminate" << endl; // 提示按下ESC键终止


    UMat frame; // 定义用于存放帧的UMat变量
    for (int i = 0; N > 0 ? (i < N) : true; i++) // 循环处理每一帧
    {
        CV_TRACE_REGION("FRAME"); // OpenCV追踪宏,用于追踪命名区域"FRAME"
        {
            CV_TRACE_REGION("read"); // 追踪读取视频帧区域
            capture.read(frame); // 读取视频帧


            if (frame.empty()) // 如果捕捉的帧为空
            {
                cerr << "Can't capture frame: " << i << std::endl; // 打印错误信息
                break; // 跳出循环
            }


            // OpenCV追踪宏,用于追踪下一个命名区域"process"在同一个C++作用域
            // 前一个"read"区域将在这一行完成标记
            // 使用这种方式来减少不必要的大括号使用
            CV_TRACE_REGION_NEXT("process"); 
            process_frame(frame); // 调用处理帧的函数


            // OpenCV追踪宏,用于追踪下一个命名区域"delay"在同一个C++作用域
            CV_TRACE_REGION_NEXT("delay");
            if (waitKey(1) == 27/*ESC*/) // 如果按下ESC键
                break; // 跳出循环
        }
    }


    return 0; // 正常退出程序
}

这段代码的功能是使用OpenCV对视频进行处理并展示。首先,定义了一个处理视频帧的函数process_frame,该函数将输入的彩色帧转换成灰度图像,并运用Canny边缘检测算法处理。在main函数中,通过命令行参数指定视频源或摄像头ID,然后读取并处理视频的每一帧,直到完成指定数量的帧或者用户按下ESC键终止。整个过程中使用了OpenCV的追踪功能来标记函数和处理不同段落的时间消耗,以便于性能分析和调试。

相关推荐
埃菲尔铁塔_CV算法14 分钟前
深度学习神经网络创新点方向
人工智能·深度学习·神经网络
艾思科蓝-何老师【H8053】33 分钟前
【ACM出版】第四届信号处理与通信技术国际学术会议(SPCT 2024)
人工智能·信号处理·论文发表·香港中文大学
weixin_452600691 小时前
《青牛科技 GC6125:驱动芯片中的璀璨之星,点亮 IPcamera 和云台控制(替代 BU24025/ROHM)》
人工智能·科技·单片机·嵌入式硬件·新能源充电桩·智能充电枪
学术搬运工1 小时前
【珠海科技学院主办,暨南大学协办 | IEEE出版 | EI检索稳定 】2024年健康大数据与智能医疗国际会议(ICHIH 2024)
大数据·图像处理·人工智能·科技·机器学习·自然语言处理
右恩1 小时前
AI大模型重塑软件开发:流程革新与未来展望
人工智能
图片转成excel表格2 小时前
WPS Office Excel 转 PDF 后图片丢失的解决方法
人工智能·科技·深度学习
阿_旭2 小时前
如何使用OpenCV和Python进行相机校准
python·opencv·相机校准·畸变校准
ApiHug2 小时前
ApiSmart x Qwen2.5-Coder 开源旗舰编程模型媲美 GPT-4o, ApiSmart 实测!
人工智能·spring boot·spring·ai编程·apihug
哇咔咔哇咔3 小时前
【科普】简述CNN的各种模型
人工智能·神经网络·cnn
李歘歘3 小时前
万字长文解读深度学习——多模态模型CLIP、BLIP、ViLT
人工智能·深度学习