linux cmake配合opencv2.4.13使用

C++调用opencv
  1. 建立main.cpp, 代码如下:

    #include 
    #include 
    #include 
    using namespace cv;
    using namespace std;
    
    int main( )
    {
        string path = "lena.jpg";
        Mat image;
        image = imread(path);
    
        if ( !image.data )
        {
            printf("No image data \n");
            return -1;
        }
        namedWindow("Display Image", WINDOW_AUTOSIZE );
        imshow("Display Image", image);
    
        waitKey(0);
    
        return 0;
    }
    
  2. 建立CMakeLists 文件
    文件内容如下:

    cmake_minimum_required(VERSION 2.8)
    
    project( demo )
    
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++0x")  
    
    find_package( OpenCV REQUIRED )
    
    add_executable( demo main.cpp )
    
    target_link_libraries( demo ${OpenCV_LIBS} )
    
  3. 编译运行

     cmake .
     make 
     ./demo
    
  4. 运行效果
    linux cmake配合opencv2.4.13使用_第1张图片


C++ opencv 第三方库libxml2(读写xml文件为例)

CMakeLists.txt 文件编写:

    ```
    cmake_minimum_required(VERSION 2.6)

    project(temperaturedetection1027)


    set (CMAKE_INCLUDE_CURRENT_DIR ON)

    if(COMMAND cmake_policy)
          cmake_policy(SET CMP0003 NEW)
    endif(COMMAND cmake_policy)

    FIND_PACKAGE(OpenCV REQUIRED)


    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++0x")  

    set(SOURCE_FILES main.cpp  write_xml.h ) 


    include_directories(/usr/include/libxml2)

    add_executable(TPD ${SOURCE_FILES})

    TARGET_LINK_LIBRARIES(TPD ${OpenCV_LIBS} xml2 )
    ```

你可能感兴趣的:(opencv,环境配置)