Skip to content

Commit b3a7aad

Browse files
committed
bin/idhash
1 parent 09141f7 commit b3a7aad

File tree

7 files changed

+73
-4
lines changed

7 files changed

+73
-4
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
/.byebug_history
1111
.DS_Store
1212
*.jpg
13-
*.png
13+
/test_images/*.png
1414

1515
/temp.*

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ gem "dhash", github: "nakilon/dhash"
77
gem "phamilie"
88
gem "mini_magick"
99
gem "dhashy"
10+
gem "phashion"
1011

1112
gem "get_process_mem"
1213
gem "mll"

README.md

100755100644
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ and visualization of IDHash (`rake compare_images -- image1.jpg image2.jpg`):
3939

4040
Here in each of 64 cells, there are two circles that color the difference between that cell and the neighbor one. If the difference is low the Importance bit is set to zero and the circle is invisible. So there are 128 pairs of corresponding circles and when you take one, if at least one circle is visible and is of different color the line is to be drawn. Here you see 15 lines and so the distance between fingerprints will be equal to 15 (that is pretty low and can be interpreted as "images look similar"). Also, you see here that floor on this photo matters -- classic dHash won't see that it's darker than wall because it's comparing only horizontal neighbors and if one photo had no floor the distance function won't notice that. Also, it sees the Important difference between the very right and left columns because the wall has a slow but visible gradient.
4141

42+
As of version 0.2.4.0 the gem includes a binary that you can call to get similar visualisation in terminal:
43+
44+
```bash
45+
idhash test_images/3f9....jpg test_images/309....jpg
46+
```
47+
48+
![screenshot](doc_bin.png)
49+
4250
### Remaining problems
4351

4452
* Neither dHash nor IDHash can't automatically detect very shifted crops and rotated images but you can make a wrapper that would call the comparison function iteratively.

Rakefile

100755100644
File mode changed.

bin/idhash

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env ruby
2+
Signal.trap(:INT){ abort "\n(interrupted by SIGINT)" }
3+
4+
unless 2 == ARGV.size
5+
puts "this command is to compare two images"
6+
puts "usage: #{__FILE__} <path to image1> <path to image2>"
7+
exit
8+
end
9+
10+
require_relative "../lib/dhash-vips"
11+
ha, hb = ARGV.map{ |filename| DHashVips::IDHash.fingerprint filename }
12+
puts "distance: #{d1 = DHashVips::IDHash.distance ha, hb}"
13+
size = 2 ** 3
14+
shift = 2 * size * size
15+
ai = ha >> shift
16+
ad = ha - (ai << shift)
17+
bi = hb >> shift
18+
bd = hb - (bi << shift)
19+
20+
_127 = shift - 1
21+
_63 = size * size - 1
22+
# width = 800
23+
# height = 800
24+
25+
d2 = 0
26+
a, b = [[ad, ai, ARGV[0]], [bd, bi, ARGV[1]]].map do |xd, xi, path|
27+
puts File.basename path
28+
hor = Array.new(size){Array.new(size){" "}}
29+
ver = Array.new(size){Array.new(size){" "}}
30+
_127.downto(0).each_with_index do |i, ii|
31+
if i > _63
32+
y, x = (_127 - i).divmod size
33+
else
34+
x, y = (_63 - i).divmod size
35+
end
36+
if xi[i] > 0
37+
target, c = if i > _63
38+
[ver, %w{ v ^ }[xd[i]]]
39+
else
40+
[hor, %w{ > < }[xd[i]]]
41+
end
42+
target[y][x] = c
43+
end
44+
if ai[i] + bi[i] > 0 && ad[i] != bd[i]
45+
d2 += 1
46+
target = if i > _63
47+
ver
48+
else
49+
hor
50+
end
51+
target[y][x] = "\e[7m#{target[y][x]}\e[27m"
52+
end
53+
end
54+
hor.zip(ver).each{ |_| puts _.join " " }
55+
end
56+
abort "something went wrong" unless d1 * 2 == d2
57+
puts "OK"

dhash-vips.gemspec

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
Gem::Specification.new do |spec|
22
spec.name = "dhash-vips"
3-
spec.version = "0.2.3.0"
3+
spec.version = "0.2.4.0"
44
spec.summary = "dHash and IDHash perceptual image hashing/fingerprinting"
5-
spec.metadata = {"source_code_uri" => "https://github.com/nakilon/dhash-vips"}
65

76
spec.author = "Victor Maslov aka Nakilon"
87
spec.email = "[email protected]"
98
spec.license = "MIT"
9+
spec.metadata = {"source_code_uri" => "https://github.com/nakilon/dhash-vips"}
1010

1111
spec.add_dependency "ruby-vips", "~> 2.0", "!= 2.1.0", "!= 2.1.1"
1212

1313
spec.require_path = "lib"
1414
spec.extensions = %w{ extconf.rb }
15-
spec.files = %w{ LICENSE.txt dhash-vips.gemspec lib/dhash-vips.rb idhash.c lib/dhash-vips-post-install-test.rb } + spec.extensions
15+
spec.files = %w{ LICENSE.txt dhash-vips.gemspec lib/dhash-vips.rb idhash.c lib/dhash-vips-post-install-test.rb } + spec.extensions +
16+
%w{ bin/idhash }
17+
spec.executables = %w{ idhash }
18+
spec.bindir = "bin"
1619
end

doc_bin.png

351 KB
Loading

0 commit comments

Comments
 (0)