Advertisement
alextrof94

OpenCV_16-03-02_cam_a

Mar 4th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 KB | None | 0 0
  1. #include "opencv2/objdetect.hpp"
  2. #include "opencv2/highgui.hpp"
  3. #include "opencv2/imgproc.hpp"
  4. #include <iostream>
  5. #include <direct.h>
  6. #include <ctime>
  7. #include "SideCode.cpp";
  8.  
  9. using namespace std;
  10. using namespace cv;
  11.  
  12. int main(int argc, const char** argv)
  13. {
  14.     VideoCapture vc;
  15.     VideoWriter vw;
  16.     Mat mask, frameNow, frame, grayFrame, oldGrayFrame, diffFrame;
  17.     string _pathToCam, _output, _maskFile;
  18.     int _fps, _threshold, _delay;
  19.     bool useMask = false, _showDiff, _showNormal, _showSmall, _consoleNormed;
  20.  
  21.     /* input */
  22.         cv::CommandLineParser parser(argc, argv,
  23.             "{pathtocam|rtsp://192.168.2.2:554/user=admin&password=&channel=1&stream=0.sdp?real_stream--rtp-caching=100|}"
  24.             //"{pathtocam||}"
  25.             "{output|records/|}"
  26.             "{maskfile| |}"
  27.             "{fps|25|}"
  28.             "{threshold|2000|}"
  29.             "{diff||}"
  30.             "{normal||}"
  31.             "{small|1|}"
  32.             "{delay|5|}"
  33.             "{normed||}"
  34.             );
  35.         _pathToCam = parser.get<string>("pathtocam");
  36.         cerr << "_pathToCam = " + _pathToCam << endl;
  37.         _output = parser.get<string>("output");
  38.         cerr << "output = " + _output << endl;
  39.         _maskFile = parser.get<string>("maskfile");
  40.         cerr << "maskfile = " + _maskFile << endl;
  41.         _fps = parser.get<int>("fps");
  42.         cerr << "fps = " + to_string(_fps) << endl;
  43.         _threshold = parser.get<int>("threshold");
  44.         cerr << "threshold = " + to_string(_threshold) << endl;
  45.         _delay = parser.get<int>("delay");
  46.         cerr << "delay = " + to_string(_delay) << endl;
  47.         _showDiff = parser.has("diff");
  48.         cerr << "diff = " + to_string(_showDiff) << endl;
  49.         _showNormal = parser.has("normal");
  50.         cerr << "normal = " + to_string(_showNormal) << endl;
  51.         _showSmall = parser.has("small");
  52.         cerr << "small = " + to_string(_showSmall) << endl;
  53.         _consoleNormed = parser.has("normed");
  54.         cerr << "normed = " + to_string(_consoleNormed) << endl;
  55.  
  56.         if (!parser.check())
  57.         {
  58.             parser.printErrors();
  59.             return 0;
  60.         }
  61.     /* end input */
  62.  
  63.     my_mkdir(_output.c_str());
  64.  
  65.     if (_maskFile.length() > 1)
  66.     {
  67.         useMask = true;
  68.         mask = imread(_maskFile);
  69.         if (!mask.empty())
  70.             cerr << "Mask loaded" << endl;
  71.     }
  72.  
  73.     if (_pathToCam.length() > 1)
  74.         vc.open(_pathToCam);
  75.     else
  76.         vc.open(0);
  77.  
  78.     if (!vc.isOpened())
  79.     {
  80.         cerr << "Can't open stream!";
  81.         return 1;
  82.     }
  83.     vc.set(CAP_PROP_FPS, _fps);
  84.  
  85.     int frame_width = (int)vc.get(CV_CAP_PROP_FRAME_WIDTH);
  86.     int frame_height = (int)vc.get(CV_CAP_PROP_FRAME_HEIGHT);
  87.  
  88.     int timeForSave = 0;
  89.     int delayConsoleThreshold = 10;
  90.     int normed;
  91.     string normedStr = "";
  92.     string fn;
  93.     char c;
  94.     while (1)
  95.     {
  96.         vc >> frameNow;
  97.  
  98.         /* mask */
  99.             if (useMask)
  100.                 frameNow.copyTo(frame, mask);
  101.             else
  102.                 frameNow.copyTo(frame);
  103.         /* end mask */
  104.  
  105.         // show normal size
  106.         if (_showNormal)
  107.             imshow("normal", frame);
  108.  
  109.         //resize
  110.         resize(frame, frame, Size(300, 200));
  111.  
  112.         // show small
  113.         if (_showSmall)
  114.             imshow("small", frame);
  115.        
  116.         // set rgb color
  117.         cvtColor(frame, grayFrame, CV_BGR2GRAY);
  118.  
  119.         // preload old-gray
  120.         if (oldGrayFrame.empty())
  121.             grayFrame.copyTo(oldGrayFrame);
  122.  
  123.         /* get visual diff */
  124.             if (_showDiff)
  125.             {
  126.                 absdiff(oldGrayFrame, grayFrame, diffFrame);
  127.                 threshold(diffFrame, diffFrame, 25, 255, cv::THRESH_BINARY);
  128.                 //imshow("gray", grayFrame);
  129.                 imshow("diff", diffFrame);
  130.             }
  131.         /* end get visual diff */
  132.  
  133.         /* record */
  134.             normed = norm(oldGrayFrame, grayFrame);
  135.             /* console normed output */
  136.                 if (_consoleNormed)
  137.                 {
  138.                     normedStr += to_string(normed) + " | ";
  139.                     delayConsoleThreshold--;
  140.                     if (delayConsoleThreshold == 0)
  141.                     {
  142.                         delayConsoleThreshold = _fps;
  143.                         cerr << "normed = " + normedStr << endl;
  144.                         normedStr = "";
  145.                     }
  146.                 }
  147.             /* end console normed output */
  148.             if (normed > _threshold)
  149.             {
  150.                 if (timeForSave == 0)
  151.                 {
  152.                     fn = _output + to_string(time(0)) + ".avi";
  153.                     vw.open(fn, CV_FOURCC('X', '2', '6', '4'), _fps, Size(frame_width, frame_height), true);
  154.                     cerr << "Start recording into " + fn << endl;
  155.                 }
  156.                 timeForSave = _fps * _delay;
  157.             }
  158.  
  159.             if (timeForSave > 0)
  160.             {
  161.                 timeForSave--;
  162.                 vw.write(frameNow);
  163.                 if (timeForSave == 0)
  164.                 {
  165.                     vw.release();
  166.                     cerr << "Recorded" << endl;
  167.                 }
  168.             }
  169.         /* end record */
  170.  
  171.         grayFrame.copyTo(oldGrayFrame);
  172.  
  173.         c = (char)waitKey(1);
  174.         if (c == 'q')
  175.             break;
  176.     }
  177.  
  178.     vc.release();
  179.     vw.release();
  180.     cv::destroyAllWindows();
  181.     return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement