Home>

I am creating an image processing program using the C ++/CLI WindowsForms application.
I am trying to display the image selected in the file selection dialog and display the image processed by OpenCV next to it, but convert the bitmap file selected in the file selection dialog to Mat and convert from Mat to bitmap file I don't know how to convert.
I would like to know about Bitmap → Mat and Mat → Bitmap conversion.

Applicable source code

When the button is clicked, the file selection dialog is opened and the selected image is displayed in picturebox1 and the processed image is displayed in picturebox2.

private: System :: Void button_Click (System :: Object ^ sender, System :: EventArgs ^ e) {
        // Create a file open dialog
        OpenFileDialog ^ dlg = gcnew OpenFileDialog;
        // File filter
        dlg->Filter = "Image file (* .bmp, *. jpg, *. png, *. tif, *. ico) | * .bmp;*. jpg;*. png;*. tif;*. ico" ;
        // Display dialog (do nothing if Cancel button is clicked)
        if (dlg->ShowDialog () == System :: Windows :: Forms :: DialogResult :: Cancel) return;
        // Create original image bitmap from bitmap file
        Bitmap ^ bmp1 = gcnew Bitmap (dlg->FileName);
        // display on picturebox1
        pictureBox1->Image = bmp1;
        // Create bitmap for processing from bitmap file
        Bitmap ^ bmp2 = gcnew Bitmap (dlg->FileName);
--------------------------------
Convert bmp2 selected in the file selection dialog to Mat
↓ ↓
Image processing using OpenCV
↓ ↓
Convert image processed Mat to bitmap file (bmp3 etc.)
--------------------------------// Display the image processed in picturebox2
: PictureBox2->Image = bmp3;
        }
Supplemental information (FW/tool version etc.)

Visual studio community 2015
OpenCV 3.4.0

Append

The following code does not convert Bitmap to Mat, but converts the path (dlg->FileName) selected by file selection from System :: String ^ type to std :: string and loads it with imread . After that, the image read by imread is processed and then displayed on picureBox2.
It is a problem here, but when it is displayed with imshow, it will be flipped up and down and left and right, and if it is displayed in pictureBox2 with the drawImage function, the image will collapse.
I'm sorry, but I would appreciate it if you could tell me about tips and sites that might be helpful.

private: System :: Void button_Click (System :: Object ^ sender, System :: EventArgs ^ e) {
        // Create a file open dialog
        OpenFileDialog ^ dlg = gcnew OpenFileDialog;
        // File filter
        dlg->Filter = "Image file (* .bmp, *. jpg, *. png, *. tif, *. ico) | * .bmp;*. jpg;*. png;*. tif;*. ico" ;
        // Display dialog (do nothing if Cancel button is clicked)
        if (dlg->ShowDialog () == System :: Windows :: Forms :: DialogResult :: Cancel) return;
        // Create original image bitmap from bitmap file
        Bitmap ^ bmp1 = gcnew Bitmap (dlg->FileName);
        // display on picturebox1
        pictureBox1->Image = bmp1;
// Convert the file path (dlg->FileName (System :: String ^ type)) selected in the file selection dialog to std :: string
        string imgfl = msclr :: interop :: marshal_as<std :: string>(dlg->FileName);
        // Read with imread
Mat img = imread (imgfl);
----------------------------------------Image processing using OpenCV
         --------------------------
        // Display the image processed in picturebox2
        drawImage (pictureBox2, img);
        }
                   //drawing
    private: System :: Void drawImage (PictureBox ^ pic, Mat mat) {
        if ((pic->Image == nullptr) || (pic->Width! = mat.cols)
            || (pic->Height! = mat.rows)) {
            pic->Width = mat.cols;
            pic->Height = mat.rows;
            Bitmap ^ bmpPicBox = gcnew Bitmap (pic->Width, pic->Height);
            pic->Image = bmpPicBox;
        }
        Graphics ^ g = Graphics :: FromImage (pic->Image);
        Bitmap ^ bmp = gcnew Bitmap (mat.cols, mat.rows, mat.step,
            System :: Drawing :: Imaging :: PixelFormat :: Format32bppRgb, IntPtr (mat.data));
        g->DrawImage (bmp, 0, 0, mat.cols, mat.rows);
        pic->Refresh ();
        delete g;
    }


The one read from Bitmap from the left and output to pictureBox1, the one read by Mat and displayed in pictureBox2, the one read by Mat and displayed by imshow.
First, when it is read by imread, the top, bottom, left and right are reversed for some reason, and when trying to output to pictureBox2, the image will collapse.
] (f71d220f54b286edb664607c017fb12a.png)

  • Answer # 1

    Is it the same as below?


    C ++/CLI.
    How can I convert Bitmap to OpenCV Mat?


    Addition:

    I tried to put OpenCvSharp into a project made with Nu ++ from NuGet at VS2015 at hand,
    It wasn't added to the reference for some reason manually
    (Project folder) \ packages \ OpenCvSharp-AnyCPU.2.4.10.20170306 \ lib \ net45 \ *. Dll
    Added.

    The compilation passed normally.

    I'm not familiar with OpenCV, so I will say something appropriate.
    Since the Bitmap type can also access each pixel via a pointer,
    I thought I could convert it myself.
    It's a bit annoying if the format is different ...
    (I felt like Bitmap was not converted to RGBA when reading GIF or something)