在pictureBox中显示cv :: Mat(opencv 2.4.3)(Visual C ++ 2010)

时间:2013-05-03 18:17:55

标签: visual-studio-2010 visual-c++ opencv

我需要使用openFileDialog以Mat格式读取图像并将其显示在pictureBox中(在Visual C ++ / Visual Studio 2010中)。

我搜索了很多但找不到答案。

我正在使用此代码:

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

}

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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;