Class: PH

Inherits:
Object
  • Object
show all
Defined in:
lib/ph.rb

Constant Summary collapse

NotGreyscaleError =
Class.new(StandardError)
IncorrectDimensionsError =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pixels_2d) ⇒ PH

Returns a new instance of PH.

Raises:



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

def initialize(pixels_2d)
  @pixels = pixels_2d
  @size = pixels_2d.size

  raise NotGreyscaleError if pixels_2d.flatten.count != @size**2
  raise IncorrectDimensionsError if !(Math.sqrt(size) % 8).zero?
end

Instance Attribute Details

#pixelsObject (readonly)

Returns the value of attribute pixels.



4
5
6
# File 'lib/ph.rb', line 4

def pixels
  @pixels
end

#sizeObject (readonly)

Returns the value of attribute size.



4
5
6
# File 'lib/ph.rb', line 4

def size
  @size
end

Class Method Details

.hash(pixels_2d) ⇒ Object



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

def hash(pixels_2d)
  new(pixels_2d).hash
end

.vector(pixels_2d) ⇒ Object



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

def vector(pixels_2d)
  new(pixels_2d).vector
end

Instance Method Details

#hashObject



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

def hash
  # Binary to hex string
  vector.join.to_i(2).to_s(16)
end

#vectorObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ph.rb', line 32

def vector
  @_vector ||= begin
    # Get DCT2D of the pixels
    dct_pixels = dct2d(pixels)
    # Get high frequency corner
    sqrt = Math.sqrt(size).to_i
    coords = Array.new(2, sqrt)
    corner = flat1d(coords, dct_pixels)
    corner_size = corner.length
    # Median values
    med = median(corner)

    result = Array.new(size, 0)

    corner.each.with_index do |f, i|
      # Compare each value to the median
      result[i] = 1 if f > med
    end

    result
  end
end