我现在在linux上使用OpenCV 2.4.3。在我的/dev/中有一个叫video0的设备,它可以很好地工作。
目前,我正在使用以下代码来尝试访问相机。这段代码是在qtcreator中编写的,并使用qmake进行编译,以包含必要的库。对于OpenCV库,我在我的机器上下载了源代码,并按照网站上的说明进行了编译。
+ + main.cpp:
#include <iostream>
#include <opencv2/opencv.hpp>
#include <cstdlib>
int main(int argc, char** argv) {
int cameraNumber = 0;
if (argc>1)
cameraNumber = atoi(argv[1]);
cv::VideoCapture camera;
camera.open(cameraNumber);
if(!camera.isOpened()) {
std::cerr<<"Error opening camera"<<std::endl;
exit(1);
}
camera.set(CV_CAP_PROP_FRAME_WIDTH,640);
camera.set(CV_CAP_PROP_FRAME_HEIGHT,480);
while(true) {
cv::Mat cameraFrame;
camera>>cameraFrame;
if (cameraFrame.empty()){
std::cerr<<"No frame read from camera"<<std::endl;
exit(1);
}
imshow("Camera",cameraFrame);
char keypress = cv::waitKey(20);
if (keypress==27) {
break;
}
}
return 0;
}
+ + test.pro
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../opt/OpenCV-2.4.3/lib/release/ -lopencv_core
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../opt/OpenCV-2.4.3/lib/debug/ -lopencv_core
else:unix: LIBS += -L$$PWD/../../../../opt/OpenCV-2.4.3/lib/ -lopencv_core
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../opt/OpenCV-2.4.3/lib/release/ -lopencv_highgui
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../opt/OpenCV-2.4.3/lib/debug/ -lopencv_highgui
else:unix: LIBS += -L$$PWD/../../../../opt/OpenCV-2.4.3/lib/ -lopencv_highgui
INCLUDEPATH += $$PWD/../../../../opt/OpenCV-2.4.3/include
DEPENDPATH += $$PWD/../../../../opt/OpenCV-2.4.3/include
但是运行程序时输出Error opening camera
并退出,说明相机没有打开成功。我已经谷歌了一段时间,没有找到任何可能的解决方案来解决这个问题。谁能给我一些建议如何解决这个问题?谢谢!
可能您缺少linux所需的一些附加库。
根据OpenCV安装指南,额外的先决条件,当在linux上使用OpenCV时,您需要安装这些库:ffmpeg, libgstreamer, libv4l, libxine, unicap, libdc1394 2.x。
在Ubuntu 12.04上,我安装了以下包(*sudo apt-get install __*):
- python-opencv
- libhighgui2.3
- libhighgui-dev ffmpeg
- libgstreamer0.10-0
- libv4l-0
- libv4l-dev
- libxine2
- libunicap2
- libdc1394-22
祝你好运!