Opencv learning-security monitoring suspicious walking alarm

Source: Internet
Author: User
Opencv learning-security monitoring suspicious walking alarm

/*
* ===================================================== ========================================================== ======
*
* Filename: motiondetect. c
*
* Description: A method of detecting the motion
*
* Version: 1.0
* Created: 03:32:21
* Revision: None
* Compiler: gcc
*
* Author: futuredaemon (BUPT), gnuhpc@gmail.com
* Company: bupt_united
*
* ===================================================== ========================================================== ======
*/
# Include <stdio. h>
# Include <time. h>
# Include <cv. h>
# Include <cxcore. h>
# Include

Int main (INT argc, char ** argv)
{
// Declare the iplimage pointer
Iplimage * pframe = NULL; // pframe is a frame captured by a video.
Iplimage * pframe1 = NULL; // the first frame.
Iplimage * pframe2 = NULL; // second Frame
Iplimage * pframe3 = NULL; // The third frame.

Iplimage * pfrimg = NULL; // pfrimg indicates the grayscale image of the current frame.
Iplimage * pbkimg = NULL; // pbkimg indicates the grayscale image of the current background.
Iplimage * pbkimgtran = NULL; // pbkimgtran indicates the image processed by the current background.
Iplimage * pfrimgtran = NULL; // pfrimgtran indicates the image processed by the current background.

Cvmat * pframemat = NULL; // pframemat indicates the current grayscale matrix.
Cvmat * pfrmat = NULL; // The pfrmat is the current foreground graph matrix. The current frame minus the background image.
Cvmat * Bg1 = NULL;
Cvmat * bg2 = NULL;
Cvmat * bg3 = NULL;
Cvmat * pfrmatb = NULL; // prospect of pfrmatb binarization ()
Cvmat * pbkmat = NULL;
Cvmat * pzeromat = NULL; // used to calculate the Bg1-bg2 Value
Cvmat * pzeromatb = NULL; // used to calculate the pzeromat threshold and then determine the number of temporary matrices with zeros

Cvcapture * pcapture = NULL;

Int warningnum = 0; // number of times a foreign body intrusion is detected
Int nfrmnum = 0; // Frame Count
Int status = 0; // status flag

// Create a window
Cvnamedwindow ("video", 1 );
Cvnamedwindow ("background", 1); // background
Cvnamedwindow ("foreground", 1); // foreground
// Arrange the window in sequence
Cvmovewindow ("video", 30, 0 );
Cvmovewindow ("background", 360, 0 );
Cvmovewindow ("foreground", 690, 0 );

If (argc> 2)
{
Fprintf (stderr, "Usage: bkgrd [video_file_name]/n ");
Return-1;
}

// Open the camera
If (argc = 1)
If (! (Pcapture = cvcapturefromcam (-1 )))
{
Fprintf (stderr, "can not open camera./N ");
Return-2;
}

// Open the video file
If (argc = 2)
If (! (Pcapture = cvcapturefromfile (argv [1])
{
Fprintf (stderr, "can not open video file % s/n", argv [1]);
Return-2;
}

// Start timing
Time_t start, end;
Time (& START); // time () returns the number of seconds (10 digits) Since 00:00:00, January 1, January 1, 1970 ).
Printf ("% d/N", start );
// Read the video frame by frame
While (pframe = cvqueryframe (pcapture ))
{
Nfrmnum ++;
// If it is the first frame, the memory needs to be applied and initialized
If (nfrmnum = 1)
{

Pbkimg = cvcreateimage (cvsize (pframe-> width, pframe-> height), ipl_depth_8u, 1 );
Pfrimg = cvcreateimage (cvsize (pframe-> width, pframe-> height), ipl_depth_8u, 1 );
Pbkimgtran = cvcreateimage (cvsize (pframe-> width, pframe-> height), ipl_depth_8u, 1 );
Pfrimgtran = cvcreateimage (cvsize (pframe-> width, pframe-> height), ipl_depth_8u, 1 );

Pbkmat = cvcreatemat (pframe-> height, pframe-> width, cv_32fc1 );
Pzeromat = cvcreatemat (pframe-> height, pframe-> width, cv_32fc1 );
Pfrmat = cvcreatemat (pframe-> height, pframe-> width, cv_32fc1 );
Pfrmatb = cvcreatemat (pframe-> height, pframe-> width, cv_8uc1 );
Pzeromatb = cvcreatemat (pframe-> height, pframe-> width, cv_8uc1 );
Pframemat = cvcreatemat (pframe-> height, pframe-> width, cv_32fc1 );
Cvzero (pzeromat );
// Convert to a single-channel image for reprocessing
Cvcvtcolor (pframe, pbkimg, cv_bgr2gray );
// Convert to a matrix
Cvconvert (pfrimg, pbkmat );
}
Else/* this is not the first frame */
{
// Pfrimg indicates the grayscale image of the current frame.
Cvcvtcolor (pframe, pfrimg, cv_bgr2gray );

// Pframemat indicates the current grayscale matrix.
Cvconvert (pfrimg, pframemat );

// Pfrmat is the foreground map matrix. The current frame minus the background image.
Cvabsdiff (pframemat, pbkmat, pfrmat );

// Foreground diagram of pfrmatb binarization (0, 1)
Cvthreshold (pfrmat, pfrmatb, 60, 1, cv_thresh_binary );

// Convert the image matrix to the image format for display
Cvconvert (pbkmat, pbkimgtran );
Cvconvert (pfrmat, pfrimgtran );

// Display the image
Cvshowimage ("video", pframe );
Cvshowimage ("background", pbkimgtran); // display the background
Cvshowimage ("foreground", pfrimgtran); // display the foreground

// The above is the work to be done for each captured frame. The following is a risk detection
If (cvcountnonzero (pfrmatb)> 10000 & status = 0) // indicates that the first frame contains more than images
{/* Save the current frame as the first frame */
Pframe1 = cvcloneimage (pframe );
Bg1 = cvclonemat (pfrmat );
Status = 1; // continue to collect 2nd Frames
}
Else if (cvcountnonzero (pfrmatb) <10000 & status = 1) // indicates that the foreign body in the first frame is greater than bytes, and the second frame is not, an alarm is triggered.
{
Printf ("No. % d warning !!!! /N ", warningnum ++ );
Status = 0;
}
Else if (cvcountnonzero (pfrmatb)> 10000 & status = 1) // indicates that the foreign body in the first and second frames is larger than bytes.
{
Pframe2 = cvcloneimage (pframe );
Bg2 = cvclonemat (pfrmat );

Cvabsdiff (Bg1, bg2, pzeromat );
Cvthreshold (pzeromat, pzeromatb, 20, 1, cv_thresh_binary );
If (cvcountnonzero (pzeromatb)> 3000) // indicates they are not continuous. In this case, an alarm is triggered.
{
Printf ("No. % d warning !!!! /N ", warningnum ++ );
Status = 0;
}
Else
{
Status = 2; // continue to collect 3rd Frames
}
}
Else if (cvcountnonzero (pfrmatb) <10000 & status = 2) // indicates that the foreign body in the first and second frames is larger than bytes, but the third frame does not
{
// Alarm
Printf ("No. % d warning !!!! /N ", warningnum ++ );
Status = 0;
}
Else if (cvcountnonzero (pfrmatb)> 10000 & status = 2) // It indicates that the number of foreign bodies in three consecutive frames is greater than bytes.
{
Pframe3 = cvcloneimage (pframe );
Bg3 = cvclonemat (pfrmat );

Cvabsdiff (bg2, bg3, pzeromat );
Cvthreshold (pzeromat, pzeromatb, 20, 1, cv_thresh_binary );
If (cvcountnonzero (pzeromatb)> 3000) // indicates they are not continuous. In this case, an alarm is triggered.
{
Printf ("No. % d warning !!!! /N ", warningnum ++ );
}
Else // indicates bg2, bg3 continuous
{
Cvreleasemat (& pbkmat );
Pbkmat = cvclonemat (pframemat); // update the background
}
Status = 0; // enter the next collection process
}

// If there is a button event, it will jump out of the loop
// This wait time also provides time completion display for the cvshowimage Function
// The wait time can be adjusted based on the CPU speed
If (cvwaitkey (2)> = 0)
Break;
}/* The End Of The else */
}/* The end of th while */

// Destruction window
Cvdestroywindow ("video ");
Cvdestroywindow ("background ");
Cvdestroywindow ("foreground ");

// Release the image and Matrix
Cvreleaseimage (& pfrimg );
Cvreleaseimage (& pbkimg );

Cvreleasemat (& pframemat );
Cvreleasemat (& pfrmat );
Cvreleasemat (& pbkmat );

Cvreleasecapture (& pcapture );

Return 0;
}

The following describes how to implement motion detection and background update:

(1) Open up static memory and initialize and prepare for acquisition of images;

(2) collect the image and define the parameter K as the image sequence count. When 1st images are collected, the matrix and image are initialized based on the size information of the first frame, and the first frame is dimmed and converted into a matrix, as the background image and matrix. If K is not equal to 1, the current frame is grayscale and converted into a matrix as the image and matrix of the current frame. Use the image matrix of the current frame and the image matrix of the background frame to calculate the foreground map matrix and perform binarization to calculate the number of pixels that are significantly different from the background frame, that is, the number of zeros after binarization.

When the foreign body in the first frame is larger than bytes, the current frame needs to be stored as the first frame, and the state of the system is changed to 1-the second frame is collected;

When the foreign body of the first and second frames is greater than image points, the current frame is stored as the second frame. The two frames are consecutive by judging the difference between the first and second frames, if it is continuous, the system status is changed to 2 -- the third frame is collected. If it is not continuous, an alarm is triggered, and the system status is changed to 0 -- the background frame is collected;

When the foreign body in the first and second frames is larger than bytes, but the third frame is not, an alarm is triggered;

If the foreign body of three consecutive frames is greater than image points, the current frame is stored as the third frame. The difference between the second frame and the third frame is determined to determine whether the two frames are consecutive, if this parameter is set to continuous, the background is updated. If the parameter is set to discontinuous, an alarm is triggered. Then convert the system status to 0-capture the background frame.

Note that there is a state machine between 0-1-2-0.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.