Changeset 28667 for trunk/psLib/src/imageops
- Timestamp:
- Jul 13, 2010, 2:34:04 PM (16 years ago)
- Location:
- trunk/psLib/src/imageops
- Files:
-
- 2 edited
-
psImageCovariance.c (modified) (15 diffs)
-
psImageInterpolate.c (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/imageops/psImageCovariance.c
r28405 r28667 34 34 static float imageCovarianceCalculate(const psKernel *covar, // Original covariance matrix 35 35 const psKernel *kernel, // Convolution kernel 36 int x, int y // Coordinates in output covariance matrix 36 int x, int y, // Coordinates in output covariance matrix 37 float scale // Scale to apply 37 38 ) 38 39 { … … 83 84 } 84 85 85 return s um;86 return scale * sum; 86 87 } 87 88 … … 91 92 PS_ASSERT_THREAD_JOB_NON_NULL(job, false); 92 93 psAssert(job->args, "No job arguments"); 93 psAssert(job->args->n == 5, "Wrong number of job arguments: %ld", job->args->n);94 psAssert(job->args->n == 6, "Wrong number of job arguments: %ld", job->args->n); 94 95 95 96 psKernel *out = job->args->data[0]; // Output covariance matrix … … 98 99 int x = PS_SCALAR_VALUE(job->args->data[3], S32); // x coordinate in output covariance matrix 99 100 int y = PS_SCALAR_VALUE(job->args->data[4], S32); // y coordinate in output covariance matrix 100 101 out->kernel[y][x] = imageCovarianceCalculate(covar, kernel, x, y); 101 float scale = PS_SCALAR_VALUE(job->args->data[5], F32); // Scaling to apply 102 103 out->kernel[y][x] = imageCovarianceCalculate(covar, kernel, x, y, scale); 102 104 103 105 return true; … … 127 129 128 130 // Check for non-finite elements 131 double sumKernel = 0.0, sumKernel2 = 0.0; // Sum of the kernel 129 132 for (int y = kernel->yMin; y <= kernel->yMax; y++) { 130 133 for (int x = kernel->xMin; x <= kernel->xMax; x++) { … … 135 138 return NULL; 136 139 } 140 sumKernel += kernel->kernel[y][x]; 141 sumKernel2 += PS_SQR(kernel->kernel[y][x]); 137 142 } 138 143 } … … 155 160 int yMin = kernel->yMin - kernel->yMax + covar->yMin, yMax = kernel->yMax - kernel->yMin + covar->yMax; 156 161 psKernel *out = psKernelAlloc(xMin, xMax, yMin, yMax); // Covariance matrix for output 162 float scale = 1.0 / sumKernel2; // Scaling to apply 157 163 158 164 for (int y = yMin; y <= yMax; y++) { … … 165 171 PS_ARRAY_ADD_SCALAR(job->args, x, PS_TYPE_S32); 166 172 PS_ARRAY_ADD_SCALAR(job->args, y, PS_TYPE_S32); 173 PS_ARRAY_ADD_SCALAR(job->args, scale, PS_TYPE_F32); 167 174 if (!psThreadJobAddPending(job)) { 168 175 psFree(covar); … … 170 177 } 171 178 } else { 172 out->kernel[y][x] = imageCovarianceCalculate(covar, kernel, x, y); 173 } 174 } 175 } 176 psFree(covar); 179 out->kernel[y][x] = imageCovarianceCalculate(covar, kernel, x, y, scale); 180 } 181 } 182 } 177 183 178 184 if (threaded && !psThreadPoolWait(true)) { … … 180 186 return false; 181 187 } 188 189 psFree(covar); 182 190 183 191 return out; … … 194 202 195 203 // Check for non-finite elements 204 double sumKernel2 = 0.0; // Sum of the squared kernel 196 205 for (int y = kernel->yMin; y <= kernel->yMax; y++) { 197 206 for (int x = kernel->xMin; x <= kernel->xMax; x++) { … … 202 211 return NAN; 203 212 } 213 sumKernel2 += PS_SQR(kernel->kernel[y][x]); 204 214 } 205 215 } … … 215 225 } 216 226 217 float factor = imageCovarianceCalculate(covar, kernel, 0, 0); // Covariance factor 227 float scale = 1.0 / sumKernel2; // Scale to apply 228 float factor = imageCovarianceCalculate(covar, kernel, 0, 0, scale); // Covariance factor 218 229 psFree(covar); 219 230 return factor; … … 338 349 } 339 350 } 340 psFree(covar);341 342 351 if (threaded && !psThreadPoolWait(true)) { 343 352 psError(PS_ERR_UNKNOWN, false, "Error waiting for threads."); 344 353 return false; 345 354 } 355 psFree(covar); 346 356 347 357 return out; … … 616 626 if (set && !threaded) { 617 627 { 618 psThreadTask *task = psThreadTaskAlloc("PSLIB_IMAGE_COVARIANCE_CALCULATE", 5);628 psThreadTask *task = psThreadTaskAlloc("PSLIB_IMAGE_COVARIANCE_CALCULATE", 6); 619 629 task->function = &imageCovarianceCalculateThread; 620 630 psThreadTaskAdd(task); -
trunk/psLib/src/imageops/psImageInterpolate.c
r26892 r28667 427 427 428 428 // Determine the result of the interpolation after all the math has been done 429 #define INTERPOLATE_RESULT() \ 430 psImageInterpolateStatus status = PS_INTERPOLATE_STATUS_ERROR; /* Status of interpolation */ \ 431 *imageValue = sumKernel > 0 ? sumImage / sumKernel : interp->badImage; \ 432 if (wantVariance) { \ 433 *varianceValue = sumVariance / (sumKernel2 - sumBad); \ 434 } \ 435 if (sumKernel == 0.0) { \ 436 /* No kernel contributions */ \ 437 if (haveMask && maskValue) { \ 438 *maskValue |= interp->badMask; \ 439 } \ 440 status = PS_INTERPOLATE_STATUS_BAD; \ 441 } else if (sumBad == 0) { \ 442 /* Completely good pixel */ \ 443 status = PS_INTERPOLATE_STATUS_GOOD; \ 444 } else if (sumBad < PS_SQR(interp->poorFrac) * sumKernel2) { \ 445 /* Some pixels masked: poor pixel */ \ 446 if (haveMask && maskValue) { \ 447 *maskValue |= interp->poorMask; \ 448 } \ 449 status = PS_INTERPOLATE_STATUS_POOR; \ 450 } else { \ 451 /* Many pixels (or a few important pixels) masked: bad pixel */ \ 452 if (haveMask && maskValue) { \ 453 *maskValue |= interp->badMask; \ 454 } \ 455 status = PS_INTERPOLATE_STATUS_BAD; \ 456 } 429 static psImageInterpolateStatus interpolateResult(const psImageInterpolation *interp, 430 double *imageValue, double *varianceValue, 431 psImageMaskType *maskValue, 432 double sumImage, double sumVariance, double sumBad, 433 double sumKernel, double sumKernel2, 434 bool wantVariance, bool haveMask) 435 { 436 *imageValue = sumKernel > 0 ? sumImage / sumKernel : interp->badImage; 437 if (wantVariance) { 438 if (sumBad > 0) { 439 sumVariance *= sumKernel2 / (sumKernel2 - sumBad); 440 } 441 *varianceValue = sumVariance / PS_SQR(sumKernel); 442 } 443 if (sumKernel == 0.0) { 444 // No kernel contributions at all 445 if (haveMask && maskValue) { 446 *maskValue |= interp->badMask; 447 } 448 return PS_INTERPOLATE_STATUS_BAD; 449 } 450 if (sumBad == 0) { 451 // Completely good pixel 452 return PS_INTERPOLATE_STATUS_GOOD; 453 } 454 if (sumBad < PS_SQR(interp->poorFrac) * sumKernel2) { 455 // Some pixels masked: poor pixel 456 if (haveMask && maskValue) { 457 *maskValue |= interp->poorMask; 458 } 459 return PS_INTERPOLATE_STATUS_POOR; 460 } 461 // Many pixels (or a few important pixels) masked: bad pixel 462 if (haveMask && maskValue) { 463 *maskValue |= interp->badMask; 464 } 465 return PS_INTERPOLATE_STATUS_BAD; 466 } 457 467 458 468 // Interpolation engine for separable interpolation kernels … … 703 713 } 704 714 705 INTERPOLATE_RESULT();706 707 715 psFree(xKernelNew); 708 716 psFree(yKernelNew); … … 710 718 psFree(yKernel2New); 711 719 712 return status; 720 return interpolateResult(interp, imageValue, varianceValue, maskValue, sumImage, sumVariance, sumBad, 721 sumKernel, sumKernel2, wantVariance, haveMask); 713 722 } 714 723 … … 861 870 } 862 871 863 INTERPOLATE_RESULT(); 864 865 return status; 872 return interpolateResult(interp, imageValue, varianceValue, maskValue, sumImage, sumVariance, sumBad, 873 sumKernel, sumKernel2, wantVariance, haveMask); 866 874 } 867 875
Note:
See TracChangeset
for help on using the changeset viewer.
