OpenCV Webcam feed在PictureBox visual studio 2015中不显示

您好,我使用opencv从我的默认相机获取网络摄像头供稿,并且希望将它显示在我的窗体上的一个图片框中。 我的网络摄像头出现故障,但由于某种原因,该馈送从未显示在我的照片箱中。 请有人帮忙指出/解决问题,因为我现在被困在这里。 提前致谢。

在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 int

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

  • 数据结束0x0000026AC2227F80无符号字符*
  • 数据限制0x0000026AC2227F80无符号字符*
  • 数据存储0x0000026AC2146F80无符号字符* dims 2 int标志1124024336 int行480 int
  • 大小{p = 0x000000DBDFF0DE80} cv :: MatSize

  • p 0x000000DBDFF0DE80 int * *(* cvImage).size.p 0 int

  • 步骤{p = 0x000000DBDFF0DEC8 buf = {Length = 2}} cv :: MatStep
  • buf {Length = 2} unsigned __int64 []
  • p 0x000000DBDFF0DEC8 unsigned __int64 *
  • 你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 System :: Drawing :: Graphics ^
  • RED >>>>>>> pictureBox 0x0000026aa62a1388 {borderStyle = System :: Windows :: Forms :: BorderStyle :: None image = 0x0000026aa62a1fa0 sizeMode = System :: Windows :: Forms :: PictureBoxSizeMode :: Normal ...}系统: :视窗:形式:图片框^
  • 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 ^
  • ForeColor {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 ^ Flags 2 int
  • FrameDimensionsList {Length = 1} array ^ Height 480 int Horizo​​ntalResolution 96.000000 float
  • 调色板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} array ^ PropertyItems {长度= 0}数组^
  • RawFormat 0x0000026aa62a3488 {...} System :: Drawing :: Imaging :: ImageFormat ^
  • 大小{Width = 640 Height = 480} System :: Drawing :: Size Tag System :: Object ^ VerticalResolution 96.000000 float Width 640 int nativeImage 2657511290992 __int64 rawData array ^ userData System :: Object ^ 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 =}系统::绘图::图片^ PICTUREBOXSTATE_asyncOperationInProgress 1 INT PICTUREBOXSTATE_cancellationPending 2 INT PICTUREBOXSTATE_inInitialization 64 INT PICTUREBOXSTATE_needToLoadImageLocation 32 INT PICTUREBOXSTATE_useDefaultErrorImage 8 INT PICTUREBOXSTATE_useDefaultInitialImage 4 INT PICTUREBOXSTATE_waitOnLoad 16 INT从右至左系统:视窗:形式: RightToLeft :: No System :: Windows :: Forms :: RightToLeft SizeMode System :: Windows :: Forms :: PictureBoxSizeMode :: Normal System :: Windows :: Forms :: PictureBoxSizeMode TabIndex 1 int TabStop false bool Text“”System :: String ^ WaitOnLoad false bool borderStyle System :: Windows :: Forms :: BorderStyle :: None System :: Windows :: Forms :: BorderStyle contentLength 0 int currentAsyncLoadOperation System :: ComponentModel :: AsyncOperation ^ currentlyAnimating 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
  • image 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} array ^ Height 16 int Horizo​​ntalResolution 96.000000 float
  • RED >>>>>>>调色板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} array ^ PropertyItems {长度= 0}数组^
  • RED >>>>>>> RawFormat 0x0000026aa629f900 {...} System :: Drawing :: Imaging :: ImageFormat ^
  • Size {Width = 14 Height = 16} System :: Drawing :: Size Tag System :: Object ^ VerticalResolution 96.000000 float Width 14 int nativeImage 2657511196256 __int64 rawData array ^ userData System :: Object ^ RED >>>>>>> internalSyncObject 0x0000026aa629d900 System :: Object ^ loadCompletedDelegate System :: Threading :: SendOrPostCallback ^ loadCompletedKey 0x0000026aa629af20 System :: Object ^ loadProgressChangedKey 0x0000026aa629af38 System :: Object ^ loadProgressDelegate System :: Threading :: SendOrPostCallback ^ localImageStreamReader System :: IO :: StreamReader ^
  • pictureBoxState {data = 12} System :: Collections :: Specialized :: BitVector32 readBlockSize 4096 int readBuffer array ^
  • 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 ^

  • 不得不编辑我的代码,但我几乎解决了我的问题,使用这里提供的答案:https://stackoverflow.com/a/12628861/5728859


    尝试这个

    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);
    
         }
     }
    
    链接地址: http://www.djcxy.com/p/35413.html

    上一篇: OpenCV Webcam feed not displaying in PictureBox visual studio 2015

    下一篇: OpenCV HSV value changes depending on location on the screen/camera