Displaying webcam feed in cv::Mat format in a picturebox
Asked Answered
M

1

7

I am using OpenCV to take a live stream from a webcam and after detecting faces. I am resizing them so that only my face is displayed.

But the problem is that I am doing all this in C++ Windows Forms and I want it to be displayed in a PictureBox instead of getting the display in OpenCV imshow() window.

I'm using cv::Mat so I am having a great deal of problem with displaying in the picture box.

I have tried converting it into IplImage but that didn't work either. Also, I have tried Google but I couldn't get a working solution. I've been trying this for 3 days.

Here's my code for displaying:

                 face = getFace(frame);
                 cv::imshow("window",face);

where frame and face are cv::Mat

Maisonette answered 27/9, 2012 at 7:48 Comment(4)
To display into a picturebox, you need to convert that IplImage into a bitmap.Eugenol
can you pls provide the line of code for that?Maisonette
I only have C# code for constructing a Bitmap. It takes the data pointer (IplImage->dataOrigin), and the size to construct it. Since a PictureBox is really a C# thing, I have not used it in C++. For C++ I would rather recommend doing OpenGL or DirectX display, since its just so so much faster.Eugenol
try using Qt if possible for display...its easy...you can display any image read by OpenCV with Qt created display window and not OpenCV window...specialy big images where u need to scroll down n right...Xebec
D
6

Here is a C++ CLR function to draw OpenCV mat on any Windows Form Control:

void DrawCVImage(System::Windows::Forms::Control^ control, cv::Mat& colorImage)
{
    System::Drawing::Graphics^ graphics = control->CreateGraphics();
    System::IntPtr ptr(colorImage.ptr());
    System::Drawing::Bitmap^ b  = gcnew System::Drawing::Bitmap(colorImage.cols,colorImage.rows,colorImage.step,System::Drawing::Imaging::PixelFormat::Format24bppRgb,ptr);
    System::Drawing::RectangleF rect(0,0,control->Width,control->Height);
    graphics->DrawImage(b,rect);
    delete graphics;
}

This function can only draw 8 bit 3 channel images.

Try experimenting with Pixel Format of the bitmap for other image types.

Destiny answered 27/9, 2012 at 19:39 Comment(2)
Correct me if I'm wrong, but don't you need a graphics->Dispose(); call at the end after you've used it to draw? "Instead, you must call CreateGraphics every time that you want to use the Graphics object, and then call Dispose when you are finished using it.Oaten
@kdbanman... Thankyou for pointing it out. I wasn't aware of that. In C++ CLR, we must call delete operator instead of Dispose.Destiny

© 2022 - 2024 — McMap. All rights reserved.