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

时间:2013-07-11 19:13:48

标签: 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中的每一条指令 我搜索了我的问题并安装了其他依赖项 此

  • 蟒-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;
}

尚未加载项目文件夹中的图片

2 个答案:

答案 0 :(得分:2)

你忘了启动凸轮了。您必须使用int作为参数的构造函数。见here

// 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 stuff

CvCapture *capture;
capture = cvCaptureFromCAM( 0 );

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

答案 1 :(得分:0)

如果您使用的是Opencv 2.4.6,那么它已经被固定:http://opencv.org/hot-fix-for-opencv-2-4-6.html