How to display OpenCV Mat on MFC View

后端 未结 3 1020
暖寄归人
暖寄归人 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;
    }
    
    0 讨论(0)
  • 2020-12-15 02:02

    From MSDN:

    lpvBits [in] A pointer to the color data stored as an array of bytes. For more information, see the following Remarks section.

    This is the pointer you must init with the data returned from Mat::data.

    0 讨论(0)
  • 2020-12-15 02:15

    Here is another possible way of displaying OpenCV data in MFC which I use and works great:

    IplImage* image// <-- this contains the image you want to display
    CvvImage tempdefault;
    RECT myrect;   // <-- specifiy where on the screen you want it to be displayed
    myrect.top    =  0;
    myrect.bottom = _pictureh;
    myrect.left   = _picturex;
    myrect.right =  _picturew+_picturex;
    tempdefault.Create(_pictureh,_picturew,32);
    tempdefault.CopyOf(image);
    tempdefault.DrawToHDC(pDC->GetSafeHdc(),&myrect);
    
    0 讨论(0)
提交回复
热议问题