首页 > 解决方案 > 在视频 OpenCV C++ 中去除阴影并添加跟踪

问题描述

上一个输出

以上是我从代码中得到的输出,但是图像中有大量阴影,有什么方法可以去除阴影吗?并且还添加了为移动汽车创建盒子的对象跟踪?太感谢了

//create Background Subtractor objects
Ptr < BackgroundSubtractor > pBackSub;
if (parser.get <String>("algo") == "MOG2")
    pBackSub = createBackgroundSubtractorMOG2();
VideoCapture capture(parser.get <String>("input")); //input video
Mat frame, fgMask;
while (true) {
    capture >> frame;
    if (frame.empty()) //break if frame empty
        break;
    //update the background model
    pBackSub - > apply(frame, fgMask);
    //erode the frame with 3x3 kernel
    Mat frame_eroded_with_3x3_kernel;
    erode(fgMask, frame_eroded_with_3x3_kernel, getStructuringElement(MORPH_RECT, Size(3, 3)));
    //dilate the frame with 2x2 kernel
    Mat frame_dilate_with_2x2_kernel;
    dilate(frame_eroded_with_3x3_kernel, frame_dilate_with_2x2_kernel, getStructuringElement(MORPH_RECT, Size(2, 2)));
    //show the current frame and the fg mask
    imshow("Frame", frame);
    imshow("FG Mask", fgMask);
    imshow("After eroded with 3x3 kernel", frame_eroded_with_3x3_kernel);
    imshow("After dilate with 2x2 kernel", frame_dilate_with_2x2_kernel);
    //get the input from the keyboard
    int keyboard = waitKey(30);
    if (keyboard == 'q' || keyboard == 27)
        break;
}
return 0;
}

标签: c++opencv

解决方案


您的输出可能是正确的。首先不要使用移动摄像机视频。场景也需要稳定且光照条件良好。您可以尝试 MOG2 设置的不同参数。历史影响先前框架如何影响当前。varThreshold 可以极大地帮助您。detectShadows=false 比较好,你可以试试 false 和 true 看看区别。您可以删除检测到的阴影,但这些方法有局限性。

cv::createBackgroundSubtractorMOG2 (int history=500, double varThreshold=16, bool detectShadows=true)

您可以通过使用额外的过滤和形态学操作来增强输出,例如在噪声有用的情况下。搜索以下两个功能的相关信息并尝试应用。

cv::dilate
cv::erode

重点很简单。不要指望奇迹。这不适合计算机视觉中的许多任务。在大多数应用程序中,检测和其他任务不基于背景减法。在下图中,由于车灯改变条件和阴影,背景减法失败。 汽车检测

检测基于代表汽车的特征,而不是检测不是背景的东西。对于大多数应用程序来说,这是更好的方法。Haar,LBP 检测或深度学习。您可以在我的页面funvision上找到许多检测教程


推荐阅读