OpenCV的Mat与ATL/MFC的CImage相互转换

转自:http://blog.csdn.net/liangjialang/article/details/33320093

头文件

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. class 你的类名  
  2. {  
  3. public:   
  4.     /*MatToCImage 
  5.     *简介: 
  6.     *   OpenCV的Mat转ATL/MFC的CImage,仅支持单通道灰度或三通道彩色 
  7.     *参数: 
  8.     *   mat:OpenCV的Mat 
  9.     *   cimage:ATL/MFC的CImage 
  10.     */  
  11.     void MatToCImage(Mat& mat, CImage& cimage);  
  12.   
  13.   
  14.     /*CImageToMat 
  15.     *简介: 
  16.     *   ATL/MFC的CImage转OpenCV的Mat,仅支持单通道灰度或三通道彩色 
  17.     *参数: 
  18.     *   cimage:ATL/MFC的CImage 
  19.     *   mat:OpenCV的Mat 
  20.     */  
  21.     void CImageToMat(CImage& cimage, Mat& mat);  
  22. };  

实现文件

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. void 你的类名::MatToCImage(Mat& mat, CImage& cimage)  
  2. {  
  3.     if (0 == mat.total())  
  4.     {  
  5.         return;  
  6.     }  
  7.   
  8.   
  9.     int nChannels = mat.channels();  
  10.     if ((1 != nChannels) && (3 != nChannels))  
  11.     {  
  12.         return;  
  13.     }  
  14.     int nWidth    = mat.cols;  
  15.     int nHeight   = mat.rows;  
  16.   
  17.   
  18.     //重建cimage  
  19.     cimage.Destroy();  
  20.     cimage.Create(nWidth, nHeight, 8 * nChannels);  
  21.   
  22.   
  23.     //拷贝数据  
  24.   
  25.   
  26.     uchar* pucRow;                                  //指向数据区的行指针  
  27.     uchar* pucImage = (uchar*)cimage.GetBits();     //指向数据区的指针  
  28.     int nStep = cimage.GetPitch();                  //每行的字节数,注意这个返回值有正有负  
  29.   
  30.   
  31.     if (1 == nChannels)                             //对于单通道的图像需要初始化调色板  
  32.     {  
  33.         RGBQUAD* rgbquadColorTable;  
  34.         int nMaxColors = 256;  
  35.         rgbquadColorTable = new RGBQUAD[nMaxColors];  
  36.         cimage.GetColorTable(0, nMaxColors, rgbquadColorTable);  
  37.         for (int nColor = 0; nColor < nMaxColors; nColor++)  
  38.         {  
  39.             rgbquadColorTable[nColor].rgbBlue = (uchar)nColor;  
  40.             rgbquadColorTable[nColor].rgbGreen = (uchar)nColor;  
  41.             rgbquadColorTable[nColor].rgbRed = (uchar)nColor;  
  42.         }  
  43.         cimage.SetColorTable(0, nMaxColors, rgbquadColorTable);  
  44.         delete []rgbquadColorTable;  
  45.     }  
  46.   
  47.   
  48.     for (int nRow = 0; nRow < nHeight; nRow++)  
  49.     {  
  50.         pucRow = (mat.ptr<uchar>(nRow));  
  51.         for (int nCol = 0; nCol < nWidth; nCol++)  
  52.         {  
  53.             if (1 == nChannels)  
  54.             {  
  55.                 *(pucImage + nRow * nStep + nCol) = pucRow[nCol];  
  56.             }  
  57.             else if (3 == nChannels)  
  58.             {  
  59.                 for (int nCha = 0 ; nCha < 3; nCha++)  
  60.                 {  
  61.                     *(pucImage + nRow * nStep + nCol * 3 + nCha) = pucRow[nCol * 3 + nCha];  
  62.                 }             
  63.             }  
  64.         }     
  65.     }  
  66. }  
  67.   
  68. void 你的类名::CImageToMat(CImage& cimage, Mat& mat)  
  69. {  
  70.     if (true == cimage.IsNull())  
  71.     {  
  72.         return;  
  73.     }  
  74.   
  75.   
  76.     int nChannels = cimage.GetBPP() / 8;  
  77.     if ((1 != nChannels) && (3 != nChannels))  
  78.     {  
  79.         return;  
  80.     }  
  81.     int nWidth    = cimage.GetWidth();  
  82.     int nHeight   = cimage.GetHeight();  
  83.   
  84.   
  85.     //重建mat  
  86.     if (1 == nChannels)  
  87.     {  
  88.         mat.create(nHeight, nWidth, CV_8UC1);  
  89.     }  
  90.     else if(3 == nChannels)  
  91.     {  
  92.         mat.create(nHeight, nWidth, CV_8UC3);  
  93.     }  
  94.   
  95.   
  96.     //拷贝数据  
  97.   
  98.   
  99.     uchar* pucRow;                                  //指向数据区的行指针  
  100.     uchar* pucImage = (uchar*)cimage.GetBits();     //指向数据区的指针  
  101.     int nStep = cimage.GetPitch();                  //每行的字节数,注意这个返回值有正有负  
  102.   
  103.   
  104.     for (int nRow = 0; nRow < nHeight; nRow++)  
  105.     {  
  106.         pucRow = (mat.ptr<uchar>(nRow));  
  107.         for (int nCol = 0; nCol < nWidth; nCol++)  
  108.         {  
  109.             if (1 == nChannels)  
  110.             {  
  111.                 pucRow[nCol] = *(pucImage + nRow * nStep + nCol);  
  112.             }  
  113.             else if (3 == nChannels)  
  114.             {  
  115.                 for (int nCha = 0 ; nCha < 3; nCha++)  
  116.                 {  
  117.                     pucRow[nCol * 3 + nCha] = *(pucImage + nRow * nStep + nCol * 3 + nCha);  
  118.                 }             
  119.             }  
  120.         }     
  121.     }  
  122. }  


    • 1
      点赞
    • 1
      收藏
      觉得还不错? 一键收藏
    • 0
      评论
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值