Commit 20c212c0400259d67bbfcbdc90489811fa967216
1 parent
4a6f666c
Ziqi added functions to attain area
Showing
4 changed files
with
390 additions
and
1 deletions
Show diff stats
envi/bil.h
... | ... | @@ -387,6 +387,124 @@ public: |
387 | 387 | return true; |
388 | 388 | } |
389 | 389 | |
390 | + | |
391 | + //providing the left and the right bound data, return baseline-corrected band height | |
392 | + bool baseline_band(double lb, double rb, T* lp, T* rp, double wavelength, T* result){ | |
393 | + | |
394 | + unsigned XY = R[0] * R[1]; | |
395 | + band(result, wavelength); //get band | |
396 | + | |
397 | + //perform the baseline correction | |
398 | + double r = (double) (wavelength - lb) / (double) (rb - lb); | |
399 | + for(unsigned i=0; i < XY; i++){ | |
400 | + result[i] =(T) (result[i] - (rp[i] - lp[i]) * r - lp[i] ); | |
401 | + } | |
402 | + return true; | |
403 | + } | |
404 | + //untested | |
405 | + //providing the left and the right bound wavelength, return baseline-corrected band height | |
406 | + bool height(double lb, double rb, double bandwavelength){ | |
407 | + | |
408 | + T* lp; | |
409 | + T* rp; | |
410 | + T* result; | |
411 | + | |
412 | + lp = (T*) malloc(sizeof(R[0] * R[1] * sizeof(T))); //memory allocation | |
413 | + rp = (T*) malloc(sizeof(R[0] * R[1] * sizeof(T))); | |
414 | + heigth = (T*) malloc(sizeof(R[0] * R[1] * sizeof(T))); | |
415 | + | |
416 | + band(lp, lb); //get the data of the left bounbd and the right bound | |
417 | + band(rp, rb); | |
418 | + | |
419 | + baseline_band(lb, rb, lp, rp, bandwavelength, result); | |
420 | + | |
421 | + return true; | |
422 | + } | |
423 | + | |
424 | + //untested | |
425 | + //calculate the area between two bound point(including baseline correction) | |
426 | + bool area(double lb, double rb){ | |
427 | + | |
428 | + T* lp; //left band pointer | |
429 | + T* rp; //right band pointer | |
430 | + T* cur; //current band 1 | |
431 | + T* cur2; //current band 2 | |
432 | + T* result; //area result | |
433 | + | |
434 | + unsigned XY = R[0] * R[1]; | |
435 | + unsigned S = XY * sizeof(T); | |
436 | + | |
437 | + lp = (T*) malloc(S); //memory allocation | |
438 | + rp = (T*) malloc(S); | |
439 | + cur = (T*) malloc(S); | |
440 | + cur2 = (T*) malloc(S); | |
441 | + result = (T*) malloc(S); | |
442 | + | |
443 | + memset(result, (char)0, S); | |
444 | + | |
445 | + //find the wavelenght position in the whole band | |
446 | + unsigned int n = w.size(); | |
447 | + unsigned int ai = 0; //left bound position | |
448 | + unsigned int bi = n - 1; //right bound position | |
449 | + | |
450 | + | |
451 | + | |
452 | + //to make sure the left and the right bound are in the bandwidth | |
453 | + if (lb < w[0] || rb < w[0] || lb > w[n-1] || rb >w[n-1]){ | |
454 | + std::cout<<"ERROR: left bound or right bound out of bandwidth"<<std::endl; | |
455 | + exit(1); | |
456 | + } | |
457 | + //to make sure rigth bound is bigger than left bound | |
458 | + else if(lb > rb){ | |
459 | + std::cout<<"ERROR: right bound should be bigger than left bound"<<std::endl; | |
460 | + exit(1); | |
461 | + } | |
462 | + | |
463 | + //get the position of lb and rb | |
464 | + while (lb >= w[ai]){ | |
465 | + ai++; | |
466 | + } | |
467 | + while (rb <= w[bi]){ | |
468 | + bi--; | |
469 | + } | |
470 | + | |
471 | + band(lp, lb); | |
472 | + band(rp, rb); | |
473 | + | |
474 | + //calculate the beginning and the ending part | |
475 | + baseline_band(lb, rb, lp, rp, w[bi], cur); //ending part | |
476 | + for(unsigned j = 0; j < XY; j++){ | |
477 | + result[j] += (rb - w[bi]) * cur[j] / 2.0; | |
478 | + } | |
479 | + baseline_band(lb, rb, lp, rp, w[ai], cur); //beginnning part | |
480 | + for(unsigned j = 0; j < XY; j++){ | |
481 | + result[j] += (w[ai] - lb) * cur[j] / 2.0; | |
482 | + } | |
483 | + //calculate the area | |
484 | + ai++; | |
485 | + for(unsigned i = ai; i <= bi ;i++) | |
486 | + { | |
487 | + baseline_band(lb, rb, lp, rp, w[ai], cur2); | |
488 | + for(unsigned j = 0; j < XY; j++) | |
489 | + { | |
490 | + result[j] += (w[ai] - w[ai-1]) * (cur[j] + cur2[j]) / 2.0; | |
491 | + } | |
492 | + std::swap(cur,cur2); //swap the band pointers | |
493 | + } | |
494 | + ///////////////for testing use | |
495 | + std::ofstream text("area_result.txt",std::ios::out); | |
496 | + for (unsigned i = 0; i < 640; i++) | |
497 | + { | |
498 | + for (unsigned j = 0; j < 384; j++) | |
499 | + { | |
500 | + text<<result[i*384+j]<<", "; | |
501 | + } | |
502 | + text<<std::endl; | |
503 | + } | |
504 | + /////////////// | |
505 | + return true; | |
506 | + } | |
507 | + | |
390 | 508 | //create mask file |
391 | 509 | bool mask(unsigned char* p, double mask_band, double threshold){ |
392 | 510 | ... | ... |
envi/bip.h
... | ... | @@ -477,6 +477,123 @@ public: |
477 | 477 | return true; |
478 | 478 | } |
479 | 479 | |
480 | + //providing the left and the right bound data, return baseline-corrected band height | |
481 | + bool baseline_band(double lb, double rb, T* lp, T* rp, double wavelength, T* result){ | |
482 | + | |
483 | + unsigned XY = R[0] * R[1]; | |
484 | + band(result, wavelength); //get band | |
485 | + | |
486 | + //perform the baseline correction | |
487 | + double r = (double) (wavelength - lb) / (double) (rb - lb); | |
488 | + for(unsigned i=0; i < XY; i++){ | |
489 | + result[i] =(T) (result[i] - (rp[i] - lp[i]) * r - lp[i] ); | |
490 | + } | |
491 | + return true; | |
492 | + } | |
493 | + //untested | |
494 | + //providing the left and the right bound wavelength, return baseline-corrected band height | |
495 | + bool height(double lb, double rb, double bandwavelength){ | |
496 | + | |
497 | + T* lp; | |
498 | + T* rp; | |
499 | + T* result; | |
500 | + | |
501 | + lp = (T*) malloc(sizeof(R[0] * R[1] * sizeof(T))); //memory allocation | |
502 | + rp = (T*) malloc(sizeof(R[0] * R[1] * sizeof(T))); | |
503 | + heigth = (T*) malloc(sizeof(R[0] * R[1] * sizeof(T))); | |
504 | + | |
505 | + band(lp, lb); //get the data of the left bounbd and the right bound | |
506 | + band(rp, rb); | |
507 | + | |
508 | + baseline_band(lb, rb, lp, rp, bandwavelength, result); | |
509 | + | |
510 | + return true; | |
511 | + } | |
512 | + | |
513 | + //untested | |
514 | + //calculate the area between two bound point(including baseline correction) | |
515 | + bool area(double lb, double rb){ | |
516 | + | |
517 | + T* lp; //left band pointer | |
518 | + T* rp; //right band pointer | |
519 | + T* cur; //current band 1 | |
520 | + T* cur2; //current band 2 | |
521 | + T* result; //area result | |
522 | + | |
523 | + unsigned XY = R[0] * R[1]; | |
524 | + unsigned S = XY * sizeof(T); | |
525 | + | |
526 | + lp = (T*) malloc(S); //memory allocation | |
527 | + rp = (T*) malloc(S); | |
528 | + cur = (T*) malloc(S); | |
529 | + cur2 = (T*) malloc(S); | |
530 | + result = (T*) malloc(S); | |
531 | + | |
532 | + memset(result, (char)0, S); | |
533 | + | |
534 | + //find the wavelenght position in the whole band | |
535 | + unsigned int n = w.size(); | |
536 | + unsigned int ai = 0; //left bound position | |
537 | + unsigned int bi = n - 1; //right bound position | |
538 | + | |
539 | + | |
540 | + | |
541 | + //to make sure the left and the right bound are in the bandwidth | |
542 | + if (lb < w[0] || rb < w[0] || lb > w[n-1] || rb >w[n-1]){ | |
543 | + std::cout<<"ERROR: left bound or right bound out of bandwidth"<<std::endl; | |
544 | + exit(1); | |
545 | + } | |
546 | + //to make sure rigth bound is bigger than left bound | |
547 | + else if(lb > rb){ | |
548 | + std::cout<<"ERROR: right bound should be bigger than left bound"<<std::endl; | |
549 | + exit(1); | |
550 | + } | |
551 | + | |
552 | + //get the position of lb and rb | |
553 | + while (lb >= w[ai]){ | |
554 | + ai++; | |
555 | + } | |
556 | + while (rb <= w[bi]){ | |
557 | + bi--; | |
558 | + } | |
559 | + | |
560 | + band(lp, lb); | |
561 | + band(rp, rb); | |
562 | + | |
563 | + //calculate the beginning and the ending part | |
564 | + baseline_band(lb, rb, lp, rp, w[bi], cur); //ending part | |
565 | + for(unsigned j = 0; j < XY; j++){ | |
566 | + result[j] += (rb - w[bi]) * cur[j] / 2.0; | |
567 | + } | |
568 | + baseline_band(lb, rb, lp, rp, w[ai], cur); //beginnning part | |
569 | + for(unsigned j = 0; j < XY; j++){ | |
570 | + result[j] += (w[ai] - lb) * cur[j] / 2.0; | |
571 | + } | |
572 | + //calculate the area | |
573 | + ai++; | |
574 | + for(unsigned i = ai; i <= bi ;i++) | |
575 | + { | |
576 | + baseline_band(lb, rb, lp, rp, w[ai], cur2); | |
577 | + for(unsigned j = 0; j < XY; j++) | |
578 | + { | |
579 | + result[j] += (w[ai] - w[ai-1]) * (cur[j] + cur2[j]) / 2.0; | |
580 | + } | |
581 | + std::swap(cur,cur2); //swap the band pointers | |
582 | + } | |
583 | + ///////////////for testing use | |
584 | + std::ofstream text("area_result.txt",std::ios::out); | |
585 | + for (unsigned i = 0; i < 640; i++) | |
586 | + { | |
587 | + for (unsigned j = 0; j < 384; j++) | |
588 | + { | |
589 | + text<<result[i*384+j]<<", "; | |
590 | + } | |
591 | + text<<std::endl; | |
592 | + } | |
593 | + /////////////// | |
594 | + return true; | |
595 | + } | |
596 | + | |
480 | 597 | //create mask file |
481 | 598 | bool mask(unsigned char* p, double mask_band, double threshold){ |
482 | 599 | ... | ... |
envi/bsq.h
... | ... | @@ -309,7 +309,125 @@ public: |
309 | 309 | target.close(); |
310 | 310 | return true; |
311 | 311 | } |
312 | - | |
312 | + | |
313 | + //providing the left and the right bound data, return baseline-corrected band height | |
314 | + bool baseline_band(double lb, double rb, T* lp, T* rp, double wavelength, T* result){ | |
315 | + | |
316 | + unsigned XY = R[0] * R[1]; | |
317 | + band(result, wavelength); //get band | |
318 | + | |
319 | + //perform the baseline correction | |
320 | + double r = (double) (wavelength - lb) / (double) (rb - lb); | |
321 | + for(unsigned i=0; i < XY; i++){ | |
322 | + result[i] =(T) (result[i] - (rp[i] - lp[i]) * r - lp[i] ); | |
323 | + } | |
324 | + return true; | |
325 | + } | |
326 | + //untested | |
327 | + //providing the left and the right bound wavelength, return baseline-corrected band height | |
328 | + bool height(double lb, double rb, double bandwavelength){ | |
329 | + | |
330 | + T* lp; | |
331 | + T* rp; | |
332 | + T* result; | |
333 | + | |
334 | + lp = (T*) malloc(sizeof(R[0] * R[1] * sizeof(T))); //memory allocation | |
335 | + rp = (T*) malloc(sizeof(R[0] * R[1] * sizeof(T))); | |
336 | + heigth = (T*) malloc(sizeof(R[0] * R[1] * sizeof(T))); | |
337 | + | |
338 | + band(lp, lb); //get the data of the left bounbd and the right bound | |
339 | + band(rp, rb); | |
340 | + | |
341 | + baseline_band(lb, rb, lp, rp, bandwavelength, result); | |
342 | + | |
343 | + return true; | |
344 | + } | |
345 | + | |
346 | + //untested | |
347 | + //calculate the area between two bound point(including baseline correction) | |
348 | + bool area(double lb, double rb){ | |
349 | + | |
350 | + T* lp; //left band pointer | |
351 | + T* rp; //right band pointer | |
352 | + T* cur; //current band 1 | |
353 | + T* cur2; //current band 2 | |
354 | + T* result; //area result | |
355 | + | |
356 | + unsigned XY = R[0] * R[1]; | |
357 | + unsigned S = XY * sizeof(T); | |
358 | + | |
359 | + lp = (T*) malloc(S); //memory allocation | |
360 | + rp = (T*) malloc(S); | |
361 | + cur = (T*) malloc(S); | |
362 | + cur2 = (T*) malloc(S); | |
363 | + result = (T*) malloc(S); | |
364 | + | |
365 | + memset(result, (char)0, S); | |
366 | + | |
367 | + //find the wavelenght position in the whole band | |
368 | + unsigned int n = w.size(); | |
369 | + unsigned int ai = 0; //left bound position | |
370 | + unsigned int bi = n - 1; //right bound position | |
371 | + | |
372 | + | |
373 | + | |
374 | + //to make sure the left and the right bound are in the bandwidth | |
375 | + if (lb < w[0] || rb < w[0] || lb > w[n-1] || rb >w[n-1]){ | |
376 | + std::cout<<"ERROR: left bound or right bound out of bandwidth"<<std::endl; | |
377 | + exit(1); | |
378 | + } | |
379 | + //to make sure rigth bound is bigger than left bound | |
380 | + else if(lb > rb){ | |
381 | + std::cout<<"ERROR: right bound should be bigger than left bound"<<std::endl; | |
382 | + exit(1); | |
383 | + } | |
384 | + | |
385 | + //get the position of lb and rb | |
386 | + while (lb >= w[ai]){ | |
387 | + ai++; | |
388 | + } | |
389 | + while (rb <= w[bi]){ | |
390 | + bi--; | |
391 | + } | |
392 | + | |
393 | + band(lp, lb); | |
394 | + band(rp, rb); | |
395 | + | |
396 | + //calculate the beginning and the ending part | |
397 | + baseline_band(lb, rb, lp, rp, w[bi], cur); //ending part | |
398 | + for(unsigned j = 0; j < XY; j++){ | |
399 | + result[j] += (rb - w[bi]) * cur[j] / 2.0; | |
400 | + } | |
401 | + baseline_band(lb, rb, lp, rp, w[ai], cur); //beginnning part | |
402 | + for(unsigned j = 0; j < XY; j++){ | |
403 | + result[j] += (w[ai] - lb) * cur[j] / 2.0; | |
404 | + } | |
405 | + //calculate the area | |
406 | + ai++; | |
407 | + for(unsigned i = ai; i <= bi ;i++) | |
408 | + { | |
409 | + baseline_band(lb, rb, lp, rp, w[ai], cur2); | |
410 | + for(unsigned j = 0; j < XY; j++) | |
411 | + { | |
412 | + result[j] += (w[ai] - w[ai-1]) * (cur[j] + cur2[j]) / 2.0; | |
413 | + } | |
414 | + std::swap(cur,cur2); //swap the band pointers | |
415 | + } | |
416 | + ///////////////for testing use | |
417 | + std::ofstream text("area_result.txt",std::ios::out); | |
418 | + for (unsigned i = 0; i < 640; i++) | |
419 | + { | |
420 | + for (unsigned j = 0; j < 384; j++) | |
421 | + { | |
422 | + text<<result[i*384+j]<<", "; | |
423 | + } | |
424 | + text<<std::endl; | |
425 | + } | |
426 | + /////////////// | |
427 | + return true; | |
428 | + } | |
429 | + | |
430 | + | |
313 | 431 | //create mask file |
314 | 432 | bool mask(unsigned char* p, double mask_band, double threshold){ |
315 | 433 | ... | ... |
envi/envi.h
... | ... | @@ -310,6 +310,42 @@ public: |
310 | 310 | return false; |
311 | 311 | } |
312 | 312 | |
313 | + //do baseline correction and caculate the area between two bounds | |
314 | + bool area(double lb, double rb){ | |
315 | + if(header.interleave == envi_header::BSQ){ //if the infile is bsq file | |
316 | + if(header.data_type ==envi_header::float32) | |
317 | + return ((bsq<float>*)file)->area(lb, rb); | |
318 | + else if(header.data_type == envi_header::float64) | |
319 | + return ((bsq<double>*)file)->area(lb, rb); | |
320 | + else | |
321 | + std::cout<<"ERROR: unidentified data type"<<std::endl; | |
322 | + } | |
323 | + | |
324 | + else if(header.interleave == envi_header::BIL){ //if the infile is bil file | |
325 | + if(header.data_type ==envi_header::float32) | |
326 | + return ((bil<float>*)file)->area(lb, rb); | |
327 | + else if(header.data_type == envi_header::float64) | |
328 | + return ((bil<double>*)file)->area(lb, rb); | |
329 | + else | |
330 | + std::cout<<"ERROR: unidentified data type"<<std::endl; | |
331 | + } | |
332 | + | |
333 | + else if(header.interleave == envi_header::BIP){ //if the infile is bip file | |
334 | + if(header.data_type ==envi_header::float32) | |
335 | + return ((bip<float>*)file)->area(lb, rb); | |
336 | + else if(header.data_type == envi_header::float64) | |
337 | + return ((bip<double>*)file)->area(lb, rb); | |
338 | + else | |
339 | + std::cout<<"ERROR: unidentified data type"<<std::endl; | |
340 | + } | |
341 | + | |
342 | + else{ | |
343 | + std::cout<<"ERROR: unidentified file type"<<std::endl; | |
344 | + exit(1); | |
345 | + } | |
346 | + return false; | |
347 | + } | |
348 | + | |
313 | 349 | bool close(){ |
314 | 350 | if(header.interleave == envi_header::BSQ){ |
315 | 351 | if(header.data_type ==envi_header::float32) | ... | ... |