简体   繁体   English

如何在 MFC 窗口中输出 cv::Mat

[英]How to output cv::Mat in MFC window

I have 2 programs, MFC program and OpenCV.我有 2 个程序,MFC 程序和 OpenCV。 How I should to unite programs?我应该如何统一程序? How output cv::Mat in MFC window?如何在 MFC 窗口中输出 cv::Mat? What MFC widget I should be use?我应该使用什么 MFC 小部件? How I should convert cv::Mat to MFC widgets?我应该如何将 cv::Mat 转换为 MFC 小部件?

MFC program: MFC程序:

#include <afxwin.h> 

        class CMyMainWnd : public CFrameWnd
        {
        public:
            CMyMainWnd() { Create(NULL, L"My title"); }
        };

        class CMyApp : public CWinApp
        {
        public:
            CMyApp() {};
            virtual BOOL InitInstance() {
                m_pMainWnd = new CMyMainWnd();
                m_pMainWnd->ShowWindow(SW_SHOW);
                return TRUE;
            }
        };

        CMyApp theApp;

OpenCV program: OpenCV 程序:

#include "opencv2/opencv.hpp"
#include <iostream>

using namespace std;
using namespace cv;

int main() {

    VideoCapture cap(0);

    while (1) {

        Mat frame;
        cap >> frame;

        if (frame.empty())
            break;

        imshow("Frame", frame);

        char c = (char)waitKey(25);
        if (c == 27)
            break;
    }

    cap.release();

    destroyAllWindows();

    return 0;
}

Your main example differs in that it doesn't directly create any window.您的main示例的不同之处在于它不直接创建任何窗口。 It lets opencv create the window.它让 opencv 创建窗口。

To duplicate that example, simply move the opencv loop in to CWinApp::InitInstance and don't create a separate MFC window.要复制该示例,只需将 opencv 循环移动到CWinApp::InitInstance并且不要创建单独的 MFC 窗口。

To unite opencv with MFC, use opencv to grab the video frame, and draw the frame on MFC window.要将opencv与MFC结合起来,使用opencv抓取视频帧,并在MFC窗口上绘制帧。

CWinApp::OnIdle can be used to continuously update the window. CWinApp::OnIdle可用于持续更新窗口。 opencv's frame includes the bitmap bits, these bits can be copied directly to MFC windows's HDC using SetDIBitsToDevice or similar functions. opencv 的frame包括位图位,这些位可以使用SetDIBitsToDevice或类似函数直接复制到 MFC windows 的HDC

class CMyMainWnd : public CFrameWnd
{
    cv::VideoCapture video;
public:
    CMyMainWnd()
    {
        video.open(0);
    }

    void CMyMainWnd::OnPaint()
    {
        CPaintDC dc(this);

        cv::Mat mat;
        video >> mat;
        if(mat.empty())
            return;

        BITMAPINFOHEADER bi = { sizeof(bi) };
        bi.biWidth = mat.cols;
        bi.biHeight = -mat.rows;
        bi.biBitCount = (WORD)(mat.channels() * 8);
        bi.biPlanes = 1;

        SetDIBitsToDevice(dc, 0, 0, mat.cols, mat.rows, 0, 0, 0, mat.rows,
            mat.data, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
    }

    DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP(CMyMainWnd, CWnd)
    ON_WM_PAINT()
END_MESSAGE_MAP()

class CMyApp : public CWinApp
{
public:
    virtual BOOL InitInstance()
    {
        CWinApp::InitInstance();
        CMyMainWnd *wnd = new CMyMainWnd();
        m_pMainWnd = wnd;
        wnd->Create(NULL, L"My title");
        wnd->ShowWindow(SW_SHOW);
        return TRUE;
    }

    BOOL OnIdle(LONG lCount)
    {
        CWinApp::OnIdle(lCount);
        m_pMainWnd->Invalidate(FALSE);
        return TRUE;
    }
};

CMyApp theApp;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM