6

I'm trying to implement/convert the daltonize algorithm for correcting images for colour-blind people into ruby.

There are two primary reference implementations written in javascript and python + other implementations in languages/environments I'm not familiar with.

I have virtually no experience with image processing, let alone with VIPS / ruby-vips. I'm wondering how to make the first steps. The documentation seems primarily in C/C++ and very little on the ruby side. It's also extremely detailed. I'm not even sure which basic operations to use. Looks like the lin function is a good starting point, but I'm not exactly sure how to apply it.

Anybody with some VIPS experience can probably work out the entire algorithm in a few minutes. I wonder if anybody can give me some pointers on where to start. Specifically:

  • How to access a single (R/G/B) element?
  • Are there better approaches based on the daltonize implementations?
gingerlime
  • 5,206
  • 4
  • 37
  • 66

2 Answers2

5

(note This was a very old answer and described ruby-vips as of two major versions ago. I've updated it for the 2.0.16 gem, the current version in November 2019)

There is complete documentation here:

https://rubydoc.info/gems/ruby-vips

The Vips section has a tutorial-style introduction:

https://rubydoc.info/gems/ruby-vips/Vips

For example:

require 'vips'

if ARGV.length < 2
    raise "usage: #{$PROGRAM_NAME}: input-file output-file"
end

im = Vips::Image.new_from_file ARGV[0], access: :sequential

im *= [1, 2, 1]

mask = Vips::Image.new_from_array [
        [-1, -1, -1],
        [-1, 16, -1],
        [-1, -1, -1]
       ], 8
im = im.conv mask, precision: :integer

im.write_to_file ARGV[1]

This opens an image in streaming mode, multiplies the middle band (green) by two, sharpens the image with an integer convolution, and writes the result back. You can run it like this:

./example.rb x.jpg y.ppm

There's a full "daltonize" example in the ruby-vips repo:

https://github.com/libvips/ruby-vips/blob/master/example/daltonize8.rb

jcupitt
  • 10,213
  • 2
  • 23
  • 39
  • 1
    Wow. Thanks so much. It makes me feel so stupid spending hours trying to convert the js/python algorithms. I knew someone with experience with this would only take a few minutes to work this out. I'll try to see if I can figure out how this works so I can maybe learn a bit in the process. I wish I could +1000 this. Thanks! – gingerlime Jan 29 '13 at 14:16
  • @YoavAner, also see this: http://stackoverflow.com/questions/10709995/ruby-vips-image-processing-library-are-there-any-good-examples-of-usage – Stanislav Pankevich Jan 31 '13 at 14:03
  • Thanks @Stanislaw - I did see your question already, but was still feeling very lost on how to make any progress with the daltonize conversion. I haven't tried the suggested implementation yet, but hopefully will dig into it again very soon. – gingerlime Jan 31 '13 at 19:52
  • @user894763 - I just did a small edit to your daltonize code since it was throwing errors. Not that I'm complaining, but after running it, I got a slightly different output than expected unfortunately... maybe one of the matrices has an error? I'll play with it more and see if I can spot anything. – gingerlime Jan 31 '13 at 22:27
  • 1
    Hey guys, remember that ruby-vips has wiki with "examples" and "basic concepts" pages: https://github.com/jcupitt/ruby-vips/wiki. After you succeed, feel free to add your daltonize code there. Examples page really lacks the good use cases for ruby-vips. – Stanislav Pankevich Feb 01 '13 at 06:08
  • 1
    @Stanislaw. I've added a modified example to the wiki. I tested it quickly and it works. The difference between the original implementation above and the version of the wiki is that the wiki version uses LMS and not `bradford cone space` and that seems to work better somehow. – gingerlime Feb 01 '13 at 20:30
  • Answer rewritten for current ruby-vips, Nov 2019. – jcupitt Nov 15 '19 at 10:09
1

For newcomers: ruby-vips has wiki: https://github.com/jcupitt/ruby-vips/wiki with 'Examples' and 'Basic concepts' pages in it. They show the basics of ruby-vips usage.

Also, feel free to add your own use cases there, like @YoavAner did (Daltonize example).

Stanislav Pankevich
  • 11,044
  • 8
  • 69
  • 129