当前位置: 首页 > article >正文

OpenCV运动分析和目标跟踪(1)累积操作函数accumulate()的使用

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

算法描述

将一个图像添加到累积图像中。
该函数将 src 或其部分元素添加到 dst 中:
dst ( x , y ) ← dst ( x , y ) + src ( x , y ) if mask ( x , y ) ≠ 0 \texttt{dst} (x,y) \leftarrow \texttt{dst} (x,y) + \texttt{src} (x,y) \quad \text{if} \quad \texttt{mask} (x,y) \ne 0 dst(x,y)dst(x,y)+src(x,y)ifmask(x,y)=0
该函数支持多通道图像。每个通道独立处理。
cv::accumulate 函数可以用于收集由静止相机拍摄的场景背景的统计数据,并用于进一步的前景-背景分割。

函数原型


void cv::accumulate	
(
	InputArray 	src,
	InputOutputArray 	dst,
	InputArray 	mask = noArray() 
)	

参数

  • 参数src 输入图像,类型为 CV_8UC(n),CV_16UC(n),CV_32FC(n) 或 CV_64FC(n),其中 n 是一个正整数。

  • 参数dst 累积图像,与输入图像具有相同数量的通道,并且深度为 CV_32F 或 CV_64F。

  • 参数mask 可选的操作掩码。

代码示例



#include <iostream>
#include <opencv2/opencv.hpp>

int main()
{
    // 加载一个真实的图像
    cv::Mat sourceImage = cv::imread( "/media/dingxin/data/study/OpenCV/sources/images/sun2.jpg", cv::IMREAD_COLOR );
    if ( sourceImage.empty() )
    {
        std::cout << "Error loading image" << std::endl;
        return -1;
    }

    // 获取源图像的尺寸和通道数
    cv::Size imageSize = sourceImage.size();
    int numChannels = sourceImage.channels();

    // 输出源图像的尺寸和类型
    std::cout << "Source Image Size: " << imageSize << std::endl;
    std::cout << "Source Image Type: " << sourceImage.type() << std::endl;
    std::cout << "Source Image Channels: " << numChannels << std::endl;

    // 创建一个空的累积图像
    cv::Mat cumulativeImage = cv::Mat::zeros(imageSize, CV_32FC(numChannels)); // 累积图像类型为 CV_32FC3

    // 输出累积图像的尺寸和类型
    std::cout << "Cumulative Image Size: " << cumulativeImage.size() << std::endl;
    std::cout << "Cumulative Image Type: " << cumulativeImage.type() << std::endl;
    std::cout << "Cumulative Image Channels: " << cumulativeImage.channels() << std::endl;

    // 将源图像转换为浮点类型
    cv::Mat sourceImageFloat;
    sourceImage.convertTo(sourceImageFloat, CV_32FC(numChannels), 1.0 / 255.0);

    // 输出转换后的图像尺寸和类型
    std::cout << "Converted Image Size: " << sourceImageFloat.size() << std::endl;
    std::cout << "Converted Image Type: " << sourceImageFloat.type() << std::endl;
    std::cout << "Converted Image Channels: " << sourceImageFloat.channels() << std::endl;

    // 创建一个掩码图像
    cv::Mat mask = cv::Mat::ones(imageSize, CV_8U) * 255; // 全部像素为255,即不使用掩码

    // 输出掩码图像的尺寸和类型
    std::cout << "Mask Image Size: " << mask.size() << std::endl;
    std::cout << "Mask Image Type: " << mask.type() << std::endl;

    // 确保累积图像和源图像的尺寸一致
    if (cumulativeImage.rows != sourceImageFloat.rows || cumulativeImage.cols != sourceImageFloat.cols) {
        std::cout << "Error: Cumulative image and source image do not have the same size." << std::endl;
        return -1;
    }

    // 确保累积图像和源图像的通道数一致
    if (cumulativeImage.channels() != sourceImageFloat.channels()) {
        std::cout << "Error: Cumulative image and source image do not have the same number of channels." << std::endl;
        return -1;
    }

    // 累积源图像到累积图像中
    int numAccumulations = 100; // 增加累加次数
    for (int i = 0; i < numAccumulations; ++i) {
        cv::accumulate(sourceImageFloat, cumulativeImage, mask);
    }

    // 显示累积图像
    cv::Mat normalizedCumulativeImage;
    cv::normalize(cumulativeImage, normalizedCumulativeImage, 0, 255, cv::NORM_MINMAX, CV_8U);

    // 使用高对比度的色彩映射
    cv::Mat enhancedCumulativeImage;
    cv::applyColorMap(normalizedCumulativeImage, enhancedCumulativeImage, cv::COLORMAP_JET);

    cv::imshow("Original Image", sourceImage);
    cv::imshow("Cumulative Image", enhancedCumulativeImage);
    
    cv::waitKey( 0 );
    return 0;
}

运行结果

在这里插入图片描述


http://www.kler.cn/news/308205.html

相关文章:

  • 5分钟配置Nginx?(二)
  • 用Facebook广告提升本地业务的影响力
  • redis中的5中数据结构
  • 建筑工程资料保护策略:打造安全的建筑文档管理方案
  • 【SpringBoot3】面向切面 AspectJ AOP 使用详解
  • 2022高教社杯全国大学生数学建模竞赛C题 问题一(1) Python代码
  • 这个时代唯一“不变“的又是{变}
  • Unity for Android使用蓝牙低功耗Bluetooth LE
  • 十.在vue中,发送axios请求应该放在created里还是mounted里?详解
  • 书生大模型全链路开源体系,学习
  • 5G Multicast/Broadcast Services(MBS) (二) Multicast
  • Spring Boot-Session管理问题
  • CentOS7更换阿里云yum更新源
  • C# USB通信技术(通过LibUsbDotNet库)
  • linux-硬件与设备管理-设备挂载与管理
  • NLP:微调BERT进行文本分类
  • Java高级Day43-类加载
  • mysql 修改索引
  • 服务端接口性能优化有哪些方案?
  • 安卓在子线程中使用Toast
  • [Linux]:进程间通信(下)
  • vue2、vue3生成二维码
  • 算法 | 基础排序算法:插入排序、选择排序、交换排序、归并排序
  • xml重点笔记(尚学堂 3h)
  • 使用 GaLore 预训练LLaMA-7B
  • 72、结合无人机进行rk3588oak-lite跟踪目标物体进行识别、跟踪、保持距离
  • Java数据结构应用(力扣题20. 有效的括号)
  • Excel 基础知识-操作手册1
  • 2024/9/16 dataloader、tensorboard、transform
  • 三十八、Go-redis快速入门