Moving object detection algorithm based on OpenCv
- 2020-06-01 10:45:41
- OfStack
An OpenCv-based moving object detection algorithm based on 1 implementation can be used to detect pedestrians or other moving objects.
#include <stdio.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
int main( int argc, char** argv )
// The statement IplImage Pointer to the
IplImage* pFrame = NULL;
IplImage* pFrImg = NULL;
IplImage* pBkImg = NULL;
CvMat* pFrameMat = NULL;
CvMat* pFrMat = NULL;
CvMat* pBkMat = NULL;
CvCapture* pCapture = NULL;
int nFrmNum = 0;
// Create a window
cvNamedWindow("video", 1);
cvNamedWindow("background",1);
cvNamedWindow("foreground",1);
// Arrange the Windows in order
cvMoveWindow("video", 30, 0);
cvMoveWindow("background", 360, 0);
cvMoveWindow("foreground", 690, 0);
argc = 1;
if( argc > 2 )
{
fprintf(stderr, "Usage: bkgrd [video_file_name]\n");
return -1;
}
// Turn on 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;
}
// The video is read frame by frame
while(pFrame = cvQueryFrame( pCapture ))
{
nFrmNum++;
// If it is the first 1 Frame, need to apply memory, and initialize
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);
pBkMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
pFrMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
pFrameMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
// Convert to single channel image for reprocessing
cvCvtColor(pFrame, pBkImg, CV_BGR2GRAY);
cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY);
cvConvert(pFrImg, pFrameMat);
cvConvert(pFrImg, pFrMat);
cvConvert(pFrImg, pBkMat);
}
else
{
cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY);
cvConvert(pFrImg, pFrameMat);
// A gaussian filter is used to smooth the image
//cvSmooth(pFrameMat, pFrameMat, CV_GAUSSIAN, 3, 0, 0);
// Subtract the current frame from the background
cvAbsDiff(pFrameMat, pBkMat, pFrMat);
//2 Valuate the foreground diagram
cvThreshold(pFrMat, pFrImg, 60, 255.0, CV_THRESH_BINARY);
// Morphological filtering was performed to remove the noise
//cvErode(pFrImg, pFrImg, 0, 1);
//cvDilate(pFrImg, pFrImg, 0, 1);
// Update background
cvRunningAvg(pFrameMat, pBkMat, 0.003, 0);
// Converts the background to an image format for display
cvConvert(pBkMat, pBkImg);
// According to the image
cvShowImage("video", pFrame);
cvShowImage("background", pBkImg);
cvShowImage("foreground", pFrImg);
// If there is a key event, the loop is broken
// This wait is also cvShowImage The function provides the time to complete the display
// Wait time can be based on CPU Speed adjustment
if( cvWaitKey(2) >= 0 )
break;
}
}
// Destruction of the window
cvDestroyWindow("video");
cvDestroyWindow("background");
cvDestroyWindow("foreground");
// Release image and matrix
cvReleaseImage(&pFrImg);
cvReleaseImage(&pBkImg);
cvReleaseMat(&pFrameMat);
cvReleaseMat(&pFrMat);
cvReleaseMat(&pBkMat);
cvReleaseCapture(&pCapture);
return 0;
}