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>\n");
return -1;
} Mat image;
image = imread( argv[1], 1 ); if ( !image.data )
{
printf("No image data \n");
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:

Using OpenCV with gcc and CMake的更多相关文章

  1. opencv学习_15 (利用cmake查看opencv的源码)

    当我们有时想查看opencv自带的函数的源代码,比如函数cvCreateImage, 此时我们选中cvCreateImage, 点击鼠标右键->转到定义,我们会很惊讶的发现为什么只看到了cvCr ...

  2. bsd pkg install gcc gmake cmake gdb cgdb

    bsd pkg install gcc gmake cmake gdb cgdb 安装pkg帮助文档并查看文档# pkg help install# man pkg-install # pkg sea ...

  3. opencv配置过程 (cmake,vs2013,qt 5.4)

    平台及软件: Windows 7 X86 Visual Studio 2013 OpenCV3.0.0 Cmake3.3 1.下载Windows下的安装文件OpenCV-3.0.0.exe,解压,选择 ...

  4. OpenCV.3.4.6_VS2015&cmake编译x86版本的bin&lib

    ZC:<<OpenCV3编程入门>> 的 2.2.2 中也有该内容的讲解 1.参考网址:opencv3.3.0+vs2015+cmake编译opencv x86 - wowo的 ...

  5. 非root源码安装gcc和cmake

    一.安装cmake 1.下载cmale 下载地址:https://cmake.org/files tar -zxvf cmake-3.19.8.tar.gz cd cmake-3.19.8/ ./co ...

  6. opencv Installation in Linux and hello world

    http://opencv.org/quickstart.html Installation in Linux These steps have been tested for Ubuntu 10.0 ...

  7. ubuntu12.04安装Opencv2.4.9

    之前在Linux下装过几次opencv,但几乎每次都要查一下怎么安装,这次索性记录一下安装过程,不用每次都看其他人的教程了. 至于安装过程,可以直接参考官方文档.在解压后的文件夹下opencv\bui ...

  8. OpenCV linux cmake添加使用

    安装好opencv之后: 只需要添加一下,就可以方便的使用opencv了,find_package opencv 会寻找FindOpenCV.cmake find_package(OpenCV REQ ...

  9. 在ubunt14.04(linux)下利用cmake编译运行opencv程序

    今天在电脑上安装好了opencv环境,迫不及待的想写个程序来测试一下.但是在windows下我们用vs等集成开发工具.可是在linux下我们应该怎么办呢? 这里我们用了opencv推荐的cmake来编 ...

随机推荐

  1. main(int argc , char *argv[])

    #include <unistd.h>#include <stdlib.h>#include <stdio.h> int main(int argc, char * ...

  2. xp 下卸载 硬盘安装的 ubuntu (本人的悲伤史)

    正常启动XP系统,到http://www.sysint.no 下载 MBRFIX.zip,解压,把文件放在C盘, 点击“开始”==“运行”==“cmd”,出现下面图 输入cd\,如下图, 再按回车键, ...

  3. DOM Ready 详解

    DOM Ready 概述 熟悉jQuery的人, 都知道DomReady事件. window.onload事件是在页面所有的资源都加载完毕后触发的. 如果页面上有大图片等资源响应缓慢, 会导致wind ...

  4. iOS 改变UILabel部分颜色

    //协议 UILabel *xieLabel = [[UILabel alloc] init]; xieLabel.textColor = TextGrayColor; xieLabel.font = ...

  5. 从小白进阶ubuntu高手的必经之路—命令

    精选的十二个ubuntu下的命令,熟记于心,则能甩掉ubuntu小白标签,高手的伟岸形象焕然生发.一.管理员权限绝大部分情况下,命令的行为须要被赋予管理员权限才能执行.命令 sudo 作用:赋予当前命 ...

  6. 事件tou

    #define EV_TIMER_RESOLUTION 1 /* 1 msec */ #define EV_READ_EVENT EPOLLIN #define EV_WRITE_EVENT EPOL ...

  7. uva 11922 - Permutation Transformer

    splay的题: 学习白书上和网上的代码敲的: #include <cstdio> #include <cstring> #include <cstdlib> #i ...

  8. Performance Test of List<T>, LinkedList<T>, Queue<T>, ConcurrentQueue<T>

    //Test Group 1 { var watch = Stopwatch.StartNew(); var list = new List<int>(); ; j < ; j++) ...

  9. Linux下实现定时器Timer的几种方法

    http://blog.csdn.net/lxmky/article/details/7669296 第六章 IO复用:select和poll函数 http://www.cnblogs.com/4ti ...

  10. 解决qt5窗口不刷新(测试窗口类型,测试窗口属性)

    QApplication::notify #if QT_VERSION >= 0x050000        if (QEvent::Show == pEvent->type())     ...