OpenCV implements a simple camera video monitoring program
- 2020-06-19 11:08:58
- OfStack
How to find the key points in a lengthy surveillance video? We know that most of the information in the surveillance video is useless, which is equivalent to a still image. We need to wait for the monitoring of the scope of the abnormal conditions before tracking.
This is actually one of the simplest computer image processing program. The simple idea is this: First sample the camera, and when the background is stable, use the image as a reference. Then, during the monitoring process, if a video frame with a large contrast to the baseline image appears, the capture program is started and the abnormal area is marked.
The procedure is as follows:
#include <cv.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <highgui.h>
#define ESC 0x1b
#define TRUE 1
#define FALSE 0
// Detect image exceptions, only called at sampling time.
// Returning true indicates that an exception has been detected and a resampling is required.
// Return false to indicate that no exception was detected in 1 The reference image can be obtained after fixed time.
int detect(CvCapture* capture, IplImage* std, IplImage* frm, CvRect* rect);
// Image sampling to determine the baseline image in order to monitor scene changes
// Return true to indicate successful sampling, return false to indicate failed sampling
int gather(CvCapture* capture, IplImage* std, CvRect* rect);
// Video camera surveillance, rectangular box marked with a large contrast to the baseline image range.
void monitor(CvCapture* capture, IplImage* std, CvRect* rect);
// o x The square of
int square(int x);
int main(int argc, char* argv[])
{
CvCapture* capture; // Camera source
IplImage* std; // Benchmark image
CvRect rect; // The exception location is identified by a rectangle
capture = cvCreateCameraCapture(0);
if (!capture) return -1;
std = cvQueryFrame(capture);
rect = cvRect(-1, -1, 0, 0);
std = cvCloneImage(std);
cvNamedWindow("Monitor Screen");
if (gather(capture, std, &rect))
{
monitor(capture, std, &rect);
}
cvDestroyWindow("Monitor Screen");
cvReleaseImage(&std);
cvReleaseCapture(&capture);
return 0;
}
int detect(CvCapture* capture, IplImage* std, IplImage* frm, CvRect* rect)
{
int x, y; // Loop variables
int f = FALSE; // An identifier that detected an exception
int x1 = -1, x2 = 0; // The range of abscissa of the abnormal area
int y1 = -1, y2 = 0; // The ordinate range of the rectangle in the abnormal area
uchar *ptr1b, *ptr1g, *ptr1r; // Reference image for each pixel 3 The value of the color channel
uchar *ptr2b, *ptr2g, *ptr2r; // Live image per pixel 3 The value of the color channel
int squaresum; // To calculate RGB Sum of squares of difference
// Iterate over each of the images 1 The real-time sampling graph is compared with the benchmark graph to detect each of the two 1 a
// The pixel RGB Sum of squares of difference. When the value is greater than 8192 When (converted to grayscale means
// The gray difference between the two is greater than 90 ), an exception is immediately reported, and only after the traversal is completed, no difference is found
// Frequent reports of no abnormalities.
for (y = 0; y < std->height; y++)
{
for (x = 0; x < std->width; x++)
{
ptr1b = cvPtr2D(std, y, x) + 0; ptr2b = cvPtr2D(frm, y, x) + 0;
ptr1g = cvPtr2D(std, y, x) + 1; ptr2g = cvPtr2D(frm, y, x) + 1;
ptr1r = cvPtr2D(std, y, x) + 2; ptr2r = cvPtr2D(frm, y, x) + 2;
squaresum =
square(*ptr1b - *ptr2b) +
square(*ptr1g - *ptr2g) +
square(*ptr1r - *ptr2r);
if (squaresum > 8192)
{
if (f)
{
if (x < x1) x1 = x; else if (x > x2) x2 = x;
if (y < y1) y1 = y; else if (y > y2) y2 = y;
}
else
{
f = TRUE;
x1 = x; y1 = y;
x2 = x; y2 = y;
}
}
}
}
if (x2 - x1 > frm->width / 4 || y2 - y1 > frm->height / 4)
{
f = TRUE;
}
else
{
f = FALSE;
}
*rect = cvRect(x1, y1, x2 - x1, y2 - y1);
return f;
}
int gather(CvCapture* capture, IplImage* std, CvRect* rect)
{
IplImage* frm;
int except = FALSE; // An identifier that detected an exception
int finish = FALSE; // Sample the completed identification
clock_t start_time, real_time; // Temporal monitoring
start_time = clock();
while (!finish)
{
frm = cvQueryFrame(capture);
cvShowImage("Monitor Screen", frm);
except = detect(capture, std, frm, rect);
if (except)
{
start_time = clock();
cvCopyImage(frm, std);
}
if (cvWaitKey(15) == ESC) break;
real_time = clock();
if (real_time - start_time >= 3000)
{
finish = TRUE;
}
}
return finish;
}
void monitor(CvCapture* capture, IplImage* std, CvRect* rect)
{
IplImage* frm;
int except = FALSE;
int finish = FALSE;
while (!finish)
{
frm = cvQueryFrame(capture);
except = detect(capture, std, frm, rect);
if (except)
{
cvRectangle(
frm,
cvPoint(rect->x, rect->y),
cvPoint(rect->x + rect->width, rect->y + rect->height),
cvScalar(0, 0, 255),
4);
}
cvShowImage("Monitor Screen", frm);
if (cvWaitKey(15) == ESC)
{
finish = TRUE;
}
}
}
int square(int x)
{
return x * x;
}