Changeset 1407 for trunk/psLib/src/image/psImageStats.c
- Timestamp:
- Aug 6, 2004, 2:06:06 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/image/psImageStats.c (modified) (21 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/image/psImageStats.c
r1406 r1407 1 1 2 /** @file psImageStats.c 2 3 * \brief Routines for calculating statistics on images. … … 9 10 * @author George Gusciora, MHPCC 10 11 * 11 * @version $Revision: 1. 29$ $Name: not supported by cvs2svn $12 * @date $Date: 2004-08-0 6 22:34:05$12 * @version $Revision: 1.30 $ $Name: not supported by cvs2svn $ 13 * @date $Date: 2004-08-07 00:06:06 $ 13 14 * 14 15 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 32 33 33 34 /// This routine must determine the various statistics for the image. 35 34 36 /***************************************************************************** 35 37 NOTE: verify that image/mask have the correct types, and sizes. 36 38 *****************************************************************************/ 37 psStats *psImageStats(psStats *stats, 38 psImage *in, 39 psImage *mask, 40 int maskVal) 41 { 42 psVector* junkData=NULL; 43 psVector* junkMask=NULL; 39 psStats *psImageStats(psStats * stats, psImage * in, psImage * mask, int maskVal) 40 { 41 psVector *junkData = NULL; 42 psVector *junkMask = NULL; 44 43 45 44 if (stats == NULL) { 46 psError(__func__, "The input psStats struct can not be NULL.");45 psError(__func__, "The input psStats struct can not be NULL."); 47 46 return NULL; 48 47 } 49 48 50 49 if (in == NULL) { 51 psError(__func__, "The input image can not be NULL.");50 psError(__func__, "The input image can not be NULL."); 52 51 return NULL; 53 52 } 54 53 55 54 if (stats->options == 0) { 56 psError(__func__, "No statistic option/operation was specified.");55 psError(__func__, "No statistic option/operation was specified."); 57 56 return stats; 58 57 } 59 60 // st uff the image data into a psVector struct.58 // stuff the image data into a psVector 59 // struct. 61 60 junkData = psAlloc(sizeof(psVector)); 62 61 junkData->type = in->type; 63 junkData->nalloc = in->numRows *in->numCols;62 junkData->nalloc = in->numRows * in->numCols; 64 63 junkData->n = junkData->nalloc; 65 junkData->data.V = in->data.V[0]; // since psImage data is contiguous... 64 junkData->data.V = in->data.V[0]; // since 65 // psImage 66 // data 67 // is 68 // contiguous... 66 69 67 70 if (mask != NULL) { 68 71 if (mask->type.type != PS_TYPE_MASK) { 69 psError(__func__, "Expected the mask image type not found (type=%x)", 70 mask->type.type); 72 psError(__func__, "Expected the mask image type not found (type=%x)", mask->type.type); 71 73 psFree(junkData); 72 74 return NULL; 73 75 } 74 75 // st uff the mask data into a psVector struct.76 // stuff the mask data into a psVector 77 // struct. 76 78 junkMask = psAlloc(sizeof(psVector)); 77 79 junkMask->type = mask->type; 78 junkMask->nalloc = mask->numRows *mask->numCols;80 junkMask->nalloc = mask->numRows * mask->numCols; 79 81 junkMask->n = junkMask->nalloc; 80 82 junkMask->data.V = mask->data.V[0]; … … 85 87 psFree(junkMask); 86 88 psFree(junkData); 87 return (stats);89 return (stats); 88 90 } 89 91 … … 93 95 NOTE: verify that image/mask have the, correct types and sizes. 94 96 *****************************************************************************/ 95 psHistogram *psImageHistogram(psHistogram *out, 96 psImage *in, 97 psImage *mask, 98 unsigned int maskVal) 99 { 100 psVector *junkData=NULL; 101 psVector *junkMask=NULL; 97 psHistogram *psImageHistogram(psHistogram * out, psImage * in, psImage * mask, unsigned int maskVal) 98 { 99 psVector *junkData = NULL; 100 psVector *junkMask = NULL; 102 101 103 102 // NOTE: Verify this action. 104 if ((out == NULL) || 105 (in == NULL)) { 106 return(NULL); 103 if ((out == NULL) || (in == NULL)) { 104 return (NULL); 107 105 } 108 106 109 107 junkData = psAlloc(sizeof(psVector)); 110 108 junkData->type = in->type; 111 junkData->nalloc = in->numRows *in->numCols;109 junkData->nalloc = in->numRows * in->numCols; 112 110 junkData->n = junkData->nalloc; 113 junkData->data.V = in->data.V[0]; // since psImage data is contiguous... 111 junkData->data.V = in->data.V[0]; // since 112 // psImage 113 // data 114 // is 115 // contiguous... 114 116 115 117 if (mask != NULL) { 116 118 if (mask->type.type != PS_TYPE_MASK) { 117 psError(__func__, "Expected the mask image type not found (type=%x)", 118 mask->type.type); 119 psError(__func__, "Expected the mask image type not found (type=%x)", mask->type.type); 119 120 psFree(junkData); 120 121 return NULL; 121 122 } 122 123 // st uff the mask data into a psVector struct.123 // stuff the mask data into a psVector 124 // struct. 124 125 junkMask = psAlloc(sizeof(psVector)); 125 126 junkMask->type = mask->type; 126 junkMask->nalloc = mask->numRows *mask->numCols;127 junkMask->nalloc = mask->numRows * mask->numCols; 127 128 junkMask->n = junkMask->nalloc; 128 129 junkMask->data.V = mask->data.V[0]; … … 134 135 psFree(junkData); 135 136 136 return (out);137 return (out); 137 138 } 138 139 … … 141 142 int i = 0; 142 143 float tmp = 0.0; 143 float *scalingFactors = (float *) psAlloc(n * sizeof(float)); 144 145 for (i=0;i<n;i++) { 146 // ((2.0 * (float) i) / ((float) (n-1))) - 1.0; 147 // tmp = (float) (i + 1); 148 tmp = (float) (n - i); 149 tmp = (M_PI * (tmp - 0.5)) / ((float) n); 144 float *scalingFactors = (float *)psAlloc(n * sizeof(float)); 145 146 for (i = 0; i < n; i++) { 147 // ((2.0 * (float) i) / ((float) (n-1))) 148 // - 1.0; 149 // tmp = (float) (i + 1); 150 tmp = (float)(n - i); 151 tmp = (M_PI * (tmp - 0.5)) / ((float)n); 150 152 scalingFactors[i] = cos(tmp); 151 153 } 152 154 153 return (scalingFactors);155 return (scalingFactors); 154 156 } 155 157 … … 165 167 int i = 0; 166 168 float tmp = 0.0; 169 167 170 return p_psCalcScaleFactorsFit(n); 168 171 169 172 printf("Should not get here\n"); 170 float *scalingFactors = (float *) psAlloc(n * sizeof(float)); 171 for (i=0;i<n;i++) { 172 // scalingFactors[i] = ((2.0 * (float) i) / ((float) (n-1))) - 1.0; 173 tmp = (float) (n - i); 174 tmp = (M_PI * (tmp - 0.5)) / ((float) n); 173 float *scalingFactors = (float *)psAlloc(n * sizeof(float)); 174 175 for (i = 0; i < n; i++) { 176 // scalingFactors[i] = ((2.0 * (float) i) 177 // / ((float) (n-1))) - 1.0; 178 tmp = (float)(n - i); 179 tmp = (M_PI * (tmp - 0.5)) / ((float)n); 175 180 scalingFactors[i] = cos(tmp); 176 181 } 177 return (scalingFactors);182 return (scalingFactors); 178 183 } 179 184 … … 184 189 int j = 0; 185 190 186 chebPolys = (psPolynomial1D **) psAlloc(maxChebyPoly * 187 sizeof(psPolynomial1D *));188 for (i=0;i<maxChebyPoly;i++) {189 chebPolys[i] = psPolynomial1DAlloc(i+1);190 } 191 192 // Create the Chebyshev polynomials.Polynomial i has i-th order.191 chebPolys = (psPolynomial1D **) psAlloc(maxChebyPoly * sizeof(psPolynomial1D *)); 192 for (i = 0; i < maxChebyPoly; i++) { 193 chebPolys[i] = psPolynomial1DAlloc(i + 1); 194 } 195 196 // Create the Chebyshev polynomials. 197 // Polynomial i has i-th order. 193 198 chebPolys[0]->coeff[0] = 1; 194 199 chebPolys[1]->coeff[1] = 1; 195 for (i=2;i<maxChebyPoly;i++) { 196 for (j=0;j<chebPolys[i-1]->n;j++) { 197 chebPolys[i]->coeff[j+1] = 2 * chebPolys[i-1]->coeff[j]; 198 } 199 for (j=0;j<chebPolys[i-2]->n;j++) { 200 chebPolys[i]->coeff[j]-= chebPolys[i-2]->coeff[j]; 201 } 202 } 203 204 return(chebPolys); 205 } 206 200 for (i = 2; i < maxChebyPoly; i++) { 201 for (j = 0; j < chebPolys[i - 1]->n; j++) { 202 chebPolys[i]->coeff[j + 1] = 2 * chebPolys[i - 1]->coeff[j]; 203 } 204 for (j = 0; j < chebPolys[i - 2]->n; j++) { 205 chebPolys[i]->coeff[j] -= chebPolys[i - 2]->coeff[j]; 206 } 207 } 208 209 return (chebPolys); 210 } 207 211 208 212 /***************************************************************************** … … 220 224 over all pixels (x,y) in the image. 221 225 *****************************************************************************/ 222 psPolynomial2D * 223 psImageFitPolynomial(const psImage *input, 224 psPolynomial2D *coeffs) 226 psPolynomial2D *psImageFitPolynomial(const psImage * input, psPolynomial2D * coeffs) 225 227 { 226 228 int x = 0; … … 235 237 float tmp = 0.0; 236 238 237 // Create the sums[][] data structure. This will hold the LHS of equation 238 // 29 in the ADD: sums[k][l] = SUM { image(x,y) * Tk(x) * Tl(y) } 239 sums = (float **) psAlloc(coeffs->nX * sizeof(float *)); 240 for (i=0;i<coeffs->nX;i++) { 241 sums[i] = (float *) psAlloc(coeffs->nY * sizeof(float)); 242 } 243 244 // We scale the pixel positions to values between -1.0 and 1.0 239 // Create the sums[][] data structure. This 240 // will hold the LHS of 241 // equation 242 // 29 in the ADD: sums[k][l] = SUM { 243 // image(x,y) * Tk(x) * Tl(y) } 244 sums = (float **)psAlloc(coeffs->nX * sizeof(float *)); 245 for (i = 0; i < coeffs->nX; i++) { 246 sums[i] = (float *)psAlloc(coeffs->nY * sizeof(float)); 247 } 248 249 // We scale the pixel positions to values 250 // between -1.0 and 1.0 245 251 rScalingFactors = p_psCalcScaleFactorsFit(input->numRows); 246 252 cScalingFactors = p_psCalcScaleFactorsFit(input->numCols); 247 253 248 // Determine how many Chebyshev polynomials are needed, then create them. 254 // Determine how many Chebyshev polynomials 255 // are needed, then create them. 249 256 maxChebyPoly = coeffs->nX; 250 257 if (coeffs->nY > coeffs->nX) { … … 254 261 255 262 // Sanity check for the Chebyshevs. 256 for (i =0;i<coeffs->nX;i++) {257 for (j =0;j<coeffs->nY;j++) {263 for (i = 0; i < coeffs->nX; i++) { 264 for (j = 0; j < coeffs->nY; j++) { 258 265 tmp = 0.0; 259 for (x=0;x<input->numRows;x++) { 260 tmp+= psPolynomial1DEval(rScalingFactors[x], chebPolys[i]) * 261 psPolynomial1DEval(rScalingFactors[x], chebPolys[j]); 266 for (x = 0; x < input->numRows; x++) { 267 tmp += 268 psPolynomial1DEval 269 (rScalingFactors[x], chebPolys[i]) * psPolynomial1DEval(rScalingFactors[x], chebPolys[j]); 262 270 263 271 } 264 //printf("SUM(Cheby(%d) * Cheby(%d)) is %f\n", i, j, tmp); 272 // printf("SUM(Cheby(%d) * Cheby(%d)) 273 // is %f\n", i, j, tmp); 265 274 } 266 275 } 267 276 268 277 // Compute the sums[][] data structure. 269 for (i =0;i<coeffs->nX;i++) {270 for (j =0;j<coeffs->nY;j++) {278 for (i = 0; i < coeffs->nX; i++) { 279 for (j = 0; j < coeffs->nY; j++) { 271 280 sums[i][j] = 0.0; 272 for (x=0;x<input->numRows;x++) { 273 for (y=0;y<input->numCols;y++) { 274 sums[i][j]+= input->data.F32[x][y] * 275 psPolynomial1DEval(rScalingFactors[x], chebPolys[i]) * 276 psPolynomial1DEval(cScalingFactors[y], chebPolys[j]); 281 for (x = 0; x < input->numRows; x++) { 282 for (y = 0; y < input->numCols; y++) { 283 sums[i][j] += 284 input->data.F32[x][y] * 285 psPolynomial1DEval 286 (rScalingFactors[x], 287 chebPolys[i]) * psPolynomial1DEval(cScalingFactors[y], chebPolys[j]); 277 288 } 278 289 } … … 280 291 } 281 292 282 for (i =0;i<coeffs->nX;i++) {283 for (j =0;j<coeffs->nY;j++) {293 for (i = 0; i < coeffs->nX; i++) { 294 for (j = 0; j < coeffs->nY; j++) { 284 295 coeffs->coeff[i][j] = sums[i][j]; 285 coeffs->coeff[i][j] /= (float)(input->numRows * input->numCols);296 coeffs->coeff[i][j] /= (float)(input->numRows * input->numCols); 286 297 287 298 if ((i != 0) && (j != 0)) { 288 coeffs->coeff[i][j] *= 4.0;289 } else 290 if ((i == 0) && (j == 0)) {291 coeffs->coeff[i][j]*= 1.0;292 } else {293 coeffs->coeff[i][j]*= 2.0;294 }295 }296 } 297 298 // Free the Chebyshev polynomials that werecreated in this routine.299 for (i =0;i<maxChebyPoly;i++) {299 coeffs->coeff[i][j] *= 4.0; 300 } else if ((i == 0) && (j == 0)) { 301 coeffs->coeff[i][j] *= 1.0; 302 } else { 303 coeffs->coeff[i][j] *= 2.0; 304 } 305 } 306 } 307 308 // Free the Chebyshev polynomials that were 309 // created in this routine. 310 for (i = 0; i < maxChebyPoly; i++) { 300 311 psFree(chebPolys[i]); 301 312 } … … 303 314 304 315 // Free some data 305 for (i =0;i<coeffs->nX;i++) {316 for (i = 0; i < coeffs->nX; i++) { 306 317 psFree(sums[i]); 307 318 } … … 310 321 psFree(rScalingFactors); 311 322 312 return (coeffs);323 return (coeffs); 313 324 } 314 325 … … 316 327 317 328 *****************************************************************************/ 318 int 319 psImageEvalPolynomial(const psImage *input, 320 const psPolynomial2D *coeffs) 329 int psImageEvalPolynomial(const psImage * input, const psPolynomial2D * coeffs) 321 330 { 322 331 int x = 0; … … 331 340 float polySum = 0.0; 332 341 333 // Create the sums[][] data structure. This will hold the LHS of equation 334 // 29 in the ADD: sums[k][l] = SUM { image(x,y) * Tk(x) * Tl(y) } 335 sums = (float **) psAlloc(coeffs->nX * sizeof(float *)); 336 for (i=0;i<coeffs->nX;i++) { 337 sums[i] = (float *) psAlloc(coeffs->nY * sizeof(float)); 338 } 339 for (i=0;i<coeffs->nX;i++) { 340 for (j=0;j<coeffs->nY;j++) { 342 // Create the sums[][] data structure. This 343 // will hold the LHS of 344 // equation 345 // 29 in the ADD: sums[k][l] = SUM { 346 // image(x,y) * Tk(x) * Tl(y) } 347 sums = (float **)psAlloc(coeffs->nX * sizeof(float *)); 348 for (i = 0; i < coeffs->nX; i++) { 349 sums[i] = (float *)psAlloc(coeffs->nY * sizeof(float)); 350 } 351 for (i = 0; i < coeffs->nX; i++) { 352 for (j = 0; j < coeffs->nY; j++) { 341 353 sums[i][j] = 0.0; 342 354 } 343 355 } 344 356 345 // We scale the pixel positions to values between -1.0 and 1.0 357 // We scale the pixel positions to values 358 // between -1.0 and 1.0 346 359 rScalingFactors = p_psCalcScaleFactorsEval(input->numRows); 347 360 cScalingFactors = p_psCalcScaleFactorsEval(input->numCols); 348 361 349 // Determine how many Chebyshev polynomials are needed, then create them. 362 // Determine how many Chebyshev polynomials 363 // are needed, then create them. 350 364 maxChebyPoly = coeffs->nX; 351 365 if (coeffs->nY > coeffs->nX) { … … 355 369 chebPolys = p_psCreateChebyshevPolys(maxChebyPoly); 356 370 357 for (x =0;x<input->numRows;x++) {358 for (y =0;y<input->numCols;y++) {371 for (x = 0; x < input->numRows; x++) { 372 for (y = 0; y < input->numCols; y++) { 359 373 polySum = 0.0; 360 for (i=0;i<coeffs->nX;i++) { 361 for (j=0;j<coeffs->nY;j++) { 362 polySum+= psPolynomial1DEval(rScalingFactors[x], chebPolys[i]) * 363 psPolynomial1DEval(cScalingFactors[y], chebPolys[j]) * 364 coeffs->coeff[i][j]; 374 for (i = 0; i < coeffs->nX; i++) { 375 for (j = 0; j < coeffs->nY; j++) { 376 polySum += 377 psPolynomial1DEval 378 (rScalingFactors[x], 379 chebPolys[i]) * 380 psPolynomial1DEval(cScalingFactors[y], chebPolys[j]) * coeffs->coeff[i][j]; 365 381 366 382 } … … 370 386 } 371 387 372 // Free the Chebyshev polynomials that were created in this routine. 373 for (i=0;i<maxChebyPoly;i++) { 388 // Free the Chebyshev polynomials that were 389 // created in this routine. 390 for (i = 0; i < maxChebyPoly; i++) { 374 391 psFree(chebPolys[i]); 375 392 } … … 377 394 378 395 // Free some data 379 for (i =0;i<coeffs->nX;i++) {396 for (i = 0; i < coeffs->nX; i++) { 380 397 psFree(sums[i]); 381 398 } … … 384 401 psFree(rScalingFactors); 385 402 386 return (0);387 } 403 return (0); 404 }
Note:
See TracChangeset
for help on using the changeset viewer.
