grayphash 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -106,6 +106,68 @@
106
106
  x = (x + (x >> 4)) & m4;
107
107
  return INT2NUM((x * h01)>>56);
108
108
  }
109
+
110
+ extern "C" VALUE t_histogram(VALUE self, VALUE lfile, VALUE channel) {
111
+ Check_Type(lfile, T_STRING);
112
+ char* file = RSTRING_PTR(lfile);
113
+
114
+ const unsigned int ch = NUM2UINT(channel);
115
+ CImg<uint8_t> src;
116
+ src.load(file);
117
+
118
+ CImg<float> meanfilter(7,7,1,1,1);
119
+ CImg<float> img;
120
+ if (src.spectrum() == 3){
121
+ img = src.channel(ch).get_convolve(meanfilter);
122
+ } else if (src.spectrum() == 4){
123
+ int width = img.width();
124
+ int height = img.height();
125
+ int depth = img.depth();
126
+ img = src.crop(0,0,0,0,width-1,height-1,depth-1,2).channel(ch).get_convolve(meanfilter);
127
+ } else {
128
+ img = src.channel(0).get_convolve(meanfilter);
129
+ }
130
+
131
+ float total = img.width() * img.height();
132
+ VALUE result_hist = rb_ary_new2(256);
133
+ const CImg<float> hist = img.histogram(256);
134
+ cimg_forX(hist,pos) {
135
+ rb_ary_push(result_hist, rb_float_new(hist[pos] / total));
136
+ }
137
+
138
+ return result_hist;
139
+ }
140
+
141
+
142
+ extern "C" VALUE t_histogram4lumin(VALUE self, VALUE lfile) {
143
+ Check_Type(lfile, T_STRING);
144
+ char* file = RSTRING_PTR(lfile);
145
+
146
+ CImg<uint8_t> src;
147
+ src.load(file);
148
+
149
+ CImg<float> meanfilter(7,7,1,1,1);
150
+ CImg<float> img;
151
+ if (src.spectrum() == 3){
152
+ img = src.RGBtoYCbCr().channel(0).get_convolve(meanfilter);
153
+ } else if (src.spectrum() == 4){
154
+ int width = img.width();
155
+ int height = img.height();
156
+ int depth = img.depth();
157
+ img = src.crop(0,0,0,0,width-1,height-1,depth-1,2).RGBtoYCbCr().channel(0).get_convolve(meanfilter);
158
+ } else {
159
+ img = src.channel(0).get_convolve(meanfilter);
160
+ }
161
+
162
+ float total = img.width() * img.height();
163
+ VALUE result_hist = rb_ary_new2(256);
164
+ const CImg<float> hist = img.histogram(256);
165
+ cimg_forX(hist,pos) {
166
+ rb_ary_push(result_hist, rb_float_new(hist[pos] / total));
167
+ }
168
+
169
+ return result_hist;
170
+ }
109
171
 
110
172
 
111
173
  VALUE Grayphash;
@@ -116,5 +178,8 @@
116
178
  rb_define_method(Grayphash, "about", (ruby_method*) &t_about, 0);
117
179
  rb_define_method(Grayphash, "phash", (ruby_method*) &t_phash, 1);
118
180
  rb_define_method(Grayphash, "hamming", (ruby_method*) &t_hamming, 2);
181
+ rb_define_method(Grayphash, "histogram", (ruby_method*) &t_histogram, 2);
182
+ rb_define_method(Grayphash, "histogram", (ruby_method*) &t_histogram, 2);
183
+ rb_define_method(Grayphash, "histogram4lumin", (ruby_method*) &t_histogram4lumin, 1);
119
184
  }
120
185
 
@@ -0,0 +1,56 @@
1
+ require './grayphash'
2
+ require 'pp'
3
+
4
+ include Grayphash
5
+
6
+ $start = 0
7
+ $degree = 10
8
+
9
+ def max_metric hist1, hist2
10
+ h1 = hist1.each_with_index.sort.reverse[$start,$degree]
11
+ h2 = hist2.each_with_index.sort.reverse[$start,$degree]
12
+ #pp h2
13
+ result = 0
14
+ [0, $degree-1].each do |i|
15
+ #[0, 255].each do |i|
16
+ #result += (hist1[i] - hist2[i]).abs.to_f # / (hist1[i] + hist2[i] + 1)
17
+ result += (h1[i][1] - h2[i][1]).abs.to_f / (h1[i][1] + h2[i][1] + 1) # это даёт хороший результат. Если расстояние меньше 0,02, то скорее всего мы попали в нужную картинку.
18
+ end
19
+ return result
20
+ end
21
+
22
+ def progress_max_metric hist1, hist2
23
+ h1 = hist1.each_with_index.sort.reverse[$start,$degree]
24
+ h2 = hist2.each_with_index.sort.reverse[$start,$degree]
25
+ #pp h2
26
+ result = 0
27
+ k = 1.0
28
+ [0, $degree-1].each do |i|
29
+ result += k.to_f * (h1[i][1] - h2[i][1]).abs.to_f / (h1[i][1] + h2[i][1] + 1)
30
+ k /= 2
31
+ end
32
+ return result
33
+ end
34
+
35
+
36
+
37
+ histos = { }
38
+ path = '../../../pics/rotate/'
39
+ Dir.foreach(path) do |entry|
40
+ next unless File.file? path+entry
41
+ #puts entry
42
+ histos[entry] = histogram4lumin path+entry
43
+ end
44
+
45
+
46
+ pic = ARGV[0]
47
+ h = histogram4lumin path+pic
48
+
49
+ dists = {}
50
+ histos.each do |k,v|
51
+ dists[k] = progress_max_metric h, v
52
+ end
53
+
54
+ dists = dists.to_a.sort_by {|a,b| b}
55
+
56
+ pp dists
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grayphash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-30 00:00:00.000000000 Z
12
+ date: 2012-08-08 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: pHash image wrapper
15
15
  email: tauraloke@gmail.com
@@ -20,6 +20,7 @@ extra_rdoc_files: []
20
20
  files:
21
21
  - lib/grayphash.rb
22
22
  - ext/grayphash/grayphash.cpp
23
+ - ext/grayphash/histo.rb
23
24
  - ext/grayphash/extconf.rb
24
25
  homepage: http://github.com/tauraloke/grayphash
25
26
  licenses: