- Mastering OpenCV 4
- Roy Shilkrot David Millán Escrivá
- 342字
- 2021-07-02 14:47:34
Accessing the webcam
To access a computer's webcam or camera device, you can simply call the open() function on a cv::VideoCapture object (OpenCV's method of accessing your camera device), and pass 0 as the default camera ID number. Some computers have multiple cameras attached, or they do not work with a default camera of 0, so it is common practice to allow the user to pass the desired camera number as a command-line argument, in case they want to try camera 1, 2, or -1, for example. We will also try to set the camera resolution to 640 x 480 using cv::VideoCapture::set() to run faster on high-resolution cameras.
You can put this code in the main() function of your main.cpp file:
auto cameraNumber = 0;
if (argc> 1)
cameraNumber = atoi(argv[1]);
// Get access to the camera.
cv::VideoCapture camera;
camera.open(cameraNumber);
if (!camera.isOpened()) {
std::cerr<<"ERROR: Could not access the camera or video!"<< std::endl;
exit(1);
}
// Try to set the camera resolution.
camera.set(cv::CV_CAP_PROP_FRAME_WIDTH, 640);
camera.set(cv::CV_CAP_PROP_FRAME_HEIGHT, 480);
After the webcam has been initialized, you can grab the current camera image as a cv::Mat object (OpenCV's image container). You can grab each camera frame by using the C++ streaming operator from your cv::VideoCapture object in a cv::Mat object, just like if you were getting input from a console.
- DevOps:軟件架構(gòu)師行動(dòng)指南
- iOS Game Programming Cookbook
- Visual Basic .NET程序設(shè)計(jì)(第3版)
- Docker and Kubernetes for Java Developers
- Mastering RabbitMQ
- Game Programming Using Qt Beginner's Guide
- Mastering QGIS
- Nginx Essentials
- 網(wǎng)店設(shè)計(jì)看這本就夠了
- 編譯系統(tǒng)透視:圖解編譯原理
- SQL基礎(chǔ)教程(視頻教學(xué)版)
- Functional Kotlin
- 持續(xù)集成與持續(xù)交付實(shí)戰(zhàn):用Jenkins、Travis CI和CircleCI構(gòu)建和發(fā)布大規(guī)模高質(zhì)量軟件
- Visual C++從入門(mén)到精通(第2版)
- Spring Boot學(xué)習(xí)指南:構(gòu)建云原生Java和Kotlin應(yīng)用程序