C # OpenCV VideoWriter сохраняет в неожиданных цветах - PullRequest
0 голосов
/ 27 марта 2019

Я использую камеру 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 Ответ

1 голос
/ 27 марта 2019

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.

...