00001 #include "trees.h"
00002 #include "histogram.h"
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 void HIST_INIT (
00013 HISTOGRAM *hist
00014
00015 ) {
00016 register int i;
00017
00018 hist->bin = 0;
00019 hist->cbin = 0;
00020 hist->edge = 0;
00021 hist->numpixels = 0;
00022 hist->r = (double)0.0;
00023 hist->per = 0;
00024 for(i = 0; i < HIST_RESOL; i++)
00025 hist->dir[i] = hist->mag[i] = 0;
00026 }
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 void HIST_MAG (
00037 IMAGE_BYTE *iMag,
00038 MOSAIC_INDEX *index,
00039 HISTOGRAM *globalhist,
00040 HISTOGRAM **localhist,
00041 int nrows,
00042 int ncols
00043
00044 ) {
00045 register int i, j, k;
00046 int row, col;
00047
00048 int progmeter;
00049 double progress;
00050 double localpercent;
00051 double localprogress;
00052 int total, increment;
00053 int cancel=0;
00054
00055 progmeter = ProgMeter_Create("Magnitude Histograms");
00056 progress = 0.0;
00057
00058 if (progmeter) {
00059 localpercent = 0.75;
00060 localprogress = 0.0;
00061 total = index->hend - index->hstart;
00062 increment = rint((double)total/5.0) + 1;
00063 }
00064
00065
00066 for(k = index->hstart; k <= index->hend; k++) {
00067
00068 if (progmeter) {
00069 if (((k-index->hstart)%increment) == 0) {
00070 localprogress = ((double)(k-index->hstart)/(double)total)*100.0;
00071 cancel = ProgMeter_Update(progmeter,(localprogress*localpercent)+progress);
00072 }
00073 }
00074
00075 for(j = index->vstart[k]; j <= index->vend[k]; j++) {
00076 row = (index->vstart[k]-index->roi->top[0])/HIST_BLOCK_DIM;
00077 col = (k-index->hstart)/HIST_BLOCK_DIM;
00078 localhist[row][col].mag[iMag->data[0][j-index->voffset[k]][k]]++;
00079 localhist[row][col].numpixels++;
00080 }
00081 }
00082
00083 if (progmeter) {
00084 progress = 75.0;
00085 cancel = ProgMeter_Update(progmeter,progress);
00086 }
00087
00088 if (progmeter) {
00089 localpercent = 0.25;
00090 localprogress = 0.0;
00091 total = nrows;
00092 increment = rint((double)total/5.0) + 1;
00093 }
00094
00095
00096 for(i = 0; i < nrows; i++) {
00097 if (progmeter) {
00098 if ((i%increment) == 0) {
00099 localprogress = ((double)i/(double)total)*100.0;
00100 cancel = ProgMeter_Update(progmeter,(localprogress*localpercent)+progress);
00101 }
00102 }
00103
00104 for(j = 0; j < ncols; j++) {
00105 for(k = 0; k < HIST_RESOL; k++) {
00106 globalhist->mag[k] += localhist[i][j].mag[k];
00107 globalhist->numpixels += localhist[i][j].numpixels;
00108 }
00109 }
00110 }
00111
00112
00113 if (progmeter) {
00114 progress = 100.0;
00115 cancel = ProgMeter_Update(progmeter,progress);
00116 }
00117
00118 if (progmeter) {
00119 ProgMeter_Destroy(progmeter);
00120 }
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 void HIST_RTH_PERCENTILE (
00132 HISTOGRAM *globalhist,
00133 HISTOGRAM **localhist,
00134 double r,
00135 int nrows,
00136 int ncols
00137
00138 ) {
00139 register int i, j, k;
00140 int sum;
00141
00142 int progmeter;
00143 double progress;
00144 double localpercent;
00145 double localprogress;
00146 int total, increment;
00147 int cancel=0;
00148
00149 progmeter = ProgMeter_Create("Computing Rth Percentile");
00150 progress = 0.0;
00151
00152 if (progmeter) {
00153 localpercent = 1.0;
00154 localprogress = 0.0;
00155 total = nrows;
00156 increment = rint((double)total/5.0) + 1;
00157 }
00158
00159 globalhist->r = r;
00160
00161
00162 for(i = 0; i < nrows; i++) {
00163
00164 if (progmeter) {
00165 if ((i%increment) == 0) {
00166 localprogress = ((double)i/(double)total)*100.0;
00167 cancel = ProgMeter_Update(progmeter,(localprogress*localpercent)+progress);
00168 }
00169 }
00170
00171 for(j = 0; j < ncols; j++) {
00172 localhist[i][j].r = r;
00173 for(k = HIST_RESOL-1, sum = 0; k >= 0; k--) {
00174 sum += localhist[i][j].mag[k];
00175 if (sum >= localhist[i][j].numpixels*r) {
00176 localhist[i][j].per = k;
00177 break;
00178 }
00179 }
00180 }
00181 }
00182
00183
00184
00185 for(k = HIST_RESOL-1, sum = 0; k >= 0; k--) {
00186 sum += globalhist->mag[k];
00187 if (sum >= globalhist->numpixels*r) {
00188 globalhist->per = k;
00189 break;
00190 }
00191 }
00192
00193 if (progmeter) {
00194 progress = 100.0;
00195 cancel = ProgMeter_Update(progmeter,progress);
00196 }
00197
00198 if (progmeter) {
00199 ProgMeter_Destroy(progmeter);
00200 }
00201
00202 }
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212 void HIST_DIR (
00213 IMAGE_BYTE *iMag,
00214 IMAGE_BYTE *iDir,
00215 MOSAIC_INDEX *index,
00216 HISTOGRAM *globalhist,
00217 HISTOGRAM **localhist,
00218 int nrows,
00219 int ncols
00220
00221 ) {
00222 register int j, k;
00223 int row, col;
00224
00225 int progmeter;
00226 double progress;
00227 double localpercent;
00228 double localprogress;
00229 int total, increment;
00230 int cancel=0;
00231
00232 progmeter = ProgMeter_Create("Direction Histograms");
00233 progress = 0.0;
00234
00235 if (progmeter) {
00236 localpercent = 1.0;
00237 localprogress = 0.0;
00238 total = index->hend - index->hstart;
00239 increment = rint((double)total/5.0) + 1;
00240 }
00241
00242 for(k = index->hstart; k <= index->hend; k++) {
00243
00244 if (progmeter) {
00245 if (((k-index->hstart)%increment) == 0) {
00246 localprogress = ((double)(k-index->hstart)/(double)total)*100.0;
00247 cancel = ProgMeter_Update(progmeter,(localprogress*localpercent)+progress);
00248 }
00249 }
00250
00251 for(j = index->vstart[k]; j <= index->vend[k]; j++) {
00252
00253 row = (index->vstart[k]-index->roi->top[0])/HIST_BLOCK_DIM;
00254 col = (k-index->hstart)/HIST_BLOCK_DIM;
00255
00256
00257 if (iMag->data[0][j-index->voffset[k]][k] >= localhist[row][col].per)
00258 localhist[row][col].dir[iDir->data[0][j-index->voffset[k]][k]] += iMag->data[0][j-index->voffset[k]][k];
00259
00260
00261 if (iMag->data[0][j-index->voffset[k]][k] >= globalhist->per)
00262 globalhist->dir[iDir->data[0][j-index->voffset[k]][k]] += iMag->data[0][j-index->voffset[k]][k];
00263
00264 }
00265 }
00266
00267 if (progmeter) {
00268 progress = 100.0;
00269 cancel = ProgMeter_Update(progmeter,progress);
00270 }
00271
00272 if (progmeter) {
00273 ProgMeter_Destroy(progmeter);
00274 }
00275 }
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285 void HIST_ORIENTATION (
00286 HISTOGRAM *globalhist,
00287 HISTOGRAM **localhist,
00288 int nrows,
00289 int ncols
00290
00291 ) {
00292 register int i, j, k;
00293 int max, bin;
00294
00295 int progmeter;
00296 double progress;
00297 double localpercent;
00298 double localprogress;
00299 int total, increment;
00300 int cancel=0;
00301
00302 progmeter = ProgMeter_Create("Computing Ring Orientation");
00303 progress = 0.0;
00304
00305 if (progmeter) {
00306 localpercent = 1.0;
00307 localprogress = 0.0;
00308 total = nrows;
00309 increment = rint((double)total/5.0) + 1;
00310 }
00311
00312
00313 for(i = 0; i < nrows; i++) {
00314
00315 if (progmeter) {
00316 if ((i%increment) == 0) {
00317 localprogress = ((double)i/(double)total)*100.0;
00318 cancel = ProgMeter_Update(progmeter,(localprogress*localpercent)+progress);
00319 }
00320 }
00321
00322 for(j = 0; j < ncols; j++) {
00323 for(k = 0, max = 0; k < HIST_RESOL; k++) {
00324 if (localhist[i][j].dir[k] > max) {
00325 max = localhist[i][j].dir[k];
00326 bin = k;
00327 }
00328 }
00329 localhist[i][j].bin = bin;
00330 localhist[i][j].cbin = bin;
00331 }
00332 }
00333
00334
00335 for(k = 0, max = 0; k < HIST_RESOL; k++) {
00336 if (globalhist->dir[k] > max) {
00337 max = globalhist->dir[k];
00338 bin = k;
00339 }
00340 }
00341 globalhist->bin = bin;
00342 globalhist->cbin = bin;
00343
00344 if (progmeter) {
00345 progress = 100.0;
00346 cancel = ProgMeter_Update(progmeter,progress);
00347 }
00348
00349 if (progmeter) {
00350 ProgMeter_Destroy(progmeter);
00351 }
00352
00353 }
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364 int HIST_DIR_OF_EDGE_NEIGHBOUR (
00365 HISTOGRAM *globalhist,
00366 HISTOGRAM **localhist,
00367 int nrows,
00368 int ncols,
00369 int i,
00370 int j
00371
00372 ) {
00373 int k, l;
00374
00375 for(k = i-1; k < i+2; k ++) {
00376 if (k < 0 || k >= nrows)
00377 continue;
00378 for(l = j-1; l < j+2; l ++) {
00379 if (l < 0 || l >= ncols)
00380 continue;
00381
00382 if ( abs(localhist[k][l].bin - globalhist->bin) < LOCAL_DEVN )
00383 return(localhist[k][l].bin);
00384 }
00385 }
00386
00387 return(-1);
00388 }
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398 void HIST_RESOLVE (
00399 HISTOGRAM *globalhist,
00400 HISTOGRAM **localhist,
00401 int nrows,
00402 int ncols
00403
00404 ) {
00405 register int i, j;
00406 int bin;
00407
00408 int progmeter;
00409 double progress;
00410 double localpercent;
00411 double localprogress;
00412 int total, increment;
00413 int cancel=0;
00414
00415 progmeter = ProgMeter_Create("Resolving Ring Orientation");
00416 progress = 0.0;
00417
00418 if (progmeter) {
00419 localpercent = 1.0;
00420 localprogress = 0.0;
00421 total = nrows;
00422 increment = rint((double)total/5.0) + 1;
00423 }
00424
00425 for(i = 0; i < nrows; i++) {
00426
00427 if (progmeter) {
00428 if ((i%increment) == 0) {
00429 localprogress = ((double)i/(double)total)*100.0;
00430 cancel = ProgMeter_Update(progmeter,(localprogress*localpercent)+progress);
00431 }
00432 }
00433
00434 for(j = 0; j < ncols; j++) {
00435
00436
00437 if ( abs(localhist[i][j].bin-globalhist->bin) > LOCAL_DEVN ) {
00438
00439
00440
00441 bin = HIST_DIR_OF_EDGE_NEIGHBOUR(globalhist, localhist, nrows, ncols, i, j);
00442
00443
00444
00445 if (bin == -1) {
00446 localhist[i][j].bin = globalhist->bin;
00447 } else {
00448 localhist[i][j].bin = bin;
00449 }
00450
00451 }
00452 }
00453 }
00454
00455 if (progmeter) {
00456 progress = 100.0;
00457 cancel = ProgMeter_Update(progmeter,progress);
00458 }
00459
00460 if (progmeter) {
00461 ProgMeter_Destroy(progmeter);
00462 }
00463
00464 }
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474 int HIST_NUM_RING_NEIGH(
00475 IMAGE_BYTE *iDir,
00476 MOSAIC_INDEX *index,
00477 int m,
00478 int n,
00479 int bin
00480
00481 ) {
00482 int j, k;
00483 int diff, count = 0;
00484
00485 for(j = m - 1; j < m + 2; j++) {
00486 for(k = n - 1; k < n + 2; k++) {
00487 if (k < index->hstart || k > index->hend)
00488 continue;
00489 if (j < index->vstart[k] || j > index->vend[k])
00490 continue;
00491 diff = abs(iDir->data[0][j-index->voffset[k]][k] - bin);
00492 if (diff < DIR_TOL || (255-diff) < DIR_TOL)
00493 count++;
00494 }
00495 }
00496
00497 return(count);
00498 }
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508 void HIST_SUPPRESS (
00509 IMAGE_BYTE *iMag,
00510 IMAGE_BYTE *iDir,
00511 MOSAIC_INDEX *index,
00512 HISTOGRAM **localhist
00513
00514 ) {
00515 register int j, k;
00516 int bin, diff, row, col;
00517
00518 int progmeter;
00519 double progress;
00520 double localpercent;
00521 double localprogress;
00522 int total, increment;
00523 int cancel=0;
00524
00525 progmeter = ProgMeter_Create("Suppress Non-edge pixels");
00526 progress = 0.0;
00527
00528 if (progmeter) {
00529 localpercent = 1.0;
00530 localprogress = 0.0;
00531 total = index->hend - index->hstart;
00532 increment = rint((double)total/5.0) + 1;
00533 }
00534
00535 for(k = index->hstart; k <= index->hend; k++) {
00536
00537 if (progmeter) {
00538 if (((k-index->hstart)%increment) == 0) {
00539 localprogress = ((double)(k-index->hstart)/(double)total)*100.0;
00540 cancel = ProgMeter_Update(progmeter,(localprogress*localpercent)+progress);
00541 }
00542 }
00543
00544 for(j = index->vstart[k]; j <= index->vend[k]; j++) {
00545 row = (index->vstart[k]-index->roi->top[0])/HIST_BLOCK_DIM;
00546 col = (k-index->hstart)/HIST_BLOCK_DIM;
00547 bin = localhist[row][col].bin;
00548 diff = abs(iDir->data[0][j-index->voffset[k]][k] - bin);
00549
00550 if (diff > DIR_TOL && (255-diff) > DIR_TOL) {
00551 if (HIST_NUM_RING_NEIGH(iDir, index, j, k, bin) < 2)
00552 iMag->data[0][j-index->voffset[k]][k] = 0;
00553 }
00554 }
00555 }
00556
00557 if (progmeter) {
00558 progress = 100.0;
00559 cancel = ProgMeter_Update(progmeter,progress);
00560 }
00561
00562 if (progmeter) {
00563 ProgMeter_Destroy(progmeter);
00564 }
00565
00566 }
00567
00568
00569
00570
00571
00572
00573
00574 int HIST_FIND_MAX_NEIGH (
00575 IMAGE_BYTE *iMag,
00576 IMAGE_BYTE *byteimg,
00577 MOSAIC_INDEX *index,
00578 int startx,
00579 int starty,
00580 int *newx,
00581 int *newy
00582
00583 ) {
00584 int i, j, maxx, maxy, max;
00585
00586 if (startx + 1 == index->vend[starty])
00587 return(0);
00588
00589 max = 0;
00590 for(j = starty - 1; j < starty + 2; j++) {
00591
00592 if (j < index->hstart || j > index->hend)
00593 continue;
00594
00595 for(i = startx + 1; i < startx + 2; i++) {
00596
00597 if (i < index->vstart[j] || i > index->vend[j])
00598 continue;
00599
00600 if (iMag->data[0][i - index->voffset[j]][j] > max) {
00601 max = iMag->data[0][i - index->voffset[j]][j];
00602 maxx = i;
00603 maxy = j;
00604 }
00605 }
00606 }
00607
00608 if (max) {
00609 *newx = maxx;
00610 *newy = maxy;
00611 return(1);
00612 }
00613 else
00614 return(0);
00615
00616 }
00617
00618
00619
00620
00621
00622
00623
00624
00625 int HIST_NUM_NEIGH(
00626 IMAGE_BYTE *iMag,
00627 MOSAIC_INDEX *index,
00628 int m,
00629 int n
00630
00631 ) {
00632 int i, j, count = 0;
00633
00634 for(j = n - 1; j < n + 2; j++) {
00635 if (j < index->hstart || j > index->hend)
00636 continue;
00637 for(i = m - 1; i < m + 2; i++) {
00638 if (i < index->vstart[j] || i > index->vend[j])
00639 continue;
00640 if (iMag->data[0][i-index->voffset[j]][j] != 0)
00641 count++;
00642 }
00643 }
00644
00645 return(count);
00646 }