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.
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
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
Related articles
- visual studio - i want to pass multiple variables between form
- python 3x - i want to run anaconda3 python from visual studio code it may or may not move
- visual c ++ - visual studio 2019 c ++ how to get the current time
- i tried to download talib with visual studio 2019 but it doesn't work
- how to use visual studio code
- visual studio code - how to add arguments to the command that calls the vscode extension
- visual studio code - tabs typed with vs code do not match meta characters
- range selection in visual studio code becomes rectangular selection
- visual studio - compile error occurs when using arduino with vscode
- go - i get an error in visual studio code (expected ';', found 'package')
- c # - i am new to programming while writing code in visual studio, [createassetmenu] does not appear in predictive conversion
- Examples of conversion operations between parent and child classes in Java
- visual studio - how to paste a reference to a form on vbnet
- visual studio - splitcontainer panel hide/change location
- netcore5 does not appear in the target framework of visual studio 2019
- user macro cannot be used to specify the path of visual studio 2017 library
- python error in visual studio code: i want to resolve unable to import 'azurefunctions'
- visual studio - text editor screen display bug
- c - i forgot how to use visual studio
- sql server - visual studio tools for applications please tell me how to set up the smtp connection manager
- about c ++ opencv labeling errors
- [wpf/c #] when specified in imagesource by invoke in asynchronous process, image rendering process is not performed
- c ++ - e1696 cannot open source file "opencv2/opencvhpp" error occurs and cpp file cannot be built
- [visual c ++] sample code cannot be executed due to "e1696 source file cannot be opened" error
- android - i want to jump from displayalert to another dialog and make settings
- cmake - when cv :: text :: ocrtesseract :: create is built with visual studio, an unresolved external reference lnk1120 or lnk20
- c # - i want to change bitmap-24bit to 254 (reduced color) of bitmap-8bit in c♯
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)