• 简单帧差法


    代码来自:http://www.cnblogs.com/tornadomeet/archive/2012/05/01/2477629.html

     1 #include <string>
     2 #include <iostream>
     3 #include <stdio.h>
     4 #include <fstream>
     5 #include<math.h>
     6 
     7 #include <opencv2opencv.hpp>
     8 #include <opencv2highguihighgui.hpp>
     9 #include <opencv2imgprocimgproc.hpp>
    10 #include <opencv2corecore.hpp>
    11 
    12 
    13 #define threshold_diff 18 //设置简单帧差法阈值
    14 
    15 using namespace cv;
    16 using namespace std;
    17 
    18 int main(int argc, unsigned char* argv[])
    19 {
    20     Mat img_src1, img_src2, img_dst, gray1, gray2, gray_diff;
    21     bool pause = false;
    22 
    23     VideoCapture vido_file("a.avi");//在这里改相应的文件名
    24     namedWindow("foreground", 0);
    25     for (;;)
    26     {
    27         if (!pause )
    28         {
    29             vido_file >> img_src1;        //因为视频文件帧数已经固定了,所以每次到这句语句都是读取相邻的帧数,没到时间视频并不向前走
    30             if (img_src1.empty())        //【】没有这个判断的话,视频播放完了会有内存溢出错误,所以播完就直接break!
    31                 break;
    32             //GaussianBlur(img_src1, img_src1, Size(3, 3), 60);
    33             cvtColor(img_src1, gray1, CV_BGR2GRAY);
    34             imshow("video_src", img_src1);//可以事先不用新建一个窗口
    35             waitKey(5);
    36 
    37             vido_file >> img_src2;
    38             //GaussianBlur(img_src2, img_src2, Size(3, 3), 60);
    39             cvtColor(img_src2, gray2, CV_BGR2GRAY);
    40             imshow("video_src", img_src2);//可以事先不用新建一个窗口
    41 
    42             waitKey(5);
    43             subtract(gray1, gray2, gray_diff);
    44             GaussianBlur(gray_diff, gray_diff, Size(3, 3), 0,0);
    45             for (int i = 0; i<gray_diff.rows; i++)
    46             for (int j = 0; j<gray_diff.cols; j++)
    47             if (abs(gray_diff.at<unsigned char>(i, j)) >= threshold_diff)//这里模板参数一定要用unsigned char,否则就一直报错
    48                 gray_diff.at<unsigned char>(i, j) = 255;
    49             else gray_diff.at<unsigned char>(i, j) = 0;
    50 
    51             imshow("foreground", gray_diff);
    52         }
    53         char c = (char)waitKey(10);
    54         if (c == 27)
    55         {
    56             break;
    57         }
    58         if (c == ' ')
    59             pause = !pause;
    60     }
    61     return 0;
    62 }

    效果:

  • 相关阅读:
    CSS3笔记3
    blackeye部署
    解决企业员工异地办公需求
    Django基于正则表达式匹配URL
    Ubuntu修改Apache默认Web端口
    Django基础篇
    jQuery学习笔记
    HDFS NFS Gateway 无法启动、挂载失败问题(CM集群安装HDFS)
    在cm安装的大数据管理平台中集成impala之后读取hive表中的数据的设置(hue当中执行impala的数据查询)
    在hue里面集成spark2,使用oozie的调度
  • 原文地址:https://www.cnblogs.com/beihaidao/p/5675841.html
Copyright © 2020-2023  润新知