IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 26330


Ignore:
Timestamp:
Dec 3, 2009, 2:30:10 AM (16 years ago)
Author:
eugene
Message:

initial mods to calculate normalization and kernel coeffs independently (how about background?)

Location:
branches/eam_branches/20091201/psModules/src/imcombine
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/20091201/psModules/src/imcombine/pmSubtractionEquation.c

    r26318 r26330  
    3535                                  const pmSubtractionKernels *kernels, // Kernels
    3636                                  const psImage *polyValues, // Spatial polynomial values
    37                                   int footprint // (Half-)Size of stamp
     37                                  int footprint, // (Half-)Size of stamp
     38                                  const bool normOnly
    3839                                  )
    3940{
     
    5354
    5455    // Evaluate polynomial-polynomial terms
     56    // XXX we can skip this for normOnly
    5557    for (int iyOrder = 0, iIndex = 0; iyOrder <= spatialOrder; iyOrder++) {
    5658        for (int ixOrder = 0; ixOrder <= spatialOrder - iyOrder; ixOrder++, iIndex++) {
     
    6769
    6870
    69     for (int i = 0; i < numKernels; i++) {
    70         psKernel *iConv = convolutions->data[i]; // Convolution for index i
    71         for (int j = i; j < numKernels; j++) {
    72             psKernel *jConv = convolutions->data[j]; // Convolution for index j
    73 
    74             double sumCC = 0.0;         // Sum of convolution products
    75             for (int y = - footprint; y <= footprint; y++) {
    76                 for (int x = - footprint; x <= footprint; x++) {
    77                     double cc = iConv->kernel[y][x] * jConv->kernel[y][x];
     71    // if we only want to calculate the normalization (and background model?)
     72    // then we should assign these values in the matrix and vector to 1 (diagonal) or 0 (off-diagonal)
     73    // XXX need to disable normalization calculation if !normOnly
     74    if (normOnly) {
     75        psImageInit(matrix, 0.0);
     76        psVectorInit(vector, 1.0);
     77        for (int i = 0; i < matrix->numCols; i++) {
     78            matrix->data.F64[i][i] = 1.0;
     79        }
     80    } else {
     81        for (int i = 0; i < numKernels; i++) {
     82            psKernel *iConv = convolutions->data[i]; // Convolution for index i
     83            for (int j = i; j < numKernels; j++) {
     84                psKernel *jConv = convolutions->data[j]; // Convolution for index j
     85
     86                double sumCC = 0.0;         // Sum of convolution products
     87                for (int y = - footprint; y <= footprint; y++) {
     88                    for (int x = - footprint; x <= footprint; x++) {
     89                        double cc = iConv->kernel[y][x] * jConv->kernel[y][x];
    7890#ifdef USE_WEIGHT
    79                     cc *= weight->kernel[y][x];
     91                        cc *= weight->kernel[y][x];
     92#endif
     93#ifdef USE_WINDOW
     94                        if (window) {
     95                            cc *= window->kernel[y][x];
     96                        }
     97#endif
     98                        sumCC += cc;
     99                    }
     100                }
     101
     102                // Spatial variation
     103                for (int iTerm = 0, iIndex = i; iTerm < numPoly; iTerm++, iIndex += numKernels) {
     104                    for (int jTerm = 0, jIndex = j; jTerm < numPoly; jTerm++, jIndex += numKernels) {
     105                        double value = sumCC * poly2[iTerm][jTerm];
     106                        matrix->data.F64[iIndex][jIndex] = value;
     107                        matrix->data.F64[jIndex][iIndex] = value;
     108                    }
     109                }
     110            }
     111
     112            double sumRC = 0.0;             // Sum of the reference-convolution products
     113            double sumIC = 0.0;             // Sum of the input-convolution products
     114            double sumC = 0.0;              // Sum of the convolution
     115            for (int y = - footprint; y <= footprint; y++) {
     116                for (int x = - footprint; x <= footprint; x++) {
     117                    float conv = iConv->kernel[y][x];
     118                    float in = input->kernel[y][x];
     119                    float ref = reference->kernel[y][x];
     120                    double ic = in * conv;
     121                    double rc = ref * conv;
     122                    double c = conv;
     123#ifdef USE_WEIGHT
     124                    float wtVal = weight->kernel[y][x];
     125                    ic *= wtVal;
     126                    rc *= wtVal;
     127                    c *= wtVal;
    80128#endif
    81129#ifdef USE_WINDOW
    82130                    if (window) {
    83                         cc *= window->kernel[y][x];
     131                        float winVal = window->kernel[y][x];
     132                        ic *= winVal;
     133                        rc *= winVal;
     134                        c  *= winVal;
    84135                    }
    85136#endif
    86                     sumCC += cc;
    87                 }
    88             }
    89 
    90             // Spatial variation
    91             for (int iTerm = 0, iIndex = i; iTerm < numPoly; iTerm++, iIndex += numKernels) {
    92                 for (int jTerm = 0, jIndex = j; jTerm < numPoly; jTerm++, jIndex += numKernels) {
    93                     double value = sumCC * poly2[iTerm][jTerm];
    94                     matrix->data.F64[iIndex][jIndex] = value;
    95                     matrix->data.F64[jIndex][iIndex] = value;
    96                 }
    97             }
    98         }
    99 
    100         double sumRC = 0.0;             // Sum of the reference-convolution products
    101         double sumIC = 0.0;             // Sum of the input-convolution products
    102         double sumC = 0.0;              // Sum of the convolution
    103         for (int y = - footprint; y <= footprint; y++) {
    104             for (int x = - footprint; x <= footprint; x++) {
    105                 float conv = iConv->kernel[y][x];
    106                 float in = input->kernel[y][x];
    107                 float ref = reference->kernel[y][x];
    108                 double ic = in * conv;
    109                 double rc = ref * conv;
    110                 double c = conv;
    111 #ifdef USE_WEIGHT
    112                 float wtVal = weight->kernel[y][x];
    113                 ic *= wtVal;
    114                 rc *= wtVal;
    115                 c *= wtVal;
    116 #endif
    117 #ifdef USE_WINDOW
    118                 if (window) {
    119                     float winVal = window->kernel[y][x];
    120                     ic *= winVal;
    121                     rc *= winVal;
    122                     c  *= winVal;
     137                    sumIC += ic;
     138                    sumRC += rc;
     139                    sumC += c;
    123140                }
    124 #endif
    125                 sumIC += ic;
    126                 sumRC += rc;
    127                 sumC += c;
    128             }
    129         }
    130         // Spatial variation
    131         for (int iTerm = 0, iIndex = i; iTerm < numPoly; iTerm++, iIndex += numKernels) {
    132             double normTerm = sumRC * poly[iTerm];
    133             double bgTerm = sumC * poly[iTerm];
    134             matrix->data.F64[iIndex][normIndex] = normTerm;
    135             matrix->data.F64[normIndex][iIndex] = normTerm;
    136             matrix->data.F64[iIndex][bgIndex] = bgTerm;
    137             matrix->data.F64[bgIndex][iIndex] = bgTerm;
    138             vector->data.F64[iIndex] = sumIC * poly[iTerm];
    139         }
    140     }
     141            }
     142            // Spatial variation
     143            for (int iTerm = 0, iIndex = i; iTerm < numPoly; iTerm++, iIndex += numKernels) {
     144                double normTerm = sumRC * poly[iTerm];
     145                double bgTerm = sumC * poly[iTerm];
     146                matrix->data.F64[iIndex][normIndex] = normTerm;
     147                matrix->data.F64[normIndex][iIndex] = normTerm;
     148                matrix->data.F64[iIndex][bgIndex] = bgTerm;
     149                matrix->data.F64[bgIndex][iIndex] = bgTerm;
     150                vector->data.F64[iIndex] = sumIC * poly[iTerm];
     151            }
     152        }
     153    }
     154
     155    // XXX need to disable normalization calculation if !normOnly
    141156
    142157    double sumRR = 0.0;                 // Sum of the reference product
     
    574589    int index = PS_SCALAR_VALUE(job->args->data[2], S32); // Stamp index
    575590
    576     return pmSubtractionCalculateEquationStamp(stamps, kernels, index);
     591    return pmSubtractionCalculateEquationStamp(stamps, kernels, index, normOnly);
    577592}
    578593
    579594bool pmSubtractionCalculateEquationStamp(pmSubtractionStampList *stamps, const pmSubtractionKernels *kernels,
    580                                          int index)
     595                                         int index, bool normOnly)
    581596{
    582597    PM_ASSERT_SUBTRACTION_STAMP_LIST_NON_NULL(stamps, false);
     
    591606    int numBackground = PM_SUBTRACTION_POLYTERMS(kernels->bgOrder); // Number of background terms
    592607
     608    // numKernels is the number of unique kernel images (one for each Gaussian modified by a specific polynomial). 
     609    // = \sum_i^N_Gaussians [(order + 1) * (order + 2) / 2], eg for 1 Gauss and 1st order, numKernels = 3
     610
    593611    // Total number of parameters to solve for: coefficient of each kernel basis function, multipled by the
    594612    // number of coefficients for the spatial polynomial, normalisation and a constant background offset.
     
    598616    psAssert(stamp->status == PM_SUBTRACTION_STAMP_CALCULATE, "We only operate on stamps with this state.");
    599617
    600     // Generate convolutions
     618    // Generate convolutions: these are generated once and saved
    601619    if (!pmSubtractionConvolveStamp(stamp, kernels, footprint)) {
    602620        psError(PS_ERR_UNKNOWN, false, "Unable to convolve stamp %d.", index);
     
    648666        status = calculateMatrixVector(stamp->matrix1, stamp->vector1, stamp->image2, stamp->image1,
    649667                                       stamp->weight, stamps->window, stamp->convolutions1, kernels,
    650                                        polyValues, footprint);
     668                                       polyValues, footprint, normOnly);
    651669        break;
    652670      case PM_SUBTRACTION_MODE_2:
    653671        status = calculateMatrixVector(stamp->matrix1, stamp->vector1, stamp->image1, stamp->image2,
    654672                                       stamp->weight, stamps->window, stamp->convolutions2, kernels,
    655                                        polyValues, footprint);
     673                                       polyValues, footprint, normOnly);
    656674        break;
    657675      case PM_SUBTRACTION_MODE_DUAL:
     676        psAbort ("dual is disabled: need to add window and normOnly");
    658677        if (new) {
    659678            stamp->matrix2 = psImageAlloc(numKernels * numSpatial, numKernels * numSpatial, PS_TYPE_F64);
     
    739758}
    740759
    741 bool pmSubtractionCalculateEquation(pmSubtractionStampList *stamps, const pmSubtractionKernels *kernels)
     760bool pmSubtractionCalculateEquation(pmSubtractionStampList *stamps, const pmSubtractionKernels *kernels, const bool normOnly)
    742761{
    743762    PM_ASSERT_SUBTRACTION_STAMP_LIST_NON_NULL(stamps, false);
     
    759778            psArrayAdd(job->args, 1, (pmSubtractionKernels*)kernels); // Casting away const to put on array
    760779            PS_ARRAY_ADD_SCALAR(job->args, i, PS_TYPE_S32);
     780            PS_ARRAY_ADD_SCALAR(job->args, normOnly, PS_TYPE_BOOL);
    761781            if (!psThreadJobAddPending(job)) {
    762782                psFree(job);
     
    765785            psFree(job);
    766786        } else {
    767             pmSubtractionCalculateEquationStamp(stamps, kernels, i);
     787            pmSubtractionCalculateEquationStamp(stamps, kernels, i, normOnly);
    768788        }
    769789    }
  • branches/eam_branches/20091201/psModules/src/imcombine/pmSubtractionEquation.h

    r19299 r26330  
    1212bool pmSubtractionCalculateEquationStamp(pmSubtractionStampList *stamps, ///< Stamps
    1313                                         const pmSubtractionKernels *kernels, ///< Kernel parameters
    14                                          int index ///< Index of stamp
     14                                         int index, ///< Index of stamp
     15                                         const bool normOnly
    1516                                    );
    1617
    1718/// Calculate the least-squares equation to match the image quality
    1819bool pmSubtractionCalculateEquation(pmSubtractionStampList *stamps, ///< Stamps
    19                                     const pmSubtractionKernels *kernels ///< Kernel parameters
     20                                    const pmSubtractionKernels *kernels, ///< Kernel parameters
     21                                    const bool normOnly
    2022                                    );
    2123
  • branches/eam_branches/20091201/psModules/src/imcombine/pmSubtractionMatch.c

    r26318 r26330  
    560560                }
    561561
     562                // XXX step 1: calculate normalization
     563                psTrace("psModules.imcombine", 3, "Calculating equation...\n");
     564                if (!pmSubtractionCalculateEquation(stamps, kernels, true)) {
     565                    psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation.");
     566                    goto MATCH_ERROR;
     567                }
     568
     569                // XXX step 2: calculate only other parameters
     570# if (0)
    562571                psTrace("psModules.imcombine", 3, "Calculating equation...\n");
    563572                if (!pmSubtractionCalculateEquation(stamps, kernels)) {
     
    565574                    goto MATCH_ERROR;
    566575                }
     576# endif
    567577
    568578                memCheck("  calculate equation");
  • branches/eam_branches/20091201/psModules/src/imcombine/pmSubtractionThreads.c

    r21363 r26330  
    6969
    7070    {
    71         psThreadTask *task = psThreadTaskAlloc("PSMODULES_SUBTRACTION_CALCULATE_EQUATION", 3);
     71        psThreadTask *task = psThreadTaskAlloc("PSMODULES_SUBTRACTION_CALCULATE_EQUATION", 4);
    7272        task->function = &pmSubtractionCalculateEquationThread;
    7373        psThreadTaskAdd(task);
Note: See TracChangeset for help on using the changeset viewer.