C# · 12月 19, 2021

C++ ATL标准读取图片

 CImage转Mat

//CImage 转 Mat

void CImage2Mat(CImage& Image,Mat& src)

{

// CImage 转 Mat

if (Image.IsNull())

{

cout << "没加载成功" << endl;

//Message@R_571_2419@(_T(“没有加载成功”));

}

if (1 == Image.GetBPP() / 8)

{

src.create(Image.GetHeight(),Image.GetWidth(),CV_8UC1);

}

else if (3 == Image.GetBPP() / 8)

{

src.create(Image.GetHeight(),CV_8UC3);

}

//拷贝数据

uchar* pucRow; //指向 Mat 数据区的行指针

uchar* pucImage = (uchar*)Image.GetBits(); //指向 CImage 数据区的指针

int nStep = Image.GetPitch();//每行的字节数,注意这个返回值有正有负

for (int nRow = 0; nRow < Image.GetHeight(); nRow++)

{

pucRow = (src.ptr(nRow));

for (int nCol = 0; nCol < Image.GetWidth(); nCoL++)

{

if (1 == Image.GetBPP() / 8)

{

pucRow[nCol] = *(pucImage + nRow * nStep + nCol);

}

else if (3 == Image.GetBPP() / 8)

{

for (int nCha = 0; nCha < 3; nCha++)

{

pucRow[nCol * 3 + nCha] = *(pucImage + nRow * nStep + nCol * 3 + nCha);

}

}

}

}

}