void CSizedetectionDlg::CImageToMat(ATL::CImage& cimage, Mat& mat)
{
if (true == cimage.IsNull())
{
return;
}
int nChannels = cimage.GetBPP() / 8;
if ((1 != nChannels) && (3 != nChannels))
{
return;
}
int nWidth = cimage.GetWidth();
int nHeight = cimage.GetHeight();
//重建mat
if (1 == nChannels)
{
mat.create(nHeight, nWidth, CV_8UC1);
}
else if(3 == 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);
}
}
}
}
}
(有木有大哥大知道该怎么用啊,写好也后,直接调用它CImageToMat(参数,参数)这样不能用啊,该如何使用。CImage做打开图片Mat用于运算图片,做图像运算的时候还是不能读到mat里面)我直接就这样调用不行CImageToMat(g_image, g_srcImage);CImage g_image,Mat g_srcImage;她两是全局的
static void CSizedetectionDlg::CImageToMat(ATL::CImage& cimage, Mat& mat)
{
//...
}
调用
CSizedetectionDlg::CImageToMat(g_image, g_srcImage);