Skip to content

Commit

Permalink
Merge pull request #30 from dannguyen/master
Browse files Browse the repository at this point in the history
enable custom & default hamming distance speicification via Image#distance_from
  • Loading branch information
westonplatter committed Jan 6, 2014
2 parents 19ceba8 + a721dda commit 54ea1cd
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 19 deletions.
79 changes: 66 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
Phashion
========

Phashion is a Ruby wrapper around the pHash library, "perceptual hash", which
detects duplicate and near duplicate multimedia files (images, audio, video).
The wrapper currently only supports images.
Phashion is a Ruby wrapper around the [pHash library](http://phash.org/), "perceptual hash", which detects duplicate and near-duplicate multimedia files (e.g. images, audio, video, though Phashion currently only supports images.). "Near-duplicates" are images that come from the same source and show essentially the same thing, but may have differences in such features as dimensions, bytesizes, lossy-compression artifacts, and color levels.

[See an overview of Phashion on Mike's blog]
(http://www.mikeperham.com/2010/05/21/detecting-duplicate-images-with-phashion/).
Expand All @@ -20,6 +18,8 @@ wrapps these dependencies into a custom tarball that is built locally just
for this gem so you don't have to do anything special. Look in the
`ext/phashion_ext` folder for more details.


### Compatibility
Because of this complexity, it is possible the gem install will fail on your
platform. Phashion has been tested on:

Expand All @@ -28,31 +28,81 @@ platform. Phashion has been tested on:
* Ubuntu 8.04
* Ubuntu 12.04

Please open a [GitHub issue](https://github.com/westonplatter/phashion/issues/)
if you have installation problems.
Please open a [GitHub issue](https://github.com/westonplatter/phashion/issues/) if you have installation problems.

### Prerequisites

- `libpng`
- `libjpeg`
- [imagemagick](http://www.imagemagick.org/)

If you're on a Mac, you can use:

- [Homebrew](http://brew.sh/), e.g. `brew install libjpeg`
- or [Macports](http://www.macports.org/), e.g. `port install jpeg` .

Common Error: library not found for -ljpeg
------------------------------------------

If you have an error upon install, like:
#### Common Errors

ld: library not found for -ljpeg

you need to install libjpeg. If you're on a Mac, you can use
[homebrew](http://brew.sh/) `brew install libjpeg` or
[ports](http://www.macports.org/) `port install jpeg` .

- `ld: library not found for -ljpeg` – You need to install `libjpeg` if you run into this error upon `gem install`:


- `.....sh: convert: command not found; sh: gm: command not found` – You need to install [imagemagick](http://www.imagemagick.org/).


Usage
-----

### Testing if one image is a duplicate of another

require 'phashion'
img1 = Phashion::Image.new(filename1)
img2 = Phashion::Image.new(filename2)
img1.duplicate?(img2)
--> true

Optionally, you can set the minimum Hamming distance in the second argument, an options Hash:

img1.duplicate?(img2, :threshold => 5)
--> true

img1.duplicate?(img2, :threshold => 0)
--> false


### Finding the Hamming distance between two images

require 'phashion'
img1 = Phashion::Image.new(filename1)
img2 = Phashion::Image.new(filename2)
img1.distance_from(img2)
--> 6

### Threshold for dupe-detection

Currently, the maximum Hamming distance between two duplicate images is set at 15. As per [mperham's explanation](http://www.mikeperham.com/2010/05/21/detecting-duplicate-images-with-phashion/):

> A “perceptual hash” is a 64-bit value based on the discrete cosine transform of the image’s frequency spectrum data. Similar images will have hashes that are close in terms of Hamming distance. That is, a binary hash value of 1000 is closer to 0000 than 0011 because it only has one bit different whereas the latter value has two bits different. The duplicate threshold defines how many bits must be different between two hashes for the two associated images to be considered different images. Our testing showed that 15 bits is a good value to start with, it detected all duplicates with a minimum of false positives.
As a reference point, here are the Hamming distances in these test comparisons using [/test/jpg/Broccoli_Super_Food.jpg](https://github.com/westonplatter/phashion/blob/master/test/jpg/Broccoli_Super_Food.jpg) as the source image:


| Variation | Hamming distance
| ---------------------------------------------------- | ----------------:
| JPG to PNG | 0
| Lossy JPG (Photoshop Save for Web quality = 20) | 0
| Thumbnail (from 500px to 100px) | 2
| Color correction (saturation +20 w auto-correct) | 2
| Black and white | 2
| Extraneous whitespace cropped (500x349 to 466x312) | 12
| A sloppy rotation of 5 degrees clockwise | 14
| Horizontally-flipped | 32





Gem uses customized pHash 0.9.6
-------------------------------
Expand All @@ -63,10 +113,13 @@ In order to detech duplicate alpha PNGs, the gem uses a custom version of pHash
(https://github.com/westonplatter/phash/commit/ff255d2d3f93c841b98923ecbde997027f21ae36).
The gem will be moving back to the pHash master branch once it supports
detection of alpha PNG file types.


Testing
-------

To run the test suite:

#### To run the test suite:

bundle
rake compile
Expand Down
17 changes: 11 additions & 6 deletions lib/phashion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@ module Phashion
VERSION = '1.0.8'

class Image
SETTINGS = {
:dupe_threshold => 15
}

DEFAULT_DUPE_THRESHOLD = 15

attr_reader :filename
def initialize(filename)
@filename = filename
end

def duplicate?(other)
Phashion.hamming_distance(fingerprint, other.fingerprint) < SETTINGS[:dupe_threshold]
# returns: an Integer representing the hamming distance from :other
def distance_from(other)
Phashion.hamming_distance(fingerprint, other.fingerprint)
end

def duplicate?(other, opts={})
threshold = opts[:threshold] || DEFAULT_DUPE_THRESHOLD

distance_from(other) <= threshold
end

def fingerprint
Expand Down
Binary file added test/jpg/Broccoli_Super_Food.100px.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/jpg/Broccoli_Super_Food.bounding-box.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/jpg/Broccoli_Super_Food.bw.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/jpg/Broccoli_Super_Food.color-corrected.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/jpg/Broccoli_Super_Food.horizontal-flip.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/jpg/Broccoli_Super_Food.lossy.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/jpg/Broccoli_Super_Food.rotate5cw.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 75 additions & 0 deletions test/test_phashion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def test_duplicate_detection
assert_duplicate images[0], images[2]
end



def test_duplicate_detection_2
files = %w(b32aade8c590e2d776c24f35868f0c7a588f51e1.jpeg df9cc82f5b32d7463f36620c61854fde9d939f7f.jpeg e7397898a7e395c2524978a5e64de0efabf08290.jpeg)
images = files.map {|f| Phashion::Image.new("#{File.dirname(__FILE__) + '/../test/jpg/'}#{f}")}
Expand Down Expand Up @@ -98,8 +100,81 @@ def test_fingerprint_png_is_different
assert fingerprints.uniq.size == 3, "array should contain 3 unique fingerprints"
end


def test_duplicate_with_custom_distance_threshold
# note: this test depends on the smaller_jpg test still asserting a distance of 2
# note-2: threshold is a :less-than-or-equal-to comparison, which is a change from version 1.0.8
jpg = Phashion::Image.new(File.dirname(__FILE__) + '/jpg/Broccoli_Super_Food.jpg')
jpg_x = Phashion::Image.new(File.dirname(__FILE__) + '/jpg/Broccoli_Super_Food.100px.jpg')

refute(jpg.duplicate?(jpg_x, threshold: 1))
assert(jpg.duplicate?(jpg_x, threshold: 2))
end


### distance methods
def test_distance_from_jpg_to_png_dupe
jpg = Phashion::Image.new(File.dirname(__FILE__) + '/jpg/Broccoli_Super_Food.jpg')
png = Phashion::Image.new(File.dirname(__FILE__) + '/png/Broccoli_Super_Food.png')

assert_equal(jpg.distance_from(png), 0)
end

def test_distance_from_lossy_jpg
jpg = Phashion::Image.new(File.dirname(__FILE__) + '/jpg/Broccoli_Super_Food.jpg')
jpg_x = Phashion::Image.new(File.dirname(__FILE__) + '/jpg/Broccoli_Super_Food.lossy.jpg')

assert_equal(jpg.distance_from(jpg_x), 0)
end

def test_distance_from_smaller_jpg
jpg = Phashion::Image.new(File.dirname(__FILE__) + '/jpg/Broccoli_Super_Food.jpg')
jpg_x = Phashion::Image.new(File.dirname(__FILE__) + '/jpg/Broccoli_Super_Food.100px.jpg')

assert_equal(jpg.distance_from(jpg_x), 2)
end

def test_distance_from_color_correction
jpg = Phashion::Image.new(File.dirname(__FILE__) + '/jpg/Broccoli_Super_Food.jpg')
jpg_x = Phashion::Image.new(File.dirname(__FILE__) + '/jpg/Broccoli_Super_Food.color-corrected.jpg')

assert_equal(jpg.distance_from(jpg_x), 2)
end

def test_distance_from_black_and_white
jpg = Phashion::Image.new(File.dirname(__FILE__) + '/jpg/Broccoli_Super_Food.jpg')
jpg_x = Phashion::Image.new(File.dirname(__FILE__) + '/jpg/Broccoli_Super_Food.bw.jpg')

assert_equal(jpg.distance_from(jpg_x), 2)
end

def test_distance_from_bounding_box
# Control-image is cropped to remove empty whitespace around image details
# from 500x349 to 466x312
jpg = Phashion::Image.new(File.dirname(__FILE__) + '/jpg/Broccoli_Super_Food.jpg')
jpg_x = Phashion::Image.new(File.dirname(__FILE__) + '/jpg/Broccoli_Super_Food.bounding-box.jpg')

assert_equal(jpg.distance_from(jpg_x), 12)
end

def test_distance_from_rotation_of_5degrees_c2
jpg = Phashion::Image.new(File.dirname(__FILE__) + '/jpg/Broccoli_Super_Food.jpg')
jpg_x = Phashion::Image.new(File.dirname(__FILE__) + '/jpg/Broccoli_Super_Food.rotate5cw.jpg')

assert_equal(jpg.distance_from(jpg_x), 14)
end


def test_distance_from_horizontal_flip
jpg = Phashion::Image.new(File.dirname(__FILE__) + '/jpg/Broccoli_Super_Food.jpg')
jpg_x = Phashion::Image.new(File.dirname(__FILE__) + '/jpg/Broccoli_Super_Food.horizontal-flip.jpg')

assert_operator(jpg.distance_from(jpg_x), :>, Phashion::Image::DEFAULT_DUPE_THRESHOLD)
end

private


def assert_duplicate(a, b)
assert a.duplicate?(b), "#{a.filename} not dupe of #{b.filename}"
end
Expand Down

0 comments on commit 54ea1cd

Please sign in to comment.