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:

最新文章

  1. Level 4 A10: 飞张?
  2. javaWEB中的HttpServlet(企业开发使用)
  3. 详解DHV:怎么具体展示高价值
  4. python生态环境
  5. POJ 3692
  6. C#调用java接口报“Fault occurred while processing”异常问题
  7. php中12个魔术方法
  8. leetcode第40题--First Missing Positive
  9. Http相关
  10. [Swift]LeetCode926. 将字符串翻转到单调递增 | Flip String to Monotone Increasing
  11. FPM四:用OVP做查询跳转到明细
  12. hdu 3966(树链剖分+线段树区间更新)
  13. MariaDB:登陆报错:mysqladmin: connect to server at 'localhost' failed
  14. ASP.NET AJAX入门系列(11):在多个UpdatePanle中使用Timer控件
  15. 对Kalman(卡尔曼)滤波器的理解@@zz
  16. java web中读取properties文件时的路径问题
  17. lamp-linux2
  18. Docker-commit镜像提交
  19. angularjs中factory, service和provider
  20. jenkins 入门教程

热门文章

  1. android程序的安装与卸载
  2. PHP 面向对象之自定义类
  3. .net 反射访问私有变量和私有方法
  4. Apache httpd.conf的翻译
  5. 第 6 章 抽象工厂模式【Abstract Factory Pattern】
  6. TFS环境搭建
  7. 【技术贴】解决Mysql启动服务报错1067 进程意外终止
  8. 3D触控简介:建立数字刻度应用及快速活动栏
  9. 【网络流24题】 No.10 餐巾计划问题 (线性规划网络优化 最小费用最大流)
  10. [转贴]关于C++的抽象的一点新认识