一 opencv常用的数据结构
Point类
Point类数据结构表示二维坐标系的点,由坐标x, y指定的2D点.
Point pt;
pt.x=10;
pt.y=20;
cout<<pt<<endl;
运行结果:
Rect类
Rect类用来表示矩形.
1. 成员变量
x
y
width
height
2. 成员函数
1)
Size()
返回值Size表示大小
2)
area()
返回矩形面积
3)
contains(Point)
判断点是否在矩形内
4)
Inside(Rect)
判断矩形是否在该矩形内
5)
tl()
返回左上角坐标
6)
br()
返回右下角坐标
7)求两个矩形的交集并集:
Rect rect = rect1 & rect2;
Rect rect = rect1 | rect2;
3.代码以及运行结果
代码
Rect rect(10,20,10,10);
cout<<rect<<endl;
运行结果
Scalar类
Scalar()表示具有四个元素的数组,大量用来传递像素值,如RGB颜色,一般形式:
Scalar(double B, double G, double R, double Alpha)
如果用不到第四个则表示Scalar(B, G, R), 其中:
B—表示蓝色分量,G—表示绿色分量
R—表示红色分量,Alpha—表示透明度
注意:Scalar表示颜色顺序为BGR
Scalar(255, 0, 0) ----表示纯蓝色
Scalar(0, 255, 0) ----表示纯绿色
Scalar(0, 0, 255) ----表示纯红色
Scalar(255, 255, 0) ----表示青色
Scalar(0, 255, 255) ----表示黄色
二 Mat类基础
Mat的优势
我们可以借助很多方法来获取图像,存储到数字设备中记录的每个像素点的数值。
opencv2.x以上版本提供Mat结构来存储图像,优化了内存管理,不用手动开辟空间,也不必在不使用的时候立即释放,减少内存泄漏的风险。
Mat类简介
Mat是一个类,由两个数据部分组成:矩阵头(大小,通道,数据类型等)和数据块(像素值)。
Mat头部属性:
rows,cols,channels,data
其中,data是一个指向数据块的指针,可以用如
下方法判断图像是否为空:
if(!img.data) //图像数据为空
return;
if(img.empty())
return;
Mat类操作
1. Mat类创建
创建图像
Mat img;//无初始化赋值
Mat img1(100,200,CV_8UC1);
//Mat img1(Size(200,100),CV_8UC1);
两种初始化方式完成的效果相同
2.Mat类赋值拷贝
Mat img(100, 200, CV_8UC3, Scalar(0,255,0));
和原图像指向同一数据块
Mat img2(img);
Mat img3 = img2);
原图像的副本
Mat img4 = img.clone();
Mat img 5;
img.copyTo(img5);
3. 常用颜色空间
1)三种颜色空间:
RBG颜色空间、HSV/HLS颜色空间、Lab颜色空间
H:色调
S:饱和度
V:亮度
RGB颜色空间
HSV颜色空间
Lab颜色空间
取值范围
2)
转换所用函数cvtColor(src, dst, code)
code取值为:
CV_BGR2GRAY、CV_BGR2HSV、CV_BGR2Lab
/** @brief Converts an image from one color space to another.
The function converts an input image from one color space to another. In case of a transformation
to-from RGB color space, the order of the channels should be specified explicitly (RGB or BGR). Note
that the default color format in OpenCV is often referred to as RGB but it is actually BGR (the
bytes are reversed). So the first byte in a standard (24-bit) color image will be an 8-bit Blue
component, the second byte will be Green, and the third byte will be Red. The fourth, fifth, and
sixth bytes would then be the second pixel (Blue, then Green, then Red), and so on.
The conventional ranges for R, G, and B channel values are:
- 0 to 255 for CV_8U images
- 0 to 65535 for CV_16U images
- 0 to 1 for CV_32F images
In case of linear transformations, the range does not matter. But in case of a non-linear
transformation, an input RGB image should be normalized to the proper value range to get the correct
results, for example, for RGB \f$\rightarrow\f$ L\*u\*v\* transformation. For example, if you have a
32-bit floating-point image directly converted from an 8-bit image without any scaling, then it will
have the 0..255 value range instead of 0..1 assumed by the function. So, before calling cvtColor ,
you need first to scale the image down:
@code
img *= 1./255;
cvtColor(img, img, COLOR_BGR2Luv);
@endcode
If you use cvtColor with 8-bit images, the conversion will have some information lost. For many
applications, this will not be noticeable but it is recommended to use 32-bit images in applications
that need the full range of colors or that convert an image before an operation and then convert
back.
If conversion adds the alpha channel, its value will set to the maximum of corresponding channel
range: 255 for CV_8U, 65535 for CV_16U, 1 for CV_32F.
@param src input image: 8-bit unsigned, 16-bit unsigned ( CV_16UC... ), or single-precision
floating-point.
@param dst output image of the same size and depth as src.
@param code color space conversion code (see cv::ColorConversionCodes).
@param dstCn number of channels in the destination image; if the parameter is 0, the number of the
channels is derived automatically from src and code.
@see @ref imgproc_color_conversions
*/
CV_EXPORTS_W void cvtColor( InputArray src, OutputArray dst, int code, int dstCn = 0 );