IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 18, 2010, 2:34:09 PM (16 years ago)
Author:
eugene
Message:

merging changes from PAP branch with modifications to the covariance matrix calculation

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/psLib/src/imageops/psImageCovariance.c

    r27702 r28006  
    1111#include "psMemory.h"
    1212#include "psConstants.h"
     13#include "psImageStructManip.h"
     14#include "psImagePixelManip.h"
    1315#include "psImageConvolve.h"
    1416#include "psTrace.h"
     
    1618#include "psScalar.h"
    1719#include "psThread.h"
     20#include "psImageInterpolate.h"
    1821
    1922#include "psImageCovariance.h"
    2023
    2124static bool threaded = false;           // Run threaded?
    22 
    2325
    2426psKernel *psImageCovarianceNone(void)
     
    530532
    531533
     534psKernel *psImageCovarianceScale(const psKernel *in, float scale)
     535{
     536    // Trivial cases
     537    if (!in) {
     538        psKernel *out = psKernelAlloc(0, 0, 0, 0); // Output covariance
     539        out->kernel[0][0] = 1.0;
     540        return out;
     541    }
     542    PS_ASSERT_KERNEL_NON_NULL(in, NULL);
     543    if (scale == 1.0) {
     544        psImage *copy = psImageCopy(NULL, in->image, PS_TYPE_F32); // Copy of input covariance
     545        psKernel *out = psKernelAllocFromImage(copy, -in->xMin, -in->yMin); // Output covariance
     546        psFree(copy);
     547        return out;
     548    }
     549
     550    int xMinIn = in->xMin, xMaxIn = in->xMax, yMinIn = in->yMin, yMaxIn = in->yMax; // Input size
     551    int xMinOut = (float)xMinIn / scale - 0.5, xMaxOut = (float)xMaxIn / scale + 0.5;     // Output size in x
     552    int yMinOut = (float)yMinIn / scale - 0.5, yMaxOut = (float)yMaxIn / scale + 0.5;     // Output size in y
     553
     554    // Over-fill the covariance matrix so we're not troubled by edge effects
     555    psKernel *overfill = psKernelAlloc(xMinIn - 1, xMaxIn + 1, yMinIn - 1, yMaxIn + 1); // Overfilled covar
     556    psImageInit(overfill->image, 0.0);
     557    int numOverlay = (xMaxIn - xMinIn + 1) * (yMaxIn - yMinIn + 1); // Number of pixels to overlay
     558    if (psImageOverlaySection(overfill->image, in->image, 1, 1, "=") != numOverlay) {
     559        psError(psErrorCodeLast(), false, "Unable to overfill covariance matrix.");
     560        psFree(overfill);
     561        return NULL;
     562    }
     563
     564    psImageInterpolation *interp = psImageInterpolationAlloc(PS_INTERPOLATE_BILINEAR, overfill->image,
     565                                                             NULL, NULL, 0, NAN, NAN, 0xFF, 0xFF,
     566                                                             0.0, 0); // Interpolation
     567    psFree(overfill);
     568
     569    // In transforming the positions, we get +0.5 to account for the centre of the pixels being at 0.5
     570    // and +1 to account for the overfill.
     571
     572    psKernel *out = psKernelAlloc(xMinOut, xMaxOut, yMinOut, yMaxOut); // Output covariance
     573    for (int y = yMinOut; y <= yMaxOut; y++) {
     574        float yIn = y * scale + 0.5 - yMinIn + 1; // Position on input image (not the kernel)
     575        for (int x = xMinOut; x <= xMaxOut; x++) {
     576            float xIn = x * scale + 0.5 - xMinIn + 1; // Position on input (not the kernel)
     577            double value;                                     // Value on output
     578            if (!psImageInterpolate(&value, NULL, NULL, xIn, yIn, interp)) {
     579                psError(psErrorCodeLast(), false, "Unable to interpolate kernel.");
     580                return false;
     581            }
     582            out->kernel[y][x] = value;
     583        }
     584    }
     585
     586    psFree(interp);
     587
     588    return out;
     589}
     590
     591
    532592bool psImageCovarianceSetThreads(bool set)
    533593{
Note: See TracChangeset for help on using the changeset viewer.