c++ - 无法访问我的网络摄像头 opencv ubuntu

标签 c++ opencv ubuntu webcam

这是我的代码

#include <iostream>

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace std;

const int KEY_ENTER = 10;
const int KEY_ESC   = 27;
const int KEY_1         = 49;
const int KEY_2         = 50;
const int KEY_3         = 51;
const int KEY_4         = 52;
const int KEY_5         = 53;
const int KEY_6         = 54;

const int DELAY = 30;

const string WIN_NAME = "Camera View";

const string NAME[6] = {"me", "serk", "prot", "vitkt", "st", "tara"};

struct pg
{
string name;
int cnt;
pg(): name(""), cnt (0) {};
pg(string s, int c) : name(s) , cnt(c) {};
};

pg crew[6];

int main()
{
for(int i = 0; i < 6; ++i)
    crew[i] = pg(NAME[i], 0);

cv::VideoCapture cam;

cam.open(0);

cv::Mat frame;

pg cur = crew[0];

int c = 0;
for(;cam.isOpened();)
{
    try
    {
    cam >> frame;

    cv::imshow(WIN_NAME, frame);

    int key = cv::waitKey(DELAY);

    cur = (key >= KEY_1 && key <= KEY_6) ? crew[key - KEY_1] : cur;

    if(KEY_ENTER == key)
        cv::imwrite(cv::format("%s%d.jpg", cur.name.c_str(), cur.cnt++), frame);

    if(KEY_ESC == key)
        break;
    } catch (cv::Exception e)
    {
        cout << e.err << endl;
    }
}

cam.release();
return 0;
}

但我无法从相机捕获视频。 =( 我的电脑上有 Ubuntu 12.04,

我完全执行了 linux install istructions 中的每一条指令 我用谷歌搜索了我的问题并安装了额外的依赖项 这个

  • python-opencv
  • libhighgui2.3
  • libhighgui-dev
  • ffmpeg
  • libgstreamer0.10-0
  • libv4l-0
  • libv4l-dev
  • libxine2
  • libunicap2
  • libdc1394-22

以及我能找到的许多其他内容。 但还是不行。
这很荒谬,但这段代码可以在我的笔记本电脑上运行,并且具有相同的 ubuntu 发行版。 我没有编译错误。

在终端中 gstreamer 属性 打开那个相机。 有人知道该怎么做吗?请帮帮我。

我注意到它甚至不从文件加载图片

代码示例 #include“opencv2/core/core.hpp” #include“opencv2/highgui/highgui.hpp”

#include <iostream>

using namespace std;

int main()
{
system("clear");

cv::Mat picture;
picture = cv::imread("boobies.jpg");

cout << picture.rows << endl;
cv::imshow("Smile", picture);

char ch;
cin >> ch;

cv::destroyWindow("Smile");

return 0;
}

尚未从项目文件夹加载图片

最佳答案

您忘记初始化cam。您必须使用以 int 作为参数的构造函数。

// the constructor that opens video file
VideoCapture(const string& filename);
// the constructor that starts streaming from the camera
VideoCapture(int device);

这样做:

cv::VideoCapture cam(0);
cam.open(0);

此外,您可以使用cvCaptureFromCAM:

CvCapture *capture;
capture = cvCaptureFromCAM( 0 );

这将分配并初始化您的捕获实例。

关于c++ - 无法访问我的网络摄像头 opencv ubuntu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17601541/

相关文章:

c++ - 创建一个空类纯粹是为了将其与另一个类区别开来吗?

c - 找到从轮廓检测到的圆的厚度?

ubuntu - Ubuntu升级到13.10,Emacs升级到24.3.1后,Emacs无法输入日文和中文

linux - www 目录的 home FTP 别名

c++ - 矩阵加法 : Ambiguous overload for ‘operator+’

c++ - 输入图像中的无效 channel 数

python - 如何使用opencv从图形中删除其他对象?

ubuntu - 什么是 ./.local/share/Trash (Unix)

c++ - 在矩阵乘法 C++ SIMD 编程中检测到 Stack Smashing

c++ - 使用 base64 编码 CV_32FC1 Mat 数据