3

我尝试使用imwriteto 成功在 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;

        }
    }

当我尝试使用Mat内存中的 OpenCV 时,我无法让它工作。以下代码片段是我迄今为止尝试过的:

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

或者

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

我想frame在这段代码中显示而不使用imwrite. 我该如何做到这一点?

4

1 回答 1

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);

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

希望这会让你开始!

于 2012-03-08T04:10:49.970 回答