2013-07-11 36 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