Class: Dhashy

Inherits:
Object
  • Object
show all
Defined in:
lib/dhashy.rb,
lib/dhashy/version.rb

Overview

The *difference hash* is a compact representation of an image (i. e. photo) that works well for fuzzy finding of pairs of images that are substantially euqal.

Author:

Constant Summary collapse

VERSION =
"1.0.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jpg, size = 128) ⇒ Dhashy

Returns a new instance of Dhashy.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dhashy.rb', line 12

def initialize(jpg, size=128)
   self.size = size

   return from_array(jpg) if jpg.is_a? Array
   jpg = MiniMagick::Image.open(jpg.to_s) if jpg.is_a? Pathname
   jpg.combine_options do |j|
      j.resize "#{@dimension + 1}x#{@dimension + 1}!"
   end
   @pixels = jpg.get_pixels.map {|row| row.map {|p| ((p[0].to_f * 299) + (p[1] * 587) + (p[2] * 114)).to_f / 1000.0  }}
   @h = from_array(@pixels)
end

Instance Attribute Details

#sizeObject

Returns the value of attribute size.



10
11
12
# File 'lib/dhashy.rb', line 10

def size
  @size
end

Instance Method Details

#-(other) ⇒ Integer

(Hamming-) Distance, or visual difference

Parameters:

  • another (DHashy)

    hash

Returns:

  • (Integer)

    The Hamming distance (also Manhattan-distance),

      1. the number of different bits



49
50
51
# File 'lib/dhashy.rb', line 49

def -(other)
   [0,1].map { |matrix| (0...@dimension).map {|x| (0...@dimension).map {|y| (@h[matrix][x][y] == other[matrix][x][y]) ? 0 : 1 }.sum}.sum}.sum
end

#==(other, cutoff = @size / 64) ⇒ Boolean

Visual Equality

Parameters:

  • number (cutoff)

    of bits allowed to differ. Default: @size / 64 (i. e. 2 bits for default size)

Returns:

  • (Boolean)


63
64
65
# File 'lib/dhashy.rb', line 63

def ==(other, cutoff=@size / 64)
   self - other < 5
end

#[](index) ⇒ Object



53
54
55
# File 'lib/dhashy.rb', line 53

def[](index)
   @h[index]
end

#dimension(size) ⇒ Object

Our budget is spent on two square matrices:

one with the differences from row to row
the second with differences from column to column


28
29
30
# File 'lib/dhashy.rb', line 28

def dimension(size)
   Math.sqrt(size / 2).to_i
end

#display(values = nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/dhashy.rb', line 67

def display(values = nil)
 values = @h unless values
   [1,0].map do |matrix|
     (0...@dimension).map do |x|
         (0...@dimension).map do |y| 
           values[matrix][x][y]  ? '*' : '.'
         end.join(" ")
     end.join("\n")
   end.join("\n\n") + "\n"
end

#matrixObject

return [String] The concatenated row- and

column-wise matrices, compatible to the
python implementation


82
83
84
85
86
87
88
89
90
# File 'lib/dhashy.rb', line 82

def matrix
   [0,1].map do |matrix|
      (0...@dimension).map do |x|
         (0...@dimension).map do |y|
            @h[matrix][x][y] ? '1' : '0'
         end.join
      end.join("\n")
   end.join("\n")
end

#to_iObject

return [Integer] The integer of bit length

<size> representing the hash


104
105
106
107
108
109
110
111
112
113
# File 'lib/dhashy.rb', line 104

def to_i
   [0,1].map do |matrix|
      (0...@dimension).map do |x|
         (0...@dimension).map do |y|
            @h[x][y] * 2**(x*y)
         end
      end
   end
   .sum
end

#to_sObject



92
93
94
95
96
97
98
99
100
# File 'lib/dhashy.rb', line 92

def to_s
   [0,1].map do |matrix|
      (0...@dimension).map do |x|
         (0...@dimension).map do |y|
            @h[matrix][x][y] ? 1 : 0
         end.join
      end.join
   end.join
end