sudo apt update
sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libjasper
若是报错:无法定位到 libjasper软件包
则依次执行以下命令
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev
sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev
sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt install libgtk2.0-dev
sudo apt install pkg-config
sudo apt-get install libatlas-base-dev gfortran
sudo apt install libcanberra-gtk-module
cmake ..
make -j2
终端输入:sudo gedit /etc/ld.so.conf添加动态库
/usr/local/lib
终端输入:sudo ldconfig
修改 bash.bashrc 文件,打开文件后在文末加入以下两行代码
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
终端进入:/home/sen/motan/postprocess/third_parties/opencv-4.8.0/samples
cmake .
make
./opencv_example
cmakelists.txt输入:
set(OpenCV_DIR /home/sen/motan/postprocess/third_parties/opencv-4.8.0/build)
find_package(OpenCV REQUIRED)#REQUIRED是find_package命令的一个选项,它指定了一个库是否是必需的
include_directories(${OpenCV_INCLUDE_DIRS})
message(${OpenCV_INCLUDE_DIRS})
message(${OpenCV_LIBS})
target_link_libraries(HelloWorld PUBLIC ${OpenCV_LIBS})
#include
#include
#include
using namespace cv;
using namespace std;
int main()
{
// 视频保存位置
string outputVideoPath = "./test.avi";
// 打开摄像头
VideoCapture capture0(0);
VideoWriter outputVideo;
// 获取摄像机帧率
int fps = capture0.get(CAP_PROP_FPS);
// 获取当前摄像头的视频信息
cv::Size S = cv::Size((int)capture0.get(CAP_PROP_FRAME_WIDTH),
(int)capture0.get(CAP_PROP_FRAME_HEIGHT));
// 打开视频路径,设置基本信息 open函数中你参数跟上面给出的VideoWriter函数是一样的
outputVideo.open(outputVideoPath, cv::VideoWriter::fourcc('X', 'V', 'I', 'D'), fps, S, true);
if (!outputVideo.isOpened()) {
cout << "fail to open!" << endl;
return -1;
}
// 图片帧
cv::Mat frameImage;
int count = 0;
while(true){
// 读取当前帧
capture0 >> frameImage;
if(frameImage.empty()) break;
++count;
// 输出当前帧
cv::imshow("output", frameImage);
// 保存当前帧
outputVideo << frameImage;
if (char(waitKey(1)) == 'q') break;
}
return 0;
}