IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 14774


Ignore:
Timestamp:
Sep 7, 2007, 10:19:35 AM (19 years ago)
Author:
magnier
Message:

added additional functions to manage map construction

Location:
branches/eam_branch_20070830/psLib/src/imageops
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branch_20070830/psLib/src/imageops/psImageMap.c

    r14726 r14774  
    77 *  @author Eugene Magnier, IfA
    88 *
    9  *  @version $Revision: 1.1.2.4 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2007-09-03 20:29:07 $
     9 *  @version $Revision: 1.1.2.5 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2007-09-07 20:19:35 $
    1111 *
    1212 *  Copyright 2007 Institute for Astronomy, University of Hawaii
     
    2020#include "psError.h"
    2121#include "psAbort.h"
     22
     23#include "psFits.h"
    2224#include "psAssert.h"
    2325#include "psRegion.h"
     26#include "psFitsImage.h"
     27
    2428#include "psVector.h"
    2529#include "psImage.h"
     
    2832#include "psImageMap.h"
    2933#include "psImagePixelInterpolate.h"
     34#include "psImageUnbin.h"
    3035
    3136static void psImageMapFree(psImageMap *map) {
     
    3439
    3540    psFree (map->map);
    36     psFree (map->field);
     41    // psFree (map->field); XXX ??? this should be freed here, but that causes an error...
    3742    psFree (map->stats);
    3843    psFree (map->binning);
     
    5560
    5661    psImageBinningSetScale (map->binning, PS_IMAGE_BINNING_CENTER);
     62    psImageBinningSetSkip (map->binning, map->field);
    5763
    5864    return map;
    5965}
    6066
     67bool psImageMapModifyScale(psImageMap *map, int nXruff, int nYruff) {
     68
     69    assert (map);
     70
     71    map->binning->nXruff = nXruff;
     72    map->binning->nYruff = nYruff;
     73
     74    psImageRecycle (map->map, nXruff, nYruff, PS_TYPE_F32);
     75
     76    psImageBinningSetScale (map->binning, PS_IMAGE_BINNING_CENTER);
     77    psImageBinningSetSkip (map->binning, map->field);
     78
     79    return true;
     80}
     81
    6182// generate a psImageMap (or NULL) with the given number of superpixels in X and Y
    62 bool psImageMapGenerate (psImageMap *map, psVector *x, psVector *y, psVector *f, float badFrac) {
     83// this function returns an error if the output map has impossible holes
     84bool psImageMapGenerate (psImageMap *map, psVector *x, psVector *y, psVector *f, psVector *df, float badFrac) {
    6385
    6486    psImage *mask = psImageAlloc (map->map->numCols, map->map->numRows, PS_TYPE_MASK);
     
    7092    // we can do this by accumulating a vector of pixel indexes for each cell
    7193    psArray *pixelSets = psArrayAlloc (map->map->numCols*map->map->numRows);
    72     for (int i = 0; i < pixelsSets->n; i++) {
     94    for (int i = 0; i < pixelSets->n; i++) {
    7395        pixelSets->data[i] = psVectorAllocEmpty (4, PS_TYPE_S32);
    7496    }
    7597    // associate each value with a cell
    7698    for (int i = 0; i < x->n; i++) {
    77         // XXX use the psImageUnbin command to convert from x,y, to xRuff,yRuff?
    78         int xRuff = x->data.F32[i] / map->binning->nXbin;
    79         int yRuff = y->data.F32[i] / map->binning->nYbin;
     99        int xRuff = psImageBinningGetRuffX (map->binning, x->data.F32[i]);
     100        int yRuff = psImageBinningGetRuffY (map->binning, y->data.F32[i]);
    80101
    81102        int bin = xRuff + yRuff*map->map->numCols;
     103        assert (bin >= 0);
     104        assert (bin < pixelSets->n);
    82105       
    83106        psVector *pixels = pixelSets->data[bin];
    84107        pixels->data.S32[pixels->n] = i;
    85         psVectorExtend (vector, 4, 1);
    86     }
     108        psVectorExtend (pixels, 4, 1);
     109    }
     110
     111    // stats structure for getting the position centers
     112    psStats *meanStat = psStatsAlloc (PS_STAT_SAMPLE_MEAN);
    87113
    88114    // accumulate the x,y coords for each point to calculate the mean position.
     
    92118        for (int ix = 0; ix < Nx; ix++) {
    93119   
    94             // select the vector.  use psImageUnbin command to convert xRuff,yRuff to
    95             // xFine,yFine
     120            // pixel index for this cell
    96121            psVector *pixels = pixelSets->data[ix + iy*Nx];
    97122
    98             psVector xCell = psVectorAlloc (pixels->n, PS_TYPE_F32);
    99             psVector yCell = psVectorAlloc (pixels->n, PS_TYPE_F32);
    100             psVector fCell = psVectorAlloc (pixels->n, PS_TYPE_F32);
    101 
     123            // storage vectors
     124            psVector *xCell = psVectorAlloc (pixels->n, PS_TYPE_F32);
     125            psVector *yCell = psVectorAlloc (pixels->n, PS_TYPE_F32);
     126            psVector *fCell = psVectorAlloc (pixels->n, PS_TYPE_F32);
     127
     128            // error vector, if needed
     129            psVector *dfCell = NULL;
     130            if (df) {
     131                dfCell = psVectorAlloc (pixels->n, PS_TYPE_F32);
     132            }           
     133
     134            // collect data for this cell
    102135            for (int i = 0; i < pixels->n; i++) {
    103                 bin = pixels->data.S32[i];
    104                 xCell->data.F32[i] = x->data.F32[bin];
    105                 yCell->data.F32[i] = y->data.F32[bin];
    106                 fCell->data.F32[i] = f->data.F32[bin];
     136                int bin = pixels->data.S32[i];
     137                // convert x,y in the fine image to the ruff image
     138                xCell->data.F32[i]  = psImageBinningGetRuffX (map->binning, x->data.F32[bin]);
     139                yCell->data.F32[i]  = psImageBinningGetRuffY (map->binning, y->data.F32[bin]);
     140                fCell->data.F32[i]  = f->data.F32[bin];
     141                if (df) {
     142                    dfCell->data.F32[i] = df->data.F32[bin];
     143                }
    107144            }
    108145
     
    113150            // XXX need to supply a mask and skip the masked pixels when calculating the centroid
    114151            // this will not in general be properly weighted...
    115             if (psVectorStats (map->stats, fCell, NULL, NULL, 0)) {
     152            if (psVectorStats (map->stats, fCell, dfCell, NULL, 0)) {
    116153                mask->data.U8[iy][ix] = 0;
    117154                // XXX ensure only one option is selected, or save both position and width
     
    119156
    120157                // calculate the mean position and save:
     158                psStatsInit (meanStat);
    121159                psVectorStats (meanStat, xCell, NULL, NULL, 0);
    122160                xCoord->data.F32[iy][ix] = psStatsGetValue (meanStat, meanStat->options);
     161                psStatsInit (meanStat);
    123162                psVectorStats (meanStat, yCell, NULL, NULL, 0);
    124163                yCoord->data.F32[iy][ix] = psStatsGetValue (meanStat, meanStat->options);
     
    130169            psFree (yCell);
    131170            psFree (fCell);
    132         }
    133     }
    134 
    135     // at this point, for each map pixel, we have (f,x,y), or the pixel is masked.
    136 
     171            psFree (dfCell);
     172        }
     173    }
     174    psFree (pixelSets);
     175    psFree (meanStat);
     176   // at this point, for each map pixel, we have (f,x,y), or the pixel is masked.
     177
     178    psFits *fits = NULL;
     179
     180    fits = psFitsOpen ("imageMap.raw.fits", "w");
     181    psFitsWriteImage (fits, NULL, map->map, 0, NULL);
     182    psFitsClose (fits);
     183
     184    // did this analysis succeed?  (enough good or OK pixels?)
    137185    psImage *state = psImagePixelInterpolateState (&map->nBad, &map->nPoor, mask, 0xff);
    138186    map->nGood = mask->numCols * mask->numRows - map->nBad - map->nPoor;
    139187    if (map->nBad > badFrac * mask->numCols * mask->numRows) {
     188        psFree (xCoord);
     189        psFree (yCoord);
     190        psFree (mask);
    140191        return false;
    141192    }
     
    143194    // fit the valid pixels to (0,1,2) order polynomials, interpolate values to the pixel center
    144195    // XXX I need to be careful about the pixel coordinates: center is 0,0 or 0.5, 0.5?
    145     psImagePixelInterpolateCenters (map->map, xCoord, yCoord, state, mask, 0xff);
     196    psImagePixelInterpolateCenter (map->map, xCoord, yCoord, state, mask, 0xff);
    146197    psFree (xCoord);
    147198    psFree (yCoord);
    148199
     200    fits = psFitsOpen ("imageMap.ref.fits", "w");
     201    psFitsWriteImage (fits, NULL, map->map, 0, NULL);
     202    psFitsClose (fits);
     203
    149204    psImagePixelInterpolatePoor (map->map, state, mask, 0xff);
    150205
     206    fits = psFitsOpen ("imageMap.fix.fits", "w");
     207    psFitsWriteImage (fits, NULL, map->map, 0, NULL);
     208    psFitsClose (fits);
     209
     210    psFree (state);
     211    psFree (mask);
    151212    return true;
    152213}
     214
     215// using the points given, generate a map with maximum resolution that yields only good and ok pixels
     216bool psImageMapGenerateScale (psImageMap *map, psVector *x, psVector *y, psVector *f, psVector *df, float badFrac) {
     217   
     218    int nXruff, nYruff;
     219       
     220    while (!psImageMapGenerate (map, x, y, f, df, badFrac)) {
     221        // if we failed to build an acceptable map, decrease nXruff, nYruff as appropriate, and
     222        // try again...  try to keep the aspect ratio.
     223
     224        // if both axes are at 1, give up
     225        if ((map->binning->nXruff == 1) && (map->binning->nYruff == 1)) {
     226            return false;
     227        }
     228
     229        // if one axis is at 1, decrement the other
     230        if (map->binning->nXruff == 1) {
     231            nXruff = map->binning->nXruff;
     232            nYruff = map->binning->nYruff - 1;
     233            psImageMapModifyScale (map, nXruff, nYruff);
     234            continue;
     235        }
     236        if (map->binning->nYruff == 1) {
     237            nYruff = map->binning->nYruff;
     238            nXruff = map->binning->nXruff - 1;
     239            psImageMapModifyScale (map, nXruff, nYruff);
     240            continue;
     241        }
     242
     243        // otherwise, decrement the larger axis, and set the smaller based
     244        // on the aspect ratio
     245        float aRatio = map->binning->nXruff / map->binning->nYruff;
     246        if (map->binning->nXruff > map->binning->nYruff) {
     247            nXruff = map->binning->nXruff - 1;
     248            nYruff = (int)(0.5 + (nXruff / aRatio));
     249        } else {
     250            nYruff = map->binning->nYruff - 1;
     251            nXruff = (int)(0.5 + (nYruff * aRatio));
     252        }
     253
     254        psImageMapModifyScale (map, nXruff, nYruff);
     255    }
     256    return true;
     257}
     258
     259double psImageMapEval (psImageMap *map, float x, float y) {
     260
     261    double result;
     262
     263    result = psImageUnbinPixel(x, y, map->map, map->binning);
     264
     265    return result;
     266}
     267
     268psVector *psImageMapEvalVector (psImageMap *map, psVector *x, psVector *y) {
     269
     270    assert (x);
     271    assert (y);
     272    assert (x->n == y->n);
     273    assert (map);
     274
     275    psVector *result = psVectorAlloc (x->n, PS_TYPE_F32);
     276
     277    for (int i = 0; i < x->n; i++) {
     278        result->data.F32[i] = psImageUnbinPixel(x->data.F32[i], y->data.F32[i], map->map, map->binning);
     279    }
     280
     281    return result;
     282}
  • branches/eam_branch_20070830/psLib/src/imageops/psImageMap.h

    r14723 r14774  
    77 *  @author Eugene Magnier, IfA
    88 *
    9  *  @version $Revision: 1.1.2.2 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2007-09-02 02:03:58 $
     9 *  @version $Revision: 1.1.2.3 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2007-09-07 20:19:35 $
    1111 *
    1212 *  Copyright 2007 Institute for Astronomy, University of Hawaii
     
    3535psImageMap *psImageMapAlloc(psImage *field, psImageBinning *binning, psStats *stats) PS_ATTR_MALLOC;
    3636
     37bool psImageMapModifyScale(psImageMap *map, int nXruff, int nYruff);
     38
    3739// generate a psImageMap (or NULL) with the given number of superpixels in X and Y
    38 bool psImageMapGenerate (psImageMap *map, psVector *x, psVector *y, psVector *f, float badFrac);
     40bool psImageMapGenerate (psImageMap *map, psVector *x, psVector *y, psVector *f, psVector *df, float badFrac);
     41
     42bool psImageMapGenerateScale (psImageMap *map, psVector *x, psVector *y, psVector *f, psVector *df, float badFrac);
     43
     44// apply the psImageMap to the given coordinate (fine image pixels)
     45double psImageMapEval (psImageMap *map, float x, float y);
     46
     47// apply the psImageMap to the given coordinate vectors (fine image pixels)
     48psVector *psImageMapEvalVector (psImageMap *map, psVector *x, psVector *y);
    3949
    4050/// @}
Note: See TracChangeset for help on using the changeset viewer.