C#OpenCV VideoWriter保存了意外的颜色

时间:2019-03-27 06:45:37

标签: c# wpf emgucv writeablebitmap realsense

我正在使用Intel Realsense RGB相机在WPF窗口上显示实时流,并将流保存到文件中。我在窗口上显示的内容具有正确的颜色,但是当我保存它时,视频颜色关闭了(更紫色)。请查看屏幕截图:https://ibb.co/txy9Sgd

我正在使用EmguCV视频编写器保存视频。我对格式没有太多了解。我猜我在Format24bppRgb格式上做错什么了?

private Pipeline pipeline = new Pipeline(); // Create and config the pipeline to strem color and depth frames.
private CancellationTokenSource tokenSource;
private VideoWriter writer = null;

public StartTests()
{
    InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    tokenSource = new CancellationTokenSource();
    int fcc = VideoWriter.Fourcc('M', 'P', '4', 'V'); //'M', 'J', 'P', 'G'
    float fps = 15F;
    writer = new VideoWriter("testttt.mp4", fcc, fps, new System.Drawing.Size(640, 480), true);

    Config cfg = new Config();
    cfg.EnableStream(Stream.Color, 640, 480, format: Format.Rgb8);
    PipelineProfile pp = pipeline.Start(cfg);
    StartRenderFrames(pp);
}

private void StartRenderFrames(PipelineProfile pp)
{
    // Allocate bitmaps for rendring. Since the sample aligns the depth frames to the color frames, both of the images will have the color resolution
    using (VideoStreamProfile p = pp.GetStream(Stream.Color) as VideoStreamProfile)
    {
        imgColor.Source = new WriteableBitmap(p.Width, p.Height, 96d, 96d, PixelFormats.Rgb24, null);
    }
    Action<VideoFrame> updateColor = UpdateImage(imgColor);

    Task.Factory.StartNew(() =>
    {
        while (!tokenSource.Token.IsCancellationRequested)
        {
            using (FrameSet frames = pipeline.WaitForFrames()) // Wait for the next available FrameSet
            {
                VideoFrame colorFrame = frames.ColorFrame.DisposeWith(frames);

                // Save frames to file here...
                System.Drawing.Bitmap ColorImg = new System.Drawing.Bitmap(colorFrame.Width, colorFrame.Height, colorFrame.Stride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, colorFrame.Data);
                Image<Bgr, Byte> imageCV = new Image<Bgr, byte>(ColorImg); //Image Class from Emgu.CV
                Mat matFrame = imageCV.Mat;
                writer.Write(matFrame);

                // Render to WPF window here...
                Dispatcher.Invoke(DispatcherPriority.Render, updateColor, colorFrame);
            }
        }
    }, tokenSource.Token);
}

static Action<VideoFrame> UpdateImage(Image img)
{
    WriteableBitmap wbmp = img.Source as WriteableBitmap;
    return new Action<VideoFrame>(frame =>
    {
        using (frame)
        {
            var rect = new Int32Rect(0, 0, frame.Width, frame.Height);
            wbmp.WritePixels(rect, frame.Data, frame.Stride * frame.Height, frame.Stride);
        }
    });
}

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    tokenSource.Cancel();
    tokenSource.Dispose();
    pipeline.Stop();
    writer.Dispose();
    tokenSource = new CancellationTokenSource();
}

我想在保存的.mp4文件中看到一致的颜色,但是我看到的颜色很奇怪。

注意-我的代码基于以下示例:https://github.com/IntelRealSense/librealsense/blob/master/wrappers/csharp/cs-tutorial-2-capture/Window.xaml.cs

1 个答案:

答案 0 :(得分:1)

OpenCV假定为BGR颜色格式,因此,如果将RealSense流格式更改为Format.Bgra8,将WriteableBitmap格式更改为PixelFormats.Bgr24,则应该没事。

所以您应该拥有:

cfg.EnableStream(Stream.Color, 640, 480, format: Format.Bgr8);

new WriteableBitmap(p.Width, p.Height, 96d, 96d, PixelFormats.Bgr24, null);

我认为您不需要更改System.Drawing.Bitmap像素格式,因为您仅使用它来填充OpenCV mat