Changeset 14774
- Timestamp:
- Sep 7, 2007, 10:19:35 AM (19 years ago)
- Location:
- branches/eam_branch_20070830/psLib/src/imageops
- Files:
-
- 2 edited
-
psImageMap.c (modified) (11 diffs)
-
psImageMap.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branch_20070830/psLib/src/imageops/psImageMap.c
r14726 r14774 7 7 * @author Eugene Magnier, IfA 8 8 * 9 * @version $Revision: 1.1.2. 4$ $Name: not supported by cvs2svn $10 * @date $Date: 2007-09-0 3 20:29:07$9 * @version $Revision: 1.1.2.5 $ $Name: not supported by cvs2svn $ 10 * @date $Date: 2007-09-07 20:19:35 $ 11 11 * 12 12 * Copyright 2007 Institute for Astronomy, University of Hawaii … … 20 20 #include "psError.h" 21 21 #include "psAbort.h" 22 23 #include "psFits.h" 22 24 #include "psAssert.h" 23 25 #include "psRegion.h" 26 #include "psFitsImage.h" 27 24 28 #include "psVector.h" 25 29 #include "psImage.h" … … 28 32 #include "psImageMap.h" 29 33 #include "psImagePixelInterpolate.h" 34 #include "psImageUnbin.h" 30 35 31 36 static void psImageMapFree(psImageMap *map) { … … 34 39 35 40 psFree (map->map); 36 psFree (map->field);41 // psFree (map->field); XXX ??? this should be freed here, but that causes an error... 37 42 psFree (map->stats); 38 43 psFree (map->binning); … … 55 60 56 61 psImageBinningSetScale (map->binning, PS_IMAGE_BINNING_CENTER); 62 psImageBinningSetSkip (map->binning, map->field); 57 63 58 64 return map; 59 65 } 60 66 67 bool 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 61 82 // 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 84 bool psImageMapGenerate (psImageMap *map, psVector *x, psVector *y, psVector *f, psVector *df, float badFrac) { 63 85 64 86 psImage *mask = psImageAlloc (map->map->numCols, map->map->numRows, PS_TYPE_MASK); … … 70 92 // we can do this by accumulating a vector of pixel indexes for each cell 71 93 psArray *pixelSets = psArrayAlloc (map->map->numCols*map->map->numRows); 72 for (int i = 0; i < pixel sSets->n; i++) {94 for (int i = 0; i < pixelSets->n; i++) { 73 95 pixelSets->data[i] = psVectorAllocEmpty (4, PS_TYPE_S32); 74 96 } 75 97 // associate each value with a cell 76 98 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]); 80 101 81 102 int bin = xRuff + yRuff*map->map->numCols; 103 assert (bin >= 0); 104 assert (bin < pixelSets->n); 82 105 83 106 psVector *pixels = pixelSets->data[bin]; 84 107 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); 87 113 88 114 // accumulate the x,y coords for each point to calculate the mean position. … … 92 118 for (int ix = 0; ix < Nx; ix++) { 93 119 94 // select the vector. use psImageUnbin command to convert xRuff,yRuff to 95 // xFine,yFine 120 // pixel index for this cell 96 121 psVector *pixels = pixelSets->data[ix + iy*Nx]; 97 122 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 102 135 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 } 107 144 } 108 145 … … 113 150 // XXX need to supply a mask and skip the masked pixels when calculating the centroid 114 151 // 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)) { 116 153 mask->data.U8[iy][ix] = 0; 117 154 // XXX ensure only one option is selected, or save both position and width … … 119 156 120 157 // calculate the mean position and save: 158 psStatsInit (meanStat); 121 159 psVectorStats (meanStat, xCell, NULL, NULL, 0); 122 160 xCoord->data.F32[iy][ix] = psStatsGetValue (meanStat, meanStat->options); 161 psStatsInit (meanStat); 123 162 psVectorStats (meanStat, yCell, NULL, NULL, 0); 124 163 yCoord->data.F32[iy][ix] = psStatsGetValue (meanStat, meanStat->options); … … 130 169 psFree (yCell); 131 170 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?) 137 185 psImage *state = psImagePixelInterpolateState (&map->nBad, &map->nPoor, mask, 0xff); 138 186 map->nGood = mask->numCols * mask->numRows - map->nBad - map->nPoor; 139 187 if (map->nBad > badFrac * mask->numCols * mask->numRows) { 188 psFree (xCoord); 189 psFree (yCoord); 190 psFree (mask); 140 191 return false; 141 192 } … … 143 194 // fit the valid pixels to (0,1,2) order polynomials, interpolate values to the pixel center 144 195 // XXX I need to be careful about the pixel coordinates: center is 0,0 or 0.5, 0.5? 145 psImagePixelInterpolateCenter s(map->map, xCoord, yCoord, state, mask, 0xff);196 psImagePixelInterpolateCenter (map->map, xCoord, yCoord, state, mask, 0xff); 146 197 psFree (xCoord); 147 198 psFree (yCoord); 148 199 200 fits = psFitsOpen ("imageMap.ref.fits", "w"); 201 psFitsWriteImage (fits, NULL, map->map, 0, NULL); 202 psFitsClose (fits); 203 149 204 psImagePixelInterpolatePoor (map->map, state, mask, 0xff); 150 205 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); 151 212 return true; 152 213 } 214 215 // using the points given, generate a map with maximum resolution that yields only good and ok pixels 216 bool 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 259 double 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 268 psVector *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 7 7 * @author Eugene Magnier, IfA 8 8 * 9 * @version $Revision: 1.1.2. 2$ $Name: not supported by cvs2svn $10 * @date $Date: 2007-09-0 2 02:03:58$9 * @version $Revision: 1.1.2.3 $ $Name: not supported by cvs2svn $ 10 * @date $Date: 2007-09-07 20:19:35 $ 11 11 * 12 12 * Copyright 2007 Institute for Astronomy, University of Hawaii … … 35 35 psImageMap *psImageMapAlloc(psImage *field, psImageBinning *binning, psStats *stats) PS_ATTR_MALLOC; 36 36 37 bool psImageMapModifyScale(psImageMap *map, int nXruff, int nYruff); 38 37 39 // 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); 40 bool psImageMapGenerate (psImageMap *map, psVector *x, psVector *y, psVector *f, psVector *df, float badFrac); 41 42 bool 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) 45 double psImageMapEval (psImageMap *map, float x, float y); 46 47 // apply the psImageMap to the given coordinate vectors (fine image pixels) 48 psVector *psImageMapEvalVector (psImageMap *map, psVector *x, psVector *y); 39 49 40 50 /// @}
Note:
See TracChangeset
for help on using the changeset viewer.
