简体   繁体   English

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

[英]How to display a cv::Mat in a Windows Form application?

I tried to use imwrite to successfully to display an image on a Windows Form, but it damages the disk, so I need a better way to do this. 我试图使用imwrite成功在Windows窗体上显示图像,但是它损坏了磁盘,因此我需要一种更好的方法来执行此操作。

Below, is my current code, which writes the image temporarily to the hard drive: 下面是我当前的代码,该代码将图像临时写入硬盘驱动器:

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;

        }
    }

When I try to use the OpenCV Mat that is in memory, I cannot get it to work. 当我尝试使用内存中的OpenCV Mat ,无法使其正常工作。 The following code snippets are what I have tried so far: 到目前为止,我已经尝试了以下代码片段:

this->panel1->BackgroundImage = System::Drawing::Bitmap(frame);

or 要么

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

I want to display frame in this code without using imwrite . 我想在此代码中显示frame而不使用imwrite How do I accomplish this? 我该如何完成?

It is definitely a good idea to get away from writing the image to a file and then immediately reading back onto a control. 避免将图像写入文件然后立即读回控件绝对是个好主意。 It is quite inefficient as the hard drive is the slowest memory device in the system usually. 由于硬盘驱动器通常是系统中最慢的存储设备,因此效率很低。

I do not believe you are using the correct Bitmap constructor in your example above. 我不认为您在上面的示例中使用了正确的Bitmap构造函数。 You should probably be using this constructor definition. 您可能应该使用构造函数定义。 Also, telling the Bitmap object that the PixelFormat is undefined is probably not helping things either. 另外,告诉Bitmap对象PixelFormat未定义也可能PixelFormat I'm assuming you have a color camera that is returning a CV_8UC3 matrix (ie, your PixelFormat == Format24bppRgb ). 我假设您有一台返回CV_8UC3矩阵的彩色摄像机(即, PixelFormat == Format24bppRgb )。

How about try a call like this: 如何尝试这样的通话:

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

Also, remember that OpenCV natively stores color in BGR format. 另外,请记住,OpenCV本机以BGR格式存储颜色。 So, you may need to swap the red and blue channels to get the data to look right. 因此,您可能需要交换红色和蓝色通道以使数据看起来正确。

Hopefully, that will get you started! 希望这会帮助您入门!

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

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