opencv怎么打开usb摄像头
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv怎么打开usb摄像头相关的知识,希望对你有一定的参考价值。
//Program to display a video from attached default camera device#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
char *window_name = "Video";
int main()
VideoCapture cap(0); //此处默认USB摄像头
if (!cap.isOpened())
cerr << "webCam could't be opened successfully" << endl;
return EXIT_FAILURE;
namedWindow(window_name);
while (char(waitKey(1)) != 'q'&&cap.isOpened())
Mat frame;
cap >> frame;
if (frame.empty())
cout << "Video over" << endl;
break;
imshow(window_name, frame);
参考技术A 我为什么打不开摄像头呢?
Jetson TX2opencv3 打开usb摄像头
ubuntu2604
opencv3.4.0
https://blog.csdn.net/ultimate1212/article/details/80936175?utm_source=blogxgwz7
cmake_minimum_required(VERSION 2.8) project(DisplayImage) set( CMAKE_CXX_FLAGS "-std=c++11 -O3" ) find_package( OpenCV REQUIRED ) #if(CMAKE_VERSION VERSION_LESS "2.8.11") # Add OpenCV headers location to your include paths include_directories( include ${OpenCV_INCLUDE_DIRS} ) #endif() #单个包添加 #add_executable( DisplayImage src/DisplayImage.cpp ) #文件路径自动读取添加 AUX_SOURCE_DIRECTORY(./src DIR_SRCS) ADD_EXECUTABLE(DisplayImage ${DIR_SRCS}) target_link_libraries( DisplayImage ${OpenCV_LIBS} ) #设置可执行文件的输出目录 SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#include <iostream> #include <opencv2/opencv.hpp> #include <string> using namespace cv; using namespace std; int main() { VideoCapture capture(0); if(capture.isOpened()) { cout<<"success"<<endl; } Mat frame; while (capture.isOpened()) { capture >> frame; imshow("capture", frame); char key = static_cast<char>(cvWaitKey(10));//控制视频流的帧率,10ms一帧 if (key == 27) //按esc退出 break; } return 0; }
报错问题
https://blog.csdn.net/dhaduce/article/details/80379792
笔者在进行测试时,出现如下错误:
libv4l2: error setting pixformat: Invalid argument
libv4l2: error setting pixformat: Invalid argument
libv4l2: error setting pixformat: Invalid argument
libv4l2: error setting pixformat: Invalid argument
VIDEOIO ERROR: V4L2: Pixel format of incoming image is unsupported by OpenCV
Couldn‘t connect to webcam.
OPENCV默认采用0号摄像头,TX2的0号摄像头是板子上自带的板上摄像头,而我们的usb摄像头是1号,故笔者使用如下代码,解决了问题
如果上述方法不能解决问题,可以尝试如下操作:
首先检查是否安装了v4l1compat.so
dpkg -S v4l1compat.so
若没有安装,则安装;若找到该文件,则跳过安装,进行下一步
apt-cache search libv4l sudo apt-get install libv4l-ruby1.8
然后添加环境变量
export LD_PRELOAD=/usr/lib/aarch64-linux-gnueabihf/libv4l/v4l1compat.so export LD_PRELOAD=/usr/lib/aarch64-linux-gnueabihf/libv4l/v4l2convert.so
sudo ldconfig
以上是关于opencv怎么打开usb摄像头的主要内容,如果未能解决你的问题,请参考以下文章