With background subtraction MOG2 we get the shadow along but i want to use background subtraction MOG without shadow.The shadow of the person is being shown but i do not want to have the shadow at all.
When I'm using background subtraction MOG,the background substractor for MOG is not being recognised in opencv 3.1.
pMOG= new BackgroundSubtractorMOG();
int main(int argc, const char** argv)
{
// Init background substractor
Ptr pMOG = createBackgroundSubtractorMOG2().dynamicCast();
// Create empy input img, foreground and background image and foreground mask.
Mat img, foregroundMask, backgroundImage, foregroundImg;
// capture video
VideoCapture cap("C:/Users/Videos/video.mp4");
// main loop to grab sequence
for (;;) {
bool ok = cap.grab();
if (ok == false) {
cout << "Video File Not Found!" << std::endl;
}
else {
// obtain input image from source
cap.retrieve(img, CV_CAP_OPENNI_BGR_IMAGE);
// Just resize input image if you want
resize(img, img, Size(640, 480));
// create foreground mask of proper size
if (foregroundMask.empty()) {
foregroundMask.create(img.size(), img.type());
}
// compute foreground mask 8 bit image
// -1 is parameter that chose automatically your learning rate
pMOG->apply(img, foregroundMask, true ? -1 : 0);
// smooth the mask to reduce noise in image
GaussianBlur(foregroundMask, foregroundMask, Size(11, 11), 3.5, 3.5);
// threshold mask to saturate at black and white values
threshold(foregroundMask, foregroundMask, 10, 255, THRESH_BINARY);
// create black foreground image
foregroundImg = Scalar::all(0);
// Copy source image to foreground image only in area with white mask
img.copyTo(foregroundImg, foregroundMask);
//Get background image
pMOG->getBackgroundImage(backgroundImage);
// Show the results
imshow("foreground mask", foregroundMask);
imshow("foreground image", foregroundImg);
//save the results
imwrite("Result.jpg", foregroundMask);
int key6 = waitKey(40);
if (!backgroundImage.empty()) {
imshow("still background image", backgroundImage);
int key5 = waitKey(40);
}
}
}
return 0;
}
↧