Last active
February 17, 2016 00:28
-
-
Save abronte/52fda3d481f9a0b6fe3c to your computer and use it in GitHub Desktop.
Ruby opencv histogram
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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