这是我的代码
#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/