package com.github.axet.lookup.proc;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collections;
import com.github.axet.lookup.Lookup.NotFound;
import com.github.axet.lookup.common.GFirst;
import com.github.axet.lookup.common.GPoint;
import com.github.axet.lookup.common.ImageBinary;
import com.github.axet.lookup.common.ImageBinaryChannel;
import com.github.axet.lookup.common.ImageBinaryRGB;
import com.github.axet.lookup.common.ImageMultiplySum;
* http://www.fmwconcepts.com/imagemagick/similar/index.php
* 2) image1(x,y) - mean1 && image2(x,y) - mean2
* 3) [3] = (image1(x,y) - mean)(x,y) * (image2(x,y) - mean)(x,y)
* 5) [4] / (stddev1 * stddev2)
* Normalized cross correlation algorithm
static public GPoint lookup(BufferedImage i, BufferedImage t, float m) {
List<GPoint> list = lookupAll(i, t, m);
Collections.sort(list, new GFirst());
static public List<GPoint> lookupAll(BufferedImage i, BufferedImage t, float m) {
ImageBinaryRGB imageBinary = new ImageBinaryRGB(i);
ImageBinaryRGB templateBinary = new ImageBinaryRGB(t);
return lookupAll(imageBinary, templateBinary, m);
static public GPoint lookup(ImageBinary image, ImageBinary template, float m) {
List<GPoint> list = lookupAll(image, template, m);
Collections.sort(list, new GFirst());
static public List<GPoint> lookupAll(ImageBinary image, ImageBinary template, float m) {
return lookupAll(image, 0, 0, image.getWidth() - 1, image.getHeight() - 1, template, m);
static public GPoint lookup(ImageBinary image, int x1, int y1, int x2, int y2, ImageBinary template, float m) {
List<GPoint> list = lookupAll(image, x1, y1, x2, y2, template, m);
Collections.sort(list, new GFirst());
static public List<GPoint> lookupAll(ImageBinary image, int x1, int y1, int x2, int y2, ImageBinary template,
List<GPoint> list = new ArrayList<GPoint>();
for (int x = x1; x <= x2 - template.getWidth() + 1; x++) {
for (int y = y1; y <= y2 - template.getHeight() + 1; y++) {
GPoint g = lookup(image, template, x, y, m);
static double numerator(ImageBinaryChannel image, ImageBinaryChannel template, int xx, int yy) {
ImageMultiplySum m = new ImageMultiplySum(image.zeroMean, xx, yy, template.zeroMean);
static double denominator(ImageBinaryChannel image, ImageBinaryChannel template, int xx, int yy) {
double di = image.dev2n(xx, yy, xx + template.getWidth() - 1, yy + template.getHeight() - 1);
double dt = template.dev2n();
return Math.sqrt(di * dt);
static public GPoint lookup(ImageBinary image, ImageBinary template, int x, int y, float m) {
List<ImageBinaryChannel> ci = image.getChannels();
List<ImageBinaryChannel> ct = template.getChannels();
int ii = Math.min(ci.size(), ct.size());
double g = Double.MAX_VALUE;
for (int i = 0; i < ii; i++) {
double gg = gamma(ci.get(i), ct.get(i), x, y);
return new GPoint(x, y, g);
static public double gamma(ImageBinary image, ImageBinary template, int x, int y) {
List<ImageBinaryChannel> ci = image.getChannels();
List<ImageBinaryChannel> ct = template.getChannels();
int ii = Math.min(ci.size(), ct.size());
for (int i = 0; i < ii; i++) {
g += gamma(ci.get(i), ct.get(i), x, y);
static public double gammaMin(ImageBinary image, ImageBinary template, int x, int y) {
List<ImageBinaryChannel> ci = image.getChannels();
List<ImageBinaryChannel> ct = template.getChannels();
int ii = Math.min(ci.size(), ct.size());
double g = Double.MAX_VALUE;
for (int i = 0; i < ii; i++) {
g = Math.min(g, gamma(ci.get(i), ct.get(i), x, y));
static public double gamma(ImageBinaryChannel image, ImageBinaryChannel template, int xx, int yy) {
double d = denominator(image, template, xx, yy);
double n = numerator(image, template, xx, yy);