Using OpenCV with gcc and CMake
2015-09-21 13:36
447 查看
Using OpenCV with gcc and CMake
NoteWe 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):
No need to change anything when porting between Linux and Windows
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:
相关文章推荐
- linux安装安卓开发工具android studio
- 【转】linux下安装opencv
- 【Linux导论】Linux术语(Linux Terminology)
- Tizen 3.0 发布:采用 Linux 4.1 内核与 Wayland、支持64位
- shell 环境初始化顺序
- Linux下的无名管道
- Thinkpad 装 centos 7后发热量大处理
- Mysql 数据库自动备份Shell脚本
- linux syscon and regmap study note
- POJ 2112 Optimal Milking
- 阿里云服务器或者其他网站绑定域名方法
- 理解RESTful架构
- 阿里云服务器或者其他网站绑定域名方法
- centos7添加环境变量
- centos7的一些变动
- 桌面协议(1)rdesktop 24位色
- Linux内核编码规范
- docker学习笔记(二)导入导出镜像
- Hadoop的局限性
- linux下设置代理服务器