Converting OpenCV mat to WPF ImageSource

Started by
-1 comments, last by Delli 9 years, 1 month ago

Hello everybody,

that's my first post on this forum, and I'm not native english speak, so please be kind to me smile.png. I'm trying to write some application to process images using OpenCV. I like WPF, so I decide to write user interface as a separate project (dll), and include it in my c++/cli project. But then, the main problem shows up. I have hard time trying to convert cv::mat to system::window::media::Imagesource^ data. The code below is best I can get. If loaded image is bitmap it pass switch but crash on return convert (becouse convertion fails), if i load JPG or PNG, file creating Bitmap fails.


auto openDialog = gcnew SWF::OpenFileDialog();

auto result = openDialog->ShowDialog();

if( result == SWF::DialogResult::OK )
{
    cv::Mat img = cv::imread( ToStdString( openDialog->FileName ) );
    ImageToDisplay = imageSourceFromCVMat( img );
    
}

SWM::ImageSource ^ imageSourceFromCVMat( cv::Mat mat )
{
    System::IntPtr ptr( mat.ptr() );
    System::Drawing::Bitmap ^ b;
    auto type = mat.type();
    switch( mat.type() )
    {
    case CV_8UC3:
        b = gcnew System::Drawing::Bitmap( mat.cols, mat.rows, mat.step, System::Drawing::Imaging::PixelFormat::Format32bppRgb, ptr );
        break;
    case CV_8UC1:
        b = gcnew System::Drawing::Bitmap( mat.cols, mat.rows, mat.step, System::Drawing::Imaging::PixelFormat::Format8bppIndexed, ptr );
        break;
        default:
        b = gcnew System::Drawing::Bitmap( mat.cols, mat.rows, mat.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr );
        break;
    }
    SWM::ImageSourceConverter ^ converter = gcnew SWM::ImageSourceConverter();
    return( SWM::ImageSource ^ )( converter->ConvertFrom( b ) );
} 

Hope, someone can help me find solution to this conversion.

Thank!

EDIT: Noone knows any way to convert it? Please someone :)

This topic is closed to new replies.

Advertisement