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
