Java Array Average averageX(long[][] intImg, int width, int height, int i, int j, int dl, int dr)

Here you can find the source of averageX(long[][] intImg, int width, int height, int i, int j, int dl, int dr)

Description

Compute the average x coordinate of nonzero pixels

License

Open Source License

Parameter

Parameter Description
intImg the integral image
width the width of the picture
height the height of the picture
i the x coordinate
j the y coordinate
dl left offset
dr right offset

Return

the sum

Declaration

public static final int averageX(long[][] intImg, int width, int height, int i, int j, int dl, int dr) 

Method Source Code

//package com.java2s;
/* ImageUtil.java/*from  w  ww .  j a v  a 2 s .  co  m*/
 * =========================================================================
 * This file is originally part of the MathOCR Project
 *
 * Copyright (C) 2015 Chan Chung Kwong
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 */

public class Main {
    /**
     * Compute the average x coordinate of nonzero pixels
     * @param intImg the integral image
     * @param width the width of the picture
     * @param height the height of the picture
     * @param i the x coordinate
     * @param j the y coordinate
     * @param dl left offset
     * @param dr right offset
     * @return the sum
     */
    public static final int averageX(long[][] intImg, int width, int height, int i, int j, int dl, int dr) {
        int count = 0, sum = 0, xstart = Math.max(0, j - dl), xend = Math.min(width - 1, j + dr),
                ystart = Math.max(-1, i - dl), yend = Math.min(height - 1, i + dr);
        for (int k = xstart; k < xend; k++) {
            int tmp = (int) (intImg[yend][k] - (ystart != -1 ? intImg[ystart][k] : 0)
                    + (k != 0 ? -intImg[yend][k - 1] + (ystart != -1 ? intImg[ystart][k - 1] : 0) : 0));
            count += tmp;
            sum += tmp * k;
        }
        return count == 0 ? Integer.MAX_VALUE : sum / count;
    }
}

Related

  1. average4RGBA(int var0, int var1, int var2, int var3)
  2. averageChroma(float[] chromaSums, int countChromas)
  3. averageLuminance(double[] luminance)
  4. averageNonZeros(int[] array)
  5. averagePt(double[][] ndPoints)
  6. avg(byte[] values)
  7. avg(double a, double b)
  8. avg(double v1, double v2)
  9. avg(double values[], int size, boolean ignoreNan)