IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 14, 2004, 9:40:15 AM (22 years ago)
Author:
gusciora
Message:

...

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/image/psImageStats.c

    r896 r1022  
    1919/// This routine must determine the various statistics for the image.
    2020/*****************************************************************************
    21     GUS: verify that image/mask have the correct types, and sizes.
     21    NOTE: verify that image/mask have the correct types, and sizes.
    2222 *****************************************************************************/
    2323psStats *psImageStats(psStats *stats,
     
    3232    int j = 0;
    3333
    34     // GUS: Verify this action.
     34    // NOTE: Verify this action.
    3535    if ((stats == NULL) ||
    3636            (in == NULL)) {
    3737        return(NULL);
    3838    }
    39     // GUS: Verify this action.
     39    // NOTE: Verify this action.
    4040    if (stats->options == 0) {
    4141        return(stats);
     
    5252
    5353    if (mask != NULL) {
    54         // GUS: verify that mask data is PS_TYPE_U8.
    55         // GUS: figure out mask types
     54        // NOTE: verify that mask data is PS_TYPE_U8.
     55        // NOTE: figure out mask types
    5656
    5757        junkMask = psVectorAlloc(mask->numRows * mask->numCols,
    5858                                 mask->type.type);
    5959        junkMask->n = junkMask->nalloc;
    60         // GUS: Is there a more efficient way to do this?
     60        // NOTE: Is there a more efficient way to do this?
    6161        ptr=0;
    6262        for (i=0;i<mask->numRows;i++) {
     
    7878    NOTE: We assume that the psHistogram structure out has already been
    7979    allocated and initialized.
    80     GUS: verify that image/mask have the, correct types and  sizes.
     80    NOTE: verify that image/mask have the, correct types and  sizes.
    8181 *****************************************************************************/
    8282psHistogram *psImageHistogram(psHistogram *out,
     
    9191    int j = 0;
    9292
    93     // GUS: Verify this action.
     93    // NOTE: Verify this action.
    9494    if ((out == NULL) ||
    9595            (in == NULL)) {
     
    100100    junkData->n = junkData->nalloc;
    101101
    102     // GUS: Is there a more efficient way to do this?  memcopy() won't work.
     102    // NOTE: Is there a more efficient way to do this?  memcopy() won't work.
    103103    // Can we trick the junkData structure to use the image buffer, then
    104104    // untrick it before we deallocate it (so we won't deallocate that buffer
    105105    // twice?
    106106
    107     // GUS: Make sure you have the numRows/NumCols in the right place.
     107    // NOTE: Make sure you have the numRows/NumCols in the right place.
    108108    ptr=0;
    109109    for (i=0;i<in->numRows;i++) {
     
    114114
    115115    if (mask != NULL) {
    116         // GUS: verify that mask data is PS_TYPE_U8.
    117         // GUS: figure out mask types
     116        // NOTE: verify that mask data is PS_TYPE_U8.
     117        // NOTE: figure out mask types
    118118
    119119        junkMask = psVectorAlloc(mask->numRows * mask->numCols,
     
    121121        junkMask->n = junkMask->nalloc;
    122122
    123         // GUS: Is there a more efficient way to do this?
     123        // NOTE: Is there a more efficient way to do this?
    124124        ptr=0;
    125125        for (i=0;i<mask->numRows;i++) {
     
    137137    return(out);
    138138}
     139
     140float *p_psCalcScaleFactorsFit(int n)
     141{
     142    int i = 0;
     143    float tmp = 0.0;
     144    float *scalingFactors = (float *) psAlloc(n * sizeof(float));
     145
     146    for (i=0;i<n;i++) {
     147        //     ((2.0 * (float) i) / ((float) (n-1))) - 1.0;
     148        //        tmp = (float) (i + 1);
     149        tmp = (float) (n - i);
     150        tmp = (M_PI * (tmp - 0.5)) / ((float) n);
     151        scalingFactors[i] = cos(tmp);
     152    }
     153
     154    return(scalingFactors);
     155}
     156
     157float *p_psCalcScaleFactorsEval(int n)
     158{
     159    return p_psCalcScaleFactorsFit(n);
     160
     161    int i = 0;
     162    //    float tmp = 0.0;
     163    float *scalingFactors = (float *) psAlloc(n * sizeof(float));
     164    for (i=0;i<n;i++) {
     165        //          scalingFactors[i] = ((2.0 * (float) i) / ((float) (n-1))) - 1.0;
     166        tmp = (float) (i + 1);
     167        tmp = (M_PI * (tmp - 0.5)) / ((float) n);
     168        scalingFactors[i] = cos(tmp);
     169    }
     170    return(scalingFactors);
     171}
     172
     173psPolynomial1D **p_psCreateChebyshevPolys(int maxChebyPoly)
     174{
     175    psPolynomial1D **chebPolys = NULL;
     176    int i = 0;
     177    int j = 0;
     178
     179    chebPolys = (psPolynomial1D **) psAlloc(maxChebyPoly *
     180                                            sizeof(psPolynomial1D *));
     181    for (i=0;i<maxChebyPoly;i++) {
     182        chebPolys[i] = psPolynomial1DAlloc(i+1);
     183    }
     184
     185    // Create the Chebyshev polynomials.  Polynomial i has i-th order.
     186    chebPolys[0]->coeff[0] = 1;
     187    chebPolys[1]->coeff[1] = 1;
     188    for (i=2;i<maxChebyPoly;i++) {
     189        for (j=0;j<chebPolys[i-1]->n;j++) {
     190            chebPolys[i]->coeff[j+1] = 2 * chebPolys[i-1]->coeff[j];
     191        }
     192        for (j=0;j<chebPolys[i-2]->n;j++) {
     193            chebPolys[i]->coeff[j]-= chebPolys[i-2]->coeff[j];
     194        }
     195    }
     196
     197    return(chebPolys);
     198}
     199
    139200
    140201/*****************************************************************************
     
    146207    Output:
    147208    Internal Data Structures:
     209 
    148210 chebPolys[i][j]
    149         sums[i][j]
     211 
     212 sums[i][j]: This will contain the sum of
     213  input->data.F32[x][y] *
     214                psEvalPolynomial1D((float) x, chebPolys[i]) *
     215                psEvalPolynomial1D((float) y, chebPolys[j]);
     216      over all pixels (x,y) in the image.
     217 
     218 
    150219 *****************************************************************************/
    151220psPolynomial2D *
     
    153222                     psPolynomial2D *coeffs)
    154223{
     224    int k = 0;
     225    int l = 0;
    155226    int x = 0;
    156227    int y = 0;
     
    160231    psPolynomial1D **chebPolys = NULL;
    161232    int maxChebyPoly = 0;
    162 
     233    float *cScalingFactors = NULL;
     234    float *rScalingFactors = NULL;
     235    float tmp = 0.0;
    163236
    164237    // Create the sums[][] data structure.  This will hold the LHS of equation
    165238    // 29 in the ADD: sums[k][l] = SUM { image(x,y) * Tk(x) * Tl(y) }
    166 
    167239    sums = (float **) psAlloc(coeffs->nX * sizeof(float *));
    168240    for (i=0;i<coeffs->nX;i++) {
    169241        sums[i] = (float *) psAlloc(coeffs->nY * sizeof(float));
    170242    }
    171     for (i=0;i<coeffs->nX;i++) {
    172         for (j=0;j<coeffs->nY;j++) {
    173             sums[i][j] = 0.0;
    174         }
    175     }
     243
     244    // We scale the pixel positions to values between -1.0 and 1.0
     245    cScalingFactors = p_psCalcScaleFactorsFit(input->numRows);
     246    rScalingFactors = p_psCalcScaleFactorsFit(input->numCols);
    176247
    177248    // Determine how many Chebyshev polynomials are needed, then create them.
     
    180251        maxChebyPoly = coeffs->nY;
    181252    }
    182 
    183     chebPolys = (psPolynomial1D **) psAlloc(maxChebyPoly *
    184                                             sizeof(psPolynomial1D *));
    185     for (i=0;i<maxChebyPoly;i++) {
    186         chebPolys[i] = psPolynomial1DAlloc(i);
    187     }
    188 
    189     // Create the Chebyshev polynomials
    190     chebPolys[1]->coeff[0] = 1;
    191     for (i=2;i<maxChebyPoly;i++) {
    192         for (j=0;j<chebPolys[i-1]->n;j++) {
    193             chebPolys[i]->coeff[j+1] = 2 * chebPolys[i-1]->coeff[j];
    194         }
    195         for (j=0;j<chebPolys[i-2]->n;j++) {
    196             chebPolys[i]->coeff[j]-= chebPolys[i-2]->coeff[j];
    197         }
    198     }
    199 
    200     for (x=0;x<input->numRows;x++) {
    201         for (y=0;y<input->numCols;y++) {
    202             for (i=0;i<coeffs->nX;i++) {
    203                 for (j=0;j<coeffs->nY;j++) {
     253    chebPolys = p_psCreateChebyshevPolys(maxChebyPoly);
     254
     255    // Sanity check for the Chebyshevs
     256    for (i=0;i<coeffs->nX;i++) {
     257        for (j=0;j<coeffs->nY;j++) {
     258            tmp = 0.0;
     259            for (x=0;x<input->numRows;x++) {
     260                tmp+= psEvalPolynomial1D(rScalingFactors[x], chebPolys[i]) *
     261                      psEvalPolynomial1D(rScalingFactors[x], chebPolys[j]);
     262
     263            }
     264        }
     265    }
     266
     267    for (i=0;i<coeffs->nX;i++) {
     268        for (j=0;j<coeffs->nY;j++) {
     269            sums[i][j] = 0.0;
     270            for (x=0;x<input->numRows;x++) {
     271                for (y=0;y<input->numCols;y++) {
    204272                    sums[i][j]+= input->data.F32[x][y] *
    205                                  psEvalPolynomial1D((float) x, chebPolys[i]) *
    206                                  psEvalPolynomial1D((float) y, chebPolys[j]);
    207 
     273                                 psEvalPolynomial1D(rScalingFactors[x], chebPolys[i]) *
     274                                 psEvalPolynomial1D(cScalingFactors[y], chebPolys[j]);
    208275                }
    209276            }
     
    211278    }
    212279
    213     // GUS: Check the math on this at a later date.
    214     coeffs->coeff[0][0] = sums[0][0] / (coeffs->nX * coeffs->nY);
    215     for (i=0;i<coeffs->nX;i++) {
    216         coeffs->coeff[i][0] = 0.5 *
    217                               ((sums[i][0] * 4.0) / (coeffs->nX * coeffs->nY)) -
    218                               (2.0 * coeffs->coeff[0][0]);
     280    // NOTE: Check the math on this at a later date.
     281    coeffs->coeff[0][0] = sums[0][0] / ((float) (coeffs->nX * coeffs->nY));
     282    for (i=0;i<coeffs->nX;i++) {
     283        coeffs->coeff[i][0] = ((sums[i][0] * 2.0) /
     284                               ((float) (coeffs->nX * coeffs->nY))) -
     285                              coeffs->coeff[0][0];
    219286    }
    220287    for (j=0;j<coeffs->nY;j++) {
    221         coeffs->coeff[0][j] = 0.5 *
    222                               ((sums[0][j] * 4.0) / (coeffs->nX * coeffs->nY)) -
    223                               (2.0 * coeffs->coeff[0][0]);
     288        coeffs->coeff[0][j] =
     289            ((sums[0][j] * 2.0) /
     290             ((float) (coeffs->nX * coeffs->nY))) -
     291            coeffs->coeff[0][0];
    224292    }
    225293    for (i=1;i<coeffs->nX;i++) {
    226294        for (j=1;j<coeffs->nY;j++) {
    227295            coeffs->coeff[i][j] =
    228                 (sums[i][0] * 4.0) / (coeffs->nX * coeffs->nY) -
    229                 coeffs->coeff[0][0] -
    230                 coeffs->coeff[i][0] -
    231                 coeffs->coeff[0][j];
    232         }
    233     }
     296                ((sums[i][0] * 4.0) / ((float) (coeffs->nX * coeffs->nY))) -
     297                (coeffs->coeff[0][0] +
     298                 coeffs->coeff[i][0] +
     299                 coeffs->coeff[0][j]);
     300        }
     301    }
     302
     303    for (k=0;k<coeffs->nX;k++) {
     304        for (l=0;l<coeffs->nY;l++) {
     305            tmp = 0.0;
     306            for (i=0;i<coeffs->nX;i++) {
     307                for (j=0;j<coeffs->nY;j++) {
     308                    for (x=0;x<input->numRows;x++) {
     309                        for (y=0;y<input->numCols;y++) {
     310                            tmp+= (coeffs->coeff[i][j] *
     311                                   psEvalPolynomial1D(rScalingFactors[x], chebPolys[i]) *
     312                                   psEvalPolynomial1D(rScalingFactors[y], chebPolys[j]) *
     313                                   psEvalPolynomial1D(rScalingFactors[x], chebPolys[k]) *
     314                                   psEvalPolynomial1D(rScalingFactors[y], chebPolys[l]));
     315
     316                        }
     317                    }
     318                }
     319            }
     320        }
     321    }
     322
    234323
    235324    // Free the Chebyshev polynomials that were created in this routine.
     
    244333    }
    245334    psFree(sums);
     335    psFree(cScalingFactors);
     336    psFree(rScalingFactors);
    246337
    247338    return(coeffs);
     
    255346                      const psPolynomial2D *coeffs)
    256347{
    257     int i = 0;
    258     int j = 0;
    259348    int x = 0;
    260349    int y = 0;
     350    int i = 0;
     351    int j = 0;
    261352    float **sums = NULL;
    262353    psPolynomial1D **chebPolys = NULL;
    263354    int maxChebyPoly = 0;
     355    float *cScalingFactors = NULL;
     356    float *rScalingFactors = NULL;
    264357    float polySum = 0.0;
    265358
     
    275368        }
    276369    }
     370
     371    // We scale the pixel positions to values between -1.0 and 1.0
     372    cScalingFactors = p_psCalcScaleFactorsEval(input->numRows);
     373    rScalingFactors = p_psCalcScaleFactorsEval(input->numCols);
    277374
    278375    // Determine how many Chebyshev polynomials are needed, then create them.
     
    282379    }
    283380
    284     chebPolys = (psPolynomial1D **) psAlloc(maxChebyPoly *
    285                                             sizeof(psPolynomial1D *));
    286     for (i=0;i<maxChebyPoly;i++) {
    287         chebPolys[i] = psPolynomial1DAlloc(i);
    288     }
    289 
    290     // Create the Chebyshev polynomials
    291     chebPolys[1]->coeff[0] = 1;
    292     for (i=2;i<maxChebyPoly;i++) {
    293         for (j=0;j<chebPolys[i-1]->n;j++) {
    294             chebPolys[i]->coeff[j+1] = 2 * chebPolys[i-1]->coeff[j];
    295         }
    296         for (j=0;j<chebPolys[i-2]->n;j++) {
    297             chebPolys[i]->coeff[j]-= chebPolys[i-2]->coeff[j];
    298         }
    299     }
     381    chebPolys = p_psCreateChebyshevPolys(maxChebyPoly);
    300382
    301383    for (x=0;x<input->numRows;x++) {
     
    304386            for (i=0;i<coeffs->nX;i++) {
    305387                for (j=0;j<coeffs->nY;j++) {
    306                     polySum+= psEvalPolynomial1D((float)x, chebPolys[i]) *
    307                               psEvalPolynomial1D((float)y, chebPolys[j]) *
     388                    polySum+= psEvalPolynomial1D(rScalingFactors[x], chebPolys[i]) *
     389                              psEvalPolynomial1D(cScalingFactors[y], chebPolys[j]) *
    308390                              coeffs->coeff[i][j];
     391
    309392                }
    310393            }
     
    324407    }
    325408    psFree(sums);
     409    psFree(cScalingFactors);
     410    psFree(rScalingFactors);
    326411
    327412    return(0);
Note: See TracChangeset for help on using the changeset viewer.