• 简单帧差法


    代码来自: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 }

    效果:

  • 相关阅读:
    python3 解决tcp黏包方法一
    python3 udp不黏包但丢数据
    python3 类的单例模式
    python3 day07 练习题
    python3 tcp黏包情况二
    python3 tcp黏包情况一
    python3 subprocess模块
    python3 UDP协议下的socket
    python3 TCP协议下的socket
    python3 socket网络通信的整个流程
  • 原文地址:https://www.cnblogs.com/beihaidao/p/5675841.html
Copyright © 2020-2023  润新知