一.ubuntu环境下配置 opencv, 通过opencv源码编译安装(参考其他)
二.准备以下2个文件
cv.cpp 内容如下
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
using namespace cv;
int main(int argc, char** argv)
{
VideoCapture cap(0);
if (!cap.isOpened())
{
return -1;
}
Mat frame;
while (1)
{
cap >> frame;
if (frame.empty()) break;
imshow("当前视频", frame);
if (waitKey(30) >= 0)
break;
}
return 0;
}
CMakeLists.txt 内容如下
# cmake needs this line 要求的最低版本
cmake_minimum_required(VERSION 2.8)
# Define project name 定义工程名
project(example_project)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI 自动查找库
find_package(OpenCV REQUIRED)
# Declare the executable target built from your sources 声明可执行目标文件及源文件
add_executable(cv cv.cpp) # 目标文件,源文件0,源文件1,...
# Link your application with OpenCV libraries 将目标文件与库链接
target_link_libraries(cv ${OpenCV_LIBS}) # 目标文件,库路径
cmake的版本要求设置: 一般放在最开始
cmake_minimum_required(VERSION 2.8)
Set the minimum required version of cmake for a project and update Policy Settings to match the version given:
cmake_minimum_required(VERSION major.minor[.patch[.tweak]]
[FATAL_ERROR])
If the current version of CMake is lower than that required it will stop processing the project and report an error.
三.在当前目录下执行
cmake .
make
四.执行最终生成的cv, 即可以看到视频画面
./cv