Skip to content

Instantly share code, notes, and snippets.

@ser1zw
Created April 26, 2012 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ser1zw/2500365 to your computer and use it in GitHub Desktop.
Save ser1zw/2500365 to your computer and use it in GitHub Desktop.
win32screenshotで撮ったスクリーンショットをruby-opencvのCvMatに読み込む方法のベンチマーク
# win32screenshotで撮ったスクリーンショットをruby-opencvのCvMatに読み込む方法のベンチマーク
#
# 環境: Windows 7, Core i5 2.4GHz, メモリ 4.0GB, ディスプレイ解像度 1280x800px
# ruby 1.9.3p125 (2012-02-16) [i386-mingw32], OpenCV 2.3.1
#
# > ruby benchmark.rb
# (1) Bitmapデータを配列にしてset_dataで読み込む版
# user system total real
# 1.934000 0.062000 1.996000 ( 2.061118)
#
# (2) 一旦一時ファイルに書き込んでから読み込む版
# user system total real
# 0.000000 0.031000 0.031000 ( 0.020001)
require 'opencv'
require 'win32/screenshot'
require 'tempfile'
include OpenCV
require 'benchmark'
bitmap = Win32::Screenshot::Take.of(:desktop)
puts '(1) Bitmapデータを配列にしてset_dataで読み込む版'
puts Benchmark::CAPTION
puts Benchmark.measure {
byte = bitmap.bitmap
array = byte.unpack("x54C*")
data = Array.new
p = 0
bitmap.height.times{
bitmap.width.times{
a = Array.new
a << array[p];p+=1;
a << array[p];p+=1;
a << array[p];p+=1;
data << a
}
# 4で割れないとき
amari = bitmap.height%4
p += 4 - amari unless amari == 0
}
i = CvMat.new(bitmap.height, bitmap.width, :cv8u, 3)
# i.set_data(data) # `set_data': no implicit conversion from nil to integer (TypeError) なのでコメントアウト
}
puts
puts '(2) 一旦一時ファイルに書き込んでから読み込む版'
puts Benchmark::CAPTION
puts Benchmark.measure {
Tempfile.open('bitmap') { |f|
f.binmode
f.write bitmap.bitmap
image = CvMat.load(f.path)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment