2013-05-03 314 views
4

我需要使用openFileDialog在Mat窗體中讀取圖像並將其顯示在pictureBox(在Visual C++/Visual Studio 2010中)。在pictureBox(Visual C++ 2010)中顯示cv :: Mat(opencv 2.4.3)

我搜索了很多,但無法找到答案。

我使用這個代碼:

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

} 

回答

1

這個問題已經回答here

爲了您的需求,你可以做這樣的:

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); 
+0

非常感謝您的幫助。現在我只有一個問題,第一部分出現此錯誤:_error C2664:'cv :: imread':無法將參數1從'System :: String ^'轉換爲'const std :: string&'_ – 2013-05-03 23:28:41

+0

Check [這篇文章](http://stackoverflow.com/questions/946813/c-cli-converting-from-systemstring-to-stdstring)它有很多答案描述如何將'System :: String'轉換爲'std :: string'。 – sgarizvi 2013-05-04 06:10:08

+0

謝謝。這非常有用。 – 2013-05-04 19:52:44

1

您需要像這樣設置Picturebox的調色板:

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;