Changeset 27417 for trunk/psModules/src/imcombine/pmStack.c
- Timestamp:
- Mar 23, 2010, 3:23:07 PM (16 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
psModules/src/imcombine/pmStack.c (modified) (28 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:mergeinfo changed
/branches/pap_stack (added) merged: 27401,27405-27406,27409-27416
- Property svn:mergeinfo changed
-
trunk/psModules/src/imcombine/pmStack.c
r27402 r27417 35 35 36 36 //#define TESTING // Enable test output 37 //#define TEST_X 2148-1 // x coordinate to examine38 //#define TEST_Y 248-1 // y coordinate to examine37 //#define TEST_X 843-1 // x coordinate to examine 38 //#define TEST_Y 813-1 // y coordinate to examine 39 39 //#define TEST_RADIUS 0 // Radius to examine 40 40 … … 46 46 psVector *variances; // Pixel variances 47 47 psVector *weights; // Pixel weightings 48 psVector *exps; // Pixel exposures 48 49 psVector *sources; // Pixel sources (which image did they come from?) 49 50 psVector *limits; // Rejection limits … … 57 58 psFree(buffer->variances); 58 59 psFree(buffer->weights); 60 psFree(buffer->exps); 59 61 psFree(buffer->sources); 60 62 psFree(buffer->limits); … … 73 75 buffer->variances = psVectorAlloc(numImages, PS_TYPE_F32); 74 76 buffer->weights = psVectorAlloc(numImages, PS_TYPE_F32); 77 buffer->exps = psVectorAlloc(numImages, PS_TYPE_F32); 75 78 buffer->sources = psVectorAlloc(numImages, PS_TYPE_U16); 76 79 buffer->limits = psVectorAlloc(numImages, PS_TYPE_F32); … … 95 98 static bool combinationMeanVariance(float *mean, // Mean value, to return 96 99 float *var, // Variance value, to return 100 float *exp, // Exposure time, to return 101 float *expWeight, // Weighted exposure time, to return 97 102 const psVector *values, // Values to combine 98 103 const psVector *variances, // Pixel variances to combine 104 const psVector *exps, // Exposure times to combine 99 105 const psVector *weights // Weights to apply 100 106 ) … … 121 127 float sumVarianceWeight = 0.0; // Sum of the pixel variances multiplied by the global weights 122 128 float sumWeight = 0.0; // Sum of the image weights 129 float sumExp = 0.0; // Sum of the exposure time 130 float sumExpWeight = 0.0; // Sum of the exposure time multiplied by the global weights 131 int numGood = 0; // Number of good exposures 123 132 for (int i = 0; i < values->n; i++) { 124 133 sumValueWeight += values->data.F32[i] * weights->data.F32[i]; … … 127 136 sumVarianceWeight += variances->data.F32[i] * PS_SQR(weights->data.F32[i]); 128 137 } 138 if (exps) { 139 sumExp += exps->data.F32[i]; 140 sumExpWeight += exps->data.F32[i] * weights->data.F32[i]; 141 numGood++; 142 } 129 143 } 130 144 … … 136 150 if (var) { 137 151 *var = sumVarianceWeight / PS_SQR(sumWeight); 152 } 153 if (exp) { 154 *exp = sumExp; 155 } 156 if (expWeight) { 157 *expWeight = sumExpWeight; 138 158 } 139 159 return true; … … 276 296 const psArray *inputs, // Stack data 277 297 const psVector *weights, // Global (single value) weights for data, or NULL 298 const psVector *exps, // Exposures for data, or NULL 278 299 const psVector *addVariance, // Additional variance for data 279 300 const psVector *reject, // Indices of pixels to reject, or NULL … … 292 313 psVector *pixelVariances = variance ? buffer->variances : NULL; // Variances for the pixel of interest 293 314 psVector *pixelWeights = buffer->weights; // Image weights for the pixel of interest 315 psVector *pixelExps = buffer->exps; // Exposure times 294 316 psVector *pixelSources = buffer->sources; // Sources for the pixel of interest 295 317 psVector *pixelLimits = buffer->limits; // Limits for the pixel of interest … … 331 353 } 332 354 pixelWeights->data.F32[numGood] = data->weight; 355 pixelExps->data.F32[numGood] = data->exp; 333 356 pixelSources->data.U16[numGood] = i; 334 357 numGood++; … … 347 370 if (PS_SQR(x - TEST_X) + PS_SQR(y - TEST_Y) <= PS_SQR(TEST_RADIUS)) { 348 371 for (int i = 0; i < numGood; i++) { 349 fprintf(stderr, "Input %d, pixel %d,%d (%" PRIu16 "): %f %f (%f) %f % d\n",372 fprintf(stderr, "Input %d, pixel %d,%d (%" PRIu16 "): %f %f (%f) %f %f %d\n", 350 373 i, x, y, pixelSources->data.U16[i], pixelData->data.F32[i], pixelVariances->data.F32[i], 351 addVariance->data.F32[i], pixelWeights->data.F32[i], pixelSuspects->data.U8[i]); 374 addVariance->data.F32[i], pixelWeights->data.F32[i], pixelExps->data.F32[i], 375 pixelSuspects->data.U8[i]); 352 376 } 353 377 } … … 362 386 psImage *mask, // Combined mask, for output 363 387 psImage *variance, // Combined variance map, for output 388 psImage *exp, // Exposure map (time), for output 389 psImage *expnum, // Exposure map (number) for output 390 psImage *expweight, // Exposure map (weighted time) for output 364 391 int num, // Number of good pixels 365 392 combineBuffer *buffer, // Buffer with vectors 366 393 int x, int y, // Coordinates of interest; frame of output image 367 394 psImageMaskType bad, // Value for bad pixels 368 bool safe // Safe combination? 395 bool safe, // Safe combination? 396 float invTotalWeight // Inverse of total weight for all inputs 369 397 ) 370 398 { … … 372 400 psVector *pixelVariances = variance ? buffer->variances : NULL; // Variances for the pixel of interest 373 401 psVector *pixelWeights = buffer->weights; // Image weights for the pixel of interest 402 psVector *pixelExps = buffer->exps; // Exposure times 374 403 375 404 // Default option is that the pixel is bad 376 405 float imageValue = NAN, varianceValue = NAN; // Value for combined image and variance map 377 406 psImageMaskType maskValue = bad; // Value for combined mask 407 float expValue = 0.0, expWeightValue = NAN; // Exposure value (straight, and weighted) 408 378 409 switch (num) { 379 410 case 0: { … … 393 424 varianceValue = pixelVariances->data.F32[0]; 394 425 } 426 if (exp) { 427 expValue = pixelExps->data.F32[0]; 428 expWeightValue = pixelExps->data.F32[0]; 429 } 395 430 maskValue = 0; 396 431 #ifdef TESTING … … 411 446 // Automatically accept the mean of the pixels only if we're not playing safe 412 447 if (!safe) { 413 float mean, var; // Mean and variance from combination 414 if (combinationMeanVariance(&mean, &var, pixelData, pixelVariances, pixelWeights)) { 415 imageValue = mean; 416 varianceValue = var; 448 if (combinationMeanVariance(&imageValue, &varianceValue, &expValue, &expWeightValue, 449 pixelData, pixelVariances, pixelExps, pixelWeights)) { 417 450 maskValue = 0; 418 451 #ifdef TESTING 419 452 if (PS_SQR(x - TEST_X) + PS_SQR(y - TEST_Y) <= PS_SQR(TEST_RADIUS)) { 420 453 fprintf(stderr, "Two inputs to combine using unsafe, pixel %d,%d --> %f %f\n", 421 x, y, mean, var);454 x, y, imageValue, varianceValue); 422 455 } 423 456 #endif … … 435 468 default: { 436 469 // Can combine without too much worrying 437 float mean, var; // Mean and variance of the combination438 if (!combinationMeanVariance(&mean, &var, pixelData, pixelVariances, pixelWeights)) {470 if (!combinationMeanVariance(&imageValue, &varianceValue, &expValue, &expWeightValue, 471 pixelData, pixelVariances, pixelExps, pixelWeights)) { 439 472 break; 440 473 } 441 imageValue = mean;442 varianceValue = var;443 474 maskValue = 0; 444 475 #ifdef TESTING 445 476 if (PS_SQR(x - TEST_X) + PS_SQR(y - TEST_Y) <= PS_SQR(TEST_RADIUS)) { 446 fprintf(stderr, "Combined inputs, pixel %d,%d --> %f %f\n", x, y, mean, var);477 fprintf(stderr, "Combined inputs, pixel %d,%d --> %f %f\n", x, y, imageValue, varianceValue); 447 478 } 448 479 #endif … … 456 487 variance->data.F32[y][x] = varianceValue; 457 488 } 458 459 #ifdef TESTING 460 mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] = num; 461 #endif 462 489 if (exp) { 490 exp->data.F32[y][x] = expValue; 491 } 492 if (expnum) { 493 expnum->data.PS_TYPE_IMAGE_MASK_DATA[y][x] = num; 494 } 495 if (expweight) { 496 expweight->data.F32[y][x] = expWeightValue * invTotalWeight; 497 } 463 498 464 499 return; … … 803 838 int *numCols, int *numRows, // Size of (sky) images 804 839 const psArray *input, // Input array of pmStackData to validate 805 const pmReadout *output // Output readout 840 const pmReadout *output, // Output readout 841 const pmReadout *exp // Exposure map 806 842 ) 807 843 { … … 869 905 } 870 906 907 if (exp) { 908 PM_ASSERT_READOUT_NON_NULL(exp, false); 909 if (exp->image) { 910 PS_ASSERT_IMAGES_SIZE_EQUAL(exp->image, output->image, false); 911 } 912 if (exp->mask) { 913 PS_ASSERT_IMAGES_SIZE_EQUAL(exp->mask, output->image, false); 914 } 915 } 916 871 917 return true; 872 918 } … … 937 983 938 984 /// Constructor 939 pmStackData *pmStackDataAlloc(pmReadout *readout, float weight, float addVariance)985 pmStackData *pmStackDataAlloc(pmReadout *readout, float weight, float exp, float addVariance) 940 986 { 941 987 pmStackData *data = psAlloc(sizeof(pmStackData)); // Stack data, to return … … 946 992 data->inspect = NULL; 947 993 data->weight = weight; 994 data->exp = exp; 948 995 data->addVariance = addVariance; 949 996 … … 952 999 953 1000 /// Stack input images 954 bool pmStackCombine(pmReadout *combined, psArray *input, psImageMaskType maskVal, psImageMaskType maskSuspect, 955 psImageMaskType bad, int kernelSize, float iter, float rej, float sys, float olympic, 1001 bool pmStackCombine(pmReadout *combined, pmReadout *expmaps, psArray *input, 1002 psImageMaskType maskVal, psImageMaskType maskSuspect, 1003 psImageMaskType bad, int kernelSize, 1004 float iter, float rej, float sys, float olympic, 956 1005 bool useVariance, bool safe, bool rejection) 957 1006 { … … 959 1008 int num; // Number of inputs 960 1009 int numCols, numRows; // Size of (sky) images 961 if (!validateInputData(&haveVariances, &num, &numCols, &numRows, input, combined )) {1010 if (!validateInputData(&haveVariances, &num, &numCols, &numRows, input, combined, expmaps)) { 962 1011 return false; 963 1012 } … … 977 1026 psVector *addVariance = psVectorAlloc(num, PS_TYPE_F32); // Additional variance for each image 978 1027 psVector *weights = psVectorAlloc(num, PS_TYPE_F32); // Relative weighting for each image 1028 psVector *exps = psVectorAlloc(num, PS_TYPE_F32); // Exposure times for each image 979 1029 psArray *stack = psArrayAlloc(num); // Stack of readouts 1030 float totalExpWeight = 0.0; // Total value of all weighted exposure times 1031 float totalExp = 0.0; // Total exposure time 980 1032 for (int i = 0; i < num; i++) { 981 1033 pmStackData *data = input->data[i]; // Stack data for this input 982 1034 if (!data) { 983 1035 weights->data.F32[i] = 0.0; 1036 exps->data.F32[i] = NAN; 984 1037 continue; 985 1038 } 986 1039 weights->data.F32[i] = data->weight; 1040 exps->data.F32[i] = data->exp; 1041 totalExp += exps->data.F32[i]; 1042 totalExpWeight += exps->data.F32[i] * weights->data.F32[i]; 987 1043 pmReadout *ro = data->readout; // Readout of interest 988 1044 stack->data[i] = psMemIncrRefCounter(ro); … … 1003 1059 } 1004 1060 } 1061 totalExpWeight = totalExp / totalExpWeight; // Convert to inverse 1005 1062 1006 1063 int minInputCols, maxInputCols, minInputRows, maxInputRows; // Smallest and largest values to combine … … 1045 1102 combined->variance = psImageAlloc(numCols, numRows, PS_TYPE_F32); 1046 1103 combinedVariance = combined->variance; 1104 } 1105 1106 psImage *exp = NULL, *expnum = NULL, *expweight = NULL; // Exposure map and exposure number 1107 if (expmaps) { 1108 if (!expmaps->image) { 1109 expmaps->image = psImageAlloc(numCols, numRows, PS_TYPE_F32); 1110 } 1111 exp = expmaps->image; 1112 1113 if (!expmaps->mask) { 1114 expmaps->mask = psImageAlloc(numCols, numRows, PS_TYPE_IMAGE_MASK); 1115 } 1116 expnum = expmaps->mask; 1117 1118 if (!expmaps->variance) { 1119 expmaps->variance = psImageAlloc(numCols, numRows, PS_TYPE_F32); 1120 } 1121 expweight = expmaps->variance; 1047 1122 } 1048 1123 … … 1083 1158 bool suspect; // Suspect pixels in stack? 1084 1159 combineExtract(&num, &suspect, buffer, combinedImage, combinedMask, combinedVariance, 1085 input, weights, addVariance, reject, x, y, maskVal, maskSuspect); 1086 combinePixels(combinedImage, combinedMask, combinedVariance, num, buffer, x, y, bad, safe); 1160 input, weights, exps, addVariance, reject, x, y, maskVal, maskSuspect); 1161 combinePixels(combinedImage, combinedMask, combinedVariance, exp, expnum, expweight, 1162 num, buffer, x, y, bad, safe, totalExpWeight); 1087 1163 1088 1164 if (iter > 0) {
Note:
See TracChangeset
for help on using the changeset viewer.
