OpenCV 中的背景扣除(C++)
我想实现一种背景平均方法。我在一秒钟内拍摄了 50 帧图像,其中一些帧包含闪电,我想将其提取为前景。这些帧是用固定相机拍摄的,并且帧被拍摄为灰度。我想做的是:
- 获取背景模型
- 后,将每一帧与背景模型进行比较,以确定该帧中是否有光照。
我阅读了一些有关如何使用 cvAcc() 来完成此操作的文档,但我很难理解如何完成此操作。我希望有一段代码可以指导我并链接到可以帮助我理解如何实现这一点的文档。
预先感谢您。
I want to implement a background averaging method. I have 50 frames of images taken in one second and some of the frames contain lightning which I want to extract as the foreground. The frames are taken with a stationary camera and the frames are taken as grayscales. What I want to do is:
- Get the background model
- After, compare each frame to the background model to determine whether there is lighting in that frame or not.
I read some documents on how this can possible be done by using cvAcc() but am having a difficulty understanding how this can be done. I would appreciate a piece of code which guide me and links to documents that can help me understand how I can implement this.
Thanking you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们在一个项目中承担了同样的任务。
为了获得背景模型,我们只需创建一个BackgroundModel类,捕获前(比方说)50帧并计算平均帧以避免背景模型中的像素错误。
例如,如果您从相机获取 8 位灰度图像 (CV_8UC1),则可以使用 CV_16UC1 初始化模型以避免剪切。
现在,等待第一帧计算模型,只需将每一帧添加到模型中并计算接收到的帧的数量。
createMask() 函数计算我们用于模型的平均帧。
现在,您只需通过BackgroundModel 类将所有帧发送到函数subtract() 即可。如果结果是空的 cv::Mat,则仍会计算掩码。否则,您会得到一个减去的帧。
最后但并非最不重要的一点是,您可以使用
标量和(const Mat& mtx)
计算像素总和并确定它是否是带有灯光的帧。
We had the same task in one of our projects.
To get the background model, we simply create a class BackgroundModel, capture the first (lets say) 50 frames and calculate the average frame to avoid pixel errors in the background model.
For example, if you get an 8-bit greyscale image (CV_8UC1) from your camera, you initialize your model with CV_16UC1 to avoid clipping.
Now, waiting for the first frames to calculate your model, just add every frame to the model and count the amount of received frames.
The createMask() function calculates the average frame which we use for the model.
Now, you just send all the frames the way through the BackgroundModel class to a function subtract(). If the result is an empty cv::Mat, the mask is still calculated. Otherwise, you get a subtracted frame.
Last but not least, you can use
Scalar sum(const Mat& mtx)
to calculate the pixel sum and decide if it's a frame with lights on it.
MyPolygon 函数屏蔽 ROI,然后计算绝对像素差并计算白色像素的数量。
srcImage :参考图像。
MyPolygon function mask the ROI and after that, it calculates the abs Pixel difference and calculates the number of white pixels.
srcImage : Reference image.