Changeset 2760 for trunk/psModules/src/pmSubtractSky.c
- Timestamp:
- Dec 20, 2004, 11:38:46 AM (22 years ago)
- File:
-
- 1 edited
-
trunk/psModules/src/pmSubtractSky.c (modified) (23 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psModules/src/pmSubtractSky.c
r2755 r2760 6 6 * @author GLG, MHPCC 7 7 * 8 * @version $Revision: 1. 8$ $Name: not supported by cvs2svn $9 * @date $Date: 2004-12- 18 02:27:42$8 * @version $Revision: 1.9 $ $Name: not supported by cvs2svn $ 9 * @date $Date: 2004-12-20 21:38:46 $ 10 10 * 11 11 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 20 20 21 21 /****************************************************************************** 22 *****************************************************************************/ 23 int p_psDetermineNumBits(unsigned int data) 24 { 25 int i; 26 unsigned int tmpData = data; 27 int numBits = 0; 28 29 for (i=0;i<sizeof(unsigned int);i++) { 22 p_psDetermineNumBits(data): This routine takes an insigned int as an argument 23 and returns the number of non-zero bits. 24 *****************************************************************************/ 25 psS32 p_psDetermineNumBits(psU32 data) 26 { 27 psS32 i; 28 psU32 tmpData = data; 29 psS32 numBits = 0; 30 31 for (i=0;i<sizeof(psU32);i++) { 30 32 if (0x0001 && tmpData) { 31 33 numBits++; … … 37 39 38 40 /****************************************************************************** 41 getHighestPriorityStatOption(statOptions): this routine takes as in put a 42 psStats->options with multiple options set and returns one with a single 43 option set according to the precedence set in the SDRS. 39 44 *****************************************************************************/ 40 45 psU64 getHighestPriorityStatOption(psU64 statOptions) … … 85 90 psStats *myStats = psStatsAlloc(statOptions); 86 91 87 for ( introw = 0; row < origImage->numRows ; row+=binFactor) {88 for ( intcol = 0; col < origImage->numCols ; col+=binFactor) {89 intcount = 0;90 for ( intbinRow = 0; binRow <= binFactor ; binRow++) {91 for ( intbinCol = 0; binCol <= binFactor ; binCol++) {92 for (psS32 row = 0; row < origImage->numRows ; row+=binFactor) { 93 for (psS32 col = 0; col < origImage->numCols ; col+=binFactor) { 94 psS32 count = 0; 95 for (psS32 binRow = 0; binRow <= binFactor ; binRow++) { 96 for (psS32 binCol = 0; binCol <= binFactor ; binCol++) { 92 97 if (((row + binRow) < origImage->numRows) && 93 98 ((col + binCol) < origImage->numCols)) { … … 123 128 124 129 /****************************************************************************** 130 CalculatePolyTerms(xOrder, yOrder): this routine will calculate the number of 131 coefficients (or terms) in a polynomial of order (xOrder, yOrder). 125 132 *****************************************************************************/ 126 133 psS32 CalculatePolyTerms(psS32 xOrder, psS32 yOrder) … … 194 201 195 202 sums[i][j] == x^i * y^j 196 *****************************************************************************/ 197 // XXX: Use variable size arrays for polynomial sums. 203 204 XXX: Use a psImage for the sums data structure? 205 XXX: Check for non-NULL sums argument? 206 XXX: Check for positive x- and yOrder. 207 XXX: Use variable size arrays for polynomial sums. 208 *****************************************************************************/ 198 209 #define PS_MAX_POLYNOMIAL_ORDER 20 199 210 void buildSums(psF64 x, … … 221 232 222 233 /****************************************************************************** 234 ImageFitPolynomial(myPoly, binnedImage, maskImage): this private routine takes 235 an input image along with a mask and fits a polynomial to it. The degree of 236 the polynomial is specified by input parameter myPoly, and need not be 237 symmetrical in orders of X and Y. The polynomial must be type 238 PS_POLYNOMIAL_ORD. If there are not enough rows or columns in the input image 239 for the order of the polynomial, then that order is reduced. The algorithm 240 used in this routine is based on that of the pilot project ADD, but is not 241 documented anywhere. 223 242 *****************************************************************************/ 224 243 psPolynomial2D *ImageFitPolynomial(psPolynomial2D *myPoly, … … 263 282 psS32 aRow; 264 283 psS32 aCol; 265 // XXX: Document this. The myPoly->nX and ->nY terms are actual 1 larger284 // XXX: Document this. The myPoly->nX and ->nY terms are actually 1 larger 266 285 // than the order of the polynomial. 267 286 psS32 **polyTerms = buildPolyTerms(myPoly->nX-1, myPoly->nY-1); … … 273 292 psVector *outPerm = psVectorAlloc(localPolyTerms, PS_TYPE_F64); 274 293 294 // 295 // Initialize A matrix and B vector. 296 // 275 297 for(i=0;i<A->numRows;i++) { 276 298 for(j=0;j<A->numCols;j++) { … … 282 304 } 283 305 306 307 // 308 // We build the A matrix and B vector. 309 // 284 310 for (x=0;x<binnedImage->numRows;x++) { 285 311 for (y=0;y<binnedImage->numCols;y++) { … … 315 341 for (aRow=0;aRow<localPolyTerms;aRow++) { 316 342 for (aCol=0;aCol<localPolyTerms;aCol++) { 317 psTrace(".psModule.pmSubtractSky.ImageFitPolynomial", 6,343 psTrace(".psModule.pmSubtractSky.ImageFitPolynomial", 8, 318 344 "A[%d][%d] is %f\n", aRow, aCol, A->data.F64[aRow][aCol]); 319 345 } … … 325 351 } 326 352 327 353 // 328 354 // Solve the matrix equations for the polynomial coefficients C. 355 // 329 356 Aout = psMatrixLUD(Aout, outPerm, A); 330 357 psVector *C = psVectorAlloc(localPolyTerms, PS_TYPE_F64); 331 358 psMatrixLUSolve(C, Aout, B, outPerm); 332 359 360 // 333 361 // Set the appropriate coefficients in the myPoly structure. 362 // 334 363 for (i=0;i<localPolyTerms;i++) { 335 364 myPoly->coeff[ polyTerms[i][0] ][ polyTerms[i][1] ] = C->data.F64[i]; … … 338 367 } 339 368 369 // 340 370 // Free data structures that were allocated in this module. 371 // 341 372 for (i=0;i<localPolyTerms;i++) { 342 373 psFree(polyTerms[i]); … … 349 380 psFree(outPerm); 350 381 382 // 351 383 // We restore the original size of the polynomial and set remaining 352 384 // coefficients to 0.0. 385 // 353 386 if (oldPolyX != -1) { 354 387 myPoly->nX = oldPolyX; … … 382 415 void *fitSpec, 383 416 psFit fit, 384 intbinFactor,417 psS32 binFactor, 385 418 psStats *stats, 386 float clipSD) 387 { 419 psF32 clipSD) 420 { 421 PS_READOUT_CHECK_NULL(in, NULL); 422 PS_READOUT_CHECK_EMPTY(in, NULL); 423 PS_READOUT_CHECK_TYPE(in, PS_TYPE_F32, NULL); 388 424 psTrace(".psModule.pmSubtractSky", 4, 389 425 "---- pmSubtractSky() begin ----\n"); … … 396 432 psImage *origImage = in->image; 397 433 psImage *binnedImage = NULL; 398 psPolynomial2D *myPoly ;434 psPolynomial2D *myPoly = NULL; 399 435 psImage *binnedMaskImage = NULL; 400 436 psU32 oldStatOptions = 0; 437 438 // 439 // Determine which statistic to use when binning pixels, if any. 440 // 401 441 psStatsOptions statOptions = stats->options; 402 if (1 >p_psDetermineNumBits(statOptions)) {442 if (1 < p_psDetermineNumBits(statOptions)) { 403 443 //XXX psWarning(PS_ERR_UNKNOWN,true, "Multiple statistical options have been requested.\n"); 404 444 statOptions = getHighestPriorityStatOption(statOptions); 405 445 // XXX: Don't modify input parameter. 446 oldStatOptions = stats->options; 406 447 stats->options = statOptions; 407 if (statOptions <= 0) {408 psLogMsg(__func__, PS_LOG_WARN,409 "WARNING: pmSubtractSky(): unallowable stats->options was requested\n");410 return(in);411 }448 } 449 if (0 >= p_psDetermineNumBits(statOptions)) { 450 psLogMsg(__func__, PS_LOG_WARN, 451 "WARNING: pmSubtractSky(): no stats->options was requested\n"); 452 return(in); 412 453 } 413 454 … … 430 471 // 431 472 // Bin the input image according to input parameters. 432 // 433 if ((binFactor <= 1) || 434 (stats == NULL) || 435 (0 == p_psDetermineNumBits(statOptions))) { 473 // Create a new binned image mask. 474 // 475 if ((binFactor <= 1) || (stats == NULL) || (0 == p_psDetermineNumBits(statOptions))) { 476 // No binning is required here. Simply create a copy of the image 477 // and a mask. 436 478 binnedImage = psImageCopy(binnedImage, origImage, PS_TYPE_F32); 479 if (in->mask != NULL) { 480 binnedMaskImage = psImageCopy(binnedMaskImage, in->mask, PS_TYPE_U8); 481 } else { 482 binnedMaskImage = psImageAlloc(binnedImage->numCols, 483 binnedImage->numRows, 484 PS_TYPE_U8); 485 PS_IMAGE_SET_U8(binnedMaskImage, 0); 486 } 437 487 } else { 438 // Add the original image mask in here. 439 binnedImage = psImageCopy(binnedImage, origImage, PS_TYPE_F32); 440 binnedImage = psImageRebin(NULL, binnedImage, NULL, 0, binFactor, stats); 488 binnedImage = psImageRebin(NULL, origImage, in->mask, 0, binFactor, stats); 489 binnedMaskImage = psImageAlloc(binnedImage->numCols, 490 binnedImage->numRows, 491 PS_TYPE_U8); 492 PS_IMAGE_SET_U8(binnedMaskImage, 0); 441 493 } 442 494 psTrace(".psModule.pmSubtractSky", 4, … … 456 508 myStats = psImageStats(myStats, binnedImage, NULL, 0); 457 509 p_psGetStatValue(myStats, &binnedMean); 458 psTrace(".psModule.pmSubtractSky", 8,510 psTrace(".psModule.pmSubtractSky", 6, 459 511 "binned Mean is %f\n", binnedMean); 460 512 … … 463 515 p_psGetStatValue(myStats, &binnedStdev); 464 516 psFree(myStats); 465 psTrace(".psModule.pmSubtractSky", 8,517 psTrace(".psModule.pmSubtractSky", 6, 466 518 "binned StDev is %f\n", binnedStdev); 467 519 468 520 // Clip all pixels which are more than clipSD sigmas from the mean. 469 // XXX: Is this correct? We simply set the mask. 470 // XXX: we must unset this later since we modify the image mask. 471 // XXX: Determine which pixels, mask or image, should be clipped. 472 473 binnedMaskImage = psImageAlloc(binnedImage->numCols, 474 binnedImage->numRows, 475 PS_TYPE_U8); 476 477 psTrace(".psModule.pmSubtractSky", 8, 521 psTrace(".psModule.pmSubtractSky", 6, 478 522 "clipSD is %f\n", clipSD); 479 523 480 for ( introw = 0; row < binnedImage->numRows ; row++) {481 for ( intcol = 0; col < binnedImage->numCols ; col++) {524 for (psS32 row = 0; row < binnedImage->numRows ; row++) { 525 for (psS32 col = 0; col < binnedImage->numCols ; col++) { 482 526 if (fabs(binnedImage->data.F32[row][col] - binnedMean) > 483 527 (clipSD * binnedStdev)) { … … 499 543 500 544 if (myPoly != NULL) { 501 // XXX:Add ordinary polynomials to psImageEvalPolynomial() 502 for (int row = 0; row < binnedImage->numRows ; row++) { 503 for (int col = 0; col < binnedImage->numCols ; col++) { 504 binnedImage->data.F32[row][col] = 505 psPolynomial2DEval(myPoly, (psF32) row, (psF32) col); 506 psTrace(".psModule.pmSubtractSky", 8, 507 "binned Image[%d][%d] is %f\n", 508 row, col, binnedImage->data.F32[row][col]); 509 } 510 } 545 // Set the pixels in the binned image to that of the polynomial. 546 binnedImage = psImageEvalPolynomial(binnedImage, myPoly); 511 547 } else { 512 548 psLogMsg(__func__, PS_LOG_WARN, … … 516 552 psFree(binnedImage); 517 553 } 554 if (oldStatOptions != 0) { 555 stats->options = statOptions; 556 } 518 557 return(in); 519 558 } … … 527 566 if (binFactor <= 1) { 528 567 // The binned image is the same size as the original image. 529 for ( introw = 0; row < origImage->numRows ; row++) {530 for ( intcol = 0; col < origImage->numCols ; col++) {568 for (psS32 row = 0; row < origImage->numRows ; row++) { 569 for (psS32 col = 0; col < origImage->numCols ; col++) { 531 570 origImage->data.F32[row][col]-= binnedImage->data.F32[row][col]; 532 571 } 533 572 } 534 573 } else { 535 for ( introw = 0; row < origImage->numRows ; row++) {536 for ( intcol = 0; col < origImage->numCols ; col++) {574 for (psS32 row = 0; row < origImage->numRows ; row++) { 575 for (psS32 col = 0; col < origImage->numCols ; col++) { 537 576 // We calculate the F32 value of the pixel coordinates in the 538 577 // binned image and then use a pixel interpolation routine to … … 548 587 549 588 psF32 binPixel = (psF32) psImagePixelInterpolate( 550 binnedImage, bin RowF64, binColF64,589 binnedImage, binColF64, binRowF64, 551 590 NULL, 0, 0.0, PS_INTERPOLATE_BILINEAR); 552 591 origImage->data.F32[row][col]-= binPixel; 553 592 554 593 psTrace(".psModule.pmSubtractSky", 8, 555 "image[%d][%d] <--> binnedImage[%.2f][%.2f]: %f (%f)\n", 556 row, col, binRowF64-0.5, binColF64-0.5, binPixel, 557 binnedImage->data.F32[(psS32)binRowF64][(psS32)binColF64]); 558 } 559 } 560 561 } 562 594 "image[%d][%d] <--> binnedImage[%.2f][%.2f]: %f\n", 595 row, col, binRowF64-0.5, binColF64-0.5, binPixel); 596 } 597 } 598 599 } 563 600 psFree(binnedMaskImage); 564 if (!((binFactor <= 1) || (stats == NULL))) { 565 psFree(binnedImage); 601 psFree(binnedImage); 602 if (oldStatOptions != 0) { 603 stats->options = statOptions; 566 604 } 567 605
Note:
See TracChangeset
for help on using the changeset viewer.
