第三章 初探opencv
Opencv的基本数据类型
typedef struct CvPoint
{
int x;
int y;
#ifdef __cplusplus
CvPoint(int _x = 0, int _y = 0): x(_x), y(_y) {}
template<typename _Tp>
CvPoint(const cv::Point_<_Tp>& pt): x((int)pt.x), y((int)pt.y) {}
template<typename _Tp>
operator cv::Point_<_Tp>() const { return cv::Point_<_Tp>(cv::saturate_cast<_Tp>(x), cv::saturate_cast<_Tp>(y)); }
#endif
}
CvPoint;
包含两个变体类型 :
typedef struct CvPoint2D32f
{
float x;
float y;
#ifdef __cplusplus
CvPoint2D32f(float _x = 0, float _y = 0): x(_x), y(_y) {}
template<typename _Tp>
CvPoint2D32f(const cv::Point_<_Tp>& pt): x((float)pt.x), y((float)pt.y) {}
template<typename _Tp>
operator cv::Point_<_Tp>() const { return cv::Point_<_Tp>(cv::saturate_cast<_Tp>(x), cv::saturate_cast<_Tp>(y)); }
#endif
}
CvPoint2D32f;
typedef struct CvPoint3D32f
{
float x;
float y;
float z;
#ifdef __cplusplus
CvPoint3D32f(float _x = 0, float _y = 0, float _z = 0): x(_x), y(_y), z(_z) {}
template<typename _Tp>
CvPoint3D32f(const cv::Point3_<_Tp>& pt): x((float)pt.x), y((float)pt.y), z((float)pt.z) {}
template<typename _Tp>
operator cv::Point3_<_Tp>() const { return cv::Point3_<_Tp>(cv::saturate_cast<_Tp>(x), cv::saturate_cast<_Tp>(y), cv::saturate_cast<_Tp>(z)); }
#endif
}
CvPoint3D32f;
------------------------------cvsize
typedef struct CvSize
{
int width;
int height;
#ifdef __cplusplus
CvSize(int w = 0, int h = 0): width(w), height(h) {}
template<typename _Tp>
CvSize(const cv::Size_<_Tp>& sz): width(cv::saturate_cast<int>(sz.width)), height(cv::saturate_cast<int>(sz.height)) {}
template<typename _Tp>
operator cv::Size_<_Tp>() const { return cv::Size_<_Tp>(cv::saturate_cast<_Tp>(width), cv::saturate_cast<_Tp>(height)); }
#endif
}
CvSize;
变体
typedef struct CvSize2D32f
{
float width;
float height;
#ifdef __cplusplus
CvSize2D32f(float w = 0, float h = 0): width(w), height(h) {}
template<typename _Tp>
CvSize2D32f(const cv::Size_<_Tp>& sz): width(cv::saturate_cast<float>(sz.width)), height(cv::saturate_cast<float>(sz.height)) {}
template<typename _Tp>
operator cv::Size_<_Tp>() const { return cv::Size_<_Tp>(cv::saturate_cast<_Tp>(width), cv::saturate_cast<_Tp>(height)); }
#endif
}
CvSize2D32f;
---------------------------cvscalar val指向四个双精度浮点型数组 存放RGBA值
typedef struct CvScalar
{
double val[4];
#ifdef __cplusplus
CvScalar() {}
CvScalar(double d0, double d1 = 0, double d2 = 0, double d3 = 0) { val[0] = d0; val[1] = d1; val[2] = d2; val[3] = d3; }
template<typename _Tp>
CvScalar(const cv::Scalar_<_Tp>& s) { val[0] = s.val[0]; val[1] = s.val[1]; val[2] = s.val[2]; val[3] = s.val[3]; }
template<typename _Tp>
operator cv::Scalar_<_Tp>() const { return cv::Scalar_<_Tp>(cv::saturate_cast<_Tp>(val[0]), cv::saturate_cast<_Tp>(val[1]), cv::saturate_cast<_Tp>(val[2]), cv::saturate_cast<_Tp>(val[3])); }
template<typename _Tp, int cn>
CvScalar(const cv::Vec<_Tp, cn>& v)
{
int i;
for( i = 0; i < (cn < 4 ? cn : 4); i++ ) val[i] = v.val[i];
for( ; i < 4; i++ ) val[i] = 0;
}
#endif
}
CvScalar;
-------------------------cvrect
typedef struct CvRect
{
int x;
int y;
int width;
int height;
#ifdef __cplusplus
CvRect(int _x = 0, int _y = 0, int w = 0, int h = 0): x(_x), y(_y), width(w), height(h) {}
template<typename _Tp>
CvRect(const cv::Rect_<_Tp>& r): x(cv::saturate_cast<int>(r.x)), y(cv::saturate_cast<int>(r.y)), width(cv::saturate_cast<int>(r.width)), height(cv::saturate_cast<int>(r.height)) {}
template<typename _Tp>
operator cv::Rect_<_Tp>() const { return cv::Rect_<_Tp>((_Tp)x, (_Tp)y, (_Tp)width, (_Tp)height); }
#endif
}
CvRect;
矩阵和图像类型
图像操作:
缩放
单通道提取
找出特定通道的最大最小
两图像求和
对图像进行阀值操作
cvMat矩阵结构
新建一个二维矩阵的原型
/** @brief Creates a matrix header and allocates the matrix data.
The function call is equivalent to the following code:
@code
CvMat* mat = cvCreateMatHeader(rows, cols, type);
cvCreateData(mat);
@endcode
@param rows Number of rows in the matrix
@param cols Number of columns in the matrix
@param type The type of the matrix elements in the form
CV_<bit depth><S|U|F>C<number of channels> , where S=signed, U=unsigned, F=float. For
example, CV _ 8UC1 means the elements are 8-bit unsigned and the there is 1 channel, and CV _
32SC2 means the elements are 32-bit signed and there are 2 channels.
*/
CVAPI(CvMat*) cvCreateMat( int rows,//行
int cols,//列
int type );//预定义类型
cvCreateMat由多个原函数组成:
//创建cvmat结构,不分配内存
CVAPI(CvMat*) cvCreateMatHeader( int rows, int cols, int type );
//数据的内存分配
CVAPI(void) cvCreateData( CvArr* arr );
//根据现有矩阵,创建一个新的矩阵
/** Creates an exact copy of the input matrix (except, may be, step value) */
CVAPI(CvMat*) cvCloneMat( const CvMat* mat );
//释放矩阵
CVAPI(void) cvReleaseMat( CvMat** mat );
cvMat结构体
typedef struct CvMat
{
int type;
int step;
/* for internal use only */
int* refcount;
int hdr_refcount;
union
{
uchar* ptr;
short* s;
int* i;
float* fl;
double* db;
} data;
#ifdef __cplusplus
union
{
int rows;
int height;
};
union
{
int cols;
int width;
};
#else
int rows;
int cols;
#endif
#ifdef __cplusplus
CvMat() {}
CvMat(const CvMat& m) { memcpy(this, &m, sizeof(CvMat));}
CvMat(const cv::Mat& m);
#endif
}
CvMat;
矩阵数据的存取
访问矩阵的三种方法:
简单的方法
函数原型
#define CV_MAT_ELEM( mat, elemtype, row, col )
CvMat* mat = cvCreateMat(5,5,CV_32FC1);
//从矩阵中提取一个元素
float element_3_2 = CV_MAT_ELEM(*mat,//矩阵
float,//元素类型
3,//行
2);//列
//同时读取数据并设置数据
CvMat* mat = cvCreateMat(5, 5, CV_32FC1);
//从矩阵中提取一个元素
float element_3_2 = 7.7;
*((float*)CV_MAT_ELEM_PTR(*mat,//矩阵
3,//行
2))//列
= element_3_2;
麻烦的方法
//访问普通矩阵中的方法
CVAPI(uchar*) cvPtr1D( const CvArr* arr, //矩阵指针参数
int idx0, //索引的整数值
int* type CV_DEFAULT(NULL));//输出值的类型
CVAPI(uchar*) cvPtr2D( const CvArr* arr,
int idx0,
int idx1,
int* type CV_DEFAULT(NULL) );
CVAPI(uchar*) cvPtr3D( const CvArr* arr,
int idx0,
int idx1,
int idx2,
int* type CV_DEFAULT(NULL));
CVAPI(uchar*) cvPtrND( const CvArr* arr,
const int* idx, //指向整型数组的指针(索引)
int* type CV_DEFAULT(NULL),
int create_node CV_DEFAULT(1),
unsigned* precalc_hashval CV_DEFAULT(NULL));
只是读取数据
CVAPI(double) cvGetReal1D( const CvArr* arr, int idx0 );
CVAPI(double) cvGetReal2D( const CvArr* arr, int idx0, int idx1 );
CVAPI(double) cvGetReal3D( const CvArr* arr, int idx0, int idx1, int idx2 );
CVAPI(double) cvGetRealND( const CvArr* arr, const int* idx );
//会很浪费空间
CVAPI(CvScalar) cvGet1D( const CvArr* arr, int idx0 );
CVAPI(CvScalar) cvGet2D( const CvArr* arr, int idx0, int idx1 );
CVAPI(CvScalar) cvGet3D( const CvArr* arr, int idx0, int idx1, int idx2 );
CVAPI(CvScalar) cvGetND( const CvArr* arr, const int* idx );
为矩阵元素设定值
CVAPI(void) cvSetReal1D( CvArr* arr, int idx0, double value );
CVAPI(void) cvSetReal2D( CvArr* arr, int idx0, int idx1, double value );
CVAPI(void) cvSetReal3D( CvArr* arr, int idx0,
int idx1, int idx2, double value );
CVAPI(void) cvSetRealND( CvArr* arr, const int* idx, double value );
CVAPI(void) cvSet1D( CvArr* arr, int idx0, CvScalar value );
CVAPI(void) cvSet2D( CvArr* arr, int idx0, int idx1, CvScalar value );
CVAPI(void) cvSet3D( CvArr* arr, int idx0, int idx1, int idx2, CvScalar value );
CVAPI(void) cvSetND( CvArr* arr, const int* idx, CvScalar value );
处理浮点型单通道矩阵
double cvmGet( const CvMat* mat, int row, int col )
void cvmSet( CvMat* mat, int row, int col, double value )
恰当的方法
矩阵的存储是按光栅扫描顺序存储
/**
累加一个三通道矩阵中的所有元素
*/
float sum(const CvMat* mat) {
float s = 0.0f;
for (int row = 0; row < mat->rows; row++)
{
const float* ptr = (const float*)(mat->data.ptr + row*mat->step);
for (int col = 0; col < mat->cols; col++)
{
s += *ptr++;
}
}
return (s);
}
点的数组
计算给定点的位置的计算公式
δ=(row)*Ncols*Nchannels+(col)*Nchannels+(channel)
Ncols 列数
Nchannels 通道数
IplImage数据结构
_IplImage
{
int nSize; /**< sizeof(IplImage) */
int ID; /**< version (=0)*/
int nChannels; /**< Most of OpenCV functions support 1,2,3 or 4 channels
通道数
*/
int alphaChannel; /**< Ignored by OpenCV */
int depth; /**< Pixel depth in bits:
IPL_DEPTH_8U, 无符号8位整数
IPL_DEPTH_8S, 有符号8位整数
IPL_DEPTH_16S, 有符号16位整数
IPL_DEPTH_32S, 有符号32位整数
IPL_DEPTH_32F 32位符点数单精度
IPL_DEPTH_64F 64位符点数单双精度
are supported. */
char colorModel[4]; /**< Ignored by OpenCV */
char channelSeq[4]; /**< ditto */
int dataOrder; /**< 0 - interleaved color channels, 1 - separate color channels.
cvCreateImage can only create interleaved images
取值可以是
IPL_DATA_ORDER_PIXEL 将像素点不同通道的值交错排在一起
IPL_DATA_ORDER_PLANE 将像素同通道值排在一起
*/
int origin; /**< 0 - top-left origin,
1 - bottom-left origin (Windows bitmaps style).
IPL_ORIGIN_TL
IPL_ORIGIN_BL
设置坐示原点位置 ,左上角或者左下角
*/
int align; /**< Alignment of image rows (4 or 8).
OpenCV ignores it and uses widthStep instead. */
int width; /**< Image width in pixels. */
int height; /**< Image height in pixels. */
struct _IplROI *roi; /**< Image ROI. If NULL, the whole image is selected. */
struct _IplImage *maskROI; /**< Must be NULL. */
void *imageId; /**< " " */
struct _IplTileInfo *tileInfo; /**< " " */
int imageSize; /**< Image data size in bytes
(==image->height*image->widthStep
in case of interleaved data)*/
char *imageData; /**< Pointer to aligned image data.指向第一行数据的指针 */
int widthStep; /**< Size of aligned image row in bytes.
相邻行的同列点之间的字节数
*/
int BorderMode[4]; /**< Ignored by OpenCV. */
int BorderConst[4]; /**< Ditto. */
char *imageDataOrigin; /**< Pointer to very origin of image data
(not necessarily aligned) -
needed for correct deallocation */
#ifdef __cplusplus
_IplImage() {}
_IplImage(const cv::Mat& m);
#endif
}
IplImage;
访问图像数据
#include <opencv2opencv.hpp>
using namespace cv;
void staturate_sv(IplImage* img);
int main(int argc,char** argv) {
//读入图像
IplImage* img= cvLoadImage("test.jpg");
//
staturate_sv(img);
return 0;
}
/*
在色度不变的情况下,设置每个点的饱合度为255
*/
void staturate_sv(IplImage* img) {
for (int y = 0; y < img->height; y++)
{
//指向第y行的起始位置
uchar* ptr = (uchar*)(
img->imageData + y*img->widthStep
);
for (int x = 0; x < img->width; x++)
{
//c通道在x行的位置
ptr[3 * x + 1] = 255;
ptr[3 * x + 2] = 255;
}
}
}
对ROI和widthStep的补充
可以提高计算机视觉代码的执行速度
设置和取消ORI
CVAPI(void) cvSetImageROI( IplImage* image, CvRect rect );
CVAPI(void) cvResetImageROI( IplImage* image );
使用imageROI来增加某范围的像素
/*
用imageROI来增加某范围的像素
在图像roi的蓝色通道增加150级灰度的效果
*/
void roi_add() {
//可以从控制台传入参数
IplImage* src;
//加载图像
if ((src=cvLoadImage("test.jpg"))!=0) {
//初始化坐标
int x = atoi("10");
int y = atoi("10");
int width = atoi("50");
int height = atoi("50");
int add = atoi("150");
cvSetImageROI(src,cvRect(x,y,width,height));
cvAddS(src,cvScalar(add),src);
//释放roi
cvResetImageROI(src);
cvNamedWindow("Roi_Add",1);
cvShowImage("Roi_Add",src);
cvWaitKey();
}
}
如下图所示
/*
使用widthStep方法把图像的所有像素值加1
*/
CvRect interest_rect;
void addByWdthStep(IplImage* interest_rect_img) {
interest_rect =CvRect(10,10,50,50);
IplImage* sub_img = cvCreateImageHeader(
cvSize(
interest_rect.width,
interest_rect.height
),
interest_rect_img->depth,
interest_rect_img->nChannels
);
sub_img->origin = interest_rect_img->origin;
sub_img->widthStep = interest_rect_img->widthStep;
sub_img->imageData = interest_rect_img->imageData +
interest_rect.y*interest_rect_img->widthStep +
interest_rect.x*interest_rect_img->nChannels;
cvAddS(sub_img,cvScalar(1),sub_img);
cvNamedWindow("WidthStep", 1);
cvShowImage("WidthStep", sub_img);
cvWaitKey();
cvReleaseImageHeader(&sub_img);
}
矩阵和图像操作
//计算两个数组差值的绝对值
/** dst(x,y,c) = abs(src1(x,y,c) - src2(x,y,c)) */
CVAPI(void) cvAbsDiff( const CvArr* src1,
const CvArr* src2,
CvArr* dst );
//计算数组和标量差值的绝对值
/** dst(x,y,c) = abs(src(x,y,c) - value(c)) */
CVAPI(void) cvAbsDiffS( const CvArr* src,
CvArr* dst,
CvScalar value );
//计算数组中所有元素的绝对值
#define cvAbs( src,
dst ) //计算结果写到dst
cvAbsDiffS( (src), (dst), cvScalarAll(0))
//两个数组的元素级的加运算,如果mask没有被设空,那么mask非零元素指定的dest值在函数执行后不变
/** dst(mask) = src1(mask) + src2(mask) */
CVAPI(void) cvAdd( const CvArr* src1, const CvArr* src2, CvArr* dst,
const CvArr* mask CV_DEFAULT(NULL));
//一个数据和一个标量的元素级的相加运算
/** dst(mask) = src(mask) + value */
CVAPI(void) cvAddS( const CvArr* src, CvScalar value, CvArr* dst,
const CvArr* mask CV_DEFAULT(NULL));
//两个数据的元素级的加权相加运算,可以用于实现alpha融合,即两幅图像融合
/** dst = src1 * alpha + src2 * beta + gamma */
CVAPI(void) cvAddWeighted( const CvArr* src1, double alpha,
const CvArr* src2, double beta,
double gamma, CvArr* dst );
/*
alpha融合
*/
void alpha_blends() {
IplImage *src1, *src2;
if ((src1=cvLoadImage("test2.jpg"))!=0&&(src2=cvLoadImage("test.jpg"))!=0)
{
int x = atoi("10");
int y = atoi("10");
int width = atoi("200");
int height = atoi("200");
double alpha = (double)atof("0.5");
double beta = (double)atof("0.4");
cvSetImageROI(src1, CvRect(x, y, width, height));
cvSetImageROI(src2, CvRect(0, 0, width, height));
cvAddWeighted(src1, alpha, src2, beta, 0.0, src1);
cvResetImageROI(src1);
cvNamedWindow("Alpha_blend", 1);
cvShowImage("Alpha_blend", src1);
cvWaitKey();
}
}
//两个数组进行按位与
/** dst(idx) = src1(idx) & src2(idx) */
CVAPI(void) cvAnd( const CvArr* src1, const CvArr* src2,
CvArr* dst, const CvArr* mask CV_DEFAULT(NULL));
/** dst(idx) = src(idx) & value */
CVAPI(void) cvAndS( const CvArr* src, CvScalar value,
CvArr* dst, const CvArr* mask CV_DEFAULT(NULL));
//计算数组中所有元素的平均值
/** Calculates mean value of array elements */
CVAPI(CvScalar) cvAvg( const CvArr* arr, const CvArr* mask CV_DEFAULT(NULL) );
//计算数组中所有元素的绝对值和标准差
/** Calculates mean and standard deviation of pixel values */
CVAPI(void) cvAvgSdv( const CvArr* arr, CvScalar* mean, CvScalar* std_dev,
const CvArr* mask CV_DEFAULT(NULL) );
//计算一组n维空间向量的协方差
/** Calculates covariation matrix for a set of vectors
@see @ref core_c_CovarFlags "flags"
*/
CVAPI(void) cvCalcCovarMatrix( const CvArr** vects, int count,
CvArr* cov_mat, CvArr* avg, int flags );
Flags的取值:
CV_COVAR_NORMAL 计算均值和协方差
CV_COVAR_SCRAMBLED 快速pca scrambled协方差
CV_COVAR_USE_AVG 输入均值
CV_COVAR_SCALE 重新输出缩放的协方差矩阵
//进行比较操作
/** dst(idx) = src1(idx) _cmp_op_ src2(idx) 两个数组中所有元素运用设置的比较操作*/
CVAPI(void) cvCmp( const CvArr* src1,
const CvArr* src2,
CvArr* dst,
int cmp_op );
/** dst(idx) = src1(idx) _cmp_op_ value 对数组和标量运用设置的比较操作*/
CVAPI(void) cvCmpS( const CvArr* src,
double value,
CvArr* dst,
int cmp_op );
Cmp_op 的取值如下:
CV_CMP_EQ (src1i==src2i)
CV_CMP_GT (src1i>src2i)
CV_CMP_GE (src1i>=src2i)
CV_CMP_LT (src1i<rc2i)
CV_CMP_LE (src1i<=src2i)
CV_CMP_NE (src1i!=src2i)
//用可选的缩放值转换数组元素类型
//1 将原图像的数据类型转换为目标图像的数据类型
//2 对图像数据执行线性变换
CVAPI(void) cvConvertScale( const CvArr* src,
CvArr* dst,
double scale CV_DEFAULT(1),
double shift CV_DEFAULT(0) );
//先缩放和平移,算出绝对值,然后进行数据类型的转换
/** Performs linear transformation on every source array element,
stores absolute value of the result:
dst(x,y,c) = abs(scale*src(x,y,c)+shift).
destination array must have 8u type.
In other cases one may use cvConvertScale + cvAbsDiffS */
CVAPI(void) cvConvertScaleAbs( const CvArr* src,
CvArr* dst, // 结果数据的绝对值
double scale CV_DEFAULT(1),
double shift CV_DEFAULT(0) );
//一个图像复制到别一个图像
CVAPI(void) cvCopy( const CvArr* src, //源图像
CvArr* dst, //目标图像
const CvArr* mask CV_DEFAULT(NULL) );
//array中非0元素的个数
/** Calculates number of non-zero pixels */
CVAPI(int) cvCountNonZero( const CvArr* arr );
//计算两个三维向量的向量积,必须为单通道数组
CVAPI(void) cvCrossProduct( const CvArr* src1,
const CvArr* src2,
CvArr* dst );//单通道,长度应精确为3
//将数组的通道从一个颜色空间转换到另一个颜色空间
/** @brief Converts input array pixels from one color space to another
@see cv::cvtColor
*/
CVAPI(void) cvCvtColor( const CvArr* src, CvArr* dst, int code );
CV_BGR2BGRA =0,
CV_RGB2RGBA =CV_BGR2BGRA,
CV_BGRA2BGR =1,
CV_RGBA2RGB =CV_BGRA2BGR,
CV_BGR2RGBA =2,
CV_RGB2BGRA =CV_BGR2RGBA,
CV_RGBA2BGR =3,
CV_BGRA2RGB =CV_RGBA2BGR,
CV_BGR2RGB =4,
CV_RGB2BGR =CV_BGR2RGB,
CV_BGRA2RGBA =5,
CV_RGBA2BGRA =CV_BGRA2RGBA,
CV_BGR2GRAY =6,
CV_RGB2GRAY =7,
CV_GRAY2BGR =8,
CV_GRAY2RGB =CV_GRAY2BGR,
CV_GRAY2BGRA =9,
CV_GRAY2RGBA =CV_GRAY2BGRA,
CV_BGRA2GRAY =10,
CV_RGBA2GRAY =11,
CV_BGR2BGR565 =12,
CV_RGB2BGR565 =13,
CV_BGR5652BGR =14,
CV_BGR5652RGB =15,
CV_BGRA2BGR565 =16,
CV_RGBA2BGR565 =17,
CV_BGR5652BGRA =18,
CV_BGR5652RGBA =19,
CV_GRAY2BGR565 =20,
CV_BGR5652GRAY =21,
CV_BGR2BGR555 =22,
CV_RGB2BGR555 =23,
CV_BGR5552BGR =24,
CV_BGR5552RGB =25,
CV_BGRA2BGR555 =26,
CV_RGBA2BGR555 =27,
CV_BGR5552BGRA =28,
CV_BGR5552RGBA =29,
CV_GRAY2BGR555 =30,
CV_BGR5552GRAY =31,
CV_BGR2XYZ =32,
CV_RGB2XYZ =33,
CV_XYZ2BGR =34,
CV_XYZ2RGB =35,
CV_BGR2YCrCb =36,
CV_RGB2YCrCb =37,
CV_YCrCb2BGR =38,
CV_YCrCb2RGB =39,
CV_BGR2HSV =40,
CV_RGB2HSV =41,
CV_BGR2Lab =44,
CV_RGB2Lab =45,
CV_BayerBG2BGR =46,
CV_BayerGB2BGR =47,
CV_BayerRG2BGR =48,
CV_BayerGR2BGR =49,
CV_BayerBG2RGB =CV_BayerRG2BGR,
CV_BayerGB2RGB =CV_BayerGR2BGR,
CV_BayerRG2RGB =CV_BayerBG2BGR,
CV_BayerGR2RGB =CV_BayerGB2BGR,
CV_BGR2Luv =50,
CV_RGB2Luv =51,
CV_BGR2HLS =52,
CV_RGB2HLS =53,
CV_HSV2BGR =54,
CV_HSV2RGB =55,
CV_Lab2BGR =56,
CV_Lab2RGB =57,
CV_Luv2BGR =58,
CV_Luv2RGB =59,
CV_HLS2BGR =60,
CV_HLS2RGB =61,
CV_BayerBG2BGR_VNG =62,
CV_BayerGB2BGR_VNG =63,
CV_BayerRG2BGR_VNG =64,
CV_BayerGR2BGR_VNG =65,
CV_BayerBG2RGB_VNG =CV_BayerRG2BGR_VNG,
CV_BayerGB2RGB_VNG =CV_BayerGR2BGR_VNG,
CV_BayerRG2RGB_VNG =CV_BayerBG2BGR_VNG,
CV_BayerGR2RGB_VNG =CV_BayerGB2BGR_VNG,
CV_BGR2HSV_FULL = 66,
CV_RGB2HSV_FULL = 67,
CV_BGR2HLS_FULL = 68,
CV_RGB2HLS_FULL = 69,
CV_HSV2BGR_FULL = 70,
CV_HSV2RGB_FULL = 71,
CV_HLS2BGR_FULL = 72,
CV_HLS2RGB_FULL = 73,
CV_LBGR2Lab = 74,
CV_LRGB2Lab = 75,
CV_LBGR2Luv = 76,
CV_LRGB2Luv = 77,
CV_Lab2LBGR = 78,
CV_Lab2LRGB = 79,
CV_Luv2LBGR = 80,
CV_Luv2LRGB = 81,
CV_BGR2YUV = 82,
CV_RGB2YUV = 83,
CV_YUV2BGR = 84,
CV_YUV2RGB = 85,
CV_BayerBG2GRAY = 86,
CV_BayerGB2GRAY = 87,
CV_BayerRG2GRAY = 88,
CV_BayerGR2GRAY = 89,
//YUV 4:2:0 formats family
CV_YUV2RGB_NV12 = 90,
CV_YUV2BGR_NV12 = 91,
CV_YUV2RGB_NV21 = 92,
CV_YUV2BGR_NV21 = 93,
CV_YUV420sp2RGB = CV_YUV2RGB_NV21,
CV_YUV420sp2BGR = CV_YUV2BGR_NV21,
CV_YUV2RGBA_NV12 = 94,
CV_YUV2BGRA_NV12 = 95,
CV_YUV2RGBA_NV21 = 96,
CV_YUV2BGRA_NV21 = 97,
CV_YUV420sp2RGBA = CV_YUV2RGBA_NV21,
CV_YUV420sp2BGRA = CV_YUV2BGRA_NV21,
CV_YUV2RGB_YV12 = 98,
CV_YUV2BGR_YV12 = 99,
CV_YUV2RGB_IYUV = 100,
CV_YUV2BGR_IYUV = 101,
CV_YUV2RGB_I420 = CV_YUV2RGB_IYUV,
CV_YUV2BGR_I420 = CV_YUV2BGR_IYUV,
CV_YUV420p2RGB = CV_YUV2RGB_YV12,
CV_YUV420p2BGR = CV_YUV2BGR_YV12,
CV_YUV2RGBA_YV12 = 102,
CV_YUV2BGRA_YV12 = 103,
CV_YUV2RGBA_IYUV = 104,
CV_YUV2BGRA_IYUV = 105,
CV_YUV2RGBA_I420 = CV_YUV2RGBA_IYUV,
CV_YUV2BGRA_I420 = CV_YUV2BGRA_IYUV,
CV_YUV420p2RGBA = CV_YUV2RGBA_YV12,
CV_YUV420p2BGRA = CV_YUV2BGRA_YV12,
CV_YUV2GRAY_420 = 106,
CV_YUV2GRAY_NV21 = CV_YUV2GRAY_420,
CV_YUV2GRAY_NV12 = CV_YUV2GRAY_420,
CV_YUV2GRAY_YV12 = CV_YUV2GRAY_420,
CV_YUV2GRAY_IYUV = CV_YUV2GRAY_420,
CV_YUV2GRAY_I420 = CV_YUV2GRAY_420,
CV_YUV420sp2GRAY = CV_YUV2GRAY_420,
CV_YUV420p2GRAY = CV_YUV2GRAY_420,
//YUV 4:2:2 formats family
CV_YUV2RGB_UYVY = 107,
CV_YUV2BGR_UYVY = 108,
//CV_YUV2RGB_VYUY = 109,
//CV_YUV2BGR_VYUY = 110,
CV_YUV2RGB_Y422 = CV_YUV2RGB_UYVY,
CV_YUV2BGR_Y422 = CV_YUV2BGR_UYVY,
CV_YUV2RGB_UYNV = CV_YUV2RGB_UYVY,
CV_YUV2BGR_UYNV = CV_YUV2BGR_UYVY,
CV_YUV2RGBA_UYVY = 111,
CV_YUV2BGRA_UYVY = 112,
//CV_YUV2RGBA_VYUY = 113,
//CV_YUV2BGRA_VYUY = 114,
CV_YUV2RGBA_Y422 = CV_YUV2RGBA_UYVY,
CV_YUV2BGRA_Y422 = CV_YUV2BGRA_UYVY,
CV_YUV2RGBA_UYNV = CV_YUV2RGBA_UYVY,
CV_YUV2BGRA_UYNV = CV_YUV2BGRA_UYVY,
CV_YUV2RGB_YUY2 = 115,
CV_YUV2BGR_YUY2 = 116,
CV_YUV2RGB_YVYU = 117,
CV_YUV2BGR_YVYU = 118,
CV_YUV2RGB_YUYV = CV_YUV2RGB_YUY2,
CV_YUV2BGR_YUYV = CV_YUV2BGR_YUY2,
CV_YUV2RGB_YUNV = CV_YUV2RGB_YUY2,
CV_YUV2BGR_YUNV = CV_YUV2BGR_YUY2,
CV_YUV2RGBA_YUY2 = 119,
CV_YUV2BGRA_YUY2 = 120,
CV_YUV2RGBA_YVYU = 121,
CV_YUV2BGRA_YVYU = 122,
CV_YUV2RGBA_YUYV = CV_YUV2RGBA_YUY2,
CV_YUV2BGRA_YUYV = CV_YUV2BGRA_YUY2,
CV_YUV2RGBA_YUNV = CV_YUV2RGBA_YUY2,
CV_YUV2BGRA_YUNV = CV_YUV2BGRA_YUY2,
CV_YUV2GRAY_UYVY = 123,
CV_YUV2GRAY_YUY2 = 124,
//CV_YUV2GRAY_VYUY = CV_YUV2GRAY_UYVY,
CV_YUV2GRAY_Y422 = CV_YUV2GRAY_UYVY,
CV_YUV2GRAY_UYNV = CV_YUV2GRAY_UYVY,
CV_YUV2GRAY_YVYU = CV_YUV2GRAY_YUY2,
CV_YUV2GRAY_YUYV = CV_YUV2GRAY_YUY2,
CV_YUV2GRAY_YUNV = CV_YUV2GRAY_YUY2,
// alpha premultiplication
CV_RGBA2mRGBA = 125,
CV_mRGBA2RGBA = 126,
CV_RGB2YUV_I420 = 127,
CV_BGR2YUV_I420 = 128,
CV_RGB2YUV_IYUV = CV_RGB2YUV_I420,
CV_BGR2YUV_IYUV = CV_BGR2YUV_I420,
CV_RGBA2YUV_I420 = 129,
CV_BGRA2YUV_I420 = 130,
CV_RGBA2YUV_IYUV = CV_RGBA2YUV_I420,
CV_BGRA2YUV_IYUV = CV_BGRA2YUV_I420,
CV_RGB2YUV_YV12 = 131,
CV_BGR2YUV_YV12 = 132,
CV_RGBA2YUV_YV12 = 133,
CV_BGRA2YUV_YV12 = 134,
// Edge-Aware Demosaicing
CV_BayerBG2BGR_EA = 135,
CV_BayerGB2BGR_EA = 136,
CV_BayerRG2BGR_EA = 137,
CV_BayerGR2BGR_EA = 138,
CV_BayerBG2RGB_EA = CV_BayerRG2BGR_EA,
CV_BayerGB2RGB_EA = CV_BayerGR2BGR_EA,
CV_BayerRG2RGB_EA = CV_BayerBG2BGR_EA,
CV_BayerGR2RGB_EA = CV_BayerGB2BGR_EA,
CV_COLORCVT_MAX = 139
/** Calculates determinant of input matrix 计算方阵的行列式*/
CVAPI(double) cvDet( const CvArr* mat );
//用另外一个数组对一个数组进行元素级的除法运算
/** element-wise division/inversion with scaling:
dst(idx) = src1(idx) * scale / src2(idx)
or dst(idx) = scale / src2(idx) if src1 == 0 */
CVAPI(void) cvDiv( const CvArr* src1,
const CvArr* src2,
CvArr* dst,
double scale CV_DEFAULT(1));
//计算两个向量的点积,单通道
CVAPI(double) cvDotProduct( const CvArr* src1, const CvArr* src2 );
//计算方阵的特征值和特征向量
/** Finds eigen values and vectors of a symmetric matrix */
CVAPI(void) cvEigenVV( CvArr* mat, //矩阵
CvArr* evects, //顺序保存的特征向量
CvArr* evals, //特征值
double eps CV_DEFAULT(0), //停止参数
int lowindex CV_DEFAULT(-1),
int highindex CV_DEFAULT(-1));
//围绕选定轴旋转
/** Mirror array data around horizontal (flip=0),
vertical (flip=1) or both(flip=-1) axises:
cvFlip(src) flips images vertically and sequences horizontally (inplace) */
CVAPI(void) cvFlip( const CvArr* src, //图像
CvArr* dst CV_DEFAULT(NULL), //
int flip_mode CV_DEFAULT(0));//为0时,只会绕x轴转
//矩阵乘法 d=a*op(A)*op(B)+b*op(C)
/** Extended matrix transform:
dst = alpha*op(A)*op(B) + beta*op(C), where op(X) is X or X^T */
CVAPI(void) cvGEMM( const CvArr* src1,
const CvArr* src2,
double alpha,
const CvArr* src3,
double beta,
CvArr* dst,
int tABC CV_DEFAULT(0));//转置,可以是0 或是以下几种
#define CV_GEMM_A_T 1
#define CV_GEMM_B_T 2
#define CV_GEMM_C_T 4
//从一个数组的列中复制元素
/** @overload
@param arr Input array
@param submat Pointer to the resulting sub-array header
@param col Zero-based index of the selected column
*/
CV_INLINE CvMat* cvGetCol( const CvArr* arr, //矩阵
CvMat* submat, //指向矩阵中的特定列
int col )//指定列
{
return cvGetCols( arr, submat, col, col + 1 );
}
//从数组相邻的多列中复制元素
CVAPI(CvMat*) cvGetCols( const CvArr* arr,
CvMat* submat,
int start_col,//开始列
int end_col );//结束列
//复制数组中对角线上的所有元素
CVAPI(CvMat*) cvGetDiag( const CvArr* arr,
CvMat* submat,
int diag CV_DEFAULT(0));
//返回数组的维数
CVAPI(int) cvGetDims( const CvArr* arr, int* sizes CV_DEFAULT(NULL) );
//返回一个数组的所有维的大小
CVAPI(int) cvGetDimSize( const CvArr* arr, int index );
//从一个数组的行中复制元素
CV_INLINE CvMat* cvGetRow( const CvArr* arr, CvMat* submat, int row )
{
return cvGetRows( arr, submat, row, row + 1, 1 );
}
//从一个数组的相邻行中复制数组
CVAPI(CvMat*) cvGetRows( const CvArr* arr, CvMat* submat,
int start_row, int end_row,
int delta_row CV_DEFAULT(1));
//得到二维数组的尺寸
CVAPI(CvSize) cvGetSize( const CvArr* arr );
//从一个数组的子区域复制元素
CVAPI(CvMat*) cvGetSubRect( const CvArr* arr, CvMat* submat, CvRect rect );
//检查一个数组中的元素是否在另外两个数组中的值的范转围
//检查图像中像素的灰度是否属于某一指定范围
/** dst(idx) = lower(idx) <= src(idx) < upper(idx) */
CVAPI(void) cvInRange( const CvArr* src, //图像
const CvArr* lower,//起始值
const CvArr* upper,
CvArr* dst );//如果>=lower,且<upper,dst中对应的值为0xff,否则为0
//检查一个数组的元素的值是否在另外两个标量的范围
//如果是多通道,则会分通道进行处理
/** dst(idx) = lower <= src(idx) < upper */
CVAPI(void) cvInRangeS( const CvArr* src,
CvScalar lower,
CvScalar upper,
CvArr* dst );
//求矩阵的转置
/** Inverts matrix */
CVAPI(double) cvInvert( const CvArr* src,//
CvArr* dst, //保存结果
int method CV_DEFAULT(CV_LU));//参数值有以下几种:
#define CV_LU 0 高斯消去
#define CV_SVD 1 奇异值分解
#define CV_SVD_SYM 2 对称矩阵的SVD
//计算两个向量间的马式距离
/** Calculates Mahalanobis(weighted) distance */
CVAPI(double) cvMahalanobis( const CvArr* vec1,
const CvArr* vec2,
const CvArr* mat );
//两个数组中进行元素级的取最小值
/** dst(idx) = min(src1(idx),src2(idx)) */
CVAPI(void) cvMin( const CvArr* src1, const CvArr* src2, CvArr* dst );
//一个数组和一个标量中进行元素级的取最小值
/** dst(idx) = min(src(idx),value) */
CVAPI(void) cvMinS( const CvArr* src, double value, CvArr* dst );
//两个数组中取最大值
/** dst(idx) = max(src1(idx),src2(idx)) */
CVAPI(void) cvMax( const CvArr* src1, const CvArr* src2, CvArr* dst );
//一个数组和一个标量中进行元素组的取最大值
/** dst(idx) = max(src(idx),value) */
CVAPI(void) cvMaxS( const CvArr* src, double value, CvArr* dst );
//把几个单通道图像合并为一个多通道图像
/** Merges a set of single-channel arrays into the single multi-channel array
or inserts one particular [color] plane to the array */
CVAPI(void) cvMerge( const CvArr* src0, const CvArr* src1,
const CvArr* src2, const CvArr* src3,
CvArr* dst );
//寻找数组中的最大最小值
/** Finds global minimum, maximum and their positions */
CVAPI(void) cvMinMaxLoc( const CvArr* arr, double* min_val, double* max_val,
CvPoint* min_loc CV_DEFAULT(NULL),
CvPoint* max_loc CV_DEFAULT(NULL),
const CvArr* mask CV_DEFAULT(NULL) );
//计算两个数组,元素级的乘积
/** dst(idx) = src1(idx) * src2(idx) * scale
(scaled element-wise multiplication of 2 arrays) */
CVAPI(void) cvMul( const CvArr* src1, const CvArr* src2,
CvArr* dst, double scale CV_DEFAULT(1) );
//对数组中的元素按位取反
/** dst(idx) = ~src(idx) */
CVAPI(void) cvNot( const CvArr* src, CvArr* dst );
//计算两个数组的正态相关性
/** Finds norm, difference norm or relative difference norm for an array (or two arrays)
@see ref core_c_NormFlags "flags"
*/
CVAPI(double) cvNorm( const CvArr* arr1, const CvArr* arr2 CV_DEFAULT(NULL),
int norm_type CV_DEFAULT(CV_L2),
const CvArr* mask CV_DEFAULT(NULL) );
//将数组中元素进行规一化
/** @see ref core_c_NormFlags "flags" */
CVAPI(void) cvNormalize( const CvArr* src, CvArr* dst,
double a CV_DEFAULT(1.), double b CV_DEFAULT(0.),
int norm_type CV_DEFAULT(CV_L2),
const CvArr* mask CV_DEFAULT(NULL) );
Norm_type的类型
#define CV_C 1
#define CV_L1 2
#define CV_L2 4
#define CV_MINMAX 32
//两个数组按位或
/** dst(idx) = src1(idx) | src2(idx) */
CVAPI(void) cvOr( const CvArr* src1, const CvArr* src2,
CvArr* dst, const CvArr* mask CV_DEFAULT(NULL));
//数组与标量之间按位或
/** dst(idx) = src(idx) | value */
CVAPI(void) cvOrS( const CvArr* src, CvScalar value,
CvArr* dst, const CvArr* mask CV_DEFAULT(NULL));
//通过给定操作符将二维数组约简为向量
/** @see @ref core_c_ReduceFlags "flags" */
CVAPI(void) cvReduce( const CvArr* src, CvArr* dst, int dim CV_DEFAULT(-1),
int op CV_DEFAULT(CV_REDUCE_SUM) );
//以平铺的方式进行数组复制
/** Repeats source 2d array several times in both horizontal and
vertical direction to fill destination array */
CVAPI(void) cvRepeat( const CvArr* src, CvArr* dst );
CVAPI(void) cvConvertScale( const CvArr* src, CvArr* dst,
double scale CV_DEFAULT(1),
double shift CV_DEFAULT(0) );
//用给定值初始化数组
CVAPI(void) cvSet( CvArr* arr, CvScalar value,
const CvArr* mask CV_DEFAULT(NULL) );
//所有元素初始化为0
CVAPI(void) cvSetZero( CvArr* arr );
#define cvZero cvSetZero
//将数组对角线上的设置为1,其他为0
/** Makes an identity matrix (mat_ij = i == j) */
CVAPI(void) cvSetIdentity( CvArr* mat, CvScalar value CV_DEFAULT(cvRealScalar(1)) );
//求出线性方程的解
/** Solves linear system (src1)*(dst) = (src2)
(returns 0 if src1 is a singular and CV_LU method is used) */
CVAPI(int) cvSolve( const CvArr* src1, const CvArr* src2, CvArr* dst,
int method CV_DEFAULT(CV_LU));
//将多通道数组分解成为多个单通道数组
/** Splits a multi-channel array into the set of single-channel arrays or
extracts particular [color] plane */
CVAPI(void) cvSplit( const CvArr* src, CvArr* dst0, CvArr* dst1,
CvArr* dst2, CvArr* dst3 );
//两个数组元素级相减
/** dst(mask) = src1(mask) - src2(mask) */
CVAPI(void) cvSub( const CvArr* src1, const CvArr* src2, CvArr* dst,
const CvArr* mask CV_DEFAULT(NULL));
//元素级的从数组减去标量
/** dst(mask) = src(mask) - value = src(mask) + (-value) */
CV_INLINE void cvSubS( const CvArr* src, CvScalar value, CvArr* dst,
const CvArr* mask CV_DEFAULT(NULL))
{
cvAddS( src, cvScalar( -value.val[0], -value.val[1], -value.val[2], -value.val[3]),
dst, mask );
}
//从标量中减去数组
/** dst(mask) = value - src(mask) */
CVAPI(void) cvSubRS( const CvArr* src, CvScalar value, CvArr* dst,
const CvArr* mask CV_DEFAULT(NULL));
//元素级求和
/** Finds sum of array elements */
CVAPI(CvScalar) cvSum( const CvArr* arr );
//二维矩阵的奇异值分解
/** Performs Singular Value Decomposition of a matrix */
CVAPI(void) cvSVD( CvArr* A, CvArr* W, CvArr* U CV_DEFAULT(NULL),
CvArr* V CV_DEFAULT(NULL), int flags CV_DEFAULT(0));
//奇异值回代计算
/** Performs Singular Value Back Substitution (solves A*X = B):
flags must be the same as in cvSVD */
CVAPI(void) cvSVBkSb( const CvArr* W, const CvArr* U,
const CvArr* V, const CvArr* B,
CvArr* X, int flags );
//计算矩阵迹,对角线元素的总和
/** Calculates trace of the matrix (sum of elements on the main diagonal) */
CVAPI(CvScalar) cvTrace( const CvArr* mat );
//矩阵的转置运算
/** Tranposes matrix. Square matrices can be transposed in-place */
CVAPI(void) cvTranspose( const CvArr* src, CvArr* dst );
#define cvT cvTranspose
//按位异或操作
/** dst(idx) = src1(idx) ^ src2(idx) */
CVAPI(void) cvXor( const CvArr* src1, const CvArr* src2,
CvArr* dst, const CvArr* mask CV_DEFAULT(NULL));
//数组与标量之间按位异或
/** dst(idx) = src(idx) ^ value */
CVAPI(void) cvXorS( const CvArr* src, CvScalar value,
CvArr* dst, const CvArr* mask CV_DEFAULT(NULL));
//所有通道,所有元素置0
CVAPI(void) cvSetZero( CvArr* arr );
#define cvZero cvSetZero
绘图
1直线
//函数原型
/** @brief Draws 4-connected, 8-connected or antialiased line segment connecting two points
@see cv::line
*/
CVAPI(void) cvLine( CvArr* img, //图像
CvPoint pt1, //直线的起始点
CvPoint pt2,
CvScalar color, //颜色
int thickness CV_DEFAULT(1),//线宽
int line_type CV_DEFAULT(8), //8连通,反走样模式
int shift CV_DEFAULT(0) );
//画矩形
/** @brief Draws a rectangle given two opposite corners of the rectangle (pt1 & pt2)
if thickness<0 (e.g. thickness == CV_FILLED), the filled box is drawn
@see cv::rectangle
*/
CVAPI(void) cvRectangle( CvArr* img,
CvPoint pt1,
CvPoint pt2,
CvScalar color,
int thickness CV_DEFAULT(1),
int line_type CV_DEFAULT(8),
int shift CV_DEFAULT(0));
2圆形和椭圆
//画圆
/** @brief Draws a circle with specified center and radius.
Thickness works in the same way as with cvRectangle
@see cv::circle
*/
CVAPI(void) cvCircle( CvArr* img, //图像
CvPoint center, //圆心
int radius,//半径
CvScalar color, //颜色
int thickness CV_DEFAULT(1),//线宽
int line_type CV_DEFAULT(8), //
int shift CV_DEFAULT(0));//
//椭圆
CVAPI(void) cvEllipse( CvArr* img, //图像
CvPoint center, //圆心
CvSize axes,//高度和宽度
double angle, //偏离主轴 的角度 ,x轴逆时针方向为正
double start_angle, //开始角度
double end_angle,//结束角度
CvScalar color, //颜色
int thickness CV_DEFAULT(1),//线宽
int line_type CV_DEFAULT(8),
int shift CV_DEFAULT(0));
3多边形
//一次可以绘制多个多边形
/** @brief Fills an area bounded by one or more arbitrary polygons
@see cv::fillPoly
*/
CVAPI(void) cvFillPoly( CvArr* img,
CvPoint** pts,
const int* npts, //记数点构成的数组
int contours,
CvScalar color,//颜色
int line_type CV_DEFAULT(8),
int shift CV_DEFAULT(0) );
//一次只能会制一个多边形,只能画凸多边形
/** @brief Fills convex or monotonous polygon.
@see cv::fillConvexPoly
*/
CVAPI(void) cvFillConvexPoly( CvArr* img,//图像
const CvPoint* pts,//
int npts, //
CvScalar color, //颜色
int line_type CV_DEFAULT(8),
int shift CV_DEFAULT(0));
//只画边
/** @brief Draws one or more polygonal curves
@see cv::polylines
*/
CVAPI(void) cvPolyLine( CvArr* img,
CvPoint** pts,
const int* npts,
int contours,
int is_closed,
CvScalar color,
int thickness CV_DEFAULT(1),
int line_type CV_DEFAULT(8),
int shift CV_DEFAULT(0) );
4字体和文字
/** @brief Renders text stroke with specified font and color at specified location.
CvFont should be initialized with cvInitFont
@see cvInitFont, cvGetTextSize, cvFont, cv::putText
*/
CVAPI(void) cvPutText( CvArr* img, //图像
const char* text, //文字内容
CvPoint org,//文本框左下角的位置
const CvFont* font, //字体格式
CvScalar color );//颜色
//初始化字体格式
CVAPI(void) cvInitFont( CvFont* font, //自定义的字体变量
int font_face,//可用字体变量
double hscale, //渲染时选择全高(1.0)半高(0.5)
double vscale,//创建斜体字
double shear CV_DEFAULT(0),
int thickness CV_DEFAULT(1),
int line_type CV_DEFAULT(8));
可用字体
#define CV_FONT_HERSHEY_SIMPLEX 0
#define CV_FONT_HERSHEY_PLAIN 1
#define CV_FONT_HERSHEY_DUPLEX 2
#define CV_FONT_HERSHEY_COMPLEX 3
#define CV_FONT_HERSHEY_TRIPLEX 4
#define CV_FONT_HERSHEY_COMPLEX_SMALL 5
#define CV_FONT_HERSHEY_SCRIPT_SIMPLEX 6
#define CV_FONT_HERSHEY_SCRIPT_COMPLEX 7
示例代码如下:
/*
绘图
*/
int acmeArr1[2];
CvPoint pointArr2[3];
void drawing() {
//读入图像
IplImage *img = cvLoadImage("test2.jpg");
//直线的起始点
CvPoint pit1= cvPoint(10, 10);
CvPoint pit2 = cvPoint(100,200);
//线的颜色
CvScalar color= cvScalar(255, 0, 0);
//画线
//cvLine(img, pit1, pit2, color,2,8,0);
//画矩形
//cvRectangle(img,pit1,pit2,color,CV_FILLED);
//画圆形
//cvCircle(img,pit2,100,color,CV_FILLED);
//画椭圆
//CvSize eSize = cvSize(100, 50);
//cvEllipse(img,pit2,eSize,0,0,360,color);
//画多边形
//一次可以同时画多个多边形,全填充
//初始化多边开数组
CvPoint** pointArrs = new CvPoint*[2];
pointArrs[0] = new CvPoint[3];
pointArrs[1] = new CvPoint[4];
int acmeArr1[2] = { 3,4 };
//三角形
pointArrs[0][0] = cvPoint(200,40);
pointArrs[0][1] = cvPoint(150, 80);
pointArrs[0][2]= cvPoint(250, 80);
//四边形
pointArrs[1][0] = cvPoint(150, 100);
pointArrs[1][1] = cvPoint(250, 100);
pointArrs[1][2]= cvPoint(250, 200);
pointArrs[1][3]= cvPoint(150, 200);
//初始化顶点数组
cvFillPoly(img, pointArrs, acmeArr1,2,color);
//一次只能画一个多边形
//初始化多边开数组
//pointArr2[1]= cvPoint(100, 100);
//pointArr2[2] = cvPoint(100, 300);
//pointArr2[3] = cvPoint(200, 400);
//顶点数
//int num = 3;
//cvFillConvexPoly(img, pointArr2, num,color);
//只连线画边
//cvPolyLine(img, pointArrs, acmeArr1, 2,true, color);
//字体与文字
//初始化字体格式
//CvFont myFont;
//cvInitFont(&myFont,CV_FONT_HERSHEY_COMPLEX,1.0,1.0,0.0);
//cvPutText(img,"hello retacn!",pit2,&myFont,color);
//创建窗体
cvNamedWindow("Drawing");
//显示图像
cvShowImage("Drawing", img);
cvWaitKey();
}
数据存储
存储的格式有 YAML 和 XML
图片:
//保存图像
/* save image to file */
CVAPI(int) cvSaveImage( const char* filename,//文件名称s
const CvArr* image,//图像
const int* params CV_DEFAULT(0) );
//加载图像
/* load image from file
iscolor can be a combination of above flags where CV_LOAD_IMAGE_UNCHANGED
overrides the other flags
using CV_LOAD_IMAGE_ANYCOLOR alone is equivalent to CV_LOAD_IMAGE_UNCHANGED
unless CV_LOAD_IMAGE_ANYDEPTH is specified images are converted to 8bit
*/
CVAPI(IplImage*) cvLoadImage( const char* filename, int iscolor CV_DEFAULT(CV_LOAD_IMAGE_COLOR));
CVAPI(CvMat*) cvLoadImageM( const char* filename, int iscolor CV_DEFAULT(CV_LOAD_IMAGE_COLOR));
cvMat的读取
CVAPI(void) cvSave( const char* filename, //文件名
const void* struct_ptr,//
const char* name CV_DEFAULT(NULL),
const char* comment CV_DEFAULT(NULL),
CvAttrList attributes CV_DEFAULT(cvAttrList()));
CVAPI(void*) cvLoad( const char* filename,
CvMemStorage* memstorage CV_DEFAULT(NULL),
const char* name CV_DEFAULT(NULL),
const char** real_name CV_DEFAULT(NULL) );
视频的读取
/* grab a frame, return 1 on success, 0 on fail.
this function is thought to be fast */
CVAPI(int) cvGrabFrame( CvCapture* capture );
/* initialize video file writer */
CVAPI(CvVideoWriter*) cvCreateVideoWriter( const char* filename, int fourcc,
double fps, CvSize frame_size,
int is_color CV_DEFAULT(1));
/* write frame to video file */
CVAPI(int) cvWriteFrame( CvVideoWriter* writer, const IplImage* image );
/*
往磁盘上写一个配置文件
*/
void writeCfg() {
CvMat* cmatrix=cvCreateMat(2,3,CV_8UC3);
CvFileStorage* fs = cvOpenFileStorage(
"cfg.xml",
0,
CV_STORAGE_WRITE//写数据,读数扰为CV_STORAGE_READ
);
cvWriteInt(fs, "frame_count", 10);
cvStartWriteStruct(fs, "frame_size", CV_NODE_SEQ);//开始结构编写
cvWriteInt(fs, 0, 320);
cvWriteInt(fs, 0, 200);
cvEndWriteStruct(fs);//结构结束
cvWrite(fs, "color_cvt_matrix", cmatrix);//色彩转换矩阵
cvReleaseFileStorage(&fs);//释放cvFileStorage
}
生成文件的内容如下:
<?xml version="1.0"?>
<opencv_storage>
<frame_count>10</frame_count>
<frame_size>
320 200</frame_size>
<color_cvt_matrix type_id="opencv-matrix">
<rows>2</rows>
<cols>3</cols>
<dt>"3u"</dt>
<data>
205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205
205</data></color_cvt_matrix>
</opencv_storage>
/*
读取配置文件
*/
void readCfg() {
CvFileStorage* fs = cvOpenFileStorage(
"cfg.xml",
0,
CV_STORAGE_READ//写数据,读数扰为CV_STORAGE_READ
);
int frame_count = cvReadIntByName(
fs,
0,
"frame_count",
5
);
//得到结构体
CvSeq* s = cvGetFileNodeByName(fs, 0, "frame_size")->data.seq;
int frame_width = cvReadInt((CvFileNode*)cvGetSeqElem(s,0));
int frame_height = cvReadInt((CvFileNode*)cvGetSeqElem(s, 1));
CvMat* color_cvt_matrix = (CvMat*)cvReadByName(
fs,
0,
"color_cvt_matrix"
);
cvReleaseFileStorage(&fs);
}
数据存储常用函数
cvOpenFileStorage //为读写打开文件
cvReleaseFileStorage //释放存储的数据
写操作
cvStartWriteStruct //开始写入新的数据结构
cvEndWriteStruct //结束写入数据结构
cvWriteInt //写入整型
cvWriteReal //写入浮点型
cvWriteString //写入字符串
cvWriteComment //写入一个注释定串
cvWrite //写入一个对象
cvWriteRawData //写入多个数值
cvWriteFileNode //将文件结点写入另一个文件存储器
读操作
cvGetRootFileNode //取得存储器的根节点
cvGetFileNodeName //返回文件节点名
cvGetHashedKey //为名称返回一个唯一的指什
cvGetFileNode //在映图或文件存储器中找到节点
cvGetFileNodeByName //在映图或存储器中找到相应节点
cvReadInt //读取整型
cvReadIntByName //读取一个有名称的整型
cvReadReal //读取浮点型
cvReadRealByName //读取一个有名称的浮点型
cvReadString //从文件节点取字符串
cvReadStringByName //根据名称查取得文件节点
cvRead //找到对象解码并返回指针
cvReadByName //打到对象并解码
cvReadRawData //读取多个数值
cvStartReadRawData //初始化文件结点序列的读取
cvReadRawDataSlice //读取文件节点的内容