如何在 OAML 中读取位图?

发布于 2024-07-14 11:25:05 字数 152 浏览 7 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

逐鹿 2024-07-21 11:25:05

CAMLIMAGE 应该可以做到。 还有一个 debian 软件包 (libcamlimage-ocmal-dev),以及通过 godi,如果你用它来管理你的 ocaml 包。

作为在 ocaml 中读取和操作图像的有用示例,我建议在 特征类

正如 jonathan 所说,您还可以从 ocaml 调用 C 函数(但效果不太好),例如 ImageMagick。 尽管您将对图像数据进行大量操作以将图像带入 ocaml,但您始终可以为所有函数编写 c 来将图像作为抽象数据类型进行操作 - 这似乎与什么完全相反不过,您希望用 C 而不是 ocaml 编写大部分程序。

因为我最近想尝试一下 camimages(并且在安装时遇到了一些麻烦——我不得不修改两个来自编译错误的 ml 文件,尽管这些文件非常简单)。 这是一个快速程序,black_and_white.ml,以及如何编译它。 这应该可以让人们轻松地开始使用该包(特别是动态图像生成):

   let () =
       let width  = int_of_string Sys.argv.(1)
       and length = int_of_string Sys.argv.(2)
       and name   = Sys.argv.(3)
       and black = {Color.Rgb.r = 0; g=0; b=0; }
       and white = {Color.Rgb.r = 255; g=255; b=255; } in
       let image = Rgb24.make width length black in
       for i = 0 to width-1 do
           for j = 0 to (length/2) - 1 do
               Rgb24.set image i j white;
           done;
       done;
       Png.save name [] (Images.Rgb24 image)

并编译,

ocamlopt.opt -I /usr/local/lib/ocaml/camlimages/ ci_core.cmxa graphics.cmxa ci_graphics.cmxa ci_png.cmxa black_and_white.ml -o black_and_white

并运行,

./black_and_white 20 20 test1.png

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):

   let () =
       let width  = int_of_string Sys.argv.(1)
       and length = int_of_string Sys.argv.(2)
       and name   = Sys.argv.(3)
       and black = {Color.Rgb.r = 0; g=0; b=0; }
       and white = {Color.Rgb.r = 255; g=255; b=255; } in
       let image = Rgb24.make width length black in
       for i = 0 to width-1 do
           for j = 0 to (length/2) - 1 do
               Rgb24.set image i j white;
           done;
       done;
       Png.save name [] (Images.Rgb24 image)

And to compile,

ocamlopt.opt -I /usr/local/lib/ocaml/camlimages/ ci_core.cmxa graphics.cmxa ci_graphics.cmxa ci_png.cmxa black_and_white.ml -o black_and_white

And to run,

./black_and_white 20 20 test1.png
朱染 2024-07-21 11:25:05

我不知道有什么现成的方法可以做到这一点。 您可以使用 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 with input_char, suck in the header and the data and build up the color 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.

独孤求败 2024-07-21 11:25:05

您还可以使用 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.

梦纸 2024-07-21 11:25:05

如果您不想使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文