CImage 与 Mat 相互转换
来源:互联网 发布:西安少儿编程培训机构 编辑:程序博客网 时间:2024/06/08 08:02
原文出处:点击打开链接
头文件.h
class 你的类名
{
public:
/*MatToCImage
*简介:
* OpenCV的Mat转ATL/MFC的CImage,仅支持单通道灰度或三通道彩色
*参数:
* mat:OpenCV的Mat
* cimage:ATL/MFC的CImage
*/
void MatToCImage(Mat& mat, CImage& cimage);
/*CImageToMat
*简介:
* ATL/MFC的CImage转OpenCV的Mat,仅支持单通道灰度或三通道彩色
*参数:
* cimage:ATL/MFC的CImage
* mat:OpenCV的Mat
*/
void CImageToMat(CImage& cimage, Mat& mat);
};
.cpp
void 你的类名::MatToCImage(Mat& mat, CImage& cimage)
{
if (0 == mat.total())
{
return;
}
int nChannels = mat.channels();
if ((1 != nChannels) && (3 != nChannels))
{
return;
}
int nWidth = mat.cols;
int nHeight = mat.rows;
//重建cimage
cimage.Destroy();
cimage.Create(nWidth, nHeight, 8 * nChannels);
//拷贝数据
uchar* pucRow;//指向数据区的行指针
uchar* pucImage = (uchar*)cimage.GetBits();//指向数据区的指针
int nStep = cimage.GetPitch();//每行的字节数,注意这个返回值有正有负
if (1 == nChannels)//对于单通道的图像需要初始化调色板
{
RGBQUAD* rgbquadColorTable;
int nMaxColors = 256;
rgbquadColorTable = new RGBQUAD[nMaxColors];
cimage.GetColorTable(0, nMaxColors, rgbquadColorTable);
for (int nColor = 0; nColor < nMaxColors; nColor++)
{
rgbquadColorTable[nColor].rgbBlue = (uchar)nColor;
rgbquadColorTable[nColor].rgbGreen = (uchar)nColor;
rgbquadColorTable[nColor].rgbRed = (uchar)nColor;
}
cimage.SetColorTable(0, nMaxColors, rgbquadColorTable);
delete []rgbquadColorTable;
}
for (int nRow = 0; nRow < nHeight; nRow++)
{
pucRow = (mat.ptr<uchar>(nRow));
for (int nCol = 0; nCol < nWidth; nCol++)
{
if (1 == nChannels)
{
*(pucImage + nRow * nStep + nCol) = pucRow[nCol];
}
else if (3 == nChannels)
{
for (int nCha = 0 ; nCha < 3; nCha++)
{
*(pucImage + nRow * nStep + nCol * 3 + nCha) = pucRow[nCol * 3 + nCha];
}
}
}
}
}
void 你的类名::CImageToMat(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);
}
}
}
}
}
亲测可用!
- CImage 与 Mat 相互转换
- OpenCV的Mat与ATL/MFC的CImage相互转换
- OpenCV的Mat与ATL/MFC的CImage相互转换
- Mat与IplImage相互转换
- Opencv Mat与Iplimage的相互转换
- 【OpenCV】IplImage与Mat的相互转换
- Mat 与IplImage *类型相互转换
- Mat与Iplimage之间的相互转换
- Opencv Mat与Iplimage的相互转换
- Opencv Mat与Iplimage的相互转换
- Opencv Mat与Iplimage的相互转换
- .mat文件与.txt文件相互转换
- OpenCV Mat与QImage相互转换函数
- Opencv Mat与Iplimage的相互转换
- Opencv Mat与Iplimage的相互转换
- Mat 与 QImage相互转换(亲测通过)
- Mat与IplImage和CvMat之间的相互转换
- Hobject 与 OpenCV IplImage 和 Mat 的相互转换
- 单片机学习大纲
- linux上google-chrome访问外网
- android native 使用opengl es画点线面图形(纯c++)
- systemd添加自定义系统服务设置自定义开机启动
- Android Http请求服务器返回505
- CImage 与 Mat 相互转换
- 中科院开源图形化机器学习系统Easy ML
- Android下如何通过JNI方法向上提供接口总结
- 阅读笔记Surflet-Pair-Relation Histograms: A Statistical 3D-Shape Representation for Rapid Classification
- 字符串的排列
- 《java多线程编程核心技术》
- 封装自定义滚动条
- MaxCompute分区表和非分区表使用对比
- Scala练习-斐波那契查找