git clone https://github.com/opencv/opencv.git
git -C opencv checkout 4.x
cd opencv
mkdir -p build && cd build
cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -D WITH_FFMPEG=ON ..
make -j8
sudo make install
sudo find / -iname opencv4.pc
cd ~
sudo vim /etc/profile.d/pkgconfig.sh
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
source /etc/profile
sudo vim /etc/ld.so.conf.d/opencv4.conf
/usr/local/lib
source ldconfig
cd opencv/samples/cpp/example_camke
make
./opencv_example
#include
using std::cout;
using std::endl;
#include
using namespace cv;
int main(int argc, vhar** argv){
if(argc != 2){
cout << "usage:DispalyImage.out \n" << endl;
return -1;
}
Mat image;
image = imread(argv[1], IMREAD_COLOR);
if(!image.data){
cout << "No image data \n" << endl;
return -1;
}
namedWindow("Display Image",WINDOW_AUTOSIZE);
imshow("Display Image",image);
WaitKey(0);
return 0;
}
# cmake needs this line
cmake_minimum_required(VERSION 3.10)
# Define project name
project(W_CPP)
# 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)
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS " config: ${OpenCV_DIR}")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
# set "-std=c++11"
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
# Add OpenCV headers location to your include paths
# include_directories(${OpenCV_INCLUDE_DIRS})
# Declare the executable target built from your sources
add_executable(test test.cc)
# Link your application with OpenCV libraries
target_link_libraries(test PRIVATE ${OpenCV_LIBS})
mkdir -p build
cd build
cmake ..
make
./test shen.png
参考博客: