在Mat类出现之前,OpenCV用于处理Image的三大类分别是:
IplImage, CvMat和CvArr其中IplImage及其重要。
但是它是C语言的结构体,同时有C语言的优点和缺点,需要手工管理内存的分配与销毁。
除非平台只支持C语言,否则不是要使用lplImage。
在OpenCV4.0以及更高版本中不要尝试去折腾IplImage。可能会失败。
enum { MAGIC_VAL = 0x42FF0000, AUTO_STEP = 0, CONTINUOUS_FLAG = CV_MAT_CONT_FLAG, SUBMATRIX_FLAG = CV_SUBMAT_FLAG };
enum { MAGIC_MASK = 0xFFFF0000, TYPE_MASK = 0x00000FFF, DEPTH_MASK = 7 };
//! includes several bit-fields:
// - the magic signature
// - continuity flag
// - depth
// - number of channels
int flags;
//! the matrix dimensionality, >= 2
int dims;
//! the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
int rows, cols;
//! pointer to the data
uchar* data;
//! helper fields used in locateROI and adjustROI
const uchar* datastart;
const uchar* dataend;
const uchar* datalimit;
//! custom allocator
MatAllocator* allocator;
//! and the standard allocator
static MatAllocator* getStdAllocator();
static MatAllocator* getDefaultAllocator();
static void setDefaultAllocator(MatAllocator* allocator);
//! internal use method: updates the continuity flag
void updateContinuityFlag();
//! interaction with UMat
UMatData* u;
MatSize size;
MatStep step;
#include <iostream>
#include <iomanip>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
using namespace std;
using namespace cv;
struct MatHeader
{
int nFlags, nStep0, nStep1;
int nDims, nRows, nCols;
int nDepth, nType, nChannelIndex; // 通道索引
bool bMagic, bContinuous, bSubMatrix;
void show()
{
cout << "flags = " << nFlags << "\n"
<< "dim = " << nDims << "\n"
<< "step0 = " << nStep0 << "\n"
<< "step1 = " << nStep1 << "\n"
<< "rows = " << nRows << "\n"
<< "cols = " << nCols << "\n"
<< "depth = " << nDepth << "\n"
<< "type = " << nType << "\n"
<< "channels = " << nChannelIndex + 1 << "\n"
<< "is magic = " << bMagic << "\n"
<< "is continuous = " << bContinuous << "\n"
<< "is sub matrix = " << bSubMatrix << "\n"
<< endl;
}
};
int main()
{
Mat img = cv::imread("test.jpg", cv::IMREAD_COLOR);
if (img.empty())
{
cout << "failed to read image" << endl;
return -1;
}
MatHeader header;
header.nFlags = img.flags;
header.nStep0 = img.step[0];
header.nStep1 = img.step[1];
header.nDims = img.dims;
header.nRows = img.rows;
header.nCols = img.cols;
header.nDepth = img.flags & Mat::DEPTH_MASK;
header.nType = img.flags & Mat::TYPE_MASK;
header.nChannelIndex = (img.flags & Mat::TYPE_MASK) >> 3;
header.bMagic = (img.flags & Mat::MAGIC_MASK) == Mat::MAGIC_VAL;
header.bContinuous = (img.flags & Mat::CONTINUOUS_FLAG) >> 14;
header.bSubMatrix = (img.flags & Mat::SUBMATRIX_FLAG) >> 15;
header.show();
cout << "channels() = " << img.channels() << endl;
cout << "depth() = " << img.depth() << endl;
cout << "isContinuous() = " << img.isContinuous() << endl;
cout << "isSubmatrix() = " << img.isSubmatrix() << endl;
cout << "type() = " << img.type() << endl;
cout << "elemSize() = " << img.elemSize() << endl;
cout << "elemSize1() = " << img.elemSize1() << endl;
return 0;
}