c# - 从 png 字节获取 EMGU 图像
问题描述
我正在尝试Image
从 png 字节中获取一个。
在以下示例中,我可以从文件中加载 png,但不能从文件的字节中加载:
//using Emgu.CV;
//using Emgu.CV.Structure;
var path = @"C:\path\to\my.png";
// Using the constructor with the file path works great!
var imageFromFile = new Image<Rgb, byte>(path);
var bytes = File.ReadAllBytes(path);
var size = imageFromFile.Size;
var imageFromBytes = new Image<Rgb, byte>(size);
// Assigning the bytes ails with an 'ArgumentOutOfRangeException' exception
imageFromBytes.Bytes = bytes;
这实际上是有道理的,因为imageFromBytes.Bytes
需要一个大小为:
size.Width * size.Height * 3
而 bytes 数组具有压缩 png 图像的大小。
所以,我的问题是 - 给定一个 png 字节数组,我怎样才能获得这些字节的图像实例?
解决方案
在Emgu github repo中浏览了一下之后,下面将从字节数组中获取图像。
不是那么 OOP-ish,但可以工作......
var imageFromBytes = new Image<Rgb, byte>(imageFromFile.Size);
CvInvoke.Imdecode(bytes, Emgu.CV.CvEnum.ImreadModes.AnyColor, imageFromBytes.Mat);
// Now imageFromBytes holds the uncompressed bytes.
推荐阅读
- ios - 使用推送通知快速打开特定视图
- r - 如何预测R中序列中的下一个数字
- c# - 如何去除 DataGrid 行的 3d 效果
- java - 为什么在下面的例子中,同一个对象引用了父类的变量,却访问了子类的函数?
- java - OnCompleteListener 和 CompletionListener 有什么区别?
- matlab - 将 if 条件与假设一起使用
- plot - 在 MacOS 上,如何在 Octave 5.1 上包含来自 octave-cli 的 Octave 图形工具包?
- git - 无法从 git 中删除的幻像文件
- flutter - 如何链接到轮播中的图像并使其在另一个站点上打开?
- vue.js - 如何以编程方式转到 vue-router 中的上一条路线?