OpenCV 将剪影与网络摄像头流隔离

OpenCV Isolate silhouette from webcam stream

本文关键字:摄像头 隔离 网络 OpenCV      更新时间:2023-10-16

我试图将一个人的轮廓与未知的视频流隔离开来。( 用户网络摄像头 ),使用 C++/Cinder/OpenCV 。我已经确定了轮廓并绘制了轮廓,但我没有得到整个人的轮廓,只是元素(头发,眼睛等)。

我正在使用:背景减法器MOG2 删除背景。模糊以消除噪音。自适应阈值。查找并绘制具有一定复杂性的轮廓。

法典:

Surface surface;
surface = mCapture.getSurface();
// To texture for display
textureCapture = Texture( surface );
    // Greyscale
    Mat matGrey( toOcv( surface ) );
    // Output
    Mat matForeground, matBackground;
    // Build foreground & background
    mog( matGrey, matForeground, -1 );
    mog.getBackgroundImage( matBackground );
// Build countours
Mat matContourTemp = matForeground.clone();
    // Blur to remove noise
    Mat matBlurred = matContourTemp.clone();
    cv::GaussianBlur( matContourTemp, matBlurred, cv::Size( 9, 9 ), 0 );
    textureBlurred = Texture( fromOcv( matBlurred ) );
    // Adaptive threshold
    Mat matThresh = matContourTemp.clone();
    adaptiveThreshold( matBlurred, matThresh, 255, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 3, 0 );
    textureThreshold = Texture( fromOcv( matThresh ) );
    // Contours
    vector<cv::Vec4i> hierarchy;
        // Find
        contours.clear();
        findContours( matThresh, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cv::Point( 0, 0 ) );
        // Draw contours
        Mat matContourImage( matForeground.size(), CV_8UC3, cv::Scalar( 0, 0, 0 ) );
        Scalar colors[ 3 ];
        colors[ 0 ] = Scalar( 255, 255, 255 );
        for( size_t idx = 0; idx < contours.size(); idx++){
            if( contours[ idx ].size() > 40 ){
                cv::drawContours(
                        matContourImage, contours, idx,
                        colors[ 0 ], -3,
                        100,
                        hierarchy,
                        0,
                        cv::Point( 0, 0 ) );
                };
    };
    textureContour = Texture( fromOcv( matContourImage ) );

输出:(我在这里太初级了,无法发布图像)

http://barnabysheeran.com/outgoing/stackoverflow/ss_1.pnghttp://barnabysheeran.com/outgoing/stackoverflow/ss_2.png

我希望这是一个全身充满的轮廓。

你可以在阈值化后使用侵蚀/扩张,这通常会消除噪音并拉伸白色区域,但我建议使用 BGSlibrary,我过去将它与 openFrameworks 结合使用,它确实是用于减去背景的不同算法的伟大集合。

此外,分割前的一些图像处理可能会有所帮助,但这只是一个理论。