Changeset 1022 for trunk/psLib/src/imageops/psImageStats.c
- Timestamp:
- Jun 14, 2004, 9:40:15 AM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/imageops/psImageStats.c (modified) (20 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/imageops/psImageStats.c
r896 r1022 19 19 /// This routine must determine the various statistics for the image. 20 20 /***************************************************************************** 21 GUS: verify that image/mask have the correct types, and sizes.21 NOTE: verify that image/mask have the correct types, and sizes. 22 22 *****************************************************************************/ 23 23 psStats *psImageStats(psStats *stats, … … 32 32 int j = 0; 33 33 34 // GUS: Verify this action.34 // NOTE: Verify this action. 35 35 if ((stats == NULL) || 36 36 (in == NULL)) { 37 37 return(NULL); 38 38 } 39 // GUS: Verify this action.39 // NOTE: Verify this action. 40 40 if (stats->options == 0) { 41 41 return(stats); … … 52 52 53 53 if (mask != NULL) { 54 // GUS: verify that mask data is PS_TYPE_U8.55 // GUS: figure out mask types54 // NOTE: verify that mask data is PS_TYPE_U8. 55 // NOTE: figure out mask types 56 56 57 57 junkMask = psVectorAlloc(mask->numRows * mask->numCols, 58 58 mask->type.type); 59 59 junkMask->n = junkMask->nalloc; 60 // GUS: Is there a more efficient way to do this?60 // NOTE: Is there a more efficient way to do this? 61 61 ptr=0; 62 62 for (i=0;i<mask->numRows;i++) { … … 78 78 NOTE: We assume that the psHistogram structure out has already been 79 79 allocated and initialized. 80 GUS: verify that image/mask have the, correct types and sizes.80 NOTE: verify that image/mask have the, correct types and sizes. 81 81 *****************************************************************************/ 82 82 psHistogram *psImageHistogram(psHistogram *out, … … 91 91 int j = 0; 92 92 93 // GUS: Verify this action.93 // NOTE: Verify this action. 94 94 if ((out == NULL) || 95 95 (in == NULL)) { … … 100 100 junkData->n = junkData->nalloc; 101 101 102 // GUS: Is there a more efficient way to do this? memcopy() won't work.102 // NOTE: Is there a more efficient way to do this? memcopy() won't work. 103 103 // Can we trick the junkData structure to use the image buffer, then 104 104 // untrick it before we deallocate it (so we won't deallocate that buffer 105 105 // twice? 106 106 107 // GUS: Make sure you have the numRows/NumCols in the right place.107 // NOTE: Make sure you have the numRows/NumCols in the right place. 108 108 ptr=0; 109 109 for (i=0;i<in->numRows;i++) { … … 114 114 115 115 if (mask != NULL) { 116 // GUS: verify that mask data is PS_TYPE_U8.117 // GUS: figure out mask types116 // NOTE: verify that mask data is PS_TYPE_U8. 117 // NOTE: figure out mask types 118 118 119 119 junkMask = psVectorAlloc(mask->numRows * mask->numCols, … … 121 121 junkMask->n = junkMask->nalloc; 122 122 123 // GUS: Is there a more efficient way to do this?123 // NOTE: Is there a more efficient way to do this? 124 124 ptr=0; 125 125 for (i=0;i<mask->numRows;i++) { … … 137 137 return(out); 138 138 } 139 140 float *p_psCalcScaleFactorsFit(int n) 141 { 142 int i = 0; 143 float tmp = 0.0; 144 float *scalingFactors = (float *) psAlloc(n * sizeof(float)); 145 146 for (i=0;i<n;i++) { 147 // ((2.0 * (float) i) / ((float) (n-1))) - 1.0; 148 // tmp = (float) (i + 1); 149 tmp = (float) (n - i); 150 tmp = (M_PI * (tmp - 0.5)) / ((float) n); 151 scalingFactors[i] = cos(tmp); 152 } 153 154 return(scalingFactors); 155 } 156 157 float *p_psCalcScaleFactorsEval(int n) 158 { 159 return p_psCalcScaleFactorsFit(n); 160 161 int i = 0; 162 // float tmp = 0.0; 163 float *scalingFactors = (float *) psAlloc(n * sizeof(float)); 164 for (i=0;i<n;i++) { 165 // scalingFactors[i] = ((2.0 * (float) i) / ((float) (n-1))) - 1.0; 166 tmp = (float) (i + 1); 167 tmp = (M_PI * (tmp - 0.5)) / ((float) n); 168 scalingFactors[i] = cos(tmp); 169 } 170 return(scalingFactors); 171 } 172 173 psPolynomial1D **p_psCreateChebyshevPolys(int maxChebyPoly) 174 { 175 psPolynomial1D **chebPolys = NULL; 176 int i = 0; 177 int j = 0; 178 179 chebPolys = (psPolynomial1D **) psAlloc(maxChebyPoly * 180 sizeof(psPolynomial1D *)); 181 for (i=0;i<maxChebyPoly;i++) { 182 chebPolys[i] = psPolynomial1DAlloc(i+1); 183 } 184 185 // Create the Chebyshev polynomials. Polynomial i has i-th order. 186 chebPolys[0]->coeff[0] = 1; 187 chebPolys[1]->coeff[1] = 1; 188 for (i=2;i<maxChebyPoly;i++) { 189 for (j=0;j<chebPolys[i-1]->n;j++) { 190 chebPolys[i]->coeff[j+1] = 2 * chebPolys[i-1]->coeff[j]; 191 } 192 for (j=0;j<chebPolys[i-2]->n;j++) { 193 chebPolys[i]->coeff[j]-= chebPolys[i-2]->coeff[j]; 194 } 195 } 196 197 return(chebPolys); 198 } 199 139 200 140 201 /***************************************************************************** … … 146 207 Output: 147 208 Internal Data Structures: 209 148 210 chebPolys[i][j] 149 sums[i][j] 211 212 sums[i][j]: This will contain the sum of 213 input->data.F32[x][y] * 214 psEvalPolynomial1D((float) x, chebPolys[i]) * 215 psEvalPolynomial1D((float) y, chebPolys[j]); 216 over all pixels (x,y) in the image. 217 218 150 219 *****************************************************************************/ 151 220 psPolynomial2D * … … 153 222 psPolynomial2D *coeffs) 154 223 { 224 int k = 0; 225 int l = 0; 155 226 int x = 0; 156 227 int y = 0; … … 160 231 psPolynomial1D **chebPolys = NULL; 161 232 int maxChebyPoly = 0; 162 233 float *cScalingFactors = NULL; 234 float *rScalingFactors = NULL; 235 float tmp = 0.0; 163 236 164 237 // Create the sums[][] data structure. This will hold the LHS of equation 165 238 // 29 in the ADD: sums[k][l] = SUM { image(x,y) * Tk(x) * Tl(y) } 166 167 239 sums = (float **) psAlloc(coeffs->nX * sizeof(float *)); 168 240 for (i=0;i<coeffs->nX;i++) { 169 241 sums[i] = (float *) psAlloc(coeffs->nY * sizeof(float)); 170 242 } 171 for (i=0;i<coeffs->nX;i++) { 172 for (j=0;j<coeffs->nY;j++) { 173 sums[i][j] = 0.0; 174 } 175 } 243 244 // We scale the pixel positions to values between -1.0 and 1.0 245 cScalingFactors = p_psCalcScaleFactorsFit(input->numRows); 246 rScalingFactors = p_psCalcScaleFactorsFit(input->numCols); 176 247 177 248 // Determine how many Chebyshev polynomials are needed, then create them. … … 180 251 maxChebyPoly = coeffs->nY; 181 252 } 182 183 chebPolys = (psPolynomial1D **) psAlloc(maxChebyPoly * 184 sizeof(psPolynomial1D *)); 185 for (i=0;i<maxChebyPoly;i++) { 186 chebPolys[i] = psPolynomial1DAlloc(i); 187 } 188 189 // Create the Chebyshev polynomials 190 chebPolys[1]->coeff[0] = 1; 191 for (i=2;i<maxChebyPoly;i++) { 192 for (j=0;j<chebPolys[i-1]->n;j++) { 193 chebPolys[i]->coeff[j+1] = 2 * chebPolys[i-1]->coeff[j]; 194 } 195 for (j=0;j<chebPolys[i-2]->n;j++) { 196 chebPolys[i]->coeff[j]-= chebPolys[i-2]->coeff[j]; 197 } 198 } 199 200 for (x=0;x<input->numRows;x++) { 201 for (y=0;y<input->numCols;y++) { 202 for (i=0;i<coeffs->nX;i++) { 203 for (j=0;j<coeffs->nY;j++) { 253 chebPolys = p_psCreateChebyshevPolys(maxChebyPoly); 254 255 // Sanity check for the Chebyshevs 256 for (i=0;i<coeffs->nX;i++) { 257 for (j=0;j<coeffs->nY;j++) { 258 tmp = 0.0; 259 for (x=0;x<input->numRows;x++) { 260 tmp+= psEvalPolynomial1D(rScalingFactors[x], chebPolys[i]) * 261 psEvalPolynomial1D(rScalingFactors[x], chebPolys[j]); 262 263 } 264 } 265 } 266 267 for (i=0;i<coeffs->nX;i++) { 268 for (j=0;j<coeffs->nY;j++) { 269 sums[i][j] = 0.0; 270 for (x=0;x<input->numRows;x++) { 271 for (y=0;y<input->numCols;y++) { 204 272 sums[i][j]+= input->data.F32[x][y] * 205 psEvalPolynomial1D((float) x, chebPolys[i]) * 206 psEvalPolynomial1D((float) y, chebPolys[j]); 207 273 psEvalPolynomial1D(rScalingFactors[x], chebPolys[i]) * 274 psEvalPolynomial1D(cScalingFactors[y], chebPolys[j]); 208 275 } 209 276 } … … 211 278 } 212 279 213 // GUS: Check the math on this at a later date.214 coeffs->coeff[0][0] = sums[0][0] / ( coeffs->nX * coeffs->nY);215 for (i=0;i<coeffs->nX;i++) { 216 coeffs->coeff[i][0] = 0.5 *217 ((sums[i][0] * 4.0) / (coeffs->nX * coeffs->nY)) -218 (2.0 * coeffs->coeff[0][0]);280 // NOTE: Check the math on this at a later date. 281 coeffs->coeff[0][0] = sums[0][0] / ((float) (coeffs->nX * coeffs->nY)); 282 for (i=0;i<coeffs->nX;i++) { 283 coeffs->coeff[i][0] = ((sums[i][0] * 2.0) / 284 ((float) (coeffs->nX * coeffs->nY))) - 285 coeffs->coeff[0][0]; 219 286 } 220 287 for (j=0;j<coeffs->nY;j++) { 221 coeffs->coeff[0][j] = 0.5 * 222 ((sums[0][j] * 4.0) / (coeffs->nX * coeffs->nY)) - 223 (2.0 * coeffs->coeff[0][0]); 288 coeffs->coeff[0][j] = 289 ((sums[0][j] * 2.0) / 290 ((float) (coeffs->nX * coeffs->nY))) - 291 coeffs->coeff[0][0]; 224 292 } 225 293 for (i=1;i<coeffs->nX;i++) { 226 294 for (j=1;j<coeffs->nY;j++) { 227 295 coeffs->coeff[i][j] = 228 (sums[i][0] * 4.0) / (coeffs->nX * coeffs->nY) - 229 coeffs->coeff[0][0] - 230 coeffs->coeff[i][0] - 231 coeffs->coeff[0][j]; 232 } 233 } 296 ((sums[i][0] * 4.0) / ((float) (coeffs->nX * coeffs->nY))) - 297 (coeffs->coeff[0][0] + 298 coeffs->coeff[i][0] + 299 coeffs->coeff[0][j]); 300 } 301 } 302 303 for (k=0;k<coeffs->nX;k++) { 304 for (l=0;l<coeffs->nY;l++) { 305 tmp = 0.0; 306 for (i=0;i<coeffs->nX;i++) { 307 for (j=0;j<coeffs->nY;j++) { 308 for (x=0;x<input->numRows;x++) { 309 for (y=0;y<input->numCols;y++) { 310 tmp+= (coeffs->coeff[i][j] * 311 psEvalPolynomial1D(rScalingFactors[x], chebPolys[i]) * 312 psEvalPolynomial1D(rScalingFactors[y], chebPolys[j]) * 313 psEvalPolynomial1D(rScalingFactors[x], chebPolys[k]) * 314 psEvalPolynomial1D(rScalingFactors[y], chebPolys[l])); 315 316 } 317 } 318 } 319 } 320 } 321 } 322 234 323 235 324 // Free the Chebyshev polynomials that were created in this routine. … … 244 333 } 245 334 psFree(sums); 335 psFree(cScalingFactors); 336 psFree(rScalingFactors); 246 337 247 338 return(coeffs); … … 255 346 const psPolynomial2D *coeffs) 256 347 { 257 int i = 0;258 int j = 0;259 348 int x = 0; 260 349 int y = 0; 350 int i = 0; 351 int j = 0; 261 352 float **sums = NULL; 262 353 psPolynomial1D **chebPolys = NULL; 263 354 int maxChebyPoly = 0; 355 float *cScalingFactors = NULL; 356 float *rScalingFactors = NULL; 264 357 float polySum = 0.0; 265 358 … … 275 368 } 276 369 } 370 371 // We scale the pixel positions to values between -1.0 and 1.0 372 cScalingFactors = p_psCalcScaleFactorsEval(input->numRows); 373 rScalingFactors = p_psCalcScaleFactorsEval(input->numCols); 277 374 278 375 // Determine how many Chebyshev polynomials are needed, then create them. … … 282 379 } 283 380 284 chebPolys = (psPolynomial1D **) psAlloc(maxChebyPoly * 285 sizeof(psPolynomial1D *)); 286 for (i=0;i<maxChebyPoly;i++) { 287 chebPolys[i] = psPolynomial1DAlloc(i); 288 } 289 290 // Create the Chebyshev polynomials 291 chebPolys[1]->coeff[0] = 1; 292 for (i=2;i<maxChebyPoly;i++) { 293 for (j=0;j<chebPolys[i-1]->n;j++) { 294 chebPolys[i]->coeff[j+1] = 2 * chebPolys[i-1]->coeff[j]; 295 } 296 for (j=0;j<chebPolys[i-2]->n;j++) { 297 chebPolys[i]->coeff[j]-= chebPolys[i-2]->coeff[j]; 298 } 299 } 381 chebPolys = p_psCreateChebyshevPolys(maxChebyPoly); 300 382 301 383 for (x=0;x<input->numRows;x++) { … … 304 386 for (i=0;i<coeffs->nX;i++) { 305 387 for (j=0;j<coeffs->nY;j++) { 306 polySum+= psEvalPolynomial1D( (float)x, chebPolys[i]) *307 psEvalPolynomial1D( (float)y, chebPolys[j]) *388 polySum+= psEvalPolynomial1D(rScalingFactors[x], chebPolys[i]) * 389 psEvalPolynomial1D(cScalingFactors[y], chebPolys[j]) * 308 390 coeffs->coeff[i][j]; 391 309 392 } 310 393 } … … 324 407 } 325 408 psFree(sums); 409 psFree(cScalingFactors); 410 psFree(rScalingFactors); 326 411 327 412 return(0);
Note:
See TracChangeset
for help on using the changeset viewer.
