Ideal motion/object tracking method - Problems with image noise
Hi all,
I would like to get an idea of the best way to go about tracking people with an ceiling-mounted IP camera. Ultimately, I would like to have an application that does the following:
- show the video feed in a primary window
- identifies moving objects then assigns them with an identifier of some kind (object1, object2, etc.)
- in the primary window, draw a rectangle around each object and add the object ID to it as a tag
- capture the x and y coordinates of the object or rectangle, preferably the center (it seems that this can be done using the moments() method)
- in a second window show a binary image of blobs/people in black and everything else in white
- on the binary image, draw a line from the previous x,y coordinates of each object to the current coordinates
EDIT: I am currently getting far too much noise in my binary image, and have not been able to limit it with the filters I'm using. I'm also unable to track and tag multiple objects. This may be the result of the example I borrowed from, in which only the object with the largest area is tagged. Unfortunately, I have not been able to fix this.
main.cpp - full code found in this Gist
int main(int argc, char *argv[])
{
// get cam feed and show in new window
VideoCapture cap(0);
if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the video file" << endl;
return -1;
}
setwindowSettings(); // create window with trackers that adjust filter settings
// background subtraction init
BackgroundSubtractorMOG2 bg = BackgroundSubtractorMOG2(hist,thresh,false);
cap.read(curVid);
Sleep(5000);
// show images in windows
while(1)
{
// show camera feed
bool bSuccess = cap.read(curVid); // read a new frame from video
if (!bSuccess) // if not success, break loop
{
cout << "Cannot read a frame from video file" << endl;
break;
}
// start morph functions then draw
HSV(); // apply cvtColor(curVid,imgHSV,CV_BGR2HSV);
Erosion( 0, 0 ); // choose erosion type and apply erode(imgHSV,erosion_dst,element);
Dilation( 0, 0 ); // choose dilation type and apply dilate(erosion_dst,dilation_dst,element);
// background subtraction
bg(dilation_dst,mask,-1);
imshow("mask",mask);
// find and draw contours to Mat test
trackFilteredObject();
imshow("test", test);
// escape program with keystroke
if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}