Created
August 3, 2012 14:00
-
-
Save shinobu-aoki/3247940 to your computer and use it in GitHub Desktop.
Average HashをScalaで書いてみた
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.image.{BufferedImage => BI} | |
import java.io.File | |
import javax.imageio.ImageIO | |
object avhash extends App { | |
val (w, h) = (16, 16) | |
def toHash(originFile: File) = { | |
def toGray(originFile: File) = { | |
val newImage = new BI(w, h, BI.TYPE_BYTE_GRAY) | |
val g = newImage.createGraphics() | |
try { | |
g.drawImage(ImageIO.read(originFile), 0, 0, w, h, null) | |
newImage | |
} finally { | |
g.dispose | |
} | |
} | |
def pixels = (0 until h) flatMap (y => (0 until w) map ((_, y))) | |
val gray = toGray(originFile) | |
val raster = gray.getData | |
val avg = pixels.foldLeft(0.0)((d, p) => d + raster.getSampleDouble(p._1, p._2, 0)) / (w * h) | |
pixels.map(p => raster.getSampleDouble(p._1, p._2, 0) >= avg) | |
} | |
val bits = toHash(new File(args(0))) | |
bits.map(b => if (b) "1" else "0").grouped(w).foreach(l => println(l.mkString)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
以下のサイトを参考にしました。 | |
簡単な画像の類似度計算手法「Average Hash」 | |
http://www.toyamaguchi.com/blog/archives/20120731_average_hash/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment