2009/12/22
■ [画像処理]簡単な畳み込みフィルタリングのつもり
だったはずなのだが、Ruby/SDL上のSurfaceをいじる処理云々で色々手間取って妙に時間がかかってしまった。 一応左下から順に移動平均フィルタ、ラプラシアンフィルタ、先鋭化フィルタを使った結果。
require 'sdl' auto_lock = true SDL.init(SDL::INIT_EVERYTHING) def filtering(surface, filter) p = SDL::Surface.new(SDL::HWSURFACE || SDL::SRCALPHA, surface.w, surface.h, 32, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff) 1.upto(surface.h-2) do |h| 1.upto(surface.w-2) do |w| r = 0 g = 0 b = 0 pix = surface.format.getRGBA(surface[w-1, h-1]) r += (pix[0]*filter[0][0]).round g += (pix[1]*filter[0][0]).round b += (pix[2]*filter[0][0]).round pix = surface.format.getRGBA(surface[w , h-1]) r += (pix[0]*filter[0][1]).round g += (pix[1]*filter[0][1]).round b += (pix[2]*filter[0][1]).round pix = surface.format.getRGBA(surface[w+1, h-1]) r += (pix[0]*filter[0][2]).round g += (pix[1]*filter[0][2]).round b += (pix[2]*filter[0][2]).round pix = surface.format.getRGBA(surface[w-1, h]) r += (pix[0]*filter[1][0]).round g += (pix[1]*filter[1][0]).round b += (pix[2]*filter[1][0]).round pix = surface.format.getRGBA(surface[w , h]) r += (pix[0]*filter[1][1]).round g += (pix[1]*filter[1][1]).round b += (pix[2]*filter[1][1]).round pix = surface.format.getRGBA(surface[w+1, h]) r += (pix[0]*filter[1][2]).round g += (pix[1]*filter[1][2]).round b += (pix[2]*filter[1][2]).round pix = surface.format.getRGBA(surface[w-1, h+1]) r += (pix[0]*filter[2][0]).round g += (pix[1]*filter[2][0]).round b += (pix[2]*filter[2][0]).round pix = surface.format.getRGBA(surface[w , h+1]) r += (pix[0]*filter[2][1]).round g += (pix[1]*filter[2][1]).round b += (pix[2]*filter[2][1]).round pix = surface.format.getRGBA(surface[w+1, h+1]) r += (pix[0]*filter[2][2]).round g += (pix[1]*filter[2][2]).round b += (pix[2]*filter[2][2]).round p[w,h] = [r,g,b,255] end end return p end graphics = Array.new graphics.push(SDL::Surface.load("AngelFish.png")) graphics.push(filtering(graphics[0], [[0.11, 0.11, 0.11], [0.11, 0.11, 0.11], [0.11, 0.11, 0.11]])) graphics.push(filtering(graphics[0], [[ 0, 1.0, 0], [ 1.0, -4.0, 1.0], [ 0, 1.0, 0]])) graphics.push(filtering(graphics[0], [[ 0, -1.0, 0], [-1.0, 5.0, -1.0], [ 0, -1.0, 0]])) screen = SDL::Screen.open(graphics[0].w * 2, graphics[0].h * 2, 32, 0) while true while event = SDL::Event2.poll case event when SDL::Event2::Quit exit when SDL::Event2::KeyDown exit if event.sym == SDL::Key::ESCAPE when SDL::Event2::KeyUp end end screen.put(graphics[0], 0, 0) screen.put(graphics[1], graphics[1].w, 0) screen.put(graphics[2], 0, graphics[2].h) screen.put(graphics[3], graphics[3].w, graphics[3].h) screen.updateRect(0,0,0,0) end