在MFC中转化32位深度带透明通道PNG图像到24位深度MAT图像遇到的问题。
int nWidth = cimage.GetWidth();
int nHeight = cimage.GetHeight();
if (4 == nChannels)
{
mat.create(nHeight, nWidth, CV_8UC3);
}
//拷贝数据
uchar* pucRow; //指向数据区的行指针
uchar* pucImage = (uchar*)cimage.GetBits(); //指向数据区的指针
int nStep = cimage.GetPitch(); //每行的字节数,注意这个返回值有正有负
for (int nRow = 0; nRow < nHeight; nRow++)
{
pucRow = (mat.ptr<uchar>(nRow));
for (int nCol = 0; nCol < nWidth; nCol++)
{
if (1 == nChannels)
{
pucRow[nCol] = *(pucImage + nRow * nStep + nCol);
}
else if (3 == nChannels)
{
for (int nCha = 0; nCha < 3; nCha++)
{
pucRow[nCol * 3 + nCha] = *(pucImage + nRow * nStep + nCol * 3 + nCha);
}
}
else if (4 == nChannels)
{
for (int nCha = 0; nCha < 3; nCha++)
{
pucRow[nCol * 3 + nCha] = *(pucImage + nRow * nStep + nCol * 4 + nCha);
}
}
}
}
这是我的程序,如果是4通道PNG图的话运行后无法得到图像,如果PNG是按照BGRA来排列的话我感觉应该是可以的啊不知道问题是出在哪里