Ask Your Question

Revision history [back]

How can I show a OpenCV image in a dialog of MFC.

The type of cv image is cv::mat. And there is a PictureControl in dialog, or any other control, (just for show image). There picture is a BMP file (512 *1024). I load it by "Mat img=imread("path")" , then do some processing by Opencv. And I want a good method to show it, it should not spend a lot of time (Should be less than 1ms). I have tried some methods as follows.

Method 1:

IplImage simg = src;
cvNamedWindow("IDC_STATIC_OUTPUT", 0);
cvResizeWindow("IDC_STATIC_OUTPUT", rect.Width, rect.Height);
HWND hWnd = (HWND)cvGetWindowHandle("IDC_STATIC_OUTPUT");
HWND hParent = ::GetParent(hWnd);
::SetParent(hWnd, GetDlgItem(IDC_PictureBox)->m_hWnd);
::ShowWindow(hParent, SW_HIDE);
cvShowImage("IDC_STATIC_OUTPUT", &simg);

Problem of this method: There is a flashing window out of dialog, and the running speed is slow.

Method 2: Use a function,

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;

}

Problem: The speed of update is slow (Perhaps I used it in a wrong way).And running speed also slow (maybe because piex by piex).

Method 3: Also a function. link text

Problem: I can not use that function normally.Cannot display pictures.