How to display OpenCV Mat on MFC View

后端 未结 3 1028
暖寄归人
暖寄归人 2020-12-15 01:33

I thought displaying OpenCV2 Mat on MFC View is simple but is not. This is only relevant material I found on google. Excuse me for my ignorance but I can\'t find any other m

3条回答
  •  青春惊慌失措
    2020-12-15 01:51

    CvvImage is not available in new versions of OpenCV. Using the following code you can convert Mat to CImage and then display CImage everywhere you want:

    int Mat2CImage(Mat *mat, CImage &img){
      if(!mat || mat->empty())
        return -1;
      int nBPP = mat->channels()*8;
      img.Create(mat->cols, mat->rows, nBPP);
      if(nBPP == 8)
      {
        static RGBQUAD pRGB[256];
        for (int i = 0; i < 256; i++)
            pRGB[i].rgbBlue = pRGB[i].rgbGreen = pRGB[i].rgbRed = i;
        img.SetColorTable(0, 256, pRGB);
      }
      uchar* psrc = mat->data;
      uchar* pdst = (uchar*) img.GetBits();
      int imgPitch = img.GetPitch();
      for(int y = 0; y < mat->rows; y++)
      {
        memcpy(pdst, psrc, mat->cols*mat->channels());//mat->step is incorrect for those images created by roi (sub-images!)
        psrc += mat->step;
        pdst += imgPitch;
      }
    
      return 0;
    }
    

提交回复
热议问题