【问题标题】:display cv::Mat (opencv 2.4.3) in pictureBox (Visual C++ 2010)在图片框中显示 cv::Mat (opencv 2.4.3) (Visual C++ 2010)
【发布时间】:2013-05-03 18:17:55
【问题描述】:

我需要使用 openFileDialog 读取 Mat 形式的图像并将其显示在图片框中(在 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));

}

【问题讨论】:

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


    【解决方案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);
    

    【讨论】:

    • 非常感谢您的帮助。现在我只有一个问题,第一部分我得到了这个错误: error C2664: 'cv::imread' : cannot convert parameter 1 from 'System::String ^' to 'const std::string &'
    • 检查this post,其中有许多描述如何将System::String 转换为std::string 的答案。
    • 谢谢。这非常有用。
    • 如果我使用以下代码将图像更改为灰度:cvtColor(src, src_gray, CV_BGR2GRAY);然后这个函数显示 3 个图像并排显示。我应该如何更改代码以在图片框中显示正确的灰度图像?
    【解决方案2】:

    你需要像这样设置 Picturebox 的 Palette:

    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;
    

    【讨论】:

      猜你喜欢
      • 2018-03-24
      • 2012-11-11
      • 1970-01-01
      • 2012-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-25
      • 1970-01-01
      相关资源
      最近更新 更多