private Histogram[][] computeBlocks(Histogram[][] cells) { final int numBlocksX = 1 + (cells[0].length - cellsPerBlockX) / this.blockStepX; final int numBlocksY = 1 + (cells.length - cellsPerBlockY) / this.blockStepY; final Histogram[][] blocks = new Histogram[numBlocksY][numBlocksX]; for (int y = 0; y < numBlocksY; y++) { for (int x = 0; x < numBlocksX; x++) { final Histogram[] blockData = new Histogram[cellsPerBlockX * cellsPerBlockY]; for (int j = 0, k = 0; j < cellsPerBlockY; j++) { for (int i = 0; i < cellsPerBlockX; i++) { blockData[k++] = cells[y * blockStepY + j][x * blockStepX + i]; } } blocks[y][x] = new Histogram(blockData); } } return blocks; }
private Histogram[][] computeBlocks(Histogram[][] cells) { final int numBlocksX = 1 + (cells[0].length - cellsPerBlockX) / this.blockStepX; final int numBlocksY = 1 + (cells.length - cellsPerBlockY) / this.blockStepY; final Histogram[][] blocks = new Histogram[numBlocksY][numBlocksX]; for (int y = 0; y < numBlocksY; y++) { for (int x = 0; x < numBlocksX; x++) { final Histogram[] blockData = new Histogram[cellsPerBlockX * cellsPerBlockY]; for (int j = 0, k = 0; j < cellsPerBlockY; j++) { for (int i = 0; i < cellsPerBlockX; i++) { blockData[k++] = cells[y * blockStepY + j][x * blockStepX + i]; } } blocks[y][x] = new Histogram(blockData); } } return blocks; }
@Override public Histogram computeHistogram(int x, int y, int w, int h) { final Histogram hist = new Histogram(nbins); computeHistogram(x, y, w, h, hist); return hist; }
@Override public Histogram computeHistogram(int x, int y, int w, int h, FImage extWeights) { final Histogram hist = new Histogram(nbins); final int starty = Math.max(0, y); final int startx = Math.max(0, x); final int stopy = Math.min(binMap.length, y + h); final int stopx = Math.min(binMap[0].length, x + w); for (int r = starty; r < stopy; r++) { for (int c = startx; c < stopx; c++) { final int bin = binMap[r][c]; hist.values[bin] += (extWeights.pixels[r][c] * weights[r][c]); if (wrap && bin + 1 == nbins) { hist.values[0] += (extWeights.pixels[r][c] * (1 - weights[r][c])); } if (bin + 1 < nbins) { hist.values[bin + 1] += (extWeights.pixels[r][c] * (1 - weights[r][c])); } } } return hist; }
@Override public Histogram computeHistogram(int x, int y, int w, int h) { final Histogram hist = new Histogram(nbins); computeHistogram(x, y, w, h, hist); return hist; }
@Override public Histogram computeHistogram(int x, int y, int w, int h) { final Histogram hist = new Histogram(nbins); final int starty = Math.max(0, y); final int startx = Math.max(0, x); final int stopy = Math.min(binMap.length, y + h); final int stopx = Math.min(binMap[0].length, x + w); for (int r = starty; r < stopy; r++) { for (int c = startx; c < stopx; c++) { final int bin = binMap[r][c]; hist.values[bin] += weights[r][c]; if (wrap && bin + 1 == nbins) { hist.values[0] += (1 - weights[r][c]); } if (bin + 1 < nbins) { hist.values[bin + 1] += (1 - weights[r][c]); } } } return hist; }
@Override public Histogram computeHistogram(int x, int y, int w, int h) { final Histogram hist = new Histogram(nbins); final int starty = Math.max(0, y); final int startx = Math.max(0, x); final int stopy = Math.min(binMap.length, y + h); final int stopx = Math.min(binMap[0].length, x + w); for (int r = starty; r < stopy; r++) { for (int c = startx; c < stopx; c++) { final int bin = binMap[r][c]; hist.values[bin] += weights[r][c]; if (wrap && bin + 1 == nbins) { hist.values[0] += (1 - weights[r][c]); } if (bin + 1 < nbins) { hist.values[bin + 1] += (1 - weights[r][c]); } } } return hist; }
@Override public Histogram computeHistogram(final int x, final int y, final int w, final int h) { final Histogram hist = new Histogram(nbins); final int x2 = x + w; final int y2 = y + h; for (int i = 0; i < nbins; i++) { final float val = sats[i].calculateArea(x, y, x2, y2); // rounding errors in the SAT can lead to small values that should // actually be zero hist.values[i] = val < 1e-4 ? 0 : val; } return hist; }
@Override public Histogram computeHistogram(final int x, final int y, final int w, final int h) { final Histogram hist = new Histogram(nbins); final int x2 = x + w; final int y2 = y + h; for (int i = 0; i < nbins; i++) { final float val = sats[i].calculateArea(x, y, x2, y2); // rounding errors in the SAT can lead to small values that should // actually be zero hist.values[i] = val < 1e-4 ? 0 : val; } return hist; }
@Override public Histogram computeHistogram(int x, int y, int w, int h, FImage extWeights) { final Histogram hist = new Histogram(nbins); final int starty = Math.max(0, y); final int startx = Math.max(0, x); final int stopy = Math.min(binMap.length, y + h); final int stopx = Math.min(binMap[0].length, x + w); for (int r = starty; r < stopy; r++) { for (int c = startx; c < stopx; c++) { final int bin = binMap[r][c]; hist.values[bin] += (extWeights.pixels[r][c] * weights[r][c]); if (wrap && bin + 1 == nbins) { hist.values[0] += (extWeights.pixels[r][c] * (1 - weights[r][c])); } if (bin + 1 < nbins) { hist.values[bin + 1] += (extWeights.pixels[r][c] * (1 - weights[r][c])); } } } return hist; }
/** * Create a new histogram by concatenating this one with the given ones. * * @param hs * histograms to concatenate * @return new histogram that is the concatenation of the argument * histograms */ public Histogram combine(Histogram... hs) { final int hsLength = hs == null ? 0 : hs.length; final double[][] hists = new double[1 + hsLength][]; hists[0] = this.values; for (int i = 0; i < hsLength; i++) { hists[i + 1] = hs[i].values; } return new Histogram(ArrayUtils.concatenate(hists)); } }
/** * Create a new histogram by concatenating this one with the given ones. * * @param hs * histograms to concatenate * @return new histogram that is the concatenation of the argument * histograms */ public Histogram combine(Histogram... hs) { final int hsLength = hs == null ? 0 : hs.length; final double[][] hists = new double[1 + hsLength][]; hists[0] = this.values; for (int i = 0; i < hsLength; i++) { hists[i + 1] = hs[i].values; } return new Histogram(ArrayUtils.concatenate(hists)); } }
@Override public Histogram extract(WindowedHistogramExtractor binnedData, Rectangle region, Histogram output) { final QuadtreeSampler sampler = new QuadtreeSampler(region, nlevels); final int blockSize = binnedData.getNumBins(); final List<Rectangle> rects = sampler.allRectangles(); if (output == null || output.values.length != blockSize * rects.size()) output = new Histogram(blockSize * rects.size()); final Histogram tmp = new Histogram(blockSize); for (int i = 0; i < rects.size(); i++) { final Rectangle r = rects.get(i); binnedData.computeHistogram(r, tmp); System.arraycopy(tmp.values, 0, output.values, blockSize * i, blockSize); } return output; } }
@Override public Histogram extract(WindowedHistogramExtractor binnedData, Rectangle region, Histogram output) { final QuadtreeSampler sampler = new QuadtreeSampler(region, nlevels); final int blockSize = binnedData.getNumBins(); final List<Rectangle> rects = sampler.allRectangles(); if (output == null || output.values.length != blockSize * rects.size()) output = new Histogram(blockSize * rects.size()); final Histogram tmp = new Histogram(blockSize); for (int i = 0; i < rects.size(); i++) { final Rectangle r = rects.get(i); binnedData.computeHistogram(r, tmp); System.arraycopy(tmp.values, 0, output.values, blockSize * i, blockSize); } return output; } }
@Override public Histogram extract(WindowedHistogramExtractor binnedData, Rectangle region, Histogram output) { final float dx = region.width / numBlocksX; final float dy = region.height / numBlocksY; final int blockSize = binnedData.getNumBins(); if (output == null || output.values.length != blockSize * numBlocksX * numBlocksY) output = new Histogram(blockSize * numBlocksX * numBlocksY); final RectangleSampler rs = new RectangleSampler(region, dx, dy, dx, dy); int block = 0; final Histogram tmp = new Histogram(blockSize); for (final Rectangle r : rs) { binnedData.computeHistogram(r, tmp); tmp.normaliseL2(); System.arraycopy(tmp.values, 0, output.values, blockSize * block, blockSize); block++; } return output; } }
@Override public Histogram extract(WindowedHistogramExtractor binnedData, Rectangle region, Histogram output) { final float dx = region.width / numBlocksX; final float dy = region.height / numBlocksY; final int blockSize = binnedData.getNumBins(); if (output == null || output.values.length != blockSize * numBlocksX * numBlocksY) output = new Histogram(blockSize * numBlocksX * numBlocksY); final RectangleSampler rs = new RectangleSampler(region, dx, dy, dx, dy); int block = 0; final Histogram tmp = new Histogram(blockSize); for (final Rectangle r : rs) { binnedData.computeHistogram(r, tmp); tmp.normaliseL2(); System.arraycopy(tmp.values, 0, output.values, blockSize * block, blockSize); block++; } return output; } }
@Override public Histogram extract(WindowedHistogramExtractor binnedData, Rectangle region, Histogram output) { final Histogram[][] cells = computeCells(binnedData, region); final Histogram[][] blocks = computeBlocks(cells); final int blockSize = blocks[0][0].values.length; final int blockArea = cellsPerBlockX * cellsPerBlockY; if (output == null || output.values.length != blocks[0].length * blocks.length * blockSize) output = new Histogram(blocks[0].length * blocks.length * blockSize); for (int j = 0, k = 0; j < blocks.length; j++) { for (int i = 0; i < blocks[0].length; i++, k++) { norm.normalise(blocks[j][i], blockArea); System.arraycopy(blocks[j][i].values, 0, output.values, k * blockSize, blockSize); } } return output; }
@Override public Histogram extract(WindowedHistogramExtractor binnedData, Rectangle region, Histogram output) { final Histogram[][] cells = computeCells(binnedData, region); final Histogram[][] blocks = computeBlocks(cells); final int blockSize = blocks[0][0].values.length; final int blockArea = cellsPerBlockX * cellsPerBlockY; if (output == null || output.values.length != blocks[0].length * blocks.length * blockSize) output = new Histogram(blocks[0].length * blocks.length * blockSize); for (int j = 0, k = 0; j < blocks.length; j++) { for (int i = 0; i < blocks[0].length; i++, k++) { norm.normalise(blocks[j][i], blockArea); System.arraycopy(blocks[j][i].values, 0, output.values, k * blockSize, blockSize); } } return output; }
/** * Extract the PHOG feature for the specified region of the image last * analysed with {@link #analyseImage(FImage)}. * * @param rect * the region * @return the PHOG feature */ public Histogram getFeatureVector(Rectangle rect) { final QuadtreeSampler sampler = new QuadtreeSampler(rect, nlevels + 1); Histogram hist = new Histogram(0); for (final Rectangle r : sampler) { final Histogram h = histExtractor.computeHistogram(r, magnitudes); hist = hist.combine(h); } hist.normaliseL1(); return hist; }
/** * Extract the PHOG feature for the specified region of the image last * analysed with {@link #analyseImage(FImage)}. * * @param rect * the region * @return the PHOG feature */ public Histogram getFeatureVector(Rectangle rect) { final QuadtreeSampler sampler = new QuadtreeSampler(rect, nlevels + 1); Histogram hist = new Histogram(0); for (final Rectangle r : sampler) { final Histogram h = histExtractor.computeHistogram(r, magnitudes); hist = hist.combine(h); } hist.normaliseL1(); return hist; }