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