(i) OpenCV Learning notes--linux compile run OPENCV program __linux

Source: Internet
Author: User

Recently in learning OpenCV, write notes to sum up, I use the learning environment is ubuntu16.04+kdvelop4.7+opencv3.2.0.
There are two ways to compile a OPENCV program under Linux. One is g++, the other is CMake, here is the direct use of the KDvelop4.7, the advantages of using the IDE are as follows:1, can load OpenCV Library header file, like writing #include "opencv2/opencv.hpp." 2, in the preparation of OPENCV program, can be automatic code completion, very convenient. 3, the use of the IDE, new C + + project, can automatically create new CMakeLists.txt, only need to modify it to load OpenCV compile library, you can run OPENCV program on the IDE. I am also in the other C + + IDE, such as Clion also OK. The following is an introduction to the specific implementation process.
 
Create a new Test.cpp program code as follows:

all CV-related types under #include <opencv2/opencv.hpp> using namespace cv;//are not prefixed with the
int main () {
    Mat img =imread ( "/home/douxiao/desktop/opencvtest/demo1/001.jpg")//Declare mat variable, and read the picture, imread the absolute path to the incoming picture
    imshow ("image", IMG); /Display the loaded picture in the window
    Waitkey ()/wait for the key to return
    0;

The first method uses the g++ command-line parameter method :

$ gcc test.cpp-o test ' pkg-config--cflags--libs OpenCV '
$./test//Run, show picture

In the above compilation command we actually use a tool "Pkg-config", which has the following functions:
Check the version number of the library . If the desired version of the library does not meet the requirements, it prints out the error message and avoids linking the wrong version of the library file. gets the compilation preprocessing parameters , such as macro definitions, and the location of the header file. Get the link parameters, such as the location of the library and other dependent libraries, the filename, and some other connection parameters. automatically join the other libraries that depend on the settings with this tool our compilation is convenient (but before you make sure that you have a Pkgconfig folder in the directory of the OPENCV installation link library files that you install. In this folder there is a opencv.pc file, in fact, this is the pkg-config under the OpenCV configuration file.
When using Pkg-config, the option –cflags It is used to specify the directory in which the program needs the header file at compile time , and the option –libs Specifies the dynamic link library that the program needs to link to The directory.
 
the second method is described below, using cmake:

First step:
Use Kdvelop to create a new C + + project, a lot of tutorials to build a project, Google itself.
My file path is shown below:

 
  The following screenshot of your own path directory during construction:
 
 
 

The project screenshot, will mian.cpp and CMakeLists.txt to read as follows:

#这是对CMake工具最低版本要求, here we check the version information of our CMake tool, we can use the command "CMake--version" to view
cmake_minimum_required (version 2.6) Project
(test) #这是建立一个工程项目, the name of the project in parentheses, the project name we can give any, the final program compiled the executable file is this name
Find_package (OpenCV  REQUIRED) #用Cmake查找opencv包
Set (cmake_cxx_standard) #用C ++11 Standard library compiles
set (source_ FILES main.cpp)
add_executable (Test ${source_files}) #这里括号里面的两个参数分别是工程项目名和我们要编译文件名的意思, remember the middle space key to separate
target_ Link_libraries (Test ${opencv_libs}) #这是我们链接到OpenCV库的环节, we only need to change the first parameter before the name of our project

Compile run:

To add a file:

The image appears as follows:

Success
Here's your summary for today.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.