Linux下安裝OpenCV

WoBok發表於2024-08-04

安裝

先安裝依賴庫:

sudo apt-get install build-essential libgtk2.0-dev libgtk-3-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev

根據官網教程進行安裝:
OpenCV Get Started
Operating System:
Linux
Building From Source:
Yes
Language:
C++
OpenCV安裝參考 C++

cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.x/modules ../opencv-4.x -D OPENCV_GENERATE_PKGCONFIG=YES -D CMAKE_BUILD_TYPE=Release

-D CMAKE_BUILD_TYPE=Release
此條為可選項,如果不新增,cmake預設使用Release
最後不要忘記sudo make install

檢視版本號

opencv_version

測試

#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv )
{
        Mat image;
        image = imread( argv[1], 1 );
        imshow("Display Image", image);
        waitKey(0);
        return 0;
}

main.cpp

cmake_minimum_required(VERSION 3.0)
project(ImageShow)
find_package(OpenCV REQUIRED)
add_executable(ImageShow main.cpp)
target_link_libraries(ImageShow PRIVATE ${OpenCV_LIBS})

CMakeLists.txt

mkdir build
cd build
cmake ..
make
./ImageShow image.jpg

將image.jpg放至build目錄下

相關文章