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

kdbanman
  • 10,161
  • 10
  • 46
  • 78
U.B.A.R
  • 221
  • 2
  • 4
  • 12
  • To display into a picturebox, you need to convert that IplImage into a bitmap. – SinisterMJ Sep 27 '12 at 08:03
  • can you pls provide the line of code for that? – U.B.A.R Sep 27 '12 at 08:50
  • 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. – SinisterMJ Sep 27 '12 at 09:05
  • 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... – rotating_image Sep 27 '12 at 12:22

1 Answers1

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.

sgarizvi
  • 16,623
  • 9
  • 64
  • 98
  • 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.](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.creategraphics(v=vs.110).aspx) – kdbanman Jul 02 '15 at 21:09
  • @kdbanman... Thankyou for pointing it out. I wasn't aware of that. In C++ CLR, we must call `delete` operator instead of `Dispose`. – sgarizvi Jul 03 '15 at 05:55