RMagickでアンチエイリアス処理をしてみた。
画像処理。
アンチエイリアス処理をしてみた。
処理はエッジ検出の応用。
RGB値が大きく変化している箇所を見つけて、中間色に置き換えることで輪郭が滑らかに見えるようにする。
require 'rubygems' require "RMagick" include Magick def antialias(image, t, filename) threshold = t; img = ImageList.new(image) for y in 1...img.rows for x in 1...img.columns src = img.pixel_color(x, y) previous_x = img.pixel_color(x-1, y) previous_y = img.pixel_color(x, y-1) dr_x = src.red - previous_x.red dg_x = src.green - previous_x.green db_x = src.blue - previous_x.blue dr_y = src.red - previous_y.red dg_y = src.green - previous_y.green db_y = src.blue - previous_y.blue dr = dr_x*dr_x + dr_y*dr_y dg = dg_x*dg_x + dg_y*dg_y db = db_x*db_x + db_y*db_y if dr + dg + db > threshold nr = (src.red + previous_x.red + previous_y.red) / 3 ng = (src.green + previous_x.green + previous_y.green) / 3 nb = (src.blue + previous_x.blue + previous_y.blue) / 3 color = Magick::Pixel.new(nr, ng, nb) img.pixel_color(x, y, color) end end end img.write filename end image = ARGV[0] t = ARGV[1] filename = ARGV[2] antialias(image, t.to_i, filename)
本日の実験画像はこちら。
日本のMANGA的白黒文化に染まってしまったデストロイマンさんです。
ruby ./antialias.rb 100000 test.jpg