如何在 OAML 中读取位图?
我想使用 OCAML 读取位图文件(从文件系统)并将像素(颜色)存储在具有位图维度的数组中,每个像素将占用数组中的一个单元格。
我找到了函数Graphics.dump_image image -> 颜色数组数组 但它不从文件中读取。
I want to read a bitmap file (from the file system) using OCAML and store the pixels (the colors) inside an array which have th dimension of the bitmap, each pixel will take one cell in the array.
I found the function Graphics.dump_image image -> color array array
but it doesn't read from a file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
CAMLIMAGE 应该可以做到。 还有一个 debian 软件包 (
libcamlimage-ocmal-dev
),以及通过 godi,如果你用它来管理你的 ocaml 包。作为在 ocaml 中读取和操作图像的有用示例,我建议在 特征类。
正如 jonathan 所说,您还可以从 ocaml 调用 C 函数(但效果不太好),例如 ImageMagick。 尽管您将对图像数据进行大量操作以将图像带入 ocaml,但您始终可以为所有函数编写 c 来将图像作为抽象数据类型进行操作 - 这似乎与什么完全相反不过,您希望用 C 而不是 ocaml 编写大部分程序。
因为我最近想尝试一下 camimages(并且在安装时遇到了一些麻烦——我不得不修改两个来自编译错误的 ml 文件,尽管这些文件非常简单)。 这是一个快速程序,
black_and_white.ml
,以及如何编译它。 这应该可以让人们轻松地开始使用该包(特别是动态图像生成):并编译,
并运行,
CAMLIMAGE should do it. There is also a debian package (
libcamlimage-ocmal-dev
), as well as an installation through godi, if you use that to manage your ocaml packages.As a useful example of reading and manipulating images in ocaml, I suggest looking over the code for a seam removal algorithm over at eigenclass.
You can also, as stated by jonathan --but not well-- call C functions from ocaml, such as ImageMagick. Although you're going to do a lot of manipulation of the image data to bring the image into ocaml, you can always write c for all your functions to manipulate the image as an abstract data type --this seems to be completely opposite of what you want though, writing most of the program in C not ocaml.
Since I recently wanted to play around with camlimages (and had some trouble installing it --I had to modify two of the ml files from compilation errors, very simple ones though). Here is a quick program,
black_and_white.ml
, and how to compile it. This should get someone painlessly started with the package (especially, dynamic image generation):And to compile,
And to run,
我不知道有什么现成的方法可以做到这一点。 您可以使用
open_in
打开文件,并使用input_char
一次读取一个字节,吸入标头和数据并构建颜色数组
对于简单格式(例如 BMP)来说,这种方式是可行的,但对于 JPG 或 PNG 之类的东西,自行解决可能会比您想要的工作更多。I don't know of an out-of-the box way to do it. You could open the file with
open_in
and read it byte at a time withinput_char
, suck in the header and the data and build up thecolor array array
that way for simple formats (e.g. BMPs) but for anything like JPGs or PNGs a roll your-own solution would probably be more work than you want to get into.您还可以使用 OCaml 的众多 SDL 绑定之一,特别是 SDL_image 绑定,它可以让您轻松加载各种图像,并提供以数组形式访问单个像素和原始数据的函数。
OCamlSDL 是一种流行的。
You could also use one of the numerous SDL bindings for OCaml, specifically the SDL_image ones, which let you load all kinds of images easily, and provides functions to access individual pixels and raw data as an array.
OCamlSDL is a popular one.
如果您不想使用 CAMLIMAGE,通常会使用原始 RGB 或 PNM/PPM(具有易于创建的标头格式,后跟 RGB 值)图像。 ImageMagick 允许您查看这些格式或将它们转换为更可用的格式。
If you don't want to use CAMLIMAGE, usually raw RGB or PNM/PPM (which have an easy to create header format followed by RGB values) images are used. ImageMagick allows you to then view this formats or convert them into more usable formats.