使用 OpenCV 进行视频稳定
我有一个用移动摄像机拍摄的视频,其中包含移动的物体。我想稳定视频,以便所有静止物体在视频源中保持静止。我如何使用OpenCV做到这一点?
例如,如果我有两个图像 prev_frame 和 next_frame,如何转换 next_frame 以使摄像机看起来静止?
I have a video feed which is taken with a moving camera and contains moving objects. I would like to stabilize the video, so that all stationary objects will remain stationary in the video feed. How can I do this with OpenCV?
i.e. For example, if I have two images prev_frame and next_frame, how do I transform next_frame so the video camera appears stationary?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我可以建议以下解决方案之一:
编辑
为了以防万一,我最好明确提及三点:
I can suggest one of the following solutions:
EDIT
Three remarks I should better mention explicitly, just in case:
OpenCV 具有estimateRigidTransform() 和warpAffine() 函数,可以很好地处理此类问题。
它非常简单,如下所示:
现在
output
包含frame2
的内容,该内容最适合与frame1
对齐。对于大位移,M 将是零矩阵,或者可能根本不是矩阵,具体取决于 OpenCV 的版本,因此您必须过滤这些矩阵而不应用它们。我不确定那有多大;也许是框架宽度的一半,也许更多。
estimateRigidTransform 的第三个参数是一个布尔值,告诉它是否也应用任意仿射矩阵或将其限制为平移/旋转/缩放。为了稳定相机的图像,您可能只需要后者。事实上,对于相机图像稳定,您可能还希望通过将返回的矩阵标准化为仅旋转和平移来消除任何缩放。
另外,对于移动摄像机,您可能希望随时间对 M 进行采样并计算平均值。
以下是有关 estimateRigidTransform() 和 warpAffine ()
OpenCV has the functions estimateRigidTransform() and warpAffine() which handle this sort of problem really well.
Its pretty much as simple as this:
Now
output
contains the contents offrame2
that is best aligned to fit toframe1
.For large shifts, M will be a zero Matrix or it might not be a Matrix at all, depending on the version of OpenCV, so you'd have to filter those and not apply them. I'm not sure how large that is; maybe half the frame width, maybe more.
The third parameter to estimateRigidTransform is a boolean that tells it whether to also apply an arbitrary affine matrix or restrict it to translation/rotation/scaling. For the purposes of stabilizing an image from a camera you probably just want the latter. In fact, for camera image stabilization you might also want to remove any scaling from the returned matrix by normalizing it for only rotation and translation.
Also, for a moving camera, you'd probably want to sample M through time and calculate a mean.
Here are links to more info on estimateRigidTransform(), and warpAffine()
openCV 现在有一个视频稳定类: http://docs.opencv.org/trunk /d5/d50/group__videostab.html
openCV now has a video stabilization class: http://docs.opencv.org/trunk/d5/d50/group__videostab.html
我从这个答案中过去了。 如何稳定网络摄像头视频?
昨天我刚刚做了一些工作(在 Python 中) )关于这个主题,主要步骤是:
cv2.goodFeaturesToTrack
来找到好的角点。cv2.calcOpticalFlowPyrLK
来跟踪角点。cv2.warpPerspective
来转换视频帧。但现在结果不太理想,可能我应该选择
SIFT keypoints
而不是goodFeatures
。来源:
稳定汽车:
I past my answer from this one. How to stabilize Webcam video?
Yesterday I just did some works (in
Python
) on this subject, main steps are:cv2.goodFeaturesToTrack
to find good corners.cv2.calcOpticalFlowPyrLK
to track the corners.cv2.findHomography
to compute the homography matrix.cv2.warpPerspective
to transform video frame.But the result is not that ideal now, may be I should choose
SIFT keypoints
other thangoodFeatures
.Source:
Stabilize the car:
我应该添加以下注释来完成zerm的回答。
如果选择一个固定物体,然后使用 zerm 的方法 (1) 处理该单个物体,这将简化您的问题。
如果您找到一个静止物体并对它应用校正,我认为可以安全地假设其他静止物体也会看起来稳定。
虽然它对于您的棘手问题当然有效,但这种方法会遇到以下问题:
检测和单应性估计有时会因各种原因而失败:遮挡、突然移动、运动模糊、严重的照明差异。您必须寻找方法来处理它。
您的目标对象可能有遮挡,这意味着它的检测将在该帧上失败,您将必须处理遮挡,这本身就是一个完整的研究主题。
根据您的硬件和解决方案的复杂性,您可能会在使用 SURF 获得实时结果时遇到一些困难。您可以尝试 opencv 的 GPU 实现或其他更快的特征检测器,如 ORB、BRIEF 或 FREAK。
I should add the following remarks to complete zerm's answer.
It will simplify your problem if one stationary object is chosen and then work with zerm's approach (1) with that single object.
If you find a stationary object and apply the correction to it, I think it is safe to assume the other stationary objects will also look stable.
Although it is certainly valid for your tough problem, you will have the following problems with this approach:
Detection and homography estimation will sometimes fail for various reasons: occlusions, sudden moves, motion blur, severe lighting differences. You will have to search ways to handle it.
Your target object(s) might have occlusions, meaning its detection will fail on that frame and you will have to handle occlusions which is itself a whole research topic.
Depending on your hardware and the complexity of your solution, you might have some troubles achieving real-time results using SURF. You might try opencv's gpu implementation or other faster feature detectors like ORB, BRIEF or FREAK.
这已经是很好的答案,但它使用了一点旧的算法,我开发了程序来解决类似的问题,所以我添加了额外的答案。
您可以使用 OpenCV 开发所有这些过程,我已经在移动设备中开发了它。 查看此存储库
Here is already good answer, but it use a little bit old algorithm and I developed the program to solve the similar problem so i add additional answer.
You can develop all this process using OpenCV and I already developed it in mobile devices. See this repository
这是一个棘手的问题,但我可以提出一个稍微简单的情况。
next_frame
移动/旋转任意量threshold(abs(prev_frame-next_frame_rotated))
来查找静态元素。您必须考虑使用什么阈值。min(template_match(prev_frame_background, next_frame_rotated_background))
next_frame
随着时间的推移,这对于多个帧来说效果不佳,所以你需要考虑使用后台累加器 因此算法寻找的背景随着时间的推移是相似的。
This is a tricky problem, but I can suggest a somewhat simple situation off the top of my head.
next_frame
by an arbitrary amountthreshold(abs(prev_frame-next_frame_rotated))
to find the static elements. You'll have to play around with what threshold value to use.min(template_match(prev_frame_background, next_frame_rotated_background))
next_frame
This won't work well for multiple frames over time, so you'll want to look into using a background accumulator so the background the algorithm looks for is similar over time.
背景:
我正在做一个研究项目,我试图计算排队的人需要多长时间才能到达柜台。我需要的第一件事是镜头,所以我去了校园并记录了一些排队买票的游客。到目前为止,我还不知道如何计算排队时间以及在录制视频时应该采取哪些预防措施。一天结束时,我发现我录制的所有镜头都是用摇晃的相机录制的。所以此时我首先需要稳定视频,然后才开发其他解决方案来计算排队时间。
使用模板匹配实现视频稳定
结果片段:
展示此技术的结果
Gif 在 gif 中可以看到,选定的静态对象在帧边界上保持静态,而运动可以通过帧边缘的黑色填充看到。
Background:
I was working on this research project where I was trying to calculate that how long would it take for person standing in the queue to reach to the counter. First thing I required was FOOTAGE so I went to the campus and recorded some tourist moving in the queue to get tickets. Till this point I had not idea how would I gonna calculate queuing time and what pre-caution should I take while recording the footage. At the end of the day I found that all the footage I recorded was recorded with shaky camera. So at this point I was first required to stabilize the video and then only develop other solution to calculate queuing time.
Video Stabilization using Template Matching
Result Footage:
Gif to show the result of this technique
As you can see in the gif that selected static object remain static w.r.t the frame boundaries while motion can be see by black filling from the edges of the frame.