PS. 由于csdn博客文章长度有限制,本文有部分内容被截掉了。
在OpenCV中文网站的wiki上有可读性更好、并且是完整的版本,欢迎浏览。
OpenCV Wiki :《OpenCV 编程简介(矩阵/图像/视频的基本读写操作)》
Introduction to programming with OpenCV
OpenCV编程简介
Gady Agam
Department of Computer Science
January 27, 2006
Illinois Institute of Technology
译: chenyusiyuan
-- January 26, 2010
-- http://blog.csdn.net/chenyusiyuan
摘要:
本文旨在帮助读者快速入门OpenCV,而无需阅读冗长的参考手册。掌握了OpenCV的以下基础知识后,有需要的话再查阅相关的参考手册。
目录
一 简介
o OpenCV的特点
o 有用的学习资源
o 命名规则
o 编译建议
o 一个例程
二 GUI命令
o 窗口管理
o 输入处理
三 OpenCV基本的数据结构
o 图像数据结构
o 矩阵与向量
o 其它数据结构
四 图像处理
o 内存分配与释放
o 读取和写入图像
o 访问图像像素
o 图像转换
o 绘图指令
五 矩阵处理
o 内存分配与释放
o 访问矩阵元素
o 矩阵/向量运算
六 视频处理
o 视频的帧捕捉
o 帧信息的读取与设置
o 保存视频文件
=================================================
一、简介
1、OpenCV的特点
(1) 总体描述
o OpenCV是一个基于C/C++语言的开源图像处理函数库
o 其代码都经过优化,可用于实时处理图像
o 具有良好的可移植性
o 可以进行图像/视频载入、保存和采集的常规操作
o 具有低级和高级的应用程序接口(API)
o 提供了面向Intel IPP高效多媒体函数库的接口,可针对你使用的Intel CPU优化代码,提高程序性能(译注:OpenCV 2.0版的代码已显著优化,无需IPP来提升性能,故2.0版不再提供IPP接口)
(2) 功能
o 图像数据操作(内存分配与释放,图像复制、设定和转换)
Image data manipulation (allocation, release, copying, setting, conversion).
o 图像/视频的输入输出(支持文件或摄像头的输入,图像/视频文件的输出)
Image and video I/O (file and camera based input, image/video file output).
o 矩阵/向量数据操作及线性代数运算(矩阵乘积、矩阵方程求解、特征值、奇异值分解)
Matrix and vector manipulation and linear algebra routines (products, solvers, eigenvalues, SVD).
o 支持多种动态数据结构(链表、队列、数据集、树、图)
Various dynamic data structures (lists, queues, sets, trees, graphs).
o 基本图像处理(去噪、边缘检测、角点检测、采样与插值、色彩变换、形态学处理、直方图、图像金字塔结构)
Basic image processing (filtering, edge detection, corner detection, sampling and interpolation, color conversion, morphological operations, histograms, image pyramids).
o 结构分析(连通域/分支、轮廓处理、距离转换、图像矩、模板匹配、霍夫变换、多项式逼近、曲线拟合、椭圆拟合、狄劳尼三角化)
Structural analysis (connected components, contour processing, distance transform, various moments, template matching, Hough transform, polygonal approximation, line fitting, ellipse fitting, Delaunay triangulation).
o 摄像头定标(寻找和跟踪定标模式、参数定标、基本矩阵估计、单应矩阵估计、立体视觉匹配)
Camera calibration (finding and tracking calibration patterns, calibration, fundamental matrix estimation, homography estimation, stereo correspondence).
o 运动分析(光流、动作分割、目标跟踪)
Motion analysis (optical flow, motion segmentation, tracking).
o 目标识别(特征方法、HMM模型)Object recognition (eigen-methods, HMM).
o 基本的GUI(显示图像/视频、键盘/鼠标操作、滑动条)
Basic GUI (display image/video, keyboard and mouse handling, scroll-bars).
o 图像标注(直线、曲线、多边形、文本标注)
Image labeling (line, conic, polygon, text drawing)
(3) OpenCV模块
o cv – 核心函数库
o cvaux – 辅助函数库
o cxcore – 数据结构与线性代数库
o highgui – GUI函数库
o ml – 机器学习函数库
2、有用的学习资源
(1) 参考手册:
o /docs/index.htm (译注:在你的OpenCV安装目录内)
? 网络资源:
o 官方网站: http://www.intel.com/technology/computing/opencv/
o 软件下载: http://sourceforge.net/projects/opencvlibrary/
(2) 书籍:
o Open Source Computer Vision Library
by Gary R. Bradski, Vadim Pisarevsky, and Jean-Yves Bouguet, Springer, 1st ed. (June, 2006).
chenyusiyuan: 补充以下书籍
o Learning OpenCV - Computer Vision with the OpenCV Library
by Gary Bradski & Adrian Kaehler, O'Reilly Media, 1 st ed. (September, 2008).
o OpenCV教程——基础篇
作者:刘瑞祯 于仕琪,北京航空航天大学出版社,出版日期:200706
(3) 视频处理例程(在 /samples/c/):
o 颜色跟踪: camshiftdemo
o 点跟踪: lkdemo
o 动作分割: motempl
o 边缘检测: laplace
(4) 图像处理例程 (在 /samples/c/):
o 边缘检测: edge
o 图像分割: pyramid_segmentation
o 形态学: morphology
o 直方图: demhist
o 距离变换: distrans
o 椭圆拟合: fitellipse
3、OpenCV 命名规则
(1) 函数名:
cvActionTargetMod(...) Action = 核心功能(core functionality) (e.g. set, create) Target = 目标图像区域(target image area) (e.g. contour, polygon) Mod = (可选的)调整语(optional modifiers) (e.g. argument type)
(2) 矩阵数据类型:
CV_<bit_depth>(S|U|F)C<number_of_channels> S = 符号整型 U = 无符号整型 F = 浮点型 E.g.: CV_8UC1 是指一个8位无符号整型单通道矩阵, CV_32FC2是指一个32位浮点型双通道矩阵.
(3) 图像数据类型:
IPL_DEPTH_<bit_depth>(S|U|F) E.g.: IPL_DEPTH_8U 图像像素数据是8位无符号整型. IPL_DEPTH_32F图像像素数据是32位浮点型.
(4) 头文件:
#include <cv.h> #include <cvaux.h> #include <highgui.h> #include <ml.h> #include <cxcore.h> // 一般不需要,cv.h 内已包含该头文件
4、编译建议
(1) Linux:
g++ hello-world.cpp -o hello-world / -I /usr/local/include/opencv -L /usr/local/lib / -lm -lcv -lhighgui -lcvaux
(2) Windows:
在Visual Studio的‘选项’和‘项目’中设置好OpenCV相关文件的路径。
5、C例程
// // hello-world.cpp // // 该程序从文件中读入一幅图像,将之反色,然后显示出来. // #include <stdlib.h> #include <stdio.h> #include <math.h> #include <cv.h> #include <highgui.h> int main(int argc, char *argv[]) { IplImage* img = 0; int height,width,step,channels; uchar *data; int i,j,k; if(argc<2){ printf("Usage: main /n/7"); exit(0); } // load an image img=cvLoadImage(argv[1]); if(!img){ printf("Could not load image file: %s/n",argv[1]); exit(0); } // get the image data height = img->height; width = img->width; step = img->widthStep; channels = img->nChannels; data = (uchar *)img->imageData; printf("Processing a %dx%d image with %d channels/n",height,width,channels); // create a window cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE); cvMoveWindow("mainWin", 100, 100); // invert the image // 相当于 cvNot(img); for(i=0;i<height;i++) for(j=0;j<</