@Override public void processImage(FImage image) { // shortcut trivial case if (this.height == 1 && this.width == 1) return; final SummedAreaTable sat = new SummedAreaTable(); sat.analyseImage(image); final int hw = width / 2; final int hh = height / 2; for (int y = 0; y < image.height; y++) { for (int x = 0; x < image.width; x++) { final int sx = Math.max(0, x - hw); final int sy = Math.max(0, y - hh); final int ex = Math.min(image.width, x + hw + 1); final int ey = Math.min(image.height, y + hh + 1); final float mean = sat.calculateArea(sx, sy, ex, ey); image.pixels[y][x] = mean; } } } }
@Override public void analyseImage(FImage image) { computeTable(image); } }
@Override public void processCorrelationMap(FImage img, FImage template, FImage corr) { SummedAreaTable sum = new SummedAreaTable(); img.analyseWith(sum); final float templateMean = FloatArrayStatsUtils.mean(template.pixels); //TODO: cache this final float[][] pix = corr.pixels; for( int y = 0; y < corr.height; y++ ) { for( int x = 0; x < corr.width; x++ ) { double num = pix[y][x]; double t = sum.calculateArea(x, y, x+template.width, y+template.height); num -= t * templateMean; pix[y][x] = (float)num; } } } },
protected void computeSATs(FImage[] magnitudeMaps) { for (int i = 0; i < nbins; i++) { sats[i] = new SummedAreaTable(magnitudeMaps[i]); } }
@Override public void computeHistogram(final int x, final int y, final int w, final int h, final Histogram hist) { final int x2 = x + w; final int y2 = y + h; final double[] values = hist.values; for (int i = 0; i < values.length; i++) { final float val = sats[i].calculateArea(x, y, x2, y2); values[i] = Math.max(0, val); // rounding errors in the SAT // might lead to small -ve's } }
@Override public void processCorrelationMap(FImage img, FImage template, FImage corr) { SummedAreaTable sum = new SummedAreaTable(); img.analyseWith(sum); final float templateMean = FloatArrayStatsUtils.mean(template.pixels); //TODO: cache this final float[][] pix = corr.pixels; for( int y = 0; y < corr.height; y++ ) { for( int x = 0; x < corr.width; x++ ) { double num = pix[y][x]; double t = sum.calculateArea(x, y, x+template.width, y+template.height); num -= t * templateMean; pix[y][x] = (float)num; } } } },
protected void computeSATs(FImage[] magnitudeMaps) { for (int i = 0; i < nbins; i++) { sats[i] = new SummedAreaTable(magnitudeMaps[i]); } }
@Override public void computeHistogram(final int x, final int y, final int w, final int h, final Histogram hist) { final int x2 = x + w; final int y2 = y + h; final double[] values = hist.values; for (int i = 0; i < values.length; i++) { final float val = sats[i].calculateArea(x, y, x2, y2); values[i] = Math.max(0, val); // rounding errors in the SAT // might lead to small -ve's } }
@Override public void processImage(FImage image) { // shortcut trivial case if (this.height == 1 && this.width == 1) return; final SummedAreaTable sat = new SummedAreaTable(); sat.analyseImage(image); final int hw = width / 2; final int hh = height / 2; for (int y = 0; y < image.height; y++) { for (int x = 0; x < image.width; x++) { final int sx = Math.max(0, x - hw); final int sy = Math.max(0, y - hh); final int ex = Math.min(image.width, x + hw + 1); final int ey = Math.min(image.height, y + hh + 1); final float mean = sat.calculateArea(sx, sy, ex, ey); image.pixels[y][x] = mean; } } } }
@Override public void processImage(FImage image) { final SummedAreaTable sat = new SummedAreaTable(image); final FImage output = new FImage(image.width, image.height); sat.calculateArea(xR, yR, xR + hw, yR + hh), sat.calculateArea(xR + hw, yR, xR + widthR, yR + hh), sat.calculateArea(xR, yR + hh, xR + hw, yR + heightR), sat.calculateArea(xR + hw, yR + hh, xR + widthR, yR + heightR), };
/** * Extract the facial keypoints from a canonical image. * The image must contain a face and have the dimensions * specified by {@link #getCanonicalImageDimension()}. * * @param canonicalImage the canonical image containing a detected face. * @return A list of facial keypoints for the image. */ public FacialKeypoint[] extractFacialKeypoints(FImage canonicalImage) { SummedAreaTable sat = new SummedAreaTable(canonicalImage); //run the haar cascade detector for each facial keypoint type FImage [] AC = new FImage[9]; for (int i=0; i<9; i++) { FImage map = MaskedHaarCascade.maskedHaarCascade(sat, model.winsize, model.winsize, model.part[i].HCas, model.part[i].talpha, model.part[i].M); AC[i] = map.multiplyInplace(-(float)model.appwt); } //and then fit the model to find the best keypoints return findParts(AC); }
/** * Calculate the sum of pixels in the image used for constructing this SAT * within the given rectangle * * @param r * rectangle * @return sum of pixels in given rectangle */ public float calculateArea(Rectangle r) { return calculateArea(Math.round(r.x), Math.round(r.y), Math.round(r.x + r.width), Math.round(r.y + r.height)); }
@Override public void analyseImage(FImage image) { computeTable(image); } }
@Override public void processImage(FImage image) { // shortcut trivial case if (this.height == 1 && this.width == 1) return; final SummedAreaTable sat = new SummedAreaTable(); sat.analyseImage(image); final int hw = width / 2; final int hh = height / 2; for (int y = 0; y < image.height; y++) { for (int x = 0; x < image.width; x++) { final int sx = Math.max(0, x - hw); final int sy = Math.max(0, y - hh); final int ex = Math.min(image.width, x + hw + 1); final int ey = Math.min(image.height, y + hh + 1); final int area = (ex - sx) * (ey - sy); final float mean = sat.calculateArea(sx, sy, ex, ey) / area; image.pixels[y][x] = mean; } } } }
@Override public void processImage(FImage image) { final SummedAreaTable sat = new SummedAreaTable(image); final FImage output = new FImage(image.width, image.height); sat.calculateArea(xR, yR, xR + hw, yR + hh), sat.calculateArea(xR + hw, yR, xR + widthR, yR + hh), sat.calculateArea(xR, yR + hh, xR + hw, yR + heightR), sat.calculateArea(xR + hw, yR + hh, xR + widthR, yR + heightR), };
@Override public void processImage(FImage image) { render(image, new SummedAreaTable(image), (int) axis.getBeginPoint().getX(), (int) axis.getBeginPoint().getY(), (int) axis.getEndPoint().getX(), (int) axis.getEndPoint().getY()); }
/** * Calculate the sum of pixels in the image used for constructing this SAT * within the given rectangle * * @param r * rectangle * @return sum of pixels in given rectangle */ public float calculateArea(Rectangle r) { return calculateArea(Math.round(r.x), Math.round(r.y), Math.round(r.x + r.width), Math.round(r.y + r.height)); }
/** * Construct a SAT from the provided image * * @param image * the image */ public SummedAreaTable(FImage image) { computeTable(image); }
@Override public void processImage(FImage image) { // shortcut trivial case if (this.height == 1 && this.width == 1) return; final SummedAreaTable sat = new SummedAreaTable(); sat.analyseImage(image); final int hw = width / 2; final int hh = height / 2; for (int y = 0; y < image.height; y++) { for (int x = 0; x < image.width; x++) { final int sx = Math.max(0, x - hw); final int sy = Math.max(0, y - hh); final int ex = Math.min(image.width, x + hw + 1); final int ey = Math.min(image.height, y + hh + 1); final int area = (ex - sx) * (ey - sy); final float mean = sat.calculateArea(sx, sy, ex, ey) / area; image.pixels[y][x] = mean; } } } }
@Override public void processImage(FImage image) { render(image, new SummedAreaTable(image), (int) axis.getBeginPoint().getX(), (int) axis.getBeginPoint().getY(), (int) axis.getEndPoint().getX(), (int) axis.getEndPoint().getY()); }