Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

first steps using ruby-vips

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?
like image 894
gingerlime Avatar asked Dec 10 '12 12:12

gingerlime


Video Answer


1 Answers

(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

like image 166
jcupitt Avatar answered Sep 19 '22 00:09

jcupitt