Created
January 22, 2015 13:44
-
-
Save mitsuse/5a19d98152414ccef777 to your computer and use it in GitHub Desktop.
Calculate dissimlarities between the source image and target images.
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
package main | |
import ( | |
"fmt" | |
"image" | |
"image/png" | |
"os" | |
"github.com/codegangsta/cli" | |
"github.com/mitsuse/mitsuse-go/image/similarity" | |
) | |
func main() { | |
app := initApp() | |
app.Run(os.Args) | |
} | |
func initApp() *cli.App { | |
app := cli.NewApp() | |
app.Name = "dissim" | |
app.Version = "0.0.1" | |
app.Usage = "Calculate dissimlarities between the source image and target images." | |
app.Flags = []cli.Flag{ | |
cli.StringFlag{ | |
Name: "source,s", | |
Value: "source.png", | |
Usage: "The path of source iamge", | |
}, | |
} | |
app.Action = func(context *cli.Context) { | |
if err := process(context); err != nil { | |
fmt.Fprintf(os.Stderr, "%s: %s\n", app.Name, err) | |
} | |
} | |
return app | |
} | |
func process(context *cli.Context) error { | |
sourceImg, err := decodeFrom(context.String("source")) | |
if err != nil { | |
return err | |
} | |
for _, targetPath := range context.Args() { | |
targetImg, err := decodeFrom(targetPath) | |
if err != nil { | |
return err | |
} | |
d, err := dissim(sourceImg, targetImg) | |
if err != nil { | |
return err | |
} | |
printDissim(targetPath, d) | |
} | |
return nil | |
} | |
func decodeFrom(imgPath string) (image.Image, error) { | |
imgFile, err := os.Open(imgPath) | |
if err != nil { | |
return nil, err | |
} | |
defer imgFile.Close() | |
return png.Decode(imgFile) | |
} | |
func dissim(m, n image.Image) (float64, error) { | |
return similarity.Dissim(m, n, similarity.AbsoluteDissim) | |
} | |
func printDissim(targetPath string, d float64) { | |
fmt.Printf("%s\t%f\n", targetPath, d) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment