GithubHelp home page GithubHelp logo

poslegm / scala-phash Goto Github PK

View Code? Open in Web Editor NEW
20.0 1.0 3.0 7.61 MB

Image comparison by hash codes

Home Page: https://chugunkov.dev/scala-phash-demo

License: MIT License

Scala 100.00%
scala images image-comparison phash hashing perceptual-hashing

scala-phash's Introduction

Scala pHash

Build Status Maven

Scala fork of pHash library. This library identifies whether images are similar. You can try it at demo page.

Original pHash uses CImg library for image processing but I could not find CImg for jvm. Therefore I use java.awt and self-made functions for image processing. Consequently, results of my library is different from original phash.

How to use

My library implements three Perceptual Hashing algorithms: Radial Hash, DCT hash and Marr hash. More info about it.

sbt dependencies

libraryDependencies += "com.github.poslegm" %% "scala-phash" % "1.2.2"

API

There is three functions for each hashing algorithm. Let's consider them by example of DCT hash:

  • def dctHash(image: BufferedImage): Either[Throwable, DCTHash] ― compute image's hash;
  • def unsafeDctHash(image: BufferedImage): DCTHash ― compute image's hash unsafely (danger of exception);
  • def dctHashDistance(hash1: DCTHash, hash2: DCTHash): Long ― compare hashes of two images.

Similar functions written for Marr and Radial Hash algorithms.

All public api with scaladocs decsribed in object PHash.

Example

import scalaphash.PHash._
import javax.imageio.ImageIO

val img1 = ImageIO.read(new File("img1.jpg"))
val img2 = ImageIO.read(new File("img2.jpg"))

val radialDistance: Either[Throwable, Double] = for {
  img1rad <- radialHash(img1)
  img2rad <- radialHash(img2)
} yield radialHashDistance(img1rad, img2rad)

radialDistance.foreach {
  case distance if distance > 0.95 => println("similar")
  case _ => println("not similar")
}

radialDistance.left.foreach(e => println(e.getMessage))

Radial distance is more when images are similar. DCT and Marr distances are less when images are similar. Recommended to make a decision on image similarity when at least two hashes pass thresholds.

radial: 0.9508017124330319
dct: 13
marr: 0.5052083333333334

radial: 0.3996241672331173
dct: 41
marr: 0.4704861111111111

Warning

My results is not compatible with original pHash. Use original library if you have an opportunity.
Also, it works much slower than c++ version (about 5-7 times).

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.