一、简介
1、OpenCV的特点
(1) 总体描述
- OpenCV是一个基于C/C++语言的开源图像处理函数库
- 其代码都经过优化,可用于实时处理图像
- 具有良好的可移植性
- 可以进行图像/视频载入、保存和采集的常规操作
- 具有低级和高级的应用程序接口(API)
- 提供了面向Intel IPP高效多媒体函数库的接口,可针对你使用的Intel CPU优化代码,提高程序性能(译注:OpenCV 2.0版的代码已显着优化,无需IPP来提升性能,故2.0版不再提供IPP接口)
(2) 功能
- 图像数据操作(内存分配与释放,图像复制、设定和转换)
Image data manipulation (allocation, release, copying, setting, conversion). - 图像/视频的输入输出(支持文件或摄像头的输入,图像/视频文件的输出)
Image and video I/O (file and camera based input, image/video file output). - 矩阵/向量数据操作及线性代数运算(矩阵乘积、矩阵方程求解、特征值、奇异值分解)
Matrix and vector manipulation and linear algebra routines (products, solvers, eigenvalues, SVD). - 支持多种动态数据结构(链表、队列、数据集、树、图)
Various dynamic data structures (lists, queues, sets, trees, graphs). - 基本图像处理(去噪、边缘检测、角点检测、采样与插值、色彩变换、形态学处理、直方图、图像金字塔结构)
Basic image processing (filtering, edge detection, corner detection, sampling and interpolation, color conversion, morphological operations, histograms, image pyramids). - 结构分析(连通域/分支、轮廓处理、距离转换、图像矩、模板匹配、霍夫变换、多项式逼近、曲线拟合、椭圆拟合、狄劳尼三角化)
Structural analysis (connected components, contour processing, distance transform, various moments, template matching, Hough transform, polygonal approximation, line fitting, ellipse fitting, Delaunay triangulation). - 摄像头定标(寻找和跟踪定标模式、参数定标、基本矩阵估计、单应矩阵估计、立体视觉匹配)
Camera calibration (finding and tracking calibration patterns, calibration, fundamental matrix estimation, homography estimation, stereo correspondence). - 运动分析(光流、动作分割、目标跟踪)
Motion analysis (optical flow, motion segmentation, tracking). ##目标识别(特征方法、HMM模型)
Object recognition (eigen-methods, HMM). - 基本的GUI(显示图像/视频、键盘/鼠标操作、滑动条)
Basic GUI (display image/video, keyboard and mouse handling, scroll-bars). ##图像标注(直线、曲线、多边形、文本标注)
Image labeling (line, conic, polygon, text drawing)
(3) OpenCV模块
- cv – 核心函数库
- cvaux – 辅助函数库
- cxcore – 数据结构与线性代数库
- highgui – GUI函数库 graphical user interface
- ml – 机器学习函数库 machine learing
2、有用的学习资源
(1) 参考手册:
- < opencv-root >/docs/index.htm (译注:在你的OpenCV安装目录< opencv-root >内)
(2) 网络资源:
- 官方网站: http://www.intel.com/technology/computing/opencv/
- 软件下载: http://sourceforge.net/projects/opencvlibrary/
(3) 书籍:
- Open Source Computer Vision Library
by Gary R. Bradski, Vadim Pisarevsky, and Jean-Yves Bouguet, Springer, 1st ed. (June, 2006). chenyusiyuan: 补充以下书籍 - Learning OpenCV - Computer Vision with the OpenCV Library
by Gary Bradski & Adrian Kaehler, O’Reilly Media, 1 st ed. (September, 2008). - OpenCV教程——基础篇
作者:刘瑞祯 于仕琪,北京航空航天大学出版社,出版日期:200706
(4) 视频处理例程(在 < opencv-root >/samples/c/):
- 颜色跟踪: camshiftdemo
- 点跟踪: lkdemo
- 动作分割: motempl
- 边缘检测: laplace
(5) 图像处理例程 (在 < opencv-root>/samples/c/):
- 边缘检测: edge
- 图像分割: pyramid_segmentation
- 形态学: morphology
- 直方图: demhist
- 距离变换: distrans
- 椭圆拟合: fitellipse
3、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 <image-file-name>\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);
// IplImage *pDstImg = cvCreateImage(cvGetSize(img),img->depth,img->nChannels);
// cvNot(img, pDstImg);
for(i=0;i<height;i++) for(j=0;j<width;j++) for(k=0;k<channels;k++)
data[i*step+j*channels+k]=255-data[i*step+j*channels+k];
// show the image
cvShowImage("mainWin", img );
// wait for a key
cvWaitKey(0);
// release the image
cvReleaseImage(&img );
return 0;
}
二、GUI 指令
1、窗口管理
(1) 创建和定位一个新窗口:
cvNamedWindow("win1", CV_WINDOW_AUTOSIZE);
cvMoveWindow("win1", 100, 100); // offset from the UL corner of the screen
(2) 载入图像:
IplImage* img=0;
img=cvLoadImage(fileName, CV_LOAD_IMAGE_COLOR);
if(!img) printf("Could not load image file: %s\n",fileName);
(3) 显示图像:
cvShowImage("win1",img);
(4) 关闭窗口:
cvDestroyWindow("win1");
(5) 改变窗口大小:
cvResizeWindow("win1",100,100); // new width/heigh in pixels
2、输入处理
(1) 处理鼠标事件:
定义一个鼠标处理程序:
void mouseHandler(int event, int x, int y, int flags, void* param) { switch(event){ case CV_EVENT_LBUTTONDOWN: if(flags & CV_EVENT_FLAG_CTRLKEY) printf("Left button down with CTRL pressed\n"); break; case CV_EVENT_LBUTTONUP: printf("Left button up\n"); break; } }
x,y: 相对于左上角的像素座标
event: CV_EVENT_LBUTTONDOWN, CV_EVENT_RBUTTONDOWN, CV_EVENT_MBUTTONDOWN,
CV_EVENT_LBUTTONUP, CV_EVENT_RBUTTONUP, CV_EVENT_MBUTTONUP,
CV_EVENT_LBUTTONDBLCLK, CV_EVENT_RBUTTONDBLCLK, CV_EVENT_MBUTTONDBLCLK,
CV_EVENT_MOUSEMOVE:flags: CV_EVENT_FLAG_CTRLKEY, CV_EVENT_FLAG_SHIFTKEY, CV_EVENT_FLAG_ALTKEY,
CV_EVENT_FLAG_LBUTTON, CV_EVENT_FLAG_RBUTTON, CV_EVENT_FLAG_MBUTTON
注册该事件处理程序:
mouseParam=5;
cvSetMouseCallback(“win1”,mouseHandler,&mouseParam); //第三个参数可以设置为NULL
(未完待续)