使用 OpenCV 的多个相机
我正在编写一个程序,使用 OpenCV 同时从两个摄像机获取图像(我这样做不是为了立体视觉)。使用标准 OpenCV 函数 cvCaptureFromCAM()
从两个相同的相机获取捕获,并在 while 循环中调用 cvQueryFrame()
两次(每次捕获一次),我当我在 cvNamedWindow
中显示它们时,最终会在两个相机中获得垃圾图像。
或者,如果我在每个相机自己的 POSIX 线程(因此相同的地址空间)中运行每个相机,我会在第二个线程中遇到分段错误,但我无法找出原因。
最后,我可以在自己的进程中运行每个相机(因此有单独的地址空间),并且一切都运行良好。我怀疑在从不同相机进行每个帧查询之前必须获取某种锁定类型的资源。希望有人对如何在同一进程和同一线程中获取两个捕获有一些想法。谢谢!
I'm writing a program that gets captures from two cameras at once using OpenCV (I'm not doing this for stereo vision). Using the standard OpenCV functions cvCaptureFromCAM()
to get captures from the two identical cameras along with cvQueryFrame()
being called twice in a while loop (once for each capture), I end up getting garbage images in both cameras as seen when I display them in a cvNamedWindow
.
Alternatively, if I run each camera in their own POSIX thread (therefore same address space) I get a segmentation fault in the second thread, for which I can't figure out the cause.
Finally, I can run each camera in its own process (therefore separate address spaces) and everything runs fine. My suspicion is that there's some sort of lock-type resource that must be acquired before doing each frame query from a different camera. Hopefully someone has some ideas for how to get both captures in the same process and same thread. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
什么操作系统?
它非常依赖于相机及其驱动程序 - 特别是在使用 Directshow 的 Windows 上,如果它们可以一起工作,如果您需要以某种顺序或某种方式启动它们,那么它们完全有可能一起工作。
如果您可以在单独的线程中独立运行相机,并让它们通过某个中性系统(命名管道、tcp 套接字、ramdisk 中的文件)写入图像,您可能会发现更可靠
What OS ?
It's very dependent on the cameras and their drivers - especially on windows using Directshow it can be complete chance if they will work together, if you need to start them in a certain order or in a certain way.
If you can run the cameras on their own in separate threads and have them write the images over some neutral system (named pipes, tcp socket, file in ramdisk) you might find that more reliable
对于每个使用 COM 库的线程,您必须调用
::CoInitializeEx( NULL,COINIT_MULTITHREADED)
一次,并在完成捕获工作时取消初始化它。为了在同一进程和同一线程中获取两个捕获,您是否为
cvQueryFrame()
使用的缓冲区分配了副本?我怀疑您的两个捕获共享来自cvQueryFrame()
的相同缓冲区,并且该缓冲区实际上是由 OpenCV 本身维护的!因此,请尝试为每次捕获创建一个副本,看看它是否有效。希望有帮助!
For each thread that uses the COM library, you must call
::CoInitializeEx( NULL,COINIT_MULTITHREADED)
once and uninitialize it when you finish the capture work.To get both captures in the same process and same thread, did you allocate a copy for the buffer used by
cvQueryFrame()
? I suspect that your two captures share the same buffer fromcvQueryFrame()
and this buffer is actually maintained by OpenCV itself! So try make a copy for each capture and see if it works.Hope it helps!