6

我对图像处理完全陌生。我对内部 JPEG 是什么以及它是如何工作的一无所知。

我想知道,是否可以在某处找到执行以下简单操作的 ruby​​ 代码:

  1. 打开jpeg文件。
  2. 遍历每个像素并将其颜色设置为 fx green。
  3. 将结果写入另一个文件。

我对如何使用 ruby​​-vips 库
https://github.com/ender672/ruby-vips实现这一点特别感兴趣

我的目标 - 学习如何使用 ruby​​-vips 执行基本的图像处理操作(伽玛校正、亮度、色调......)

任何比 ruby​​-vips 的 github 页面上的“hello world”更复杂的工作示例的链接都将受到高度赞赏!

如果有 ruby​​-vips 的替代品,我也会感谢他们。


更新

自从我问这个问题以来发生了很多事情:

4

2 回答 2

10

update ruby-vips has changed a bit since this answer was written. I've revised it for the current (2018) version.

I'm one of the maintainers of libvips, the image processing library that ruby-vips wraps.

Tim's ruby-vips repository hasn't been touched for a while. I have a fork here that works with current libvips:

https://github.com/jcupitt/ruby-vips

There are some examples here:

https://github.com/jcupitt/ruby-vips/tree/master/example

To set the red and blue channels to zero and just leave a green image you might multiply R and B by zero and G by 1. ruby-vips uses arrays to represent pixel constants, so you can just write:

out = in * [0, 1, 0]

A complete runnable example might be:

#!/usr/bin/ruby

require 'vips'

im = Vips::Image.new_from_file '/home/john/pics/theo.jpg'
im *= [0, 1, 0]
im.write_to_file 'x.jpg'

There's a trick you can use for new_from_file: if you know you will just be doing simple top-to-bottom operations on the image, like arithmetic or filtering or resize, you can tell ruby-vips that you only need sequential access to pixels:

im = Vips::Image.new_from_file '/home/john/pics/theo.jpg', access: :sequential

Now ruby-vips will stream your image. It'll run the load, the multiply and the save all in parallel and never keep more than a few scanlines of pixels in memory at any one time. This can give a really nice improvement to speed and memory use.

To change image gamma you might try something like:

im = im ** 0.5 * 255 / 255 ** 0.5

Though that'll be a bit slow (it'll call pow() three times for each pixel), it'd be much faster to make a lookup table, run the pow() on that, then map the image through the table:

lut = Vips::Image.identity
lut = lut ** 0.5 * 255 /255 ** 0.5
im = im.maplut lut

Any questions, please feel free to open them on the rubyvips issue tracker:

https://github.com/jcupitt/ruby-vips/issues

于 2012-05-23T08:24:28.200 回答
2

对不起,我不知道 ruby​​-vips,但ImageMagick在图像处理方面是经典之作。有RMagick当前 repo)形式的 Ruby 绑定,您可以从 ImageMagick 文档中获得很多功能,但这里也有三个教程以及网络上的大量示例。

如果你真的想深入研究图像处理的理论,它的根源是信号处理的一种形式(这非常令人兴奋和有益,因为它通常允许你在图像音频/视频信号上应用非常相似的算法,但是它最终会在数学上变得非常沉重 - 傅立叶变换),那么,如果数学不吓到你,我只能推荐阅读 Gonzalez 和 Woods 的,我会说这是该领域的明确参考。它很昂贵,但那里有你需要的一切,让你开始并超越。如果您想在不先花很多钱的情况下开始使用, 这里还有一个包含免费电子书链接的页面。

于 2012-05-22T21:19:15.020 回答