OpenCV 网络摄像头提要未在 PictureBox Visual Studio 2015 中显示

Posted

技术标签:

【中文标题】OpenCV 网络摄像头提要未在 PictureBox Visual Studio 2015 中显示【英文标题】:OpenCV Webcam feed not displaying in PictureBox visual studio 2015 【发布时间】:2017-07-08 21:53:50 【问题描述】:

嗨,所以我使用 opencv 从我的默认摄像头获取网络摄像头提要,我希望在我的 Windows 窗体上的图片框中显示它。我的网络摄像头打开了,但由于某种原因,提要从未显示在我的图片框上。请有人帮忙指出/解决问题,因为我现在被困在这里。 提前致谢。

在 myform.h 中,我有此代码将图片框发送到 myform.cpp 文件:

System::Windows::Forms::PictureBox^ mypicbox1(void)  

    opencv_gui::MyForm aform;
    return aform.pictureBox1;

获取videofeed并放入myform.cpp中我的图片框的代码是:

void opencv_gui::DrawCvImage(const cv::Mat& cvImage)

    System::Windows::Forms::PictureBox^ pictureBox = mypicbox1();
    // only color images are supported
    assert(cvImage.type() == CV_8UC3);

    if ((pictureBox->Image == nullptr) || (pictureBox->Width != cvImage.cols) || (pictureBox->Height != cvImage.rows))
    
        pictureBox->Width = cvImage.cols;
        pictureBox->Height = cvImage.rows;
        pictureBox->Image = gcnew System::Drawing::Bitmap(cvImage.cols, cvImage.rows);
    

    // Create System::Drawing::Bitmap from cv::Mat
    System::Drawing::Bitmap^ bmpImage = gcnew System::Drawing::Bitmap(
        cvImage.cols, cvImage.rows, cvImage.step,
        System::Drawing::Imaging::PixelFormat::Format24bppRgb,
        System::IntPtr(cvImage.data)
    );

    // Draw Bitmap over a PictureBox
    System::Drawing::Graphics^ g = System::Drawing::Graphics::FromImage(pictureBox->Image);

    g->DrawImage(bmpImage, 0, 0, cvImage.cols, cvImage.rows);
    pictureBox->Refresh();

    delete g;



//camera feed
int opencv_gui::video_cap(void)

    VideoCapture cap;

    if (!cap.open(0)) // open the default camera (camera 0), use something different from 0 otherwise;
        return 0;
    for (;;)
    
        Mat frame;
        cap >> frame;
        if (frame.empty()) break; // end of video stream
        DrawCvImage(frame);
        if (waitKey(10) == 27) break; // stop capturing by pressing ESC 
    
    // the camera will be closed automatically upon exit
    // cap.close();
    return 0;

这是我的调试日志:我使用了“>>>>>>>>”之类的箭头来显示重要参数,并使用“RED >>>>>>”来显示错误参数。

cvImage flags=1124024336 dims=2 rows=480 ... cv::Mat& 分配器 0x0000000000000000 cv::MatAllocator*

cols 640 整数

数据 0x0000026AC2146F80 无符号字符* *(*cvImage).data 0 '' 无符号字符

dataend 0x0000026AC2227F80 unsigned char* datalimit 0x0000026AC2227F80 unsigned char* datastart 0x0000026AC2146F80 无符号字符* 调暗 2 int 标志 1124024336 int 第 480 行 int

尺寸 p=0x000000DBDFF0DE80 cv::MatSize

p 0x000000DBDFF0DE80 整数* *(*cvImage).size.p 0 int

步骤 p=0x000000DBDFF0DEC8 buf=Length=2 cv::MatStep buf Length=2 无符号 __int64[] p 0x000000DBDFF0DEC8 无符号 __int64* u 0x0000026AA4814780 cv::UMatData* RED >>>>> bmpImage 0x0000026aa62a1fd0 defaultTransparentColor= System::Drawing::Bitmap^ RED >>>>>> System::Drawing::Image^ 0x0000026aa62a1fd0 nativeImage=2657511292512 rawData= userData= System::Drawing::Image^ defaultTransparentColor System::Drawing::Color g 系统::绘图::图形^ RED >>>>>> pictureBox 0x0000026aa62a1388 borderStyle=System::Windows::Forms::BorderStyle::None image=0x0000026aa62a1fa0 sizeMode=System::Windows::Forms::PictureBoxSizeMode::Normal ... System::Windows::Forms::PictureBox^ RED >>>>>> System::Windows::Forms::Control^ 0x0000026aa62a1388 ControlKeyboardRouting= PaletteTracing= FocusTracing= ... System::Windows::Forms::Control^ AllowDrop false bool BorderStyle System::Windows::Forms::BorderStyle::None System::Windows::Forms::BorderStyle CausesValidation true bool CreateParams 0x0000026aa62a15d8 className= caption= style=1442840576 ... System::Windows::Forms::CreateParams^ DefaultImeMode System::Windows::Forms::ImeMode::Disable System::Windows::Forms::ImeMode DefaultSize Width=100 Height=50 System::Drawing::Size EVENT_SIZEMODECHANGED 0x0000026aa629af50 System::Object^ ErrorImage 0x0000026aa629b110 nativeImage=2657511192608 rawData= userData= System::Drawing::Image^ 字体 0x0000026aa6280148 System::Drawing::Font^ 前景色 RGB=0x0 System::Drawing::Color 图像 0x0000026aa62a1fa0 nativeImage=2657511290992 rawData= userData= System::Drawing::Image^ [System::Drawing::Bitmap^] 0x0000026aa62a1fa0 defaultTransparentColor= System::Drawing::Bitmap^ System::MarshalByRefObject^ 0x0000026aa62a1fa0 __identity= System::MarshalByRefObject^ 标志 2 int FrameDimensionsList Length=1 数组 ^ 身高 480 英寸 Horizo​​ntalResolution 96.000000 浮点数 调色板 0x0000026aa62a25e8 flags=1129621568 entries=Length=0 System::Drawing::Imaging::ColorPalette^ PhysicalDimension width=640.00000 height=480.00000 System::Drawing::SizeF PixelFormat System::Drawing::Imaging::PixelFormat::Format32bppArgb System::Drawing::Imaging::PixelFormat PropertyIdList Length=0 数组 ^ PropertyItems Length=0 数组 ^ RawFormat 0x0000026aa62a3488 ... System::Drawing::Imaging::ImageFormat^ 尺寸 Width=640 Height=480 System::Drawing::Size 标签系统::对象^ 垂直分辨率 96.000000 浮点数 宽度 640 整数 nativeImage 2657511290992 __int64 原始数据数组 ^ 用户数据系统::对象^ ImageLocation System::String^ ImageRectangle X=0 Y=0 Width=640 Height=480 System::Drawing::Rectangle ImeMode System::Windows::Forms::ImeMode::Disable System::Windows::Forms::ImeMode InitialImage 0x0000026aa629c568 nativeImage=2657511196256 rawData= userData= System::Drawing::Image^ PICTUREBOXSTATE_asyncOperationInProgress 1 int PICTUREBOXSTATE_cancellationPending 2 int PICTUREBOXSTATE_in 初始化 64 int PICTUREBOXSTATE_needToLoadImageLocation 32 int PICTUREBOXSTATE_useDefaultErrorImage 8 int PICTUREBOXSTATE_useDefaultInitialImage 4 int PICTUREBOXSTATE_waitOnLoad 16 int RightToLeft System::Windows::Forms::RightToLeft::No System::Windows::Forms::RightToLeft SizeMode System::Windows::Forms::PictureBoxSizeMode::Normal System::Windows::Forms::PictureBoxSizeMode TabIndex 1 整数 TabStop false 布尔值 文本“”系统::字符串^ WaitOnLoad false 布尔 borderStyle System::Windows::Forms::BorderStyle::None System::Windows::Forms::BorderStyle 内容长度 0 int currentAsyncLoadOperation System::ComponentModel::AsyncOperation^ currentAnimating false bool defaultErrorImage 0x0000026aa629b110 nativeImage=2657511192608 rawData= userData= System::Drawing::Image^ defaultErrorImageForThread 0x0000026aa629b110 nativeImage=2657511192608 rawData= userData= System::Drawing::Image^ defaultErrorImageKey 0x0000026aa629af08 System::Object^ defaultInitialImage 0x0000026aa629c568 nativeImage=2657511196256 rawData= userData= System::Drawing::Image^ defaultInitialImageForThread 0x0000026aa629c568 nativeImage=2657511196256 rawData= userData= System::Drawing::Image^ defaultInitialImageKey 0x0000026aa629aef0 System::Object^ errorImage 0x0000026aa629b110 nativeImage=2657511192608 rawData= userData= System::Drawing::Image^ handleValid false bool 图像 0x0000026aa62a1fa0 nativeImage=2657511290992 rawData= userData= System::Drawing::Image^ imageInstallationType ImageInstallationType::DirectlySpecified ImageInstallationType imageLocation System::String^ initialImage 0x0000026aa629c568 nativeImage=2657511196256 rawData= userData= System::Drawing::Image^ [System::Drawing::Bitmap^] 0x0000026aa629c568 defaultTransparentColor= System::Drawing::Bitmap^ System::MarshalByRefObject^ 0x0000026aa629c568 __identity= System::MarshalByRefObject^ 标志 73744 int FrameDimensionsList Length=1 数组 ^ 身高 16 英寸 Horizo​​ntalResolution 96.000000 浮点数 RED >>>>>Palette 0x0000026aa629ea60 flags=951278840 entries=Length=0 System::Drawing::Imaging::ColorPalette^ PhysicalDimension width=14.000000 height=16.000000 System::Drawing::SizeF PixelFormat System::Drawing::Imaging::PixelFormat::Format24bppRgb System::Drawing::Imaging::PixelFormat PropertyIdList Length=0 数组 ^ PropertyItems Length=0 数组 ^ RED >>>>> RawFormat 0x0000026aa629f900 ... System::Drawing::Imaging::ImageFormat^ 尺寸 Width=14 Height=16 System::Drawing::Size 标签系统::对象^ 垂直分辨率 96.000000 浮点数 宽度 14 整数 nativeImage 2657511196256 __int64 原始数据数组 ^ 用户数据系统::对象^ 红色 >>>>>>> internalSyncObject 0x0000026aa629d900 System::Object^ loadCompletedDelegate System::Threading::SendOrPostCallback^ loadCompletedKey 0x0000026aa629af20 系统::对象^ loadProgressChangedKey 0x0000026aa629af38 系统::对象^ loadProgressDelegate System::Threading::SendOrPostCallback^ localImageStreamReader System::IO::StreamReader^ pictureBoxState data=12 System::Collections::Specialized::BitVector32 readBlockSize 4096 整数 读取缓冲区数组 ^ savedSize Width=640 Height=480 System::Drawing::Size sizeMode System::Windows::Forms::PictureBoxSizeMode::Normal System::Windows::Forms::PictureBoxSizeMode tempDownloadStream System::IO::MemoryStream^ totalBytesRead 0 int uriImageStream System::IO::Stream^

【问题讨论】:

由于我没有看到任何同步,我假设opencv_gui::video_cap 在主 GUI 线程上运行。因此,看到无限循环已经存在潜在问题的迹象。虽然waitKey 可能有点用,但除了 OpenCV UI 之外,我不会依赖它。无论如何,这在 GUI 应用程序中是一个非常尴尬的设计。您已经在帧之间等待,为什么不使用计时器? 感谢 Dan 的评论,我打算尽快清理有关循环的代码,但删除循环并手动捕获仍然不会在图片框上显示框架,这是我的主要问题。跨度> 您可以尝试在调试器中单步执行该功能吗? cvImage.cols, cvImage.rows, cvImage.step 的值是多少? cvImage.data 是否包含一些合理的值?顺便说一句,OpenCV 图像是 BGR,您将数据解释为 RGB。你可能需要一个cvtColor 来切换它。 嗨,丹,我已将调试日志的副本放入问题中。图像大小和数据参数为零,但其他参数具有合理的值(640 和 480)...我还标明了哪些调试行以红色突出显示供您查看。感谢您迄今为止的帮助。 【参考方案1】:

不得不稍微编辑我的代码,但我使用此处提供的答案几乎解决了我的问题: https://***.com/a/12628861/5728859

【讨论】:

【参考方案2】:

试试这个

private: System::Void button2_MouseClick(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e)

    if (button2->Text == "Stop")
    
        button2->Text = "Start";

    
    else if (button2->Text == "Start")
    
        button2->Text = "Stop";

    

     VideoCapture capture(0);
     Mat frame;

     while (button2->Text == "Stop")
     
         capture.read(frame);
         System::Drawing::Graphics^ graphics2 = pictureBox1->CreateGraphics();
         System::IntPtr ptr2(frame.ptr());
         System::Drawing::Bitmap^ b2 = gcnew System::Drawing::Bitmap(frame.cols,
             frame.rows, frame.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr2);
         System::Drawing::RectangleF rect2(0, 0, pictureBox1->Width, pictureBox1->Height);
         graphics2->DrawImage(b2, rect2);

     
 

【讨论】:

以上是关于OpenCV 网络摄像头提要未在 PictureBox Visual Studio 2015 中显示的主要内容,如果未能解决你的问题,请参考以下文章

OpenCV 从摄像头裁剪实时提要

当我尝试通过 OpenCv 访问网络摄像头时,Xcode 引发隐私错误

当我尝试通过OpenCv访问网络摄像头时,Xcode会引发隐私错误

当我使用 rtsp 摄像头流时,opencv 视频捕获滞后

OpenCV(Python中的cv2)VideoCapture在删除后不释放相机

有关opencv无法解码网络摄像头rtsp流的解决办法