OpenCV bindings with Lua

Geeez I’ve been waiting for this one. Finally after 39 hours work non-stop (I feel drunk) and the creation of a new ruby gem called Dub (Doxygen based Ubiquitous Binder which generates 10'018 lines of code that compile without a warning, I can PLAY with OpenCV in Rubyk.

The only source for images right now is the video signal, but that quite fun already.

opencv in ruby

If you want to reproduce the stupid rotating cube with your own face on it, heres the code:

(It is also an example in examples/GLLua/opencv.lua)

Rk patch

# run from build directory with
# ./rubyk ../examples/GLLua/opencv.rk
win = GLWindow()
cv = GLLua("../examples/GLLua/opencv.lua")
win => cv
video = VideoIn()
video => video~cv
m = Metro(3000)
m => win

Lua code

The code has been updated to use the new texture based rendering.

require('cv')

-- size must be a power of 2
size  = cv.Size(128,128)
res   = res   or cv.Mat()

n = n or 0
x = x or 0
y = y or 0
z = z or 0

function video(frame)
  cv.resize(frame, res, size, 0, 0, cv.INTER_LINEAR)
  frame_changed = true

  n = n + math.pi / 300
  x = math.cos(n / 0.9) * 360 / math.pi
  y = math.sin(n / 0.7) * 360 / math.pi
  z = math.sin(n) * 360 / math.pi
end
inlet('video', MatrixIO('send me images'))

function draw()
  gl.Clear( "COLOR_BUFFER_BIT, DEPTH_BUFFER_BIT")
  --gl.ClearDepth(1.0)
  gl.MatrixMode("MODELVIEW")
  gl.LoadIdentity()

  if frame_changed then
    rk.makeTexture(res, tex)
    frame_changed = false
  end

  gl.Translate(0.0, 0.0, -6.0)

  gl.Rotate(x, 1.0, 0.0, 0.0)
  gl.Rotate(y, 0.0, 1.0, 0.0)
  gl.Rotate(z, 0.0, 0.0, 1.0)


  gl.Color(0.5,0.5,0.0)--,0.3)
  gl.LineWidth(1.0)
  glut.WireCube(2.6)

  gl.Color(0.5,0.5,0.0,0.1)
  glut.SolidCube(2.6)

  -- simples way to draw a texture
  gl.Translate(0.0, 0.0, -1.5)
  -- do not forget to set a color or nothing will be drawn
  gl.Color(1,1,1,0.5)
  rk.drawTexture(tex, 1.3, 1.3, -1.3, -1.3)
end

function resize(width, height)
  textures = textures or gl.GenTextures(1)
  tex = textures[1]

  gl.Enable("BLEND")
  gl.Disable("DEPTH_TEST") -- not working ???
  gl.BlendFunc("SRC_ALPHA", "ONE_MINUS_SRC_ALPHA")

  gl.Enable("TEXTURE_2D")

  gl.Enable("LINE_SMOOTH")
  -- Select the projection matrix
  gl.MatrixMode("PROJECTION")
  -- reset
  gl.LoadIdentity()
  -- Calculate the aspect ratio of the view
  gl.Perspective(
    45.0,             -- Field of view angle
    width / height,   -- Aspect ration
    1.0,              -- zNear
    100.0             -- zFar
  )

  view.width  = width
  view.height = height
end
Gaspard Bucher

comments

  1. 2010-03-09 11:49 hansel

    Looks VERY cool, I’ve bookmarked this and will look at it when I have some spare time… Using Doxygen to catch the API is a good approach i.m.o.

  2. leave a comment