zoukankan      html  css  js  c++  java
  • Using OpenCV with gcc and CMake

    Using OpenCV with gcc and CMake

    Note

     

    We assume that you have successfully installed OpenCV in your workstation.

    • The easiest way of using OpenCV in your code is to use CMake. A few advantages (taken from the Wiki):
      1. No need to change anything when porting between Linux and Windows
      2. Can easily be combined with other tools by CMake( i.e. Qt, ITK and VTK )
    • If you are not familiar with CMake, checkout the tutorial on its website.

    Steps

    Create a program using OpenCV

    Let’s use a simple program such as DisplayImage.cpp shown below.

    #include <stdio.h>
    #include <opencv2/opencv.hpp>
    
    using namespace cv;
    
    int main(int argc, char** argv )
    {
        if ( argc != 2 )
        {
            printf("usage: DisplayImage.out <Image_Path>
    ");
            return -1;
        }
    
        Mat image;
        image = imread( argv[1], 1 );
    
        if ( !image.data )
        {
            printf("No image data 
    ");
            return -1;
        }
        namedWindow("Display Image", WINDOW_AUTOSIZE );
        imshow("Display Image", image);
    
        waitKey(0);
    
        return 0;
    }
    

    Create a CMake file

    Now you have to create your CMakeLists.txt file. It should look like this:

    cmake_minimum_required(VERSION 2.8)
    project( DisplayImage )
    find_package( OpenCV REQUIRED )
    include_directories( ${OpenCV_INCLUDE_DIRS} )
    add_executable( DisplayImage DisplayImage.cpp )
    target_link_libraries( DisplayImage ${OpenCV_LIBS} )
    

    Generate the executable

    This part is easy, just proceed as with any other project using CMake:

    cd <DisplayImage_directory>
    cmake .
    make
    

    Result

    By now you should have an executable (called DisplayImage in this case). You just have to run it giving an image location as an argument, i.e.:

    ./DisplayImage lena.jpg
    

    You should get a nice window as the one shown below:

    Display Image - Lena
  • 相关阅读:
    html图片预览
    网易DBA私享会分享会笔记2
    网易DBA私享会分享会笔记1
    centos6.5适用的国内yum源:网易、搜狐
    如何去除 ckeditor 上传图片后在原码中留下的 style="width: 100%;height:100px"之类的代码呢?
    关于json.ajax ,php的那点事
    去掉所有的html标签
    about JNI
    some knowledge of maven {maven实战}
    What is Proguard?
  • 原文地址:https://www.cnblogs.com/sdlypyzq/p/4825913.html
Copyright © 2011-2022 走看看