Mapping cv :: Mat (opencv 2.4.3) to pictureBox (Visual C ++ 2010)

I need to read an image in Mat form using openFileDialog and display it in a pictureBox (in Visual C ++ / Visual Studio 2010).

I searched a lot, but could not find the answer.

I am using this code:

openFileDialog1->Filter = "JPEG files (*.jpg)|*.jpg|Bitmap files (*.bmp)|*.bmp"; if(openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) { Mat img; img = imread(openFileDialog1->FileName, CV_LOAD_IMAGE_COLOR); pictureBox1->Image = (gcnew Bitmap(img.size().width, img.size().height, img.widthStep, Imaging::PixelFormat::Format24bppRgb, (IntPtr)img.data)); } 
+4
source share
2 answers

This question has already been answered here :

For your requirement, you can do it as follows:

 Mat img; img = imread(openFileDialog1->FileName, CV_LOAD_IMAGE_COLOR); System::Drawing::Graphics^ graphics = pictureBox1->CreateGraphics(); System::IntPtr ptr(img.ptr()); System::Drawing::Bitmap^ b = gcnew System::Drawing::Bitmap(img.cols,img.rows,img.step,System::Drawing::Imaging::PixelFormat::Format24bppRgb,ptr); System::Drawing::RectangleF rect(0,0,pictureBox1->Width,pictureBox1->Height); graphics->DrawImage(b,rect); 
+1
source

You need to set the Picturebox palette as follows:

 ColorPalette^ palette = pictureBox1->Image->Palette; UInt32 Alpha = 0xFF; UInt32 Intensity; for (System::UInt16 i = 0; i < palette->Entries->Length; ++i) { Intensity = i * 0xFF / 255; palette->Entries[i] = Color::FromArgb(static_cast<int>(Alpha), static_cast<int>(Intensity), static_cast<int>(Intensity), static_cast<int>(Intensity)); } pictureBox1->Image->Palette = palette; 
+1
source

Source: https://habr.com/ru/post/1479043/


All Articles