IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Dec 7, 2004, 3:38:05 PM (22 years ago)
Author:
Paul Price
Message:

Working, but not getting faint CRs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/stac/src/stacRejection.c

    r2500 r2661  
    22#include "pslib.h"
    33#include "stac.h"
     4
     5
     6#define MAX(x,y) ((x) > (y) ? (x) : (y))
     7#define MIN(x,y) ((x) < (y) ? (x) : (y))
     8
     9float stacGradient(psImage *image,      // Input for which to measure the gradient
     10                   int x, int y         // Coordinates at which to measure the gradient
     11    )
     12{
     13    float sum = 0.0;                    // The sum of surrounding pixels
     14    int num = 0;
     15    psVector *pixels = psVectorAlloc(9, PS_TYPE_F32); // Array of pixels
     16    psVector *mask = psVectorAlloc(9, PS_TYPE_U8); // Corresponding mask
     17
     18    // Get limits
     19    int xMin = MAX(x - 1, 0);
     20    int xMax = MIN(x + 1, image->numCols - 1);
     21    int yMin = MAX(y - 1, 0);
     22    int yMax = MIN(y + 1, image->numRows - 1);
     23    for (int j = yMin; j <= yMax; j++) {
     24        for (int i = xMin; i <= xMax; i++) {
     25            sum += image->data.F32[j][i];
     26            pixels->data.F32[num] = image->data.F32[j][i];
     27            mask->data.U8[num] = 1;
     28            num++;
     29        }
     30    }
     31#if 0
     32    sum -= image->data.F32[y][x];
     33    sum /= (float)(num-1);
     34//    sum -= image->data.F32[y][x];
     35    return sum / image->data.F32[y][x];
     36#endif
     37
     38    // Fill out the array
     39    for (int i = num; i < 9; i++) {
     40        pixels->data.F32[i] = 0;
     41        mask->data.U8[i] = 0;
     42    }
     43    psStats *stats = psStatsAlloc(PS_STAT_SAMPLE_MEDIAN);
     44    (void)psVectorStats(stats, pixels, mask, 0);
     45    float median = stats->sampleMedian;
     46    psFree(stats);
     47    psFree(pixels);
     48    psFree(mask);
     49    return median / image->data.F32[y][x];
     50}
     51
     52#if 0
     53float stacGradient(psImage *image,      // Input for which to measure the gradient
     54                   int x, int y         // Coordinates at which to measure the gradient
     55                   )
     56{
     57    float sum = 0.0;                    // The sum of surrounding pixels
     58    float maxDiff = 0.0;
     59    int num = 0;
     60    // Get limits
     61    int xMin = MAX(x - 1, 0);
     62    int xMax = MIN(x + 1, image->numCols - 1);
     63    int yMin = MAX(y - 1, 0);
     64    int yMax = MIN(y + 1, image->numRows - 1);
     65    for (int j = yMin; j <= yMax; j++) {
     66        for (int i = xMin; i <= xMax; i++) {
     67            if (image->data.F32[j][i] - image->data.F32[y][x] < maxDiff) {
     68                maxDiff = image->data.F32[j][i] - image->data.F32[y][x];
     69            }
     70            sum += image->data.F32[j][i];
     71            num++;
     72        }
     73    }
     74    sum -= image->data.F32[y][x];
     75    sum /= (float)(num-1);
     76    sum -= image->data.F32[y][x];
     77//    return sum / image->data.F32[y][x];
     78    return maxDiff / image->data.F32[y][x];
     79}   
     80#endif
    481
    582
     
    55132        int nxInput = ((psImage*)(inputs->data[i]))->numCols;
    56133        int nyInput = ((psImage*)(inputs->data[i]))->numRows;
    57         psImage *mask = psImageAlloc(nxInput, nyInput, PS_TYPE_F32); // The mask in the source frame
     134        psImage *mask = psImageAlloc(nxInput, nyInput, PS_TYPE_U8); // The pixel mask for the input
     135#ifdef TESTING
     136        psImage *rejmap = psImageAlloc(nxInput, nyInput, PS_TYPE_F32); // The rejections in the source frame
     137        psImage *grad = psImageAlloc(nxInput, nyInput, PS_TYPE_F32);    // The gradient image
     138#endif
    58139        psImage *reject = rejected->data[i]; // Pull out the mask in the output frame
    59140        psPlaneTransform *map = maps->data[i]; // The map from input to output
    60141
    61142        psTrace("stac.rejection", 3, "Transforming rejection mask %d....\n", i);
     143        int nBad = 0;                   // Number of bad pixels
     144
     145#ifdef CRFLUX
     146        // Set up CR output
     147        FILE *crs = NULL;               // File for outputting details of rejected pixels
     148        char crfile[MAXCHAR];   // Filename
     149        sprintf(crfile,"%s.cr",config->inputs->data[i]);
     150        if ((crs = fopen(crfile, "w")) == NULL) {
     151            psError("stac.rejection","Unable to open file for detailed output, %s\n");
     152            return NULL;
     153        }
     154#endif
    62155
    63156        // Transform the mask
     
    67160        for (int y = 0; y < nyInput; y++) {
    68161            for (int x = 0; x < nxInput; x++) {
    69                 inCoords->x = (double)x + 0.5;
    70                 inCoords->y = (double)y + 0.5;
     162                inCoords->x = (double)x;
     163                inCoords->y = (double)y;
    71164                (void)psPlaneTransformApply(outCoords, map, inCoords);
    72                 mask->data.F32[y][x] = psImagePixelInterpolate(reject, outCoords->x, outCoords->y,
     165                float maskVal = (float)psImagePixelInterpolate(reject, outCoords->x, outCoords->y,
    73166                                                               NULL, 0, 0.0, PS_INTERPOLATE_BILINEAR);
     167#ifdef TESTING
     168                rejmap->data.F32[y][x] = maskVal;
     169                grad->data.F32[y][x] = stacGradient(inputs->data[i], x, y);
     170#endif
     171                // Check threshold, then check gradient
     172                if ((maskVal > config->frac) && (stacGradient(inputs->data[i], x, y) < config->grad)) {
     173                    mask->data.U8[y][x] = 1;
     174                    nBad++;
     175#ifdef CRFLUX
     176                    fprintf(crs, "%d %d --> %f %f %f\n", x, y, ((psImage*)(inputs->data[i]))->data.F32[y][x],
     177                            maskVal, stacGradient(inputs->data[i], x, y));
     178#endif
     179                } else {
     180                    mask->data.U8[y][x] = 0;
     181                }
    74182
    75183            }
    76         }
     184        } // Iterating over pixels
     185
     186#ifdef CRFLUX
     187        // Close file used for detailed output
     188        fclose(crs);
     189#endif
     190
     191        psTrace("stac.rejection", 1, "%d pixels masked in image %d.\n", nBad, i);
     192        // Clip the image, and convert to suitable mask format
    77193
    78194#ifdef TESTING
    79195        // Write error image out to check
    80         char maskfile[MAXCHAR]; // Filename of mask image
     196        char maskfile[MAXCHAR];         // Filename of mask image
     197        char rejmapfile[MAXCHAR];       // Filename of rejection image
     198        char gradfile[MAXCHAR];         // Filename of gradient image
    81199        sprintf(maskfile,"%s.mask",config->inputs->data[i]);
     200        sprintf(rejmapfile,"%s.rejmap",config->inputs->data[i]);
     201        sprintf(gradfile,"%s.grad",config->inputs->data[i]);
    82202        psImageWriteSection(mask,0,0,0,NULL,0,maskfile);
    83 #endif
    84 
    85         // Clip the image, and convert to suitable mask format
    86         (void)psImageClip(mask, config->frac, 0.0, config->frac, 1.0);
    87         psImage *maskU8 = psImageCopy(NULL, mask, PS_TYPE_U8);
    88         psFree(mask);
    89    
     203        psImageWriteSection(rejmap,0,0,0,NULL,0,rejmapfile);
     204        psImageWriteSection(grad,0,0,0,NULL,0,gradfile);
     205        psFree(rejmap);
     206        psFree(grad);
     207#endif
     208
    90209        // Stuff into the array
    91         inputRej->data[i] = maskU8;
    92 
    93     }
     210        inputRej->data[i] = mask;
     211
     212    }
     213
    94214
    95215    psFree(inCoords);
Note: See TracChangeset for help on using the changeset viewer.