IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 30, 2007, 11:45:26 AM (19 years ago)
Author:
Paul Price
Message:

Cleaning up, consolidating some duplicated code. Fixing photometric preservation for optimum kernels.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psModules/src/imcombine/pmSubtractionParams.c

    r14671 r14709  
    99
    1010#include "pmSubtractionStamps.h"
     11#include "pmSubtraction.h"
     12#include "pmSubtractionParams.h"
    1113
    1214//////////////////////////////////////////////////////////////////////////////////////////////////////////////
    1315
     16#if 0
    1417// Convolve the reference stamp by the kernel
    1518static psKernel *convolveStamp(const pmSubtractionStamp *stamp, // Stamp to be convolved
     
    4548    return convolution;
    4649}
    47 
     50#endif
    4851
    4952// Accumulate cross-term sums for a stamp
     
    5356                            const pmSubtractionStamp *stamp, // Stamp with weight
    5457                            const psKernel *input, // Input image, I(x)
    55                             const psKernel *convolution // Convolution, conv(x)
    56     )
    57 {
    58     // Range of convolution
    59     int xMin = convolution->xMin;
    60     int xMax = convolution->xMax;
    61     int yMin = convolution->yMin;
    62     int yMax = convolution->yMax;
    63 
     58                            int kernelIndex, // Index for kernel component
     59                            int footprint // Size of region of interest
     60    )
     61{
    6462    psKernel *weight = stamp->weight;   // Weight, sigma(x)^2
    65 
    66     for (int y = yMin; y <= yMax; y++) {
    67         psF32 *in = &input->kernel[y][xMin]; // Dereference input
    68         psF32 *wt = &weight->kernel[y][xMin]; // Dereference weight
    69         psF32 *conv = &convolution->kernel[y][xMin]; // Dereference convolution
    70         for (int x = xMin; x <= xMax; x++, in++, wt++, conv++) {
     63    psKernel *convolution = stamp->convolutions->data[kernelIndex]; // Convolution of interest
     64
     65    for (int y = -footprint; y <= footprint; y++) {
     66        psF32 *in = &input->kernel[y][-footprint]; // Dereference input
     67        psF32 *wt = &weight->kernel[y][-footprint]; // Dereference weight
     68        psF32 *conv = &convolution->kernel[y][-footprint]; // Dereference convolution
     69        for (int x = -footprint; x <= footprint; x++, in++, wt++, conv++) {
    7170            double temp = *in / *wt; // Temporary product
    7271            *sumI += temp;
     
    8281                                   double *sumCC, // Sum of conv(x)^2/sigma(x)^2
    8382                                   const pmSubtractionStamp *stamp, // Stamp with input and weight
    84                                    const psKernel *convolution // Convolution, conv(x)
    85     )
    86 {
    87     // Range of convolution
    88     int xMin = convolution->xMin;
    89     int xMax = convolution->xMax;
    90     int yMin = convolution->yMin;
    91     int yMax = convolution->yMax;
    92 
     83                                   int kernelIndex, // Index for kernel component
     84                                   int footprint // Size of region of interest
     85    )
     86{
    9387    psKernel *weight = stamp->weight;   // Weight, sigma(x)^2
    94 
    95     for (int y = yMin; y <= yMax; y++) {
    96         psF32 *wt = &weight->kernel[y][xMin]; // Dereference weight
    97         psF32 *conv = &convolution->kernel[y][xMin]; // Dereference convolution
    98         for (int x = xMin; x <= xMax; x++, wt++, conv++) {
     88    psKernel *convolution = stamp->convolutions->data[kernelIndex]; // Convolution of interest
     89
     90    for (int y = -footprint; y <= footprint; y++) {
     91        psF32 *wt = &weight->kernel[y][-footprint]; // Dereference weight
     92        psF32 *conv = &convolution->kernel[y][-footprint]; // Dereference convolution
     93        for (int x = -footprint; x <= footprint; x++, wt++, conv++) {
    9994            double convNoise = *conv / *wt; // Temporary product
    10095            *sumC += convNoise;
     
    106101
    107102static double accumulateChi2(psKernel *input, // Input stamp
    108                              psKernel *convolution, // Convolution, R(x)*k(u)
    109103                             pmSubtractionStamp *stamp, // Stamp with weight
     104                             int kernelIndex, // Index for kernel component
    110105                             double coeff, // Coefficient of convolution
    111                              double bg  // Background term
    112     )
    113 {
    114     // Range of convolution
    115     int xMin = convolution->xMin;
    116     int xMax = convolution->xMax;
    117     int yMin = convolution->yMin;
    118     int yMax = convolution->yMax;
    119 
     106                             double bg,  // Background term
     107                             int footprint // Size of region of interest
     108    )
     109{
    120110    double chi2 = 0.0;
    121     psKernel *weight = stamp->weight;
    122 
    123     for (int y = yMin; y <= yMax; y++) {
    124         psF32 *in = &input->kernel[y][xMin]; // Dereference input
    125         psF32 *wt = &weight->kernel[y][xMin]; // Dereference weight
    126         psF32 *conv = &convolution->kernel[y][xMin]; // Dereference convolution
    127         for (int x = xMin; x <= xMax; x++, in++, wt++, conv++) {
     111    psKernel *weight = stamp->weight;   // Weight, sigma(x)^2
     112    psKernel *convolution = stamp->convolutions->data[kernelIndex]; // Convolution of interest
     113
     114    for (int y = -footprint; y <= footprint; y++) {
     115        psF32 *in = &input->kernel[y][-footprint]; // Dereference input
     116        psF32 *wt = &weight->kernel[y][-footprint]; // Dereference weight
     117        psF32 *conv = &convolution->kernel[y][-footprint]; // Dereference convolution
     118        for (int x = -footprint; x <= footprint; x++, in++, wt++, conv++) {
    128119            chi2 += PS_SQR(*in - bg - coeff * *conv) / *wt;
    129120        }
     
    159150static void subtractConvolution(psKernel *input, // Input stamp
    160151                                const pmSubtractionStamp *stamp, // Stamp with weight
    161                                 const psKernel *convolution, // Convolution, R(x)*k(u)
     152                                int kernelIndex, // Index for kernel component
    162153                                float coeff, // Coefficient of subtraction
    163                                 float bg // Background term
    164     )
    165 {
    166     // Range of convolution
    167     int xMin = convolution->xMin;
    168     int xMax = convolution->xMax;
    169     int yMin = convolution->yMin;
    170     int yMax = convolution->yMax;
    171 
    172     psKernel *weight = stamp->weight;   // Weight map
    173 
    174     for (int y = yMin; y <= yMax; y++) {
    175         psF32 *in = &input->kernel[y][xMin]; // Dereference input
    176         psF32 *conv = &convolution->kernel[y][xMin]; // Dereference convolution
    177         psF32 *wt = &weight->kernel[y][xMin]; // Dereference weight
    178         for (int x = xMin; x <= xMax; x++, in++, conv++, wt++) {
     154                                float bg, // Background term
     155                                int footprint // Size of region of interest
     156    )
     157{
     158    psKernel *convolution = stamp->convolutions->data[kernelIndex]; // Convolution of interest
     159
     160    for (int y = -footprint; y <= footprint; y++) {
     161        psF32 *in = &input->kernel[y][-footprint]; // Dereference input
     162        psF32 *conv = &convolution->kernel[y][-footprint]; // Dereference convolution
     163        for (int x = -footprint; x <= footprint; x++, in++, conv++) {
    179164            *in -= *conv * coeff + bg;
    180165        }
     
    229214        psKernel *input = stamp->input; // Input image of interest
    230215        psImage *copy = psImageCopy(NULL, input->image, PS_TYPE_F32); // Copy of the image
    231         inputs->data[i] = psKernelAllocFromImage(copy, size + footprint + 1, size + footprint + 1);
     216        inputs->data[i] = psKernelAllocFromImage(copy, size + footprint, size + footprint);
    232217        psFree(copy);                   // Drop reference
    233218    }
    234219
    235220    // Generate the convolutions
    236     psArray *convolutions = psArrayAlloc(numKernels); // Convolutions per kernel component and stamp
    237     for (int i = 0; i < numKernels; i++) {
    238         psKernel *kernel = kernels->preCalc->data[i]; // Precalculated kernel
    239 
    240         psArray *stampConv = psArrayAlloc(numStamps); // Convolutions for each stamp
    241         convolutions->data[i] = stampConv;
    242 
    243         for (int j = 0; j < numStamps; j++) {
    244             stampConv->data[j] = convolveStamp(stamps->data[j], kernel, footprint);
     221    for (int i = 0; i < numStamps; i++) {
     222        pmSubtractionStamp *stamp = stamps->data[i]; // Stamp of interest
     223        if (!stamp->convolutions) {
     224            stamp->convolutions = psArrayAlloc(numKernels);
     225        }
     226
     227        for (int j = 0; j < numKernels; j++) {
     228            psKernel *kernel = kernels->preCalc->data[j]; // Precalculated kernel
     229            stamp->convolutions->data[j] = p_pmSubtractionConvolveStampPrecalc(stamp->reference, kernel);
    245230        }
    246231    }
     
    264249    psVectorInit(sumCC, 0.0);
    265250    for (int i = 0; i < numKernels; i++) {
    266         psArray *stampsConv = convolutions->data[i]; // Convolutions per stamp
    267251        for (int j = 0; j < numStamps; j++) {
    268             accumulateConvolutions(&sumC->data.F64[i], &sumCC->data.F64[i], stamps->data[j],
    269                                    stampsConv->data[j]);
     252            accumulateConvolutions(&sumC->data.F64[i], &sumCC->data.F64[i], stamps->data[j], i, footprint);
    270253        }
    271254    }
     
    298281            double sumIC = 0.0;         // sum of I(x)C(x)/sigma(x)^2
    299282
    300             psArray *stampsConv = convolutions->data[i]; // Convolutions per stamp
    301 
    302283            for (int j = 0; j < numStamps; j++) {
    303                 accumulateCross(&sumI, &sumII, &sumIC, stamps->data[j], inputs->data[j], stampsConv->data[j]);
     284                accumulateCross(&sumI, &sumII, &sumIC, stamps->data[j], inputs->data[j], i, footprint);
    304285            }
    305286
     
    310291            double chi2 = 0.0;          // Chi^2
    311292            for (int j = 0; j < numStamps; j++) {
    312                 chi2 += accumulateChi2(inputs->data[j], stampsConv->data[j], stamps->data[j], coeff, bg);
     293                chi2 += accumulateChi2(inputs->data[j], stamps->data[j], i, coeff, bg, footprint);
    313294            }
    314295
     
    329310        ranking->data.S32[bestIndex] = iter;
    330311        // Remove its contribution, and don't include it in the future.
    331         psArray *stampsConv = convolutions->data[bestIndex]; // Convolutions per stamp
    332312        for (int j = 0; j < numStamps; j++) {
    333             subtractConvolution(inputs->data[j], stamps->data[j], stampsConv->data[j],
    334                                            bestCoeff, bestBG);
     313            subtractConvolution(inputs->data[j], stamps->data[j], bestIndex, bestCoeff, bestBG, footprint);
    335314        }
    336315
     
    349328    }
    350329    psFree(inputs);
    351     psFree(convolutions);
    352330    psFree(sumC);
    353331    psFree(sumCC);
     
    392370        psKernel *subtract = kernels->preCalc->data[0]; // Kernel to subtract from the rest
    393371        for (int i = 1; i < newSize; i++) {
    394             psKernel *kernel = kernels->preCalc->data[i]; // Kernel of interest
    395             psBinaryOp(kernel->image, kernel->image, "-", subtract->image);
     372            if (kernels->u->data.S32[i] % 2 == 0 && kernels->v->data.S32[i] % 2 == 0) {
     373                psKernel *kernel = kernels->preCalc->data[i]; // Kernel of interest
     374                psBinaryOp(kernel->image, kernel->image, "-", subtract->image);
     375            }
    396376        }
    397377    } else if (type == PM_SUBTRACTION_KERNEL_GUNK) {
     
    400380
    401381        for (int i = 0; i < newSize; i++) {
    402             psKernel *kernel = kernels->preCalc->data[i]; // Kernel of interest
    403             kernel->kernel[0][0] -= 1.0;
     382            if (kernels->u->data.S32[i] % 2 == 0 && kernels->v->data.S32[i] % 2 == 0) {
     383                psKernel *kernel = kernels->preCalc->data[i]; // Kernel of interest
     384                kernel->kernel[0][0] -= 1.0;
     385            }
    404386        }
    405387
Note: See TracChangeset for help on using the changeset viewer.