Ask Your Question
1

Showing Mat image in Picturebox

asked 2015-01-08 07:55:20 -0600

Icetray gravatar image

Hi guys,

I'm attempting to create a GUI for my project and I am having issues displaying the output in the GUI picturebox. I used the following function I found online to convert the Mat image to bitmap:

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

I then just did a simple:

                frame = imread("C:\\Users\\xxx\\Pictures\\picture.jpg");
                DrawCVImage(pictureBox1,frame);

but only a tiny bit of the picture appears as shown here: http://i.imgur.com/KKviOOv.jpg

Thanks in advance!

edit retag flag offensive close merge delete

Comments

System::Drawing::Imaging::PixelFormat::Format24bppRgb

Basically this is your problem. I am guessing that this is NOT the format of your frame. A frame read like that is only 8 bit 3 channel data. More than half of your image data is skipped due to taking larger step sizes.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-01-09 03:44:47 -0600 )edit

Hi Steven! Thanks for helping me out with this.

Is there a way to fix this? I'm new to C++/CLI coding and Windows Forms. I read that Mat images can only be converted to bitmap if they are in CV_8UC3 format so I tried doing a frame.convertTo(frameDisplay, CV_8UC3). It worked for 2 images but then suddenly stopped working all together.

I apologize if I'm missing what you're trying to say completely. I'm new to C++, OpenCV and especially Windows Forms. I've done all my C++/OpenCV codes. Now I'm just left with making everything more visually appealing with a proper GUI.

Once again, thanks for your help!

Icetray gravatar imageIcetray ( 2015-01-09 10:21:04 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
2

answered 2015-02-20 05:32:11 -0600

Richard Y. Hayashi gravatar image

updated 2015-02-20 05:50:38 -0600

After a day or two struggling to get an OpenCV Mat to Windows Bitmap I came across a simple solution after trying so many different things found on the Net. Try this:

Mat matImage = imread("image.bmp", CV_LOAD_IMAGE_COLOR);
cvtColor(matImage, matImage, CV_BGRA2RGBA);

hBitmap = CreateBitmap(matImage.cols, matImage.rows, 1, 32, matImage.data);

And then you have a Bitmap to use in the Picture Control/Box. This is for MFC but I think it should be similar for C++/CLI. It was an ah-ha moment.

edit flag offensive delete link more
0

answered 2018-07-26 19:22:48 -0600

I had to do this it to get it to run

//convert to stdlink text

        openFileDialog1->ShowDialog();
        fileName1 = openFileDialog1->FileName;
        //tostdString converts to std string see link ^^
        Mat src = imread(tostdString(fileName1));//, CV_LOAD_IMAGE_COLOR);
        cvtColor(src, src, CV_BGR2BGRA);
        HBITMAP hBit = CreateBitmap(src.cols, src.rows, 1, 32, src.data);

        Bitmap^ bmp = Bitmap::FromHbitmap((IntPtr)hBit);
        pictureBox1->Image = bmp;

        waitKey();
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2015-01-08 07:55:20 -0600

Seen: 16,795 times

Last updated: Jul 26 '18