Changeset 9336 for trunk/psModules/src/detrend/pmShutterCorrection.c
- Timestamp:
- Oct 5, 2006, 5:06:30 PM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/psModules/src/detrend/pmShutterCorrection.c
r9315 r9336 60 60 #include <pslib.h> 61 61 62 #include "pmFPA.h" 62 63 #include "pmShutterCorrection.h" 63 64 #include "psVectorBracket.h" … … 193 194 const psVector *counts, 194 195 const psVector *cntError, 195 float offref 196 const psVector *mask, 197 float offref, 198 int nIter, 199 float rej, 200 psMaskType maskVal 196 201 ) 197 202 { … … 232 237 line->coeff[1][1] = 0; 233 238 234 if (!psVectorFitPolynomial2D(line, NULL, 0, counts, cntError, x, y)) { 239 psStats *stats = psStatsAlloc(0); 240 stats->clipSigma = rej; 241 stats->clipIter = nIter; 242 243 if (!psVectorClipFitPolynomial2D(line, stats, mask, maskVal, counts, cntError, x, y)) { 235 244 psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to fit shutter correction.\n"); 245 psFree(stats); 236 246 psFree(x); 237 247 psFree(y); … … 239 249 return NULL; 240 250 } 251 psFree(stats); 241 252 242 253 pmShutterCorrection *corr = pmShutterCorrectionAlloc(); … … 355 366 psStatsOptions meanStat, // Statistic to use for mean 356 367 psStatsOptions stdevStat, // Statistic to use for stdev 368 int nIter, // Number of iterations per pixel 369 float rej, // Rejection limit 357 370 psMaskType maskVal // Mask value 358 371 ) … … 382 395 if (!image) { 383 396 continue; 397 } 398 if (image->type.type != PS_TYPE_F32) { 399 psError(PS_ERR_BAD_PARAMETER_TYPE, true, "Bad type for image: %x\n", image->type.type); 400 goto MEASURE_ERROR; 384 401 } 385 402 if (numRows == 0 || numCols == 0) { … … 401 418 goto MEASURE_ERROR; 402 419 } 420 if (masks) { 421 psImage *mask = masks->data[i]; 422 if (mask) { 423 if (mask->type.type != PS_TYPE_U8) { 424 psError(PS_ERR_BAD_PARAMETER_TYPE, true, "Bad type for mask: %x\n", mask->type.type); 425 goto MEASURE_ERROR; 426 } 427 if (mask->numRows != numRows || mask->numCols != numCols) { 428 psError(PS_ERR_BAD_PARAMETER_SIZE, true, 429 "Mask sizes don't match: %dx%d vs %dx%d\n", mask->numCols, mask->numRows, 430 numCols, numRows); 431 goto MEASURE_ERROR; 432 } 433 } 434 } 403 435 404 436 // Measure statistics … … 407 439 } 408 440 refs->data.F32[i] = psStatsGetValue(stats, meanStat); 441 psTrace("psModules.detrend", 3, "Reference value for image %ld = %f\n", i, refs->data.F32[i]); 409 442 if (refs->data.F32[i] <= 0.0) { 410 443 psError(PS_ERR_UNKNOWN, true, "Measured non-positive reference value.\n"); … … 424 457 samplesMean->data.F32[j][i] = psStatsGetValue(stats, meanStat) * refs->data.F32[i]; 425 458 samplesStdev->data.F32[j][i] = psStatsGetValue(stats, stdevStat) * refs->data.F32[i]; 459 psTrace("psModules.detrend", 5, "Image %ld, sample %d: %f +/- %f\n", i, j, 460 samplesMean->data.F32[j][i], samplesStdev->data.F32[j][i]); 426 461 } 427 462 } … … 437 472 errors = psImageRow(errors, samplesStdev, i); 438 473 pmShutterCorrection *guess = pmShutterCorrectionGuess(exptimes, counts); // Guess at correction 439 pmShutterCorrection *params = pmShutterCorrectionFullFit(exptimes, counts, errors, guess); // Correc'n 440 if (isfinite(params->offref)) { 441 meanRef += params->offref; 474 pmShutterCorrection *corr = pmShutterCorrectionFullFit(exptimes, counts, errors, guess); // Correct'n 475 psTrace("psModules.detrend", 5, "Sample reference value: %f\n", corr->offref); 476 if (isfinite(corr->offref)) { 477 meanRef += corr->offref; 442 478 numGood++; 443 479 } 444 psFree( params);480 psFree(corr); 445 481 psFree(guess); 446 482 } … … 455 491 } 456 492 meanRef /= (float)numGood; 493 psTrace("psModules.detrend", 3, "Mean reference value: %f\n", meanRef); 457 494 458 495 psImage *shutter = psImageAlloc(numCols, numRows, PS_TYPE_F32); // Shutter correction image 459 496 //psImage *pattern = psImageAlloc(numCols, numRows, PS_TYPE_F32); // Illumination pattern 497 psVector *mask = psVectorAlloc(num, PS_TYPE_U8); // Mask for each image 498 mask->n = num; 499 psVectorInit(mask, 0); 500 psTrace("psModules.detrend", 2, "Performing linear fit on individual pixels...\n"); 460 501 for (int y = 0; y < numRows; y++) { 461 502 for (int x = 0; x < numCols; x++) { 462 503 for (int i = 0; i < num; i++) { 463 504 psImage *image = images->data[i]; // Image of interest 505 psImage *maskImage; // Mask image 506 if (masks && (maskImage = masks->data[i])) { 507 mask->data.U8[i] = maskImage->data.U8[y][x]; 508 } 464 509 counts->data.F32[i] = image->data.F32[y][x] * refs->data.F32[i]; 465 510 errors->data.F32[i] = sqrtf(image->data.F32[y][x]) * refs->data.F32[i]; 466 511 } 467 512 468 pmShutterCorrection *corr = pmShutterCorrectionLinFit(exptimes, counts, errors, meanRef); 513 pmShutterCorrection *corr = pmShutterCorrectionLinFit(exptimes, counts, errors, mask, meanRef, 514 nIter, rej, maskVal); 469 515 shutter->data.F32[y][x] = corr->offset; 470 516 //pattern->data.F32[y][x] = corr->scale; … … 488 534 return NULL; 489 535 } 536 537 538 bool pmShutterCorrectionApply(pmReadout *readout, // Readout to which to apply shutter correction 539 const pmReadout *shutter // Shutter correction readout 540 ) 541 { 542 PS_ASSERT_PTR_NON_NULL(readout, false); 543 PS_ASSERT_PTR_NON_NULL(shutter, false); 544 PS_ASSERT_IMAGE_NON_NULL(readout->image, false); 545 PS_ASSERT_IMAGE_NON_NULL(shutter->image, false); 546 PS_ASSERT_IMAGE_TYPE(readout->image, PS_TYPE_F32, false); 547 PS_ASSERT_IMAGE_TYPE(shutter->image, PS_TYPE_F32, false); 548 549 psRegion region = psRegionSet(readout->col0, readout->col0 + readout->image->numCols, 550 readout->row0, readout->row0 + readout->image->numRows); // Detector region 551 552 pmCell *cell = readout->parent; // Parent cell 553 if (!cell) { 554 psError(PS_ERR_BAD_PARAMETER_NULL, true, 555 "Parent cell is NULL --- unable to determine exposure time.\n"); 556 return false; 557 } 558 float exptime = psMetadataLookupF32(NULL, cell->concepts, "CELL.EXPOSURE"); // Exposure time 559 if (!isfinite(exptime) || exptime < 0.0) { 560 psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Bad exposure time: %f.\n", exptime); 561 return false; 562 } 563 564 psImage *shutterImage = psImageSubset(shutter->image, region); // Subimage with shutter 565 if (!shutterImage) { 566 psString regionString = psRegionToString(region); 567 psError(PS_ERR_BAD_PARAMETER_VALUE, false, "Size mismatch: %s vs %dx%d\n", 568 regionString, shutter->image->numCols, shutter->image->numRows); 569 psFree(regionString); 570 psFree(shutterImage); 571 return false; 572 } 573 psImage *image = readout->image; // Image to correct 574 for (int y = 0; y < image->numRows; y++) { 575 for (int x = 0; x < image->numCols; x++) { 576 image->data.F32[y][x] *= exptime / (exptime + shutterImage->data.F32[y][x]); 577 } 578 } 579 580 psFree(shutterImage); 581 582 return true; 583 }
Note:
See TracChangeset
for help on using the changeset viewer.
