-
Notifications
You must be signed in to change notification settings - Fork 3
/
PHash.scala
91 lines (79 loc) · 3.15 KB
/
PHash.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package scalaphash
import java.awt.image.BufferedImage
import scala.util.control.NonFatal
object PHash {
type DCTHash = Long
type MarrHash = Array[Int]
type RadialHash = Array[Int]
/**
* Computes DCT hash value of image
* (http://www.phash.org/docs/pubs/thesis_zauner.pdf / page 21)
* @param image image for hashing
* @return 64-bit hash value or exception
*/
def dctHash(image: BufferedImage): Either[Throwable, DCTHash] =
try Right(unsafeDctHash(image))
catch { case NonFatal(e) => Left(e) }
/**
* Computes DCT hash value of image
* (http://www.phash.org/docs/pubs/thesis_zauner.pdf / page 21)
* @param image image for hashing
* @return 64-bit hash value
*/
def unsafeDctHash(image: BufferedImage): DCTHash = PHashInternal.unsafeDctHash(image)
/**
* Computes distance between two DCT hashes
* Less is better
*/
def dctHashDistance(hash1: DCTHash, hash2: DCTHash): Long = PHashInternal.dctHashDistance(hash1, hash2)
/**
* Computes Marr hash value of image
* (http://www.phash.org/docs/pubs/thesis_zauner.pdf / page 22)
* @param image image for hashing
* @param alpha coefficient for correlation kernel
* @param level coefficient for correlation kernel
* @return hash as int array or exception
*/
def marrHash(image: BufferedImage, alpha: Int = 2, level: Int = 1): Either[Throwable, MarrHash] =
try Right(unsafeMarrHash(image, alpha, level))
catch { case NonFatal(e) => Left(e) }
/**
* Computes Marr hash value of image
* (http://www.phash.org/docs/pubs/thesis_zauner.pdf / page 22)
* @param image image for hashing
* @param alpha coefficient for correlation kernel
* @param level coefficient for correlation kernel
* @return hash as int array
*/
def unsafeMarrHash(image: BufferedImage, alpha: Int = 2, level: Int = 1): MarrHash =
PHashInternal.unsafeMarrHash(image, alpha, level)
/**
* Computes distance between two Marr hashes
* Less is better
*/
def marrHashDistance(hash1: MarrHash, hash2: MarrHash): Option[Double] = PHashInternal.marrHashDistance(hash1, hash2)
/**
* Computes Radial hash value of image
* (http://www.phash.org/docs/pubs/thesis_zauner.pdf / page 24)
* @param image image for hashing
* @param projectionsCount number of projections to compute
* @return hash as int array or exception
*/
def radialHash(image: BufferedImage, projectionsCount: Int = 180): Either[Throwable, RadialHash] =
try Right(unsafeRadialHash(image, projectionsCount))
catch { case NonFatal(e) => Left(e) }
/**
* Computes Radial hash value of image
* (http://www.phash.org/docs/pubs/thesis_zauner.pdf / page 24)
* @param image image for hashing
* @param projectionsCount number of projections to compute
* @return hash as int array
*/
def unsafeRadialHash(image: BufferedImage, projectionsCount: Int = 180): RadialHash =
PHashInternal.unsafeRadialHash(image, projectionsCount)
/**
* Computes distance between two Radial hashes
* More is better
*/
def radialHashDistance(hash1: RadialHash, hash2: RadialHash): Double = PHashInternal.radialHashDistance(hash1, hash2)
}