OpenCV implements intelligent video surveillance

  • 2020-06-19 11:09:25
  • OfStack

This article shares the specific code of OpenCV to realize intelligent video surveillance for your reference. The specific content is as follows

Before in the completion of the design to find a complete implementation of the Internet code is very troublesome, I finished to share 1

Because the code is relatively simple, the code is not written in separate files, there is a need to integrate their own

Use environment Visual Studio 2010 and OpenCV 2.4.9


#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <ctime>
using namespace std;
using namespace cv;
 
int videoplay();
void on_Trackbar(int ,void*);
char* str_gettime();
int bSums(Mat src);

 
char g_str[17];
int g_nNum = 0;// Image name 
int g_nDelay = 0;
int g_npic = 0;
Mat g_filpdstMat;
int g_pointnum = 1000;// Set the pixel threshold to generate the image 
int g_pixel = 0;// pixel 
 
 
 
 
int main()
{
 
 VideoCapture capture(0);
 
 
 
 // Video output VideoWriter
 CvVideoWriter* outavi = NULL;
 //VideoWriter outavi;
 //outavi.open("sre.avi",-1, 5.0, Size(640, 480), true);
 outavi = cvCreateVideoWriter(" video .avi", -1, 5.0, cvSize(640, 480), 1);
 
 
 namedWindow(" camera ",WINDOW_AUTOSIZE);
 namedWindow(" Mobile trajectory ",WINDOW_AUTOSIZE);
 IplImage *pcpframe = NULL;
 
 
 Mat tempframe, currentframe, preframe, cpframe;
 Mat frame,jpg;
 int framenum = 0;
 // read 1 Frame processing 
 while (1)
 {
 if(!capture.isOpened())
 {
 cout << " Read failure " << endl;
 return -1;
 }
 
 
 capture >> frame;// Read the camera put each 1 The frame to frame
 
 frame.copyTo(cpframe);// the frame Assigned to cpframe Does not affect frame
 tempframe = frame;// the frame Assigned to tempframe , the impact frame
 
 flip(tempframe,g_filpdstMat,1);// Horizontal flip image 
 
 
 pcpframe = &IplImage(cpframe);// To release the window, put Mat into IplImage use 
 
 //cpframe=cvarrToMat(pcpframe);
 //ipl Transformation matrix  pBinary = &IplImage(Img)
 
 
 //7 The frame interception 1 Time input video, frequent interception operation can not come over 
 if(framenum % 7 == 0)
 {
 // The video to write 
 cvWriteFrame(outavi, pcpframe);
 }
 
 // Determine the number of frames, if is no 1 Frame, which is used as a contrast frame 
 // If it's greater than or equal to PI 2 Frame, frame difference method processing 
 framenum++; 
 
 if (framenum == 1)
 {
 cvtColor(g_filpdstMat, preframe, CV_BGR2GRAY);
 }
 if (framenum >= 2)
 {
 cvtColor(g_filpdstMat, currentframe, CV_BGR2GRAY);
 // grayscale 
 absdiff(currentframe,preframe,currentframe);// The frame difference method  
 threshold(currentframe, currentframe, 30, 255.0, CV_THRESH_BINARY);
 //2 threshold 
 
 erode(currentframe, currentframe,Mat());// corrosion 
 dilate(currentframe, currentframe,Mat());// inflation 
 
 
 g_pixel = bSums(currentframe);// Call a function bSums , calculate the white pixel points and assign to g_pixel
 // Output the current pixel value after a small delay, to prevent the data brush too fast to see clearly 
 g_nDelay++;
 if(g_nDelay > 5)
 {
 cout<< " Current white pixels: " <<g_pixel << endl;
 cout << " According to the ESC exit " << endl;
 g_nDelay = 0;
 }
 
 
 // Create the pixel point slider 
 createTrackbar(" Pixel: "," Mobile trajectory ",&g_pointnum, 20000,on_Trackbar);
 on_Trackbar(0, 0);// Call the callback function 
 
 
 // According to the image  
 imshow(" camera ", g_filpdstMat);
 imshow(" Mobile trajectory ", currentframe);
 
 }
 // Save the current frame as below 1 Before the second treatment 1 frame 
 cvtColor(g_filpdstMat, preframe, CV_BGR2GRAY);
 
 // Judge exit, and destroy the video window, otherwise under 1 The video cannot be opened 
 if((char)waitKey(10) == 27){cvReleaseVideoWriter(&outavi);break;}
 
 
 }//end while 
 
 while(1)
 {
 
 // Display prompt window 
 jpg = imread(" Mode selection .jpg", 1);
 imshow(" Mode selection ",jpg);
 
 // Set up the key Select operation 
 char key;
 key = waitKey(0);
 
 if(key == 'p' || key == 'P')// Play the video 
 videoplay();
 if(key == 'q' || key == 'Q')// exit 
 break;
 }
 return 0;
}
 
 
 
// Open the video 
int videoplay()
{
 VideoCapture video(" video .avi");
 if(!video.isOpened())
 {
 fprintf(stderr," Open the failure \n");
 return false;
 }
 while(1)
 {
 Mat frame;
 video>>frame;
 
 if(frame.empty())
 {
 break;
 }
 cvNamedWindow(" video ", CV_WINDOW_AUTOSIZE);
 imshow(" video ",frame);
 waitKey(30);
 }
 cvDestroyWindow(" video ");
 return 0;
}
 
 
 
// The slider sets a threshold to determine whether to save the current camera image 
void on_Trackbar(int ,void*)
{
 // Save pictures of people 
 if(g_pixel > g_pointnum)
 {
 g_npic++;
 if(g_npic > 5)// To avoid wind and wind, save the image after a small delay 
 {
 // Save the picture 
 cout << endl << endl;
 cout << " Site abnormal, alert response, ready to take photos ...\a" << endl; 
 imwrite(str_gettime(),g_filpdstMat);
 cout << " Current white pixels: " <<g_pixel << endl;
 cout << " According to the ESC exit " << endl;
 cout << endl;
 g_npic = 0;
 }
 }
}
 
 
// Get current date 
char* str_gettime()
{
 char tmpbuf[10];
 
 // from tz Set the time zone environment variable 
 _tzset();// Time function 
 
 // Display current date 
 _strdate(tmpbuf);
 g_str[0] = tmpbuf[6];
 g_str[1] = tmpbuf[7];
 g_str[2] = tmpbuf[0];
 g_str[3] = tmpbuf[1];
 g_str[4] = tmpbuf[3];
 g_str[5] = tmpbuf[4];
 
 _strtime(tmpbuf);
 // When a vehicle 
 g_str[6] = tmpbuf[0];
 g_str[7] = tmpbuf[1];
 g_str[8] = tmpbuf[3];
 g_str[9] = tmpbuf[4];
 g_str[10] = tmpbuf[6];
 g_str[11] = tmpbuf[7];
 
 // Provisions of pictures jpg format 
 g_str[12] = '.';
 g_str[13] = 'j';
 g_str[14] = 'p';
 g_str[15] = 'g';
 g_str[16] = '\0';
 
 
 // Displays the time to get the image 
 printf(" Generated image: %s\n", g_str);
 return g_str;
 
}
 
 
int bSums(Mat src)
{
 
 int counter = 0;
 // Iterators access pixel points 
 Mat_<uchar>::iterator it = src.begin<uchar>();
 Mat_<uchar>::iterator itend = src.end<uchar>(); 
 for (; it!=itend; ++it)
 {
 if((*it)>0) counter+=1;//2 After value, the pixel point is 0 or 255
 } 
 return counter;
}

Related articles: