Use cmake to build your own project OpenCV library configuration and search for windows system

Use cmake to build your own project OpenCV library configuration

OpenCV download and install

  • download
  • Install
  • Configure environment variables

Download from the OpenCV official website , and download the corresponding resources according to your own system.

insert image description here

After the download is complete, double-click the installation package directly. Choose the location where you want to install.

Please add a picture description
To configure environment variables, open the Windows configuration environment variable interface, and double-click Path to enter the path variable setting interface. Add Opencv environment variables. Note that you need to fill in the path of your Opencv. Not mine.

insert image description here
After setting the environment variables, remember to turn on the computer.

Cmake builds a simple opencv project test

Two files, maincpp, CMakLists.txt.
CMakLists.txt writes the following content, mainly to find the library, configure the opencv library that the project depends on, include header files, and links to library files.

cmake_minimum_required(VERSION 3.24)

project(mini_project_opencv)

set(CMAKE_CXX_STANDARD 11)

find_package(OpenCV 4.6.0 REQUIRED)

message(STATUS "OpenCV version:" ${
    
    OpenCV_VERSION})

add_executable(mini_project_opencv main.cpp)

TARGET_LINK_LIBRARIES(mini_project_opencv PRIVATE
        ${
    
    OpenCV_LIBS})

target_link_directories(mini_project_opencv PRIVATE
        ${
    
    OpenCV_INCLUDE_DIRS})

The content of main.cpp is as follows.

#include <iostream>
#include "opencv2/core/utility.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
using namespace  cv;

const char* keys = {
    
    
        "{help h usage ? | | print this message}"
        "{@image | | Image to process}"
};

int main(int argc, char **argv) {
    
    
    CommandLineParser parser(argc,argv,keys);
    parser.about("photo tool v1.0.0");
    if(parser.has("help")){
    
    
        parser.printMessage();
        return 0;
    }
    std::string imgFilePath = parser.get<String>(0);
    if(!parser.check()){
    
    
        parser.printErrors();
        return 0;
    }

    cv::Mat img = cv::imread(imgFilePath);
    if(img.data){
    
    
        namedWindow("test",cv::WINDOW_NORMAL);
        cv::imshow("test",img);
        waitKey(0);
    }

    std::cout << "Hello, World!" << std::endl;
    return 0;

}

Compile with the cmake command:
In the same directory, enter the following command line

cmake -S . -B build
camke --build build 

If there is no accident, there will be executable files generated in the build. Run to see the test image.

Guess you like

Origin blog.csdn.net/m0_49302377/article/details/130476545