[英]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来:
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 在处理它们之前,请先验证两者是否均有效
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.