头文件Header files
OpenCV提供了很多重要的C++头文件作为接口。作为初学者,需要在编程的时候加入相应的头文件,在随后的学习笔记中将会举例。如果不确定该包含什么样的头文件,那么就把下列头文件全部#include.
- #include "opencv2/core/core.hpp"
- #include "opencv2/flann/miniflann.hpp"
- #include "opencv2/imgproc/imgproc.hpp"
- #include "opencv2/photo/photo.hpp"
- #include "opencv2/video/video.hpp"
- #include "opencv2/features2d/features2d.hpp"
- #include "opencv2/objdetect/objdetect.hpp"
- #include "opencv2/calib3d/calib3d.hpp"
- #include "opencv2/ml/ml.hpp"
- #include "opencv2/highgui/highgui.hpp"
- #include "opencv2/contrib/contrib.hpp"
- #include "opencv2/core/core_c.h"
- #include "opencv2/highgui/highgui_c.h"
- #include "opencv2/imgproc/imgproc_c.h"
所有的OpenCV类和对象在cv的命名空间。所以在定义的时候采取下面一种方式即可。
- 添加 'using namespace cv' 头文件代码之后
e.g.
#include "opencv2/core/core.hpp"
using namespace cv;
#include "opencv2/core/core.hpp"
int main()
{
Mat frame = cvQueryFrame( capture );
imshow( "Video", frame );
}
#include "opencv2/core/core.hpp"
- 在每个OpenCV的类及函数前面添加 cv::
#include "opencv2/core/core.hpp"
int main()
{
cv::
Mat frame = cvQueryFrame( capture );
cv::imshow( "Video", frame );
}
数组数据类型 Data Types for Arrays
单通道数组 :
- CV_8U (8 bit 无符号整数)
- CV_8S (8 bit 有符号整数)
- CV_16U (16 bit 无符号整数)
- CV_16S (16 bit 有符号整数)
- CV_32S (32 bit 有符号整数)
- CV_32F (32 bit 浮点数)
- CV_64F (64 bit 浮点数)
e.g. : 如下图所示的是一个单通道
8 bit 无符号整数的图片,数据类型为
8 bit 无符号整数,每个像素的值均为0~255.
Single Channel Array |
多通道数组:
也可以定义所有的数据类型为多通道数组 (最高支持到512通道). 这里举例如何定义一个
CV_8U类型数据的多通道数组
- CV_8UC1 (8 bit 无符号整数单通道)
- CV_8UC2 (8 bit 无符号整数双通道)
- CV_8UC3 (8 bit 无符号整数三通道)
- CV_8UC4 (8 bit 无符号整数四通道)
- CV_8UC(n) (8 bit 无符号整数n通道 (n 定义为 1 to 512) )
e.g. 1 : 如下图所示
一个三通道
8 bit 无符号整数的图片,数据类型为8 bit 无符号整数,每个像素的值均为0~255。由于此图为三通道数组,数组包含的元组 tuples有3个元素. 第一个元组的值为 {54, 0, 34},
第2个元组的值为{58, 78, 185} 以此类推。
3 Channel Arrays |
e.g. 2 : 如下图所示一个双通道8 bit 有符号整数的图片,数据类型为8 bit 有符号整数,每个像素的值均为-128~127。由于此图为双通道数组,数组包含的元组 tuples有2个元素. 第一个元组的值为 {-85, -127},, 第2个元组的值为{25, 23} 以此类推。
2 Channel Array |
注意 : CV_8U = CV_8UC1 = CV_8UC(1)
应用举例 :
- Mat img1(3, 5, CV_32F ); //3 x 5 single-channel array with 32 bit floating point numbers
- Mat img2(23, 53, CV_64FC(5) ); //23 x 53 5-channel array with 64 bit floating point numbers
- Mat img3(Size(100, 200), CV_16UC2 ); //100 x 200 2-channel array with 16 bit unsigned integers
位元深度-简称位深 Bit Depth IplImage (C 语言)
- IPL_DEPTH_<bit_depth>(S|U|F)
- <bit_depth> 可能的值是 1,8,16,32,64
- S = Signed
- U = Unsigned
- F = Float
- 1 位深图像应为无符类型
- 8位深图像应为无符类型或有符类型
- 16位深图像应为无符类型或有符类型
- 32位深图像应为有符类型或浮点型
- 64 位深图像应为浮点型
- E.g.:
- IPL_DEPTH_1U (1 bit depth and unsigned)
- IPL_DEPTH_8U (8 bit depth and unsigned)
- IPL_DEPTH_16U
- IPL_DEPTH_32F ( 32 bit depth and float )
- IPL_DEPTH_8S
- IPL_DEPTH_16S ( 16 bit depth and signed )
- IPL_DEPTH_32S
- IPL_DEPTH_64F
位元深度 Bit Depth 每个像素位的数目。例如, IplImage 定义 IPL_DEPTH_8U每个像素采用了 8位无符整形,且每个像素的值均为0~255的整数。
目前IplImage支持的数据结构包括IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16S, IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F.