Emgu Github
Emgu
环境:
Emgu CV 4.9.0
netframework 4.8
1、下载
libemgucv-windesktop-4.9.0.5494.exe 安装后,找到安装路径下的runtime文件夹复制到c#项目Debug目录下
2、C#示例
{ var ch = mat.NumberOfChannels switch { 1 => "Gray/Depth", 3 => "BGR", 4 => "BGRA", _ => $"{mat.NumberOfChannels}ch" }; return $"{mat.Width}x{mat.Height}, {ch}, {mat.Depth}"; } // ========================= // 内部工具 // ========================= private static void EnsureDepthMatch(DepthType actual, params DepthType[] allowed) { foreach (var d in allowed) { if (actual == d) return; } throw new ArgumentException($"数据类型与 DepthType={actual} 不匹配"); } private static void ValidatePlane(byte[] plane, int expected, string name) { if (plane.Length < expected) throw new ArgumentException($"{name} 长度不足,期望 {expected},实际 {plane.Length}"); } }调用示例:
// 1) 灰度 8 位原始数据
byte[] gray = ...; // length = w*h
using var m1 = EmguCvImageHelper.FromRawGray8(gray, width: 640, height: 480);
image.Source = EmguCvImageHelper.ToBitmapSource(m1);// 2) BGR 交错原始数据
byte[] bgr = ...; // length = w*h*3
image.Source = EmguCvImageHelper.ToBitmapSourceFromRawBgr8(bgr, 640, 480);// 3) 浮点深度图
float[] depth = ...; // length = w*h
image.Source = EmguCvImageHelper.ToBitmapSourceFromRawGray32F(depth, 640, 480);// 4) 分通道(例如 R/G/B 三个数组)
byte[] r = ..., g = ..., b = ...;
using var m2 = EmguCvImageHelper.FromSeparateChannels8(640, 480, b, g, r); // OpenCV 习惯 B,G,R
image.Source = EmguCvImageHelper.ToBitmapSource(m2);// 5) 通用入口
using var m3 = EmguCvImageHelper.FromRaw(depth, 640, 480, channels: 1, DepthType.Cv32F);