opencv Mat IplImage CvScalar

来源:互联网 发布:人工智能技术瓶颈 编辑:程序博客网 时间:2024/06/05 15:14

opencv Mat IplImage CvScalar

CvMat和Mat区别:
CvMat
C中的一个结构体,

CvMat* cvCreateMat(int rows, int cols, int type);  //type:矩阵元素类型. 格式为CV_<bit_depth>(S|U|F)C<number_of_channels>.

Mat:
OpenCV最基本的数据结构,OpenCV封装的一个C++类,用来表示一个图像,和IplImage表示基本一致,但是Mat还添加了一些图像函数。Mat即矩阵(Matrix)的缩写,Mat数据结构主要包含2部分:Header和Pointer。
Header中主要包含矩阵的大小,存储方式,存储地址等信息;
Pointer中存储指向像素值的指针

class CV_EXPORTS Mat{public:    //! default constructor    Mat();    //! constructs 2D matrix of the specified size and type    // (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)    Mat(int _rows, int _cols, int _type);    Mat(Size _size, int _type);    //! constucts 2D matrix and fills it with the specified value _s.    Mat(int _rows, int _cols, int _type, const Scalar& _s);    Mat(Size _size, int _type, const Scalar& _s);    //! constructs n-dimensional matrix    Mat(int _ndims, const int* _sizes, int _type);    Mat(int _ndims, const int* _sizes, int _type, const Scalar& _s);    //! copy constructor    Mat(const Mat& m);    //! constructor for matrix headers pointing to user-allocated data    Mat(int _rows, int _cols, int _type, void* _data, size_t _step=AUTO_STEP);    Mat(Size _size, int _type, void* _data, size_t _step=AUTO_STEP);    Mat(int _ndims, const int* _sizes, int _type, void* _data, const size_t* _steps=0);    //! creates a matrix header for a part of the bigger matrix    // Mat& -> Mat    Mat(const Mat& m, const Range& rowRange, const Range& colRange=Range::all());    Mat(const Mat& m, const Rect& roi);    Mat(const Mat& m, const Range* ranges);    //! converts old-style CvMat to the new matrix; the data is not copied by default    Mat(const CvMat* m, bool copyData=false); // CvMat* -> Mat    //! converts old-style CvMatND to the new matrix; the data is not copied by default    Mat(const CvMatND* m, bool copyData=false);    //! converts old-style IplImage to the new matrix; the data is not copied by default    Mat(const IplImage* img, bool copyData=false); // IplImage* -> Mat    ...    //_type: 矩阵元素类型. 格式为CV_<bit_depth>(S|U|F)C<number_of_channels>      //如: CV_8UC1 表示8位无符号单通道矩阵, CV_32SC2表示32位有符号双通道矩阵}

Mat M;
M.data 数据区域的指针
M.dims 矩阵维度
M.sizes 维度
M.elemSize() 每个元素占的字节空间大小,与元素类型相关,如CV_8U
M.step[] 用来计算元素地址, M.step[i] 表示所有比i大的维度所占空间大小
属性相关:
rows
cols
begin
end
at
size
depth
type
elemSize
total
创建特殊矩阵:
diag:
Mat diag(int d=0) const;
//! constructs a square diagonal matrix which main diagonal is vector “d”
static Mat diag(const Mat& d);
ones
zeros
//! Matlab-style matrix initialization
static MatExpr zeros(int rows, int cols, int type);
static MatExpr zeros(Size size, int type);
static MatExpr zeros(int ndims, const int* sz, int type);
eye
矩阵操作:
t
inv
mul
cross
dot
reshape
resize
reserve
push_back
pop_back
赋值相关:
clone
copyTo
convertTo
assignTo
setTo

Mat(const IplImage* img, bool copyData=false);

Mat数据复制:
前面说过Mat包括头和数据指针,当使用Mat的构造函数初始化的时候,会将头和数据指针复制(注意:只是指针复制,指针指向的地址不会复制),若要将数据也复制,则必须使用copyTo或clone函数
图像的读取:cv:Mat通过 cv::imread;

IplImage
C语言操作OpenCV的数据结构,在当时C操纵OpenCV的时候,地位等同于Mat,
OpenCV为其提供了一个接口,很方便的直接将IplImage转化为Mat,即使用构造函数

载入图像:
函数原型:IplImage* cvLoadImage(const char* filename, int flags=CV_LOAD_IMAGE_COLOR);
filename: 读入文件的文件名
flags:
CV_LOAD_IMAGE_COLOR
默认情况下,图像是以8位,3个通道的形式被读入(默认也使得图像强制转换为3通道)
CV_LOAD_IMAGE_GRAYSCALE
强制转换为单通道
CV_LOAD_IMAGE_ANGCOLOR
则以保持原始图像通道数的方式读入
CV_LOAD_IMAGE_ANYDEPTH
图像颜色深度可为8u~64f任意深度,默认为16位图像
CV_LOAD_IMAGEUNCHANGED
使得读入图像的通道数和位数与原始图像保持一致

直接创建:
函数原型:IplImage* cvCreateImage(CvSize size, int depth, int channels);
size: 图像的宽和高
depth:代表颜色深度,使用的是以下定义的宏:
IPL_DEPTH_8U,无符号8bit整数(8u)
IPL_DEPTH_8S,有符号8bit整数(8s)
IPL_DEPTH_16S,有符号16bit整数(16s)
IPL_DEPTH_32S,有符号32bit整数(32s)
IPL_DEPTH_32F,32bit浮点数,单精度(32f)
IPL_DEPTH_64F,64bit浮点数,双精度(64f)
nChannels:通道数,为1,2,3或4

IplImage与Mat是可以相互转换的;
IplImage -> Mat:
extern IplImage* plpliamge; //假设已经创建IplImage;
cv::Mat* pmatImage = new cv:Mat(IplImage, 0): //第二个参数为0, 表示不进行像素数据copy;

Mat -> IplImage:
extern cv:Mat matImage; //假设已经创建cv:Mat;
IplImage limage = IplImage(matImage); //不进行数据copy;

Scalar CvScalar区别:
c++接口中定义为类Scalar
c接口中定义为结构体CvScalar
typedef struct CvScalar{
double val[4];
}CvScalar;

c语言方法:
1、inline CvScalar cvScalar(double val0, double val1=0,double val2=0, double val3=0);
//最通用的,可初始化0-4个通道
存放单通道图像中像素:cvScalar(255);
存放三通道图像中像素:cvScalar(255,255,255);
2. inline CvScalar cvRealScalar(double val0);
// 只使用第一个通道,val[0]=val0; 等同于cvScalar(val0,0,0,0);
3. inline CvScalar cvScalarAll(double val);
//所用通道值用同一个val赋值
4. CV_RGB
CV_RGB是OpenCV中的一个宏,用于创建一个色彩值:

#define CV_RGB(r,g,b)  cvScalar((b),(g),(r),0)。

转换为cvScalar时,rgb的顺序变为bgr,opencv中存储RGB模式图像时,默认采用的通道顺序是BGR.
如:CvScalar color = CV_RGB(rand()&255, rand()&255, rand()&255)
5、cvScalar对应(B,G,R)
cvAddS(img,cvScalar(0,0,50),img); //代表红色通道增加50

#define CV_8U   0#define CV_8S   1#define CV_16U  2#define CV_16S  3#define CV_32S  4#define CV_32F  5#define CV_64F  6typedef struct CvPoint  {      int x;      int y;  }  CvPoint;  typedef struct CvPoint2D32f  {      float x;      float y;  }  CvPoint2D32f;  typedef struct CvPoint3D32f  {      float x;      float y;      float z;  }  CvPoint3D32f; typedef  struct CvSize{      int width;     //宽      int height;   //高  }CvSize;  typedef struct CvSize2D32f{    float width;    float height;}CvSize2D32f;typedef struct CvRect{      int x;          //  x方向坐标      int y;          //  y方向坐标      int width;      //  矩阵的宽度      int height;     //  矩阵的高度 }cvGet2D CVAPI(CvScalar) cvGet2D( const CvArr* arr, int idx0, int idx1 );获得某个点的值, idx0=hight 行值, idx1=width 列值cvSet2D CVAPI(void) cvSet2D( CvArr* arr, int idx0, int idx1, CvScalar value );给某个点赋值typedef struct CvBox2D{   CvPoint2D32f center; // 盒子的中心   CvSize2D32f size; // 盒子的长和宽   float angle; // 水平轴与第一个边的夹角,用弧度表示}CvBox2D;typedef struct CvConnectedComp{   double area; // 连通域的面积   float value; // 分割域的灰度缩放值   CvRect rect; // 分割域的 ROI} CvConnectedComp;rectangle://! draws the rectangle outline or a solid rectangle with the opposite corners pt1 and pt2 in the imageCV_EXPORTS_W void rectangle(CV_IN_OUT Mat& img, Point pt1, Point pt2,                const Scalar& color, int thickness=1,                 int lineType=8, int shift=0);//! draws the rectangle outline or a solid rectangle covering rec in the imageCV_EXPORTS void rectangle(CV_IN_OUT Mat& img, Rect rec, const Scalar& color,                 int thickness=1, int lineType=8, int shift=0);
原创粉丝点击