如何在Windows窗体应用程序中显示cv :: Mat?

alo*_*uhu 2 c++ opencv video-capture image-processing face-recognition

我试图imwrite成功地在Windows窗体上显示图像,但它损坏了磁盘,所以我需要一个更好的方法来做到这一点.

下面是我当前的代码,它将图像临时写入硬盘:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

        namedWindow("video",0);
        VideoCapture cap(0);
        flag = true;
        while(flag){
            Mat frame;
            cap >> frame; // get a new frame from camera
            **imwrite("vdo.jpg",frame);**
            this->panel1->BackgroundImage = System::Drawing::Image::FromFile("vdo.jpg");

            waitKey(5);
            delete panel1->BackgroundImage;
            this->panel1->BackgroundImage = nullptr;

        }
    }
Run Code Online (Sandbox Code Playgroud)

当我尝试使用Mat内存中的OpenCV时,我无法让它工作.以下代码片段是我到目前为止所尝试的:

this->panel1->BackgroundImage = System::Drawing::Bitmap(frame);
Run Code Online (Sandbox Code Playgroud)

要么

this->panel1->BackgroundImage = gcnew System::Drawing::Bitmap( frame.widht,frame.height,System::Drawing::Imaging::PixelFormat::Undefined, ( System::IntPtr ) frame.imageData);
Run Code Online (Sandbox Code Playgroud)

我希望frame在不使用的情况下显示在此代码中imwrite.我该如何做到这一点?

mev*_*ron 5

放弃将图像写入文件然后立即读回控件是绝对是个好主意.由于硬盘驱动器通常是系统中最慢的存储设备,因此效率非常低.

我不相信你Bitmap在上面的例子中使用了正确的构造函数.您可能应该使用构造函数定义.另外,告诉Bitmap对象PixelFormat未定义的对象可能也没有帮助.我假设你有一个彩色相机返回一个CV_8UC3矩阵(即你的PixelFormat == Format24bppRgb).

怎么样试试这样的电话:

this->panel1->BackgroundImage = gcnew System::Drawing::Bitmap(frame.size().width,
                                                              frame.size().height,
                                                              frame.step,
                                                              PixelFormat::Format24bppRgb,
                                                              (IntPtr)frame.data);
Run Code Online (Sandbox Code Playgroud)

另外,请记住,OpenCV本身以BGR格式存储颜色.因此,您可能需要交换红色和蓝色通道以使数据看起来正确.

希望,这会让你开始!