2013-07-11 35 views
2

这里是我的代码无法访问我的摄像头OpenCV的Ubuntu的

#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 我GOOGLE了我的问题,并安装额外的依赖 这

  • 蟒蛇,OpenCV的
  • libhighgui2.3正是每一个指令做
  • libhighgui-dev的
  • 的ffmpeg
  • libgstreamer0.10-0
  • libv4l-0
  • libv4l-dev的
  • libxine2
  • libunicap2
  • libdc1394-22

和其他许多人,我可以找到。 但它仍然不起作用。
这是荒谬的,但是这个代码工作在我的笔记本电脑,与Ubuntu的相同的分布。 我没有编译错误。

in terminal gstreamer-properties 打开相机。 有人知道该怎么办?请帮帮我。

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

代码示例 的#include “opencv2 /核心/ 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

你忘了,用来初始化的凸轮。您必须使用构造函数与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); 

也可以使用cvCaptureFromCAMstuff

CvCapture *capture; 
capture = cvCaptureFromCAM(0); 

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

+0

嗯,但我做到了,始终没有初始化,它的工作, – theroom101

+0

我用VideoCapture凸轮(0) 或cam.open(0) 我认为它的工作原理相同的方式。 其实我觉得与Linux发行版的问题,因为我可以正常地构建昨天,第二天链接器无法找到库。 =( 我会试着重新安装。 – theroom101

+0

什么你得到的错误?因为它不工作,你是怎么改?你尝试过吗?你试过cvCaptureFromCAM。也许OpenCV的改变的东西,你目前的版本不接受它...至极版本有OpenCV的? – user1810087