Skip to content

Instantly share code, notes, and snippets.

@abronte
Last active February 17, 2016 00:28
Show Gist options
  • Save abronte/52fda3d481f9a0b6fe3c to your computer and use it in GitHub Desktop.
Save abronte/52fda3d481f9a0b6fe3c to your computer and use it in GitHub Desktop.
Ruby opencv histogram
# create_dataset.rb
require 'opencv'
def hist(file)
iplimg = OpenCV::IplImage.decode_image(open(file).read)
b, g, r = iplimg.split
dim = 3
sizes = [8,8,8]
ranges = [[0, 255],[0, 255],[0, 255]]
hist = OpenCV::CvHistogram.new(dim, sizes, OpenCV::CV_HIST_ARRAY, ranges, true)
hist.calc_hist([r, g, b])
end
open("train.txt", "wb") do |file|
Dir.glob("screens/*/*.jpg").each do |f|
puts "Processing #{f}"
h = hist(f)
vals = []
cat = f.split("/")[1]
(0..511).each do |i|
vals << h[i]
end
file.write("\"#{cat}\",#{vals.join(",")}\n")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment