在OpenCV 2.4.9中检测不良帧 [英] Detect bad frames in OpenCV 2.4.9

查看:85
本文介绍了在OpenCV 2.4.9中检测不良帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道标题有点模糊,但是我不确定该如何描述它.

I know the title is a bit vague but I'm not sure how else to describe it.

带有ffmpeg + OpenCV 2.4.9的CentOS.我正在开发一个简单的运动检测系统,该系统使用来自IP摄像机(h264)的流.

CentOS with ffmpeg + OpenCV 2.4.9. I'm working on a simple motion detection system which uses a stream from an IP camera (h264).

偶尔,流会打and并抛出坏帧"(请参阅​​下面的pic-bad.png链接).问题是,这些帧与之前的帧有很大不同,即使没有实际运动发生,也会触发运动"事件.

Once in a while the stream hiccups and throws in a "bad frame" (see pic-bad.png link below). The problem is, these frames vary largely from the previous frames and causes a "motion" event to get triggered even though no actual motion occured.

下面的图片将说明问题.

The pictures below will explain the problem.

好帧(捕获动作):

坏帧(静止不动,只是坏帧):

Bad frame (no motion, just a broken frame):

坏帧被随机捕获.我想我可以通过分析(循环)从某个特定位置下降的像素来查看它们是否相同,从而制成一个不良的帧检测器,但是我想知道是否还有其他更有效的方法,这本书"检测这些类型的不良帧并跳过它们的方法.

The bad frame gets caught randomly. I guess I can make a bad frame detector by analyzing (looping) through the pixels going down from a certain position to see if they are all the same, but I'm wondering if there is any other, more efficient, "by the book" approach to detecting these types of bad frames and just skipping over them.

谢谢!

编辑更新:

使用C ++运动检测程序通过 cvQueryFrame(camera); 抓取帧,因此我不直接与ffmpeg交互,OpenCV在后端进行操作.我正在使用从git来源编译的ffmpeg的最新版本.所有的库也都是最新的(h264等,昨天都已下载并编译).数据来自RTSP流(ffserver).我已经在多台相机(大华1-3 MP型号)上进行了测试,并且在所有这些相机上的画面毛刺都相当持久,尽管它不是连续发生的,只是偶尔出现一次(例如:每10分钟一次).

The frame is grabbed using a C++ motion detection program via cvQueryFrame(camera); so I do not directly interface with ffmpeg, OpenCV does it on the backend. I'm using the latest version of ffmpeg compiled from git source. All of the libraries are also up to date (h264, etc, all downloaded and compiled yesterday). The data is coming from an RTSP stream (ffserver). I've tested over multiple cameras (dahua 1 - 3 MP models) and the frame glitch is pretty persistent across all of them, although it doesn't happen continuously, just once on a while (ex: once every 10 minutes).

推荐答案

在第一种方法中,我想到的是通过计算不相同的像素来检查有效帧示例与我们要检查的帧之间的差异.用这个数字除以面积,我们得到测量不相似性的百分比.我猜想高于0.5可以说测试帧是无效的,因为它与有效帧的示例相差太大.

What comes to my mind in first approach is to check dissimilarity between example of valid frame and the one we are checking by counting the pixels that are not the same. Dividing this number by the area we get percentage which measures dissimilarity. I would guess above 0.5 we can say that tested frame is invalid because it differs too much from the example of valid one.

仅当您使用静态相机(它不移动)并且可以在其前面移动的物体不在最短距离内(取决于焦距,但是如果您有较宽的镜头,则此假设才适用)物体不应出现在相机前方30厘米以内的情况下,以防止物体从任何地方跳入"画面并且尺寸大于画面面积的50%.

This assumption is only appropriate if you have a static camera (it does not move) and the objects which can move in front of it are not in the shortest distance (depends from focal length, but if you have e.g. wide lenses so objects should not appear less than 30 cm in front of camera to prevent situation that objects "jumps" into a frame from nowhere and has it size bigger that 50% of frame area).

在这里,您具有执行我所说的功能的opencv函数.实际上,如果您认为运动变化会更快,则可以将相异系数调整得更大.请注意,第一个参数应该是有效帧的示例.

Here you have opencv function which does what I said. In fact you can adjust dissimilarity coefficient more large if you think motion changes will be more rapid. Please notice that first parameter should be an example of valid frame.

bool IsBadFrame(const cv::Mat &goodFrame, const cv::Mat &nextFrame) {
    // assert(goodFrame.size() == nextFrame.size())

    cv::Mat g, g2;
    cv::cvtColor(goodFrame, g, CV_BGR2GRAY);
    cv::cvtColor(nextFrame, g2, CV_BGR2GRAY);

    cv::Mat diff = g2 != g;

    float similarity = (float)cv::countNonZero(diff) / (goodFrame.size().height * goodFrame.size().width);

    return similarity > 0.5f;
}

这篇关于在OpenCV 2.4.9中检测不良帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆