TPCCLIB
Loading...
Searching...
No Matches
imgthbcv.c File Reference

Calculate between-class variance from PET image based on one threshold value, or estimate threshold that gives highest between-class variance. More...

Go to the source code of this file.

Functions

double imgBetween2ClassesVariance (IMG *img, const int fi, float threshold, int verbose)

Detailed Description

Calculate between-class variance from PET image based on one threshold value, or estimate threshold that gives highest between-class variance.

Only for testing purposes.

Author
Vesa Oikonen

Definition in file imgthbcv.c.

Function Documentation

◆ imgBetween2ClassesVariance()

double imgBetween2ClassesVariance ( IMG * img,
const int fi,
float threshold,
int verbose )

Calculate between-class variance from one frame of a PET image, based on one absolute threshold value.

Between-class variance is calculated using formula by Cao et al. 2018 doi: 10.1109/ACCESS.2018.2889013

Returns
Returns the between-class variance, or NaN in case of an error.
Parameters
imgPointer to IMG data structure; not modified.
fiTime frame of image data to use (1..dimt). Dynamic images should be summed before calling this function.
thresholdAbsolute threshold value.
verboseVerbose level

Definition at line 71 of file imgthbcv.c.

81 {
82 if(verbose>0) {printf("%s(IMG, %d, %f, ...)\n", __func__, fi, threshold); fflush(stdout);}
83 double bcv=nan("");
84 if(img==NULL || img->dimz<1 || img->dimy<1 || img->dimx<1 || img->dimt<1) return(bcv);
85 if(fi<1 || fi>img->dimt) return(bcv);
86
87 if(verbose>1) fprintf(stdout, "thresholding\n");
88 unsigned int p0=0, p1=0;
89 double s0=0.0, s1=0.0;
90 for(int zi=0; zi<img->dimz; zi++) {
91 for(int yi=0; yi<img->dimy; yi++) {
92 for(int xi=0; xi<img->dimx; xi++) {
93 if(img->m[zi][yi][xi][fi-1]<threshold) {
94 p0++; s0+=img->m[zi][yi][xi][fi-1];
95 } else if(img->m[zi][yi][xi][fi-1]>=threshold) {
96 p1++; s1+=img->m[zi][yi][xi][fi-1];
97 } // NaNs are left out with if else if
98 }
99 }
100 }
101 if(verbose>1) {
102 printf("p0=%u\n", p0);
103 printf("p1=%u\n", p1);
104 }
105 /* If no pixels in either of classes, between-class variance is 0 */
106 if(p0==0 || p1==0) {
107 bcv=0.0;
108 } else { // otherwise calculate means, and the between-class variance
109
110 /* Calculate means */
111 double mu0, mu1, mu;
112 mu0=s0/(double)p0;
113 mu1=s1/(double)p1;
114 mu=(s0+s1)/(double)(p0+p1);
115 if(verbose>1) {
116 printf("mu0=%g\n", mu0);
117 printf("mu1=%g\n", mu1);
118 printf("mu=%g\n", mu);
119 }
120
121 /* Calculate between-class variance */
122 bcv=(double)p0*(double)p1*( (mu0-mu1)*(mu0-mu1) + (mu0-mu)*(mu0-mu) + (mu1-mu)*(mu1-mu) );
123 }
124
125 if(verbose>1) {printf("BCV := %g\n", bcv); fflush(stdout);}
126 return(bcv);
127}
unsigned short int dimx
float **** m
unsigned short int dimt
unsigned short int dimz
unsigned short int dimy