Browse Source

Actually moved images, and fixed DHash method to be more consistent and pretty :)

master
Drew Short 11 years ago
parent
commit
cefb9d3f6a
  1. BIN
      src/includes/sample/sample_01_large.jpg
  2. BIN
      src/includes/sample/sample_01_medium.jpg
  3. BIN
      src/includes/sample/sample_01_small.jpg
  4. 38
      src/main/scala/com/sothr/imagetools/hash/DHash.scala
  5. 12
      src/test/scala/com/sothr/imagetools/hash/HashServiceTest.scala

BIN
src/includes/sample/sample_01_large.jpg

Before

Width: 3648  |  Height: 2736  |  Size: 5.0 MiB

BIN
src/includes/sample/sample_01_medium.jpg

Before

Width: 1824  |  Height: 1368  |  Size: 1.5 MiB

BIN
src/includes/sample/sample_01_small.jpg

Before

Width: 912  |  Height: 684  |  Size: 519 KiB

38
src/main/scala/com/sothr/imagetools/hash/DHash.scala

@ -11,17 +11,22 @@ object DHash extends PerceptualHasher with Logging {
val width = imageData.length
val height = imageData(0).length
debug(s"Image data size: ${width}x${height}")
var hash = 0L
for (row <- 0 until width) {
var previousPixel = imageData(row)(0)
var previousLocation = (row, 0)
//calculate dhash
var hash = 0
var previousPixel = imageData(height-1)(width-1)
var previousLocation = (height-1, width-1)
if (height % 2 == 0) {
previousPixel = imageData(height-1)(0)
previousLocation = (height-1, 0)
}
for (row <- 0 until height by 2) {
//process each column
for (col <- 0 until height) {
debug(s"previousPixel: $previousPixel previousLocation: $previousLocation")
//println(f"Column: $col%d")
for (col <- 0 until width by 1) {
hash <<= 1
val pixel = imageData(row)(col)
//debug(s"previousPixel: $previousPixel currentPixel: $pixel previousLocation: $previousLocation currentLocation: (${row},${col})")
//binary or the current bit based on whether the value
//of the current pixel is greater or equal to the previous pixel
if (pixel >= previousPixel) hash |= 1 else hash |= 0
@ -29,7 +34,24 @@ object DHash extends PerceptualHasher with Logging {
previousPixel = pixel
previousLocation = (row, col)
}
if ((row +1) < width) {
val nextRow = row + 1
//process each column
for (col <- (width - 1) to 0 by -1) {
hash <<= 1
val pixel = imageData(nextRow)(col)
//debug(s"previousPixel: $previousPixel currentPixel: $pixel previousLocation: $previousLocation currentLocation: (${nextRow},${col})")
//binary or the current bit based on whether the value
//of the current pixel is greater or equal to the previous pixel
if (pixel >= previousPixel) hash |= 1 else hash |= 0
//debug(s"hash: $hash")
previousPixel = pixel
previousLocation = (nextRow, col)
}
}
}
debug(s"Computed Hash: $hash from ${width * height} pixels")
hash
}
}

12
src/test/scala/com/sothr/imagetools/hash/HashServiceTest.scala

@ -51,8 +51,8 @@ class HashServiceTest extends BaseTest {
val image = ImageIO.read(sample)
debug(s"Image: width: ${image.getWidth} height: ${image.getHeight}")
val hash = HashService.getDhash(image)
debug(s"Testing that $hash = -5198308484644955238L")
assert(hash == -5198308484644955238L)
debug(s"Testing that $hash = -1689609389L")
assert(hash == -1689609389L)
}
test("Calculate DHash Medium Sample Image 1") {
@ -62,8 +62,8 @@ class HashServiceTest extends BaseTest {
val image = ImageIO.read(sample)
debug(s"Image: width: ${image.getWidth} height: ${image.getHeight}")
val hash = HashService.getDhash(image)
debug(s"Testing that $hash = -5198308484644955238L")
assert(hash == -5198308484644955238L)
debug(s"Testing that $hash = -1689609389L")
assert(hash == -1689609389L)
}
test("Calculate DHash Small Sample Image 1") {
@ -73,8 +73,8 @@ class HashServiceTest extends BaseTest {
val image = ImageIO.read(sample)
debug(s"Image: width: ${image.getWidth} height: ${image.getHeight}")
val hash = HashService.getDhash(image)
debug(s"Testing that $hash = -5198299688551933030L")
assert(hash == -5198299688551933030L)
debug(s"Testing that $hash = -1689609389L")
assert(hash == -1689609389L)
}
test("DHash Of Large, Medium, And Small Sample 1 Must Be Similar") {

Loading…
Cancel
Save