Module: SsimSort

Includes:
Magick
Defined in:
lib/ssimsort.rb

Class Method Summary collapse

Class Method Details

.cov(x, y) ⇒ Object



14
15
16
# File 'lib/ssimsort.rb', line 14

def SsimSort.cov(x,y)
	return x.zip(y).covariance
end

.get_lum(img, px = 80) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/ssimsort.rb', line 19

def SsimSort.get_lum(img,px=80)
	img = img.scale(px,px)
	lum_average = img.get_pixels(0, 0, img.columns, img.rows).map do |p|
		p.to_HSL[2]
	end
	return lum_average
end

.sort(input_path, output_path, tolerance = 0.8) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ssimsort.rb', line 58

def SsimSort.sort(input_path,output_path,tolerance=0.8)
	files = Dir.entries(input_path).map {|file| File.absolute_path("#{input_path}/#{file}")}
	files.shift(2) #Remove . and ..
	files.select!{|f| @formats=~ f}
	set = files.product(files)
	set.each do |file1,file2|
		path = "#{output_path}/#{file1.split("/").last}/"
		simil = SsimSort.ssim(file1,file2)
		if simil > tolerance
			FileUtils.makedirs(path) unless File.exists?(path)
			FileUtils.cp(file2,path)
		end
	end
end

.sort_comp(filecomp_path, input_path, output_path) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ssimsort.rb', line 73

def SsimSort.sort_comp(filecomp_path,input_path,output_path)
	filecomp = File.absolute_path(filecomp_path)
	output_path = File.absolute_path(output_path+"/")
	files = Dir.entries(input_path).map {|file| File.absolute_path("#{input_path}/#{file}")}
	files.shift(2) #Remove . and ..
	files.select!{|f| @formats=~ f}
	sim_dict = {}
	FileUtils.mkdir(output_path) unless File.exists?(output_path)
	files.each do |file|
		simil = SsimSort.ssim(filecomp,file)
		sim_dict[file] = simil	
	end
	sim_list = sim_dict.sort_by {|k,v| v}.reverse
	sim_list.each_with_index do |k,i|
		FileUtils.cp(k[0],"#{output_path}/#{i}(#{k[1]})#{File.extname(k[0])}")
	end
end

.ssim(file1, file2) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/ssimsort.rb', line 28

def SsimSort.ssim(file1,file2)
	cA,cB = 0.01, 0.03
	x,y = get_lum(ImageList.new(file1)), get_lum(ImageList.new(file2))
	var_x, var_y = x.variance, y.variance
	moy_x, moy_y = x.mean, y.mean
	a = (2*moy_x*moy_y+cA)*(2*(cov(x,y))+cB)
	b = (moy_x**2+moy_y**2+cA)*(var_x+var_y+cB)
	(a/b) < 0 ? 0 : (a/b).round(5)
end

.ssim_dir(input_path) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/ssimsort.rb', line 39

def SsimSort.ssim_dir(input_path)	
	files = Dir.entries(input_path).map {|file| File.absolute_path("#{input_path}/#{file}")}
	files.shift(2) #Remove . and ..
	files.select!{|f| @formats=~ f}
	set = files.product(files)
	set.each do |file1,file2|
		simil = SsimSort.ssim(file1,file2)
		puts "#{file1.split("/").last}\t|\t#{simil}\t|\t#{file2.split("/").last}"  
	end
end

.ssim_dir_mean(input_path) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/ssimsort.rb', line 50

def SsimSort.ssim_dir_mean(input_path)
	files = Dir.entries(input_path).map {|file| File.absolute_path("#{input_path}/#{file}")}
	files.shift(2) #Remove . and ..
	files.select!{|f| @formats=~ f}
	set = files.combination(2).to_a
	l = set.map {|file1,file2| SsimSort.ssim(file1,file2)}.mean
end