如何读取OCAML位图? [英] How to read a bitmap in OCAML?

查看:247
本文介绍了如何读取OCAML位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用OCAML读取一个位图文件(从文件系统),并存储其具有的位图的第维数组内的像素(颜色),每个像素将阵列中的一个小区。

我发现功能* Graphics.dump_image图片 - >颜色数组的数组*
  但它不从文件中读取


解决方案

CAMLIMAGE 应该这样做。还有一个Debian软件包( libcamlimage-ocmal-dev的),以及通过的 GODI ,如果​​你用它来管理你的ocaml的包。

由于阅读和操作OCaml中图像的有用的例子,我建议看在code的接缝去除算法在以上的 eigenclass

您也可以,由乔纳森说 - 但不是从ocaml的well--调用C函数,如的 ImageMagick的。虽然你会做很多的图像数据的处理来把图像送到OCaml中,你可以随时c写您的所有功能,图像处理作为一个抽象数据类型 - 这似乎是完全相反的是什么你想,虽然,大多数写在C程序不OCAML的。

由于我最近想玩弄camlimages(并遇到了一些麻烦安装它 - 我不得不修改两个文件毫升从编译错误,很简单的人,虽然)。这里是一个快速程序, black_and_white.ml ,以及如何进行编译。这应该让怕疼的人开始使用包(特别是动态图像生成):

 让()=
       让宽度= int_of_string sys.argv中。(1)
       和长度= int_of_string sys.argv中。(2)
       和名称= sys.argv中。(3)
       黑色= {Color.Rgb.r = 0; G = 0; B = 0; }
       白色= {Color.Rgb.r = 255; G = 255; B = 255; }在
       让图像= Rgb24.make宽长黑
       对于i = 0至宽度1做
           对于j = 0到(长/ 2) - 1做
               Rgb24.set图像I J白色;
           完成的;
       完成的;
       Png.save名】(Images.Rgb24图片)

和编译,

  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

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.

解决方案

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

这篇关于如何读取OCAML位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆