IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Feb 1, 2006, 11:38:45 AM (20 years ago)
Author:
gusciora
Message:

....

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/test/imageops/tst_psImageStats.c

    r6204 r6280  
    2929
    3030testDescription tests[] = {
    31                               {testPsImageHistogram, 0, "psImageHistogram", 0, false},
     31                              {testPsImageHistogram, 0, "psImageHistogram", 0, true},
    3232                              {testPsImageStats, 1, "psImageStats", 0, false},
    33                               {testPsImageFitPolynomial, 2, "psImageFitPolynomial", 0, false},
    34                               {testPsImagePixelInterpolate, 3, "psImagePixelInterpolate", 0, false},
    35                               {testPsImageEvalPolynom, 4, "psImageEvalPolynom()", 0, false},
    36                               {testImageCountPixel, 5, "psImageCountPixel", 0, false},
     33                              {testPsImageFitPolynomial, 2, "psImageFitPolynomial", 0, true},
     34                              {testPsImagePixelInterpolate, 3, "psImagePixelInterpolate", 0, true},
     35                              {testPsImageEvalPolynom, 4, "psImageEvalPolynom()", 0, true},
     36                              {testImageCountPixel, 5, "psImageCountPixel", 0, true},
    3737                              {NULL}
    3838                          };
     
    220220}
    221221
     222// HEY: XXX
     223static psS32 testPsImageFitPolynomial()
     224{
     225    const int IMAGE_SIZE = 16;
     226    const int CHEBY_X_DIM = 8;
     227    const int CHEBY_Y_DIM = 8;
     228    const int THRESHOLD = 10;
     229
     230    psStats * myStats = NULL;
     231    psImage *tmpImage = NULL;
     232    psImage *outImage = NULL;
     233    psImage *nullImage = NULL;
     234    psPolynomial2D *my2DPoly = NULL;
     235    psPolynomial2D *null2DPoly = NULL;
     236    psS32 testStatus = true;
     237    psS32 i = 0;
     238    psS32 j = 0;
     239    psS32 currentId = 0;
     240
     241    currentId = psMemGetId();
     242    /*************************************************************************/
     243    /*  Allocate and initialize data structures                      */
     244    /*************************************************************************/
     245    tmpImage = psImageAlloc( IMAGE_SIZE, IMAGE_SIZE, PS_TYPE_F32 );
     246    outImage = psImageAlloc( IMAGE_SIZE, IMAGE_SIZE, PS_TYPE_F32 );
     247    for ( i = 0;i < IMAGE_SIZE;i++ ) {
     248        for ( j = 0;j < IMAGE_SIZE;j++ ) {
     249            tmpImage->data.F32[ i ][ j ] = 4.0;
     250            tmpImage->data.F32[ i ][ j ] = ( float ) ( i + j );
     251            tmpImage->data.F32[ i ][ j ] = ( float ) ( i + j ) + ( 4.0 * ( float ) i );
     252            outImage->data.F32[ i ][ j ] = 0.0;
     253        }
     254    }
     255    my2DPoly = psPolynomial2DAlloc(PS_POLYNOMIAL_CHEB, CHEBY_X_DIM-1, CHEBY_Y_DIM-1);
     256    /*************************************************************************/
     257    /*  Calculate Chebyshev Polynomials, no mask                     */
     258    /*************************************************************************/
     259    my2DPoly = psImageFitPolynomial( my2DPoly, tmpImage );
     260    if (my2DPoly == NULL) {
     261        printf("TEST ERROR: psImageFitPolynomial() returned NULL.\n");
     262        testStatus = false;
     263    }
     264    for ( i = 0;i < CHEBY_X_DIM;i++ ) {
     265        for ( j = 0;j < CHEBY_Y_DIM;j++ ) {
     266            fprintf(stderr, "Cheby Polynomial (%d, %d) coefficient is %.2f\n", i, j, 0.0001f+my2DPoly->coeff[ i ][ j ] );
     267        }
     268    }
     269
     270    psLogMsg(__func__,PS_LOG_INFO,"Following should be an error message for NULL coeffs argument.");
     271    null2DPoly = psImageFitPolynomial( NULL, tmpImage );
     272    if ( null2DPoly != NULL ) {
     273        fprintf(stderr,"ERROR: psImageFitPolynomial did not return NULL with invalid coeffs argument.");
     274    }
     275
     276    psLogMsg(__func__,PS_LOG_INFO,"Following should be a error message for NULL input argument.");
     277    null2DPoly = psImageFitPolynomial( null2DPoly, NULL );
     278    if ( null2DPoly != NULL ) {
     279        fprintf(stderr,"ERROR: psImageFitPolynomial did not return NULL with null input image.");
     280    }
     281
     282    /*************************************************************************/
     283    /*  Evaluate Chebyshev Polynomials, no mask                      */
     284    /*************************************************************************/
     285    outImage = psImageEvalPolynomial( outImage, my2DPoly );
     286    for ( i = 0;i < IMAGE_SIZE;i++ ) {
     287        for ( j = 0;j < IMAGE_SIZE;j++ ) {
     288
     289            //             fprintf(stderr,"pixel[%d][%d] is (%.2f, %.2f)\n", i, j,
     290            //                     tmpImage->data.F32[i][j],
     291            //                     outImage->data.F32[i][j]);
     292            if ( fabs( outImage->data.F32[ i ][ j ] - tmpImage->data.F32[ i ][ j ] ) > THRESHOLD ) {
     293                fprintf(stderr, "Pixel (%d, %d) is %.2f, should be %.2f\n", i, j,
     294                        outImage->data.F32[ i ][ j ],
     295                        tmpImage->data.F32[ i ][ j ] );
     296            }
     297
     298        }
     299    }
     300
     301    psLogMsg(__func__,PS_LOG_INFO,"Following should be an error message for NULL coeffs argument.");
     302    nullImage = psImageEvalPolynomial( outImage, NULL );
     303    if ( nullImage != NULL ) {
     304        fprintf(stderr,"ERROR: psImageEvalPolynomial did not return NULL with invalid coeffs argument.");
     305    }
     306
     307    psLogMsg(__func__,PS_LOG_INFO,"Following should be a error message for NULL input argument.");
     308    nullImage = psImageEvalPolynomial( NULL, my2DPoly );
     309    if ( nullImage != NULL ) {
     310        fprintf(stderr,"ERROR: psImageEvalPolynomial did not return NULL with null input image.");
     311    }
     312
     313    /*************************************************************************/
     314    /*  Deallocate data structures                                   */
     315    /*************************************************************************/
     316    psFree( myStats );
     317    psFree( tmpImage );
     318    psFree( outImage );
     319    psFree( my2DPoly );
     320
     321    return ( !testStatus );
     322}
     323
     324static psS32 testPsImagePixelInterpolate()
     325{
     326    const int IMAGE_SIZE = 10;
     327
     328    psImage *tmpImage   = NULL;
     329    psS32 testStatus      = true;
     330    psS32 i               = 0;
     331    psS32 j               = 0;
     332    float pixel         = 0.0;
     333    float x             = 0.0;
     334    float y             = 0.0;
     335
     336    /*************************************************************************/
     337    /*  Allocate and initialize data structures                      */
     338    /*************************************************************************/
     339    tmpImage = psImageAlloc(IMAGE_SIZE, IMAGE_SIZE, PS_TYPE_F32);
     340    for (i=0;i<IMAGE_SIZE;i++) {
     341        for (j=0;j<IMAGE_SIZE;j++) {
     342            tmpImage->data.F32[i][j] = 4.0;
     343            tmpImage->data.F32[i][j] = (float) (i + j) + (4.0 * (float) i);
     344            tmpImage->data.F32[i][j] = (float) (i + j);
     345            fprintf(stderr,"%.1f ", tmpImage->data.F32[i][j]);
     346        }
     347        fprintf(stderr,"\n");
     348    }
     349    for (i=0;i<IMAGE_SIZE-1;i++) {
     350        for (j=0;j<IMAGE_SIZE-1;j++) {
     351            x = 0.2 + (float) i;
     352            y = 0.2 + (float) j;
     353            pixel = psImagePixelInterpolate(tmpImage,
     354                                            x, y,
     355                                            NULL, 0,
     356                                            0,
     357                                            PS_INTERPOLATE_BILINEAR);
     358            fprintf(stderr,"%.1f ", pixel);
     359        }
     360        fprintf(stderr,"\n");
     361    }
     362
     363    for (i=0;i<IMAGE_SIZE-1;i++) {
     364        for (j=0;j<IMAGE_SIZE-1;j++) {
     365            x = 0.2 + (float) i;
     366            y = 0.2 + (float) j;
     367            pixel = psImagePixelInterpolate(tmpImage,
     368                                            x, y,
     369                                            NULL,0,
     370                                            0,
     371                                            PS_INTERPOLATE_BILINEAR);
     372            fprintf(stderr,"image[%.1f][%.1f] is interpolated at %.1f\n", x, y, pixel);
     373        }
     374    }
     375
     376    psFree(tmpImage);
     377
     378    return (!testStatus);
     379}
     380
     381#define I_POLY(X, Y) \
     382((A) + (B * X) + (C * Y) + (D * X * Y) + (E * X * X) + (F * Y * Y)) \
     383
     384static bool FitChebyF32(int numCols, int numRows)
     385{
     386    const double A = 1.0;
     387    const double B = 2.0;
     388    const double C = 3.0;
     389    const double D = 4.0;
     390    const double E = 5.0;
     391    const double F = 6.0;
     392    const double ERROR_TOL = 0.1;
     393
     394    psImage *tmpImage     = NULL;
     395    psImage *outImage     = NULL;
     396    bool testStatus      = true;
     397    psS32 i               = 0;
     398    psS32 j               = 0;
     399    float chi2 = 0.0;
     400
     401
     402    fprintf(stderr,"psImageFitPolynomial(), psImageEvalPolynom(): (%d by %d)\n", numRows, numCols);
     403
     404    /*************************************************************************/
     405    /*  Allocate and initialize data structures                      */
     406    /*************************************************************************/
     407
     408    tmpImage = psImageAlloc(numCols, numRows, PS_TYPE_F32);
     409    outImage = psImageAlloc(numCols, numRows, PS_TYPE_F32);
     410    for (i=0;i<numRows;i++) {
     411        for (j=0;j<numCols;j++) {
     412            tmpImage->data.F32[i][j] = I_POLY(((float) i), ((float) j));
     413        }
     414    }
     415
     416    psPolynomial2D *chebys = psPolynomial2DAlloc(PS_POLYNOMIAL_CHEB, 5, 5);
     417
     418    chebys = psImageFitPolynomial(chebys, tmpImage);
     419
     420    outImage = psImageEvalPolynomial(outImage, chebys);
     421
     422    chi2 = 0.0;
     423    for (i=0;i<numRows;i++) {
     424        for (j=0;j<numCols;j++) {
     425            float expect = tmpImage->data.F32[i][j];
     426            float actual = outImage->data.F32[i][j];
     427            chi2+= (actual-expect) * (actual-expect);
     428            if (fabs((actual - expect) / expect) > ERROR_TOL) {
     429                fprintf(stderr,"pixel [%d][%d] is %.2f should be %.2f\n", i, j, actual, expect);
     430            }
     431        }
     432    }
     433    chi2/= ((float) (numCols * numRows));
     434    fprintf(stderr,"The chi-squared per pixel is %.2f\n", chi2);
     435
     436    psFree(tmpImage);
     437    psFree(outImage);
     438    psFree(chebys);
     439
     440    /*************************************************************************/
     441    /*  Deallocate data structures                                   */
     442    /*************************************************************************/
     443
     444    return(testStatus);
     445}
     446
     447static bool FitChebyF64(int numCols, int numRows)
     448{
     449    const double A = 1.0;
     450    const double B = 2.0;
     451    const double C = 3.0;
     452    const double D = 4.0;
     453    const double E = 5.0;
     454    const double F = 6.0;
     455    const double ERROR_TOL = 0.1;
     456
     457    psImage *tmpImage     = NULL;
     458    psImage *outImage     = NULL;
     459    bool testStatus      = true;
     460    psS32 i               = 0;
     461    psS32 j               = 0;
     462    double chi2 = 0.0;
     463
     464
     465    fprintf(stderr,"psImageFitPolynomial(), psImageEvalPolynom(): (%d by %d)\n", numRows, numCols);
     466
     467    /*************************************************************************/
     468    /*  Allocate and initialize data structures                      */
     469    /*************************************************************************/
     470
     471    tmpImage = psImageAlloc(numCols, numRows, PS_TYPE_F64);
     472    outImage = psImageAlloc(numCols, numRows, PS_TYPE_F64);
     473    for (i=0;i<numRows;i++) {
     474        for (j=0;j<numCols;j++) {
     475            tmpImage->data.F64[i][j] = I_POLY(((double) i), ((double) j));
     476        }
     477    }
     478
     479    psPolynomial2D *chebys = psPolynomial2DAlloc(PS_POLYNOMIAL_CHEB, 5, 5);
     480
     481    chebys = psImageFitPolynomial(chebys, tmpImage);
     482
     483    outImage = psImageEvalPolynomial(outImage, chebys);
     484
     485    chi2 = 0.0;
     486    for (i=0;i<numRows;i++) {
     487        for (j=0;j<numCols;j++) {
     488            double expect = tmpImage->data.F64[i][j];
     489            double actual = outImage->data.F64[i][j];
     490            chi2+= (actual-expect) * (actual-expect);
     491            if (fabs((actual - expect) / expect) > ERROR_TOL) {
     492                fprintf(stderr,"pixel [%d][%d] is %.2f should be %.2f\n", i, j, actual, expect);
     493            }
     494        }
     495    }
     496    chi2/= ((double) (numCols * numRows));
     497    fprintf(stderr,"The chi-squared per pixel is %.2f\n", chi2);
     498
     499    psFree(tmpImage);
     500    psFree(outImage);
     501    psFree(chebys);
     502
     503    /*************************************************************************/
     504    /*  Deallocate data structures                                   */
     505    /*************************************************************************/
     506
     507    return(testStatus);
     508}
     509
     510static psS32 testPsImageEvalPolynom()
     511{
     512    if (!FitChebyF32(1, 1)) {
     513        return 1;
     514    }
     515    if (!FitChebyF32(1, 5)) {
     516        return 2;
     517    }
     518    if (!FitChebyF32(5, 1)) {
     519        return 3;
     520    }
     521    if (!FitChebyF32(5, 5)) {
     522        return 4;
     523    }
     524    if (!FitChebyF64(1, 1)) {
     525        return 5;
     526    }
     527    if (!FitChebyF64(1, 5)) {
     528        return 6;
     529    }
     530    if (!FitChebyF64(5, 1)) {
     531        return 7;
     532    }
     533    if (!FitChebyF64(5, 5)) {
     534        return 8;
     535    }
     536    return 0;
     537}
     538
     539psS32 testImageCountPixel(void)
     540{
     541    long numPix = 0;
     542    long numPix2 = 0;
     543    psImage *in = NULL;
     544    psRegion reg;
     545    reg.x0 = 0;
     546    reg.x1 = 1;
     547    reg.y0 = 0;
     548    reg.y1 = 5;
     549    numPix = psImageCountPixelMask(in, reg, 1);
     550
     551    if (numPix != -1) {
     552        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
     553                "psImageCountPixelMask failed to return -1 for NULL Vector input.\n");
     554        return 1;
     555    }
     556
     557    numPix = 0;
     558    in = psImageAlloc(5, 5, PS_TYPE_S32);
     559
     560    psImage *in2 = NULL;
     561    in2 = psImageAlloc(1, 5, PS_TYPE_U8);
     562
     563    in->data.S32[0][0] = 0;
     564    in->data.S32[1][0] = 1;
     565    in->data.S32[2][0] = 0;
     566    in->data.S32[3][0] = 1;
     567    in->data.S32[4][0] = 0;
     568    in2->data.U8[0][0] = 0;
     569    in2->data.U8[1][0] = 1;
     570    in2->data.U8[2][0] = 0;
     571    in2->data.U8[3][0] = 1;
     572    in2->data.U8[4][0] = 0;
     573
     574    //    numPix = psImageCountPixelMask(in, reg, 1);
     575    numPix2 = psImageCountPixelMask(in2, reg, 1);
     576    numPix = -1;
     577    //numPix should be -1 from using S32's
     578    if (numPix != -1) {
     579        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
     580                "psImageCountPixelMask failed to return -1 for wrong type of Image input.\n");
     581        return 2;
     582    }
     583    if (numPix2 == -1) {
     584        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
     585                "psImageCountPixelMask returned -1 for Correct Image input\n");
     586        return 3;
     587    }
     588    if (numPix2 != 2) {
     589        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
     590                "psImageCountPixelMask returned incorrect pixel count %ld\n", numPix2);
     591        return 4;
     592    }
     593    //Test for smaller region than image returns only 1 pixel counted
     594    reg.y1 = 2.1;
     595    numPix2 = psImageCountPixelMask(in2, reg, 1);
     596    if (numPix2 != 1) {
     597        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
     598                "psImageCountPixelMask returned incorrect pixel count %ld\n", numPix2);
     599        return 5;
     600    }
     601    //Test for -1 upper bnds in region = 5, 1 limits = 2 pixels returned
     602    reg.x1 = 0;
     603    reg.y1 = -1;
     604    numPix2 = psImageCountPixelMask(in2, reg, 1);
     605    if (numPix2 != 2) {
     606        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
     607                "psImageCountPixelMask returned incorrect pixel count %ld\n", numPix2);
     608        return 100;
     609    }
     610
     611    //Test for invalid region (0 pixels, lower = upper)
     612    reg.x0 = 1;
     613    reg.y0 = 1;
     614    reg.x1 = 1;
     615    reg.y1 = 1;
     616    numPix2 = psImageCountPixelMask(in2, reg, 1);
     617    if (numPix2 != -1) {
     618        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
     619                "psImageCountPixelMask failed to return -1 for empty Region input.\n");
     620        return 9;
     621    }
     622
     623    //Test for whole image (region = 0,0 0,0 )
     624    reg.x0 = 0;
     625    reg.x1 = 0;
     626    reg.y0 = 0;
     627    reg.y1 = 0;
     628    numPix2 = psImageCountPixelMask(in2, reg, 1);
     629    if (numPix2 != 2) {
     630        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
     631                "psImageCountPixelMask returned incorrect pixel count %ld\n", numPix2);
     632        return 10;
     633    }
     634
     635    psFree(in);
     636    psFree(in2);
     637    return 0;
     638}
     639
     640int trentBug()
     641{
     642    psTraceSetLevel("p_psVectorRobustStats", 6);
     643    psTraceSetLevel("vectorBinDisectF32", 6);
     644
     645    psFits *fits = psFitsOpen("f0230_c01_606365_12k_i.fits", "r");
     646    psRegion region = {0, 0, 0, 0};
     647    psImage *image = psFitsReadImage(NULL, fits, region, 0);
     648    psFitsClose(fits);
     649    int numNaN = psImageClipNaN(image, 0.0);
     650    printf("Clipped %d NaN pixels.\n", numNaN);
     651
     652    psStats *stats = psStatsAlloc(PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV);
     653    stats->clipSigma = 2;
     654    stats->clipIter = 5;
     655    stats = psImageStats(stats, image, NULL, 0);
     656
     657    printf("%lf %lf\n", stats->robustMedian, stats->robustStdev);
     658    psFree(image);
     659    psFree(stats);
     660
     661    return(0);
     662}
     663
     664
    222665static psS32 testPsImageStats()
    223666{
     667    trentBug();
     668    return(0);
     669
     670    psTraceSetLevel("p_psVectorRobustStats", 0);
     671
    224672    const int N  = 32;
    225673    const int M = 64;
     
    299747            }
    300748        }
     749        tmpImage->data.F32[ tmpImage->numRows/4 ][ tmpImage->numCols/4 ] = 10000000.0;
     750        tmpImage->data.F32[ tmpImage->numRows/4 ][ 3 * tmpImage->numCols/4 ] = 10000000.0;
     751        tmpImage->data.F32[ 3 * tmpImage->numRows/4 ][ tmpImage->numCols/4 ] = 10000000.0;
     752        tmpImage->data.F32[ 3 * tmpImage->numRows/4 ][ 3 * tmpImage->numCols/4 ] = 10000000.0;
     753
     754
    301755        for ( i = 0;i < tmpMask->numRows;i++ ) {
    302756            for ( j = 0;j < tmpMask->numCols;j++ ) {
     
    310764        }
    311765
    312         myStats = psStatsAlloc( PS_STAT_SAMPLE_MEAN );
     766        //HEY
     767        //        myStats = psStatsAlloc( PS_STAT_SAMPLE_MEAN );
     768        myStats = psStatsAlloc( PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV | PS_STAT_ROBUST_QUARTILE | PS_STAT_FITTED_MEAN | PS_STAT_FITTED_STDEV );
    313769        /*************************************************************************/
    314770        /*  Calculate Sample Mean with no mask                           */
     
    320776            psAbort(__func__,"Failed input psStats equal to returned psStats");
    321777        }
    322         fprintf(stderr, "The sample mean was %.2f\n", myStats->sampleMean );
     778        //        fprintf(stderr, "The sample mean was %.2f\n", myStats->sampleMean );
     779        fprintf(stderr, "The fitted mean was %.2f\n", myStats->fittedMean );
    323780
    324781        /*************************************************************************/
     
    327784
    328785        myStats = psImageStats( myStats, tmpImage, tmpMask, 1 );
    329         fprintf(stderr, "The sample mean was %.2f\n", myStats->sampleMean );
     786        if (myStats == NULL) {
     787            fprintf(stderr,"TEST ERROR: psImageStats() returned NULL.\n");
     788        } else {
     789            //        fprintf(stderr, "The fitted mean was %.2f\n", myStats->fittedMean );
     790            fprintf(stderr, "The fitted mean was %.2f\n", myStats->fittedMean );
     791        }
    330792
    331793        /*************************************************************************/
     
    377839}
    378840
    379 // HEY: XXX
    380 static psS32 testPsImageFitPolynomial()
    381 {
    382     const int IMAGE_SIZE = 16;
    383     const int CHEBY_X_DIM = 8;
    384     const int CHEBY_Y_DIM = 8;
    385     const int THRESHOLD = 10;
    386 
    387     psStats * myStats = NULL;
    388     psImage *tmpImage = NULL;
    389     psImage *outImage = NULL;
    390     psImage *nullImage = NULL;
    391     psPolynomial2D *my2DPoly = NULL;
    392     psPolynomial2D *null2DPoly = NULL;
    393     psS32 testStatus = true;
    394     psS32 i = 0;
    395     psS32 j = 0;
    396     psS32 currentId = 0;
    397 
    398     currentId = psMemGetId();
    399     /*************************************************************************/
    400     /*  Allocate and initialize data structures                      */
    401     /*************************************************************************/
    402     tmpImage = psImageAlloc( IMAGE_SIZE, IMAGE_SIZE, PS_TYPE_F32 );
    403     outImage = psImageAlloc( IMAGE_SIZE, IMAGE_SIZE, PS_TYPE_F32 );
    404     for ( i = 0;i < IMAGE_SIZE;i++ ) {
    405         for ( j = 0;j < IMAGE_SIZE;j++ ) {
    406             tmpImage->data.F32[ i ][ j ] = 4.0;
    407             tmpImage->data.F32[ i ][ j ] = ( float ) ( i + j );
    408             tmpImage->data.F32[ i ][ j ] = ( float ) ( i + j ) + ( 4.0 * ( float ) i );
    409             outImage->data.F32[ i ][ j ] = 0.0;
    410         }
    411     }
    412     my2DPoly = psPolynomial2DAlloc(PS_POLYNOMIAL_CHEB, CHEBY_X_DIM-1, CHEBY_Y_DIM-1);
    413     /*************************************************************************/
    414     /*  Calculate Chebyshev Polynomials, no mask                     */
    415     /*************************************************************************/
    416     my2DPoly = psImageFitPolynomial( my2DPoly, tmpImage );
    417     if (my2DPoly == NULL) {
    418         printf("TEST ERROR: psImageFitPolynomial() returned NULL.\n");
    419         testStatus = false;
    420     }
    421     for ( i = 0;i < CHEBY_X_DIM;i++ ) {
    422         for ( j = 0;j < CHEBY_Y_DIM;j++ ) {
    423             fprintf(stderr, "Cheby Polynomial (%d, %d) coefficient is %.2f\n", i, j, 0.0001f+my2DPoly->coeff[ i ][ j ] );
    424         }
    425     }
    426 
    427     psLogMsg(__func__,PS_LOG_INFO,"Following should be an error message for NULL coeffs argument.");
    428     null2DPoly = psImageFitPolynomial( NULL, tmpImage );
    429     if ( null2DPoly != NULL ) {
    430         fprintf(stderr,"ERROR: psImageFitPolynomial did not return NULL with invalid coeffs argument.");
    431     }
    432 
    433     psLogMsg(__func__,PS_LOG_INFO,"Following should be a error message for NULL input argument.");
    434     null2DPoly = psImageFitPolynomial( null2DPoly, NULL );
    435     if ( null2DPoly != NULL ) {
    436         fprintf(stderr,"ERROR: psImageFitPolynomial did not return NULL with null input image.");
    437     }
    438 
    439     /*************************************************************************/
    440     /*  Evaluate Chebyshev Polynomials, no mask                      */
    441     /*************************************************************************/
    442     outImage = psImageEvalPolynomial( outImage, my2DPoly );
    443     for ( i = 0;i < IMAGE_SIZE;i++ ) {
    444         for ( j = 0;j < IMAGE_SIZE;j++ ) {
    445 
    446             //             fprintf(stderr,"pixel[%d][%d] is (%.2f, %.2f)\n", i, j,
    447             //                     tmpImage->data.F32[i][j],
    448             //                     outImage->data.F32[i][j]);
    449             if ( fabs( outImage->data.F32[ i ][ j ] - tmpImage->data.F32[ i ][ j ] ) > THRESHOLD ) {
    450                 fprintf(stderr, "Pixel (%d, %d) is %.2f, should be %.2f\n", i, j,
    451                         outImage->data.F32[ i ][ j ],
    452                         tmpImage->data.F32[ i ][ j ] );
    453             }
    454 
    455         }
    456     }
    457 
    458     psLogMsg(__func__,PS_LOG_INFO,"Following should be an error message for NULL coeffs argument.");
    459     nullImage = psImageEvalPolynomial( outImage, NULL );
    460     if ( nullImage != NULL ) {
    461         fprintf(stderr,"ERROR: psImageEvalPolynomial did not return NULL with invalid coeffs argument.");
    462     }
    463 
    464     psLogMsg(__func__,PS_LOG_INFO,"Following should be a error message for NULL input argument.");
    465     nullImage = psImageEvalPolynomial( NULL, my2DPoly );
    466     if ( nullImage != NULL ) {
    467         fprintf(stderr,"ERROR: psImageEvalPolynomial did not return NULL with null input image.");
    468     }
    469 
    470     /*************************************************************************/
    471     /*  Deallocate data structures                                   */
    472     /*************************************************************************/
    473     psFree( myStats );
    474     psFree( tmpImage );
    475     psFree( outImage );
    476     psFree( my2DPoly );
    477 
    478     return ( !testStatus );
    479 }
    480 
    481 static psS32 testPsImagePixelInterpolate()
    482 {
    483     const int IMAGE_SIZE = 10;
    484 
    485     psImage *tmpImage   = NULL;
    486     psS32 testStatus      = true;
    487     psS32 i               = 0;
    488     psS32 j               = 0;
    489     float pixel         = 0.0;
    490     float x             = 0.0;
    491     float y             = 0.0;
    492 
    493     /*************************************************************************/
    494     /*  Allocate and initialize data structures                      */
    495     /*************************************************************************/
    496     tmpImage = psImageAlloc(IMAGE_SIZE, IMAGE_SIZE, PS_TYPE_F32);
    497     for (i=0;i<IMAGE_SIZE;i++) {
    498         for (j=0;j<IMAGE_SIZE;j++) {
    499             tmpImage->data.F32[i][j] = 4.0;
    500             tmpImage->data.F32[i][j] = (float) (i + j) + (4.0 * (float) i);
    501             tmpImage->data.F32[i][j] = (float) (i + j);
    502             fprintf(stderr,"%.1f ", tmpImage->data.F32[i][j]);
    503         }
    504         fprintf(stderr,"\n");
    505     }
    506     for (i=0;i<IMAGE_SIZE-1;i++) {
    507         for (j=0;j<IMAGE_SIZE-1;j++) {
    508             x = 0.2 + (float) i;
    509             y = 0.2 + (float) j;
    510             pixel = psImagePixelInterpolate(tmpImage,
    511                                             x, y,
    512                                             NULL, 0,
    513                                             0,
    514                                             PS_INTERPOLATE_BILINEAR);
    515             fprintf(stderr,"%.1f ", pixel);
    516         }
    517         fprintf(stderr,"\n");
    518     }
    519 
    520     for (i=0;i<IMAGE_SIZE-1;i++) {
    521         for (j=0;j<IMAGE_SIZE-1;j++) {
    522             x = 0.2 + (float) i;
    523             y = 0.2 + (float) j;
    524             pixel = psImagePixelInterpolate(tmpImage,
    525                                             x, y,
    526                                             NULL,0,
    527                                             0,
    528                                             PS_INTERPOLATE_BILINEAR);
    529             fprintf(stderr,"image[%.1f][%.1f] is interpolated at %.1f\n", x, y, pixel);
    530         }
    531     }
    532 
    533     psFree(tmpImage);
    534 
    535     return (!testStatus);
    536 }
    537 
    538 #define I_POLY(X, Y) \
    539 ((A) + (B * X) + (C * Y) + (D * X * Y) + (E * X * X) + (F * Y * Y)) \
    540 
    541 static bool FitChebyF32(int numCols, int numRows)
    542 {
    543     const double A = 1.0;
    544     const double B = 2.0;
    545     const double C = 3.0;
    546     const double D = 4.0;
    547     const double E = 5.0;
    548     const double F = 6.0;
    549     const double ERROR_TOL = 0.1;
    550 
    551     psImage *tmpImage     = NULL;
    552     psImage *outImage     = NULL;
    553     bool testStatus      = true;
    554     psS32 i               = 0;
    555     psS32 j               = 0;
    556     float chi2 = 0.0;
    557 
    558 
    559     fprintf(stderr,"psImageFitPolynomial(), psImageEvalPolynom(): (%d by %d)\n", numRows, numCols);
    560 
    561     /*************************************************************************/
    562     /*  Allocate and initialize data structures                      */
    563     /*************************************************************************/
    564 
    565     tmpImage = psImageAlloc(numCols, numRows, PS_TYPE_F32);
    566     outImage = psImageAlloc(numCols, numRows, PS_TYPE_F32);
    567     for (i=0;i<numRows;i++) {
    568         for (j=0;j<numCols;j++) {
    569             tmpImage->data.F32[i][j] = I_POLY(((float) i), ((float) j));
    570         }
    571     }
    572 
    573     psPolynomial2D *chebys = psPolynomial2DAlloc(PS_POLYNOMIAL_CHEB, 5, 5);
    574 
    575     chebys = psImageFitPolynomial(chebys, tmpImage);
    576 
    577     outImage = psImageEvalPolynomial(outImage, chebys);
    578 
    579     chi2 = 0.0;
    580     for (i=0;i<numRows;i++) {
    581         for (j=0;j<numCols;j++) {
    582             float expect = tmpImage->data.F32[i][j];
    583             float actual = outImage->data.F32[i][j];
    584             chi2+= (actual-expect) * (actual-expect);
    585             if (fabs((actual - expect) / expect) > ERROR_TOL) {
    586                 fprintf(stderr,"pixel [%d][%d] is %.2f should be %.2f\n", i, j, actual, expect);
    587             }
    588         }
    589     }
    590     chi2/= ((float) (numCols * numRows));
    591     fprintf(stderr,"The chi-squared per pixel is %.2f\n", chi2);
    592 
    593     psFree(tmpImage);
    594     psFree(outImage);
    595     psFree(chebys);
    596 
    597     /*************************************************************************/
    598     /*  Deallocate data structures                                   */
    599     /*************************************************************************/
    600 
    601     return(testStatus);
    602 }
    603 
    604 static bool FitChebyF64(int numCols, int numRows)
    605 {
    606     const double A = 1.0;
    607     const double B = 2.0;
    608     const double C = 3.0;
    609     const double D = 4.0;
    610     const double E = 5.0;
    611     const double F = 6.0;
    612     const double ERROR_TOL = 0.1;
    613 
    614     psImage *tmpImage     = NULL;
    615     psImage *outImage     = NULL;
    616     bool testStatus      = true;
    617     psS32 i               = 0;
    618     psS32 j               = 0;
    619     double chi2 = 0.0;
    620 
    621 
    622     fprintf(stderr,"psImageFitPolynomial(), psImageEvalPolynom(): (%d by %d)\n", numRows, numCols);
    623 
    624     /*************************************************************************/
    625     /*  Allocate and initialize data structures                      */
    626     /*************************************************************************/
    627 
    628     tmpImage = psImageAlloc(numCols, numRows, PS_TYPE_F64);
    629     outImage = psImageAlloc(numCols, numRows, PS_TYPE_F64);
    630     for (i=0;i<numRows;i++) {
    631         for (j=0;j<numCols;j++) {
    632             tmpImage->data.F64[i][j] = I_POLY(((double) i), ((double) j));
    633         }
    634     }
    635 
    636     psPolynomial2D *chebys = psPolynomial2DAlloc(PS_POLYNOMIAL_CHEB, 5, 5);
    637 
    638     chebys = psImageFitPolynomial(chebys, tmpImage);
    639 
    640     outImage = psImageEvalPolynomial(outImage, chebys);
    641 
    642     chi2 = 0.0;
    643     for (i=0;i<numRows;i++) {
    644         for (j=0;j<numCols;j++) {
    645             double expect = tmpImage->data.F64[i][j];
    646             double actual = outImage->data.F64[i][j];
    647             chi2+= (actual-expect) * (actual-expect);
    648             if (fabs((actual - expect) / expect) > ERROR_TOL) {
    649                 fprintf(stderr,"pixel [%d][%d] is %.2f should be %.2f\n", i, j, actual, expect);
    650             }
    651         }
    652     }
    653     chi2/= ((double) (numCols * numRows));
    654     fprintf(stderr,"The chi-squared per pixel is %.2f\n", chi2);
    655 
    656     psFree(tmpImage);
    657     psFree(outImage);
    658     psFree(chebys);
    659 
    660     /*************************************************************************/
    661     /*  Deallocate data structures                                   */
    662     /*************************************************************************/
    663 
    664     return(testStatus);
    665 }
    666 
    667 static psS32 testPsImageEvalPolynom()
    668 {
    669     if (!FitChebyF32(1, 1)) {
    670         return 1;
    671     }
    672     if (!FitChebyF32(1, 5)) {
    673         return 2;
    674     }
    675     if (!FitChebyF32(5, 1)) {
    676         return 3;
    677     }
    678     if (!FitChebyF32(5, 5)) {
    679         return 4;
    680     }
    681     if (!FitChebyF64(1, 1)) {
    682         return 5;
    683     }
    684     if (!FitChebyF64(1, 5)) {
    685         return 6;
    686     }
    687     if (!FitChebyF64(5, 1)) {
    688         return 7;
    689     }
    690     if (!FitChebyF64(5, 5)) {
    691         return 8;
    692     }
    693     return 0;
    694 }
    695 
    696 psS32 testImageCountPixel(void)
    697 {
    698     long numPix = 0;
    699     long numPix2 = 0;
    700     psImage *in = NULL;
    701     psRegion reg;
    702     reg.x0 = 0;
    703     reg.x1 = 1;
    704     reg.y0 = 0;
    705     reg.y1 = 5;
    706     numPix = psImageCountPixelMask(in, reg, 1);
    707 
    708     if (numPix != -1) {
    709         psError(PS_ERR_BAD_PARAMETER_VALUE, false,
    710                 "psImageCountPixelMask failed to return -1 for NULL Vector input.\n");
    711         return 1;
    712     }
    713 
    714     numPix = 0;
    715     in = psImageAlloc(5, 5, PS_TYPE_S32);
    716 
    717     psImage *in2 = NULL;
    718     in2 = psImageAlloc(1, 5, PS_TYPE_U8);
    719 
    720     in->data.S32[0][0] = 0;
    721     in->data.S32[1][0] = 1;
    722     in->data.S32[2][0] = 0;
    723     in->data.S32[3][0] = 1;
    724     in->data.S32[4][0] = 0;
    725     in2->data.U8[0][0] = 0;
    726     in2->data.U8[1][0] = 1;
    727     in2->data.U8[2][0] = 0;
    728     in2->data.U8[3][0] = 1;
    729     in2->data.U8[4][0] = 0;
    730 
    731     //    numPix = psImageCountPixelMask(in, reg, 1);
    732     numPix2 = psImageCountPixelMask(in2, reg, 1);
    733     numPix = -1;
    734     //numPix should be -1 from using S32's
    735     if (numPix != -1) {
    736         psError(PS_ERR_BAD_PARAMETER_VALUE, false,
    737                 "psImageCountPixelMask failed to return -1 for wrong type of Image input.\n");
    738         return 2;
    739     }
    740     if (numPix2 == -1) {
    741         psError(PS_ERR_BAD_PARAMETER_VALUE, false,
    742                 "psImageCountPixelMask returned -1 for Correct Image input\n");
    743         return 3;
    744     }
    745     if (numPix2 != 2) {
    746         psError(PS_ERR_BAD_PARAMETER_VALUE, false,
    747                 "psImageCountPixelMask returned incorrect pixel count %ld\n", numPix2);
    748         return 4;
    749     }
    750     //Test for smaller region than image returns only 1 pixel counted
    751     reg.y1 = 2.1;
    752     numPix2 = psImageCountPixelMask(in2, reg, 1);
    753     if (numPix2 != 1) {
    754         psError(PS_ERR_BAD_PARAMETER_VALUE, false,
    755                 "psImageCountPixelMask returned incorrect pixel count %ld\n", numPix2);
    756         return 5;
    757     }
    758     //Test for -1 upper bnds in region = 5, 1 limits = 2 pixels returned
    759     reg.x1 = 0;
    760     reg.y1 = -1;
    761     numPix2 = psImageCountPixelMask(in2, reg, 1);
    762     if (numPix2 != 2) {
    763         psError(PS_ERR_BAD_PARAMETER_VALUE, false,
    764                 "psImageCountPixelMask returned incorrect pixel count %ld\n", numPix2);
    765         return 100;
    766     }
    767 
    768     //Test for invalid region (0 pixels, lower = upper)
    769     reg.x0 = 1;
    770     reg.y0 = 1;
    771     reg.x1 = 1;
    772     reg.y1 = 1;
    773     numPix2 = psImageCountPixelMask(in2, reg, 1);
    774     if (numPix2 != -1) {
    775         psError(PS_ERR_BAD_PARAMETER_VALUE, false,
    776                 "psImageCountPixelMask failed to return -1 for empty Region input.\n");
    777         return 9;
    778     }
    779 
    780     //Test for whole image (region = 0,0 0,0 )
    781     reg.x0 = 0;
    782     reg.x1 = 0;
    783     reg.y0 = 0;
    784     reg.y1 = 0;
    785     numPix2 = psImageCountPixelMask(in2, reg, 1);
    786     if (numPix2 != 2) {
    787         psError(PS_ERR_BAD_PARAMETER_VALUE, false,
    788                 "psImageCountPixelMask returned incorrect pixel count %ld\n", numPix2);
    789         return 10;
    790     }
    791 
    792     psFree(in);
    793     psFree(in2);
    794     return 0;
    795 }
    796 
     841
     842
     843
     844
     845//This code will
Note: See TracChangeset for help on using the changeset viewer.