C++ OpenCV 网络摄像头捕获问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4430367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:23:27  来源:igfitidea点击:

OpenCV webcam capture problem

c++copencvwebcam

提问by Figaro

I've installed OpenCV 2.2 and now I can't get webcam capture to work. It worked ok in 2.1. OpenCV detects a webcam, doesn't report any errors or warnings, but each frame is a gray image. I even tried a code sample from OpenCV wiki:

我已经安装了 OpenCV 2.2,现在我无法让网络摄像头捕获工作。它在 2.1 中工作正常。OpenCV 检测网络摄像头,不报告任何错误或警告,但每一帧都是灰色图像。我什至尝试了来自 OpenCV wiki 的代码示例:

VideoCapture cap(0); // open the default camera
if(!cap.isOpened())  // check if we succeeded
    return -1;

Mat edges;
namedWindow("edges",1);
for(;;)
{
    Mat frame;
    cap >> frame; // get a new frame from camera
    cvtColor(frame, edges, CV_BGR2GRAY);
    //GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
    //Canny(edges, edges, 0, 30, 3);
    imshow("edges", edges);
    if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;

Did anyone run into this issue? I'm using 64bit Win7 and Visual Studio 2010.

有没有人遇到过这个问题?我使用的是 64 位 Win7 和 Visual Studio 2010。

回答by Victor

I found the solution after a very long search.

经过长时间的搜索,我找到了解决方案。

The problem is that if doesn't have a delay between showing the frames happen this problem.

问题是如果在显示帧之间没有延迟就会发生这个问题。

The solution is put cvWaitKey(20);in loop.

解决方案被放入cvWaitKey(20);循环中。

回答by Figaro

The issue was with the camera I used, MSFT LifeCam. I tried Logitech C210 and 120 and they both work fine.

问题出在我使用的相机 MSFT LifeCam 上。我试过罗技 C210 和 120,它们都工作正常。

回答by user1555123

Here is the Solution.

这是解决方案。

Every image frame captured is being converted into grayscale image in the first line below. Commenting and running the code alone will show an error since since you are not capturing any processed image into the edges frame, which is being displayed in imshow.

在下面的第一行中,捕获的每个图像帧都被转换为灰度图像。单独注释和运行代码将显示错误,因为您没有将任何处理过的图像捕获到 imshow 中显示的边缘帧中。

Hence comment the cvtColorline and change the second parameter in imshowto frame. This will allow you to display the colour video from the webcam.

因此,注释该cvtColor行并将第二个参数更改imshowframe. 这将允许您显示来自网络摄像头的彩色视频。

cvtColor(frame, edges, CV_BGR2GRAY);

imshow("edges", frame);

回答by Machado

I really don't know anything about OpenCV, but Isn't the problem on the following line ?

我真的对 OpenCV 一无所知,但是下面这行不是问题吗?

cvtColor(frame, edges, CV_BGR2GRAY);

Seems like you are intentionally converting a B-G-R color space into a Grayscale space.

似乎您有意将 BGR 色彩空间转换为灰度空间。

Shouldn't it be something like:

不应该是这样的:

cvtColor(frame, edges, CV_BGR2RGB);

回答by David

You might try a look at this postas well.

你也可以试试看这篇文章

To put it simple, changing

简单来说,改变

from

import cv

导入简历

to

import cv2.cv as cv

将 cv2.cv 导入为 cv

worked for me. See also the post here.

为我工作。另请参阅此处的帖子。

回答by TheEngineer

I had the same problem. I figured that it might be the settings on my camera, because I was supposed to have an input of 640x480px (which I could not adjust) and I could not adjust the permissions for the feed. So I installed a virtual webcam, which pretty much resolved both issues. I got an input dialogue, chose the virtual camera and it worked. I managed to set it to 640x480 in the app too.

我有同样的问题。我想这可能是我的相机上的设置,因为我应该有 640x480px 的输入(我无法调整)并且我无法调整提要的权限。所以我安装了一个虚拟网络摄像头,它几乎解决了这两个问题。我得到了一个输入对话框,选择了虚拟摄像机,它工作正常。我也设法在应用程序中将其设置为 640x480。

My virtual camera was called ManyCam, but it looks like it is made for teenage girls, wanted me to install 3 other apps and I'm still not sure weather it doesn't come with 'complementary' trojans.

我的虚拟相机名为 ManyCam,但它看起来像是为十几岁的女孩设计的,想让我安装其他 3 个应用程序,但我仍然不确定它是否没有“补充”木马程序。

However, it lets you adjust colors, hue, contrast etc. and other stuff you might need for testing.

但是,它可以让您调整颜色、色调、对比度等以及您可能需要进行测试的其他内容。

回答by Nico Carosio

I am using cvtColor and found that

我正在使用 cvtColor 并发现

cvtColor(image,image,CV_BGR2RGB);didn't work.

cvtColor(image,image,CV_BGR2RGB);没有用。

But the good news is that this change work !!

但好消息是这种改变奏效了!!

cvtColor(image,image,**COLOR_BGR2RGB**);

Also include:

还包括:

include opencv2/imgproc/imgproc.hpp

and in the .pro file the library:

并在 .pro 文件中的库:

-lopencv_imgproc 

回答by Saransh Kejriwal

Try this:

尝试这个:

VideoCapture cap(0); // open the default camera
if(!cap.isOpened())  // check if we succeeded
{
  cout<<"Cam not captured\n";
}

Mat frame;
namedWindow("frame");
for(;;)
{
    cap >> frame; // get a new frame from camera
    imshow("frame", frame);

    if(waitKey(10) >= 0) break;
}

return 0;