軟體版本:
Window 10
Visual Studio 2015 C++
OpenCV 4.0.0
電腦:
MSI GF65 Thin 95D
大哥們好,小弟目前正在學習利用OpenCV處理影像的技術,目前想嘗試利用OpenCV開啟筆記型電腦的內建攝像頭,代碼如下:
#include <iostream>
#include <string>
#include "Windows.h"
#include <ctime>
using namespace std;
// OpenCV includes
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
// OpenCV command line parser functions
// Keys accecpted by command line parser
const char* keys =
{
"{help h usage ? | | print this message}"
"{@video | | Video file, if not defined try to use webcamera}"
};
int main(int argc, const char** argv)
{
CommandLineParser parser(argc, argv, keys);
parser.about("Chapter 2. v1.0.0");
//If requires help show
if (parser.has("help"))
{
parser.printMessage();
return 0;
}
String videoFile = parser.get<String>(0);
// Check if params are correctly parsed in his variables
if (!parser.check())
{
parser.printErrors();
return 0;
}
int ID = 0;
VideoCapture cap; // open the default camera
if (videoFile != "")
cap.open(videoFile);
else{
cap.open(0);
Sleep(1000);
/*while (!cap.isOpened() && ID<=1000) {
ID++;
cap.open(ID);
}*/
}
if (!cap.isOpened()) { // check if we succeeded
OutputDebugStringA("\r\n Still cannot find a camera!! \r \n\n");
return -1;
}
namedWindow("Video", 1);
for (;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
if (frame.empty())
return 0;
imshow("Video", frame);
if (waitKey(30) >= 0) break;
}
// Release the camera or video cap
cap.release();
return 0;
}
主要是在cap.open(0)后,呼叫cap.isOpened()一直呈現0,網上找了許多解決方案,目前已經試過將cap.open(ID)中的ID不停更改,從-1到700都試過,結果是一樣的,不知道大家有沒有一樣的問題或者知道如何解決呢?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/126166.html
標籤:OpenCV
上一篇:物流管理導論
