IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 21316


Ignore:
Timestamp:
Feb 5, 2009, 10:15:32 AM (17 years ago)
Author:
Paul Price
Message:

Adding function to truncate a covariance matrix. This should help to keep them from growing too large (we're using kernels of half-size ~ 15 pixels for stack, which makes the covariance matrix grow by 60 pixels, which puts a big stress on psImageCovarianceCalculate).

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

Legend:

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

    r21298 r21316  
    55#include <stdio.h>
    66#include <limits.h>
     7#include <string.h>
    78
    89#include "psMemory.h"
     
    172173    return sum;
    173174}
     175
     176
     177psKernel *psImageCovarianceTruncate(const psKernel *covar, float frac)
     178{
     179    PS_ASSERT_KERNEL_NON_NULL(covar, NULL);
     180    PS_ASSERT_FLOAT_WITHIN_RANGE(frac, 0, 1, NULL);
     181
     182    int xMin = covar->xMin, xMax = covar->xMax, yMin = covar->yMin, yMax = covar->yMax; // Range
     183
     184    double sum = 0.0;                   // Sum of covariance
     185    for (int y = yMin; y <= yMax; y++) {
     186        for (int x = xMin; x <= xMax; x++) {
     187            sum += covar->kernel[y][x];
     188        }
     189    }
     190
     191    // Determine radius for truncation
     192    double threshold = (1.0 - frac) * sum; // Threshold for truncation
     193    int maxRadius = PS_MAX(PS_MAX(PS_MAX(xMax, yMax), -xMin), yMin); // Maximum radius of covariance matrix
     194    double enclosed = NAN;              // Enclosed value
     195    int radius;                         // Radius at which to truncate
     196    for (radius = 0; radius <= maxRadius && enclosed < threshold; radius++) {
     197        // Not so efficient to execute, but easy to code; it should be fast anyway
     198        enclosed = 0.0;
     199        for (int y = PS_MAX(yMin, -radius); y <= PS_MIN(yMax, radius); y++) {
     200            for (int x = PS_MAX(xMin, -radius); x <= PS_MIN(xMax, radius); x++) {
     201                enclosed += covar->kernel[y][x];
     202            }
     203        }
     204    }
     205
     206    // Generate truncated version
     207    psKernel *trunc = psKernelAlloc(-radius, radius, -radius, radius); // Truncated covariance matrix
     208    int numBytes = (2 * radius + 1) * PSELEMTYPE_SIZEOF(PS_TYPE_F32); // Number of bytes to copy
     209    for (int y = -radius; y <= radius; y++) {
     210        memcpy(trunc->kernel[y], covar->kernel[y], numBytes);
     211    }
     212
     213    return trunc;
     214}
  • trunk/psLib/src/imageops/psImageCovariance.h

    r21298 r21316  
    55 * @author Paul Price, IfA
    66 *
    7  * @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
    8  * @date $Date: 2009-02-05 00:42:48 $
     7 * @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
     8 * @date $Date: 2009-02-05 20:15:32 $
    99 * Copyright 2009 Institute for Astronomy, University of Hawaii
    1010 */
     
    2121// storage difficult; and if that's not enough, the time to do the calculation is definitely impractical).
    2222// Since there are (generally) lots of zeros in the covariance matrix, and the same basic pattern repeats (for
    23 // background pixels), we can just carry that pattern.  We carry this in a psKernel, since the values are the
    24 // covariance between the pixel of consideration (at 0,0 in the kernel) and neighbouring pixels.  Note that
    25 // this may not be strictly correct near sources, but this is the best we can do (and much better than most
    26 // currently do).
     23// background pixels), we can just carry that pattern ("covariance pseudo-matrix").  We carry this in a
     24// psKernel, since the values are the covariance between the pixel of consideration (at 0,0 in the kernel) and
     25// neighbouring pixels.  Note that this may not be strictly correct near sources, but this is the best we can
     26// do (and much better than most currently do).
    2727
    2828/// Allocate a covariance pseudo-matrix with no covariance
     
    5050    );
    5151
     52/// Truncate covariance pseudo-matrix
     53///
     54/// The covariance pseudo-matrix is truncated by removing the outer regions that contribute less than the
     55/// nominated fraction of the total.
     56psKernel *psImageCovarianceTruncate(
     57    const psKernel *covar,              ///< Covariance pseudo-matrix
     58    float frac                          ///< Fraction of covariance to truncate
     59    );
    5260
    5361/// @}
Note: See TracChangeset for help on using the changeset viewer.