Kosy Onyenso
Kosy Onyenso

Reputation: 155

OpenCV Webcam feed not displaying in PictureBox visual studio 2015

Hi so I used opencv to get webcam feed from my default camera and I wish to display it in a picturebox on my windows form. My webcam comes on but for some reason, the feed is never displayed on my picturebox. Please could someone help point out/solve the issue as I am stuck here right now. Thanks in advance.

In myform.h, I have this code to send the picturebox to the myform.cpp file:

System::Windows::Forms::PictureBox^ mypicbox1(void)  
{
    opencv_gui::MyForm aform;
    return aform.pictureBox1;
}

and the code to draw get the videofeed and put into my picture box in myform.cpp is:

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

This is my debug log: I have used arrows like ">>>>>>>" to show important parameters and the words "RED >>>>>>>" to show error parameters.

Upvotes: 0

Views: 1603

Answers (2)

Muhammad Bin Ali
Muhammad Bin Ali

Reputation: 205

try this

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

     }
 }

Upvotes: 0

Kosy Onyenso
Kosy Onyenso

Reputation: 155

Had to edit my code a bit but I pretty much solved my issue using the answer provided here: https://stackoverflow.com/a/12628861/5728859

Upvotes: 0

Related Questions