IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 24832


Ignore:
Timestamp:
Jul 16, 2009, 5:31:32 PM (17 years ago)
Author:
Paul Price
Message:

Adding function to provide weighted average of covariance matrices.

Location:
trunk/psLib/src/imageops
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/imageops/psImageCovariance.c

    r23882 r24832  
    282282}
    283283
     284psKernel *psImageCovarianceAverageWeighted(const psArray *array, const psVector *weights)
     285{
     286    PS_ASSERT_ARRAY_NON_NULL(array, NULL);
     287    PS_ASSERT_ARRAY_NON_EMPTY(array, NULL);
     288    if (!weights) {
     289        return psImageCovarianceAverage(array);
     290    }
     291    PS_ASSERT_VECTOR_TYPE(weights, PS_TYPE_F32, NULL);
     292
     293    int xMin = INT_MAX, xMax = INT_MIN, yMin = INT_MAX, yMax = INT_MIN; // Range for covariance
     294    double sumWeights = 0.0;            // Sum of weights
     295    for (int i = 0; i < array->n; i++) {
     296        psKernel *covar = array->data[i]; // Covariance matrix
     297        if (!covar) {
     298            continue;
     299        }
     300        xMin = PS_MIN(xMin, covar->xMin);
     301        xMax = PS_MAX(xMax, covar->xMax);
     302        yMin = PS_MIN(yMin, covar->yMin);
     303        yMax = PS_MAX(yMax, covar->yMax);
     304        sumWeights += weights->data.F32[i];
     305    }
     306    if (sumWeights == 0) {
     307        psError(PS_ERR_BAD_PARAMETER_SIZE, true, "No covariance matrices supplied for summation");
     308        return NULL;
     309    }
     310
     311    psKernel *sum = psKernelAlloc(xMin, xMax, yMin, yMax); // Summed covariance
     312    for (int i = 0; i < array->n; i++) {
     313        psKernel *covar = array->data[i]; // Covariance matrix
     314        if (!covar) {
     315            continue;
     316        }
     317        for (int y = covar->yMin; y <= covar->yMax; y++) {
     318            for (int x = covar->xMin; x <= covar->xMax; x++) {
     319                if (!isfinite(covar->kernel[y][x])) {
     320                    psError(PS_ERR_BAD_PARAMETER_VALUE, true,
     321                            "Non-finite covariance matrix element at %d,%d for input %d",
     322                            x, y, i);
     323                    psFree(sum);
     324                    return NULL;
     325                }
     326                sum->kernel[y][x] += weights->data.F32[i] * covar->kernel[y][x];
     327            }
     328        }
     329    }
     330    psBinaryOp(sum->image, sum->image, "/", psScalarAlloc((float)sumWeights, PS_TYPE_F32));
     331
     332    return sum;
     333}
     334
    284335
    285336psKernel *psImageCovarianceTruncate(const psKernel *covar, float frac)
  • trunk/psLib/src/imageops/psImageCovariance.h

    r23882 r24832  
    5757    );
    5858
     59/// Weighted average of multiple covariance pseudo-matrices
     60psKernel *psImageCovarianceAverageWeighted(
     61    const psArray *array,               ///< Array of covariance pseudo-matrices
     62    const psVector *weights             ///< Weights for each (F32)
     63    );
     64
    5965/// Truncate covariance pseudo-matrix
    6066///
Note: See TracChangeset for help on using the changeset viewer.