简体   繁体   English

OpenCV C ++视频捕获图像背景减去

[英]OpenCV C++ videocapture image background subtracting

I am trying to make a simple program that will subtract an object from a background with OpenCV and C++. 我正在尝试制作一个简单的程序,该程序将使用OpenCV和C ++从背景中减去一个对象。

The idea is to use the VideoCapture to: 这个想法是使用VideoCapture来:

  • Capture a static background (without the object) 捕获静态背景(没有对象)
  • Then continuously capture the current frame and subtract that from the background 然后连续捕获当前帧并从背景中减去当前帧

However, I have a problem when sending the captured data to my BackgroundSubtraction() function. 但是,将捕获的数据发送到BackgroundSubtraction()函数时遇到问题。 It gives me an error: 它给我一个错误:

Unhandled exception at 0x77d815 in OpenCV_BackgroundSubtraction.exe: 0xC000005: Acces violation regarding location 0x04e30050 OpenCV_BackgroundSubtraction.exe中0x77d815的未处理异常:0xC000005:与位置0x04e30050有关的访问冲突

However, sometimes it seems to work, other times not (using Visual Studio 2010 C++ on Windows 7 64-bit). 但是,有时它似乎可以正常运行,而其他时候却不起作用(在Windows 7 64位系统上使用Visual Studio 2010 C ++)。

I have a feeling that it has something to do with memory allocation and priority of the functions. 我觉得它与内存分配和功能的优先级有关。 It seems like the VideoCapture grabber maybe is not fast enough the grab/write the frames before I send it to BackgroundSubtraction(). 似乎VideoCapture采集卡在将其发送到BackgroundSubtraction()之前,抓取/写入帧的速度可能不够快。

The built-in webcam in my laptop works fine (ie it shows a picture), but something in my code is wrong. 我的笔记本电脑中的内置网络摄像头工作正常(即显示图片),但是我的代码中有错误。 I have tried playing around with some delays, but it doesn't seem to have an influence. 我尝试了一些延迟,但似乎没有影响。

Here is my code: 这是我的代码:

Mat BackgroundSubtraction(Mat background, Mat current);

int main()
{
Mat colorImage;
Mat gray;

// Background subtraction
Mat backgroundImage;
Mat currentImage;
Mat object; // the object to track

VideoCapture capture, capture2;

capture2.open(0);

// Initial frame
while (backgroundImage.empty())
{
    capture2 >> backgroundImage;
    cv::imshow("Background", backgroundImage);
    waitKey(100);
    capture2.release();

}

capture.open(0);

// Tracking the object
while (true)
{
    capture >> currentImage;

    if ((char)waitKey(300) == 'q') // Small delay
        break;

            // The problem happens when calling BackgroundSubtraction()
    object = BackgroundSubtraction(backgroundImage, backgroundImage);
    cv::imshow("Current frame", currentImage);
    cv::imshow("Object", object);
}

Mat BackgroundSubtraction(Mat background, Mat current)
{

    // Convert to black and white
Mat background_bw;
Mat current_bw;
cvtColor(background, background_bw, CV_RGB2GRAY);
cvtColor(current, current_bw, CV_RGB2GRAY);

Mat newObject(background_bw.rows, background_bw.cols, CV_8UC1);

for (int y = 0; y < newObject.rows; y++)
{
    for (int x = 0; x < newObject.cols; x++)
    {
                    // Subtract the two images
                    newObject.at<uchar>(y, x) = background_bw.at<uchar>(y, x)
            - current_bw.at<uchar>(y, x);
    }
}

return newObject;
}

Thanks in advance! 提前致谢!

Ps. 附言 Even though there might be some built-in functions to do the work, I would rather make the algorithm myself. 即使可能有一些内置函数可以完成这项工作,我还是希望自己编写算法。

There are several things that you could try to change to identify your problem. 您可以尝试更改几项以识别问题。 But my guess would be that one of the images that you pass to the subtracter function is not valid. 但是我的猜测是您传递给减法器功能的图像之一无效。 Please verify that both are in fact valid before processing them 在处理它们之前,请先验证两者是否均有效

  1. You should make sure that your captures are created and released correctly. 您应该确保正确创建并释放了捕获。 I cant remember whether the capture.release() is busy-waiting or not, but I would verify that the capture is working. 我记不住capture.release()是否正在等待,但是我将验证捕获是否正常工作。 Actually i would probably only use a single capture, since your using only one camera. 实际上,由于您仅使用一个摄像头,因此我可能只会使用单个捕获。
  2. Your not verifying that your currentImage is actually created, and thereby valid. 您没有验证您的currentImage是否已实际创建并因此有效。
  3. Also I remember that sometime ago it was not possible to modify the image created by the capture object, but one had to make a copy of the frame before processing it. 我还记得,有时无法修改捕获对象创建的图像,但是必须在处理帧之前对其进行复制。 You could try this as well 您也可以尝试
  4. Additionally i noted that you are passing the same image for both the foreground and background image. 另外,我注意到您为前景和背景图像传递了相同的图像。 It's not a problem per se, but probably not what you want to do. 本质上这不是问题,但可能不是您想要做的。

And finally you should try to debug your program to identify the exact place in your code where an exception is thrown. 最后,您应该尝试调试程序,以识别代码中引发异常的确切位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM