我现在在 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
并退出,这意味着相机没有成功打开。我已经用谷歌搜索了一段时间,没有找到任何可能的解决方案来解决这个问题。有人能给我一些关于如何解决这个问题的建议吗?谢谢!