C# OpenCV VideoWriter 以意想不到的颜色保存
Posted
技术标签:
【中文标题】C# OpenCV VideoWriter 以意想不到的颜色保存【英文标题】:C# OpenCV VideoWriter saves in unexpected colors 【发布时间】:2019-03-27 06:45:36 【问题描述】:我正在使用 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】: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
。
【讨论】:
我试图寻找您提到的这些格式,但它们在选项中不可用。你的意思是改变这些位置? ``` imgColor.Source = new WriteableBitmap(p.Width, p.Height, 96d, 96d, PixelFormats.Rgb24, null); System.Drawing.Bitmap ColorImg = new System.Drawing.Bitmap(colorFrame.Width, colorFrame.Height, colorFrame.Stride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, colorFrame.Data); ```以上是关于C# OpenCV VideoWriter 以意想不到的颜色保存的主要内容,如果未能解决你的问题,请参考以下文章