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

Functions for extracting TACs from image data. More...

#include "libtpcimgp.h"

Go to the source code of this file.

Functions

int imgAverageTAC (IMG *img, float *tac)
 
int imgAverageMaskTAC (IMG *img, IMG *timg, float *tac)
 
int imgAverageAUC (IMG *img, float *avgauc)
 
int imgMaskTAC (IMG *img, IMG *mask, double *tac, int verbose)
 
int imgMaskRoiNr (IMG *img, INTEGER_LIST *list)
 
int imgVoiMaskTAC (IMG *img, IMG *mask, int mv, double *tac, int verbose)
 

Detailed Description

Functions for extracting TACs from image data.

Author
Vesa Oikonen

Definition in file imgeval.c.

Function Documentation

◆ imgAverageAUC()

int imgAverageAUC ( IMG * img,
float * avgauc )

Calculates the Area-Under-Curve of an average time-activity curve of all pixels or bins in the specified IMG data.

See also
imgAverageMaskTAC, imgAverageTAC, imgAvg
Returns
Returns 0, if ok.
Parameters
img(Dynamic) IMG data.
avgaucPointer to a float were the average TAC AUC will be written.

Definition at line 91 of file imgeval.c.

96 {
97
98 if(img->status<IMG_STATUS_OCCUPIED) return(1);
99 if(avgauc==NULL) return(2);
100 *avgauc=0.0;
101 unsigned long long pxlNr=img->dimz*img->dimy*img->dimx;
102 if(pxlNr<1) return(3);
103 for(int fi=0; fi<img->dimt; fi++) {
104 float fv=0.0;
105 float fl=img->end[fi]-img->start[fi]; if(fl<=0.0) return(4);
106 for(int pi=0; pi<img->dimz; pi++)
107 for(int yi=0; yi<img->dimy; yi++)
108 for(int xi=0; xi<img->dimx; xi++)
109 fv+=img->m[pi][yi][xi][fi];
110 fv/=(float)pxlNr;
111 *avgauc+=fv*fl;
112 }
113 return(0);
114}
#define IMG_STATUS_OCCUPIED
unsigned short int dimx
float **** m
char status
unsigned short int dimt
float * start
unsigned short int dimz
unsigned short int dimy
float * end

◆ imgAverageMaskTAC()

int imgAverageMaskTAC ( IMG * img,
IMG * timg,
float * tac )

Calculates an average time-activity curve of pixels or bins in the specified IMG data.

Mask image specifies the pixels that are included in the average. If all pixels are to be averaged, then NULL can be given instead of mask image.

See also
imgMaskTAC, imgThresholdingLowHigh, imgThresholdMaskCount, imgAverageAUC, imgAverageTAC
Returns
Returns 0, if ok.
Parameters
img(Dynamic) IMG data from which cluster TAC is computed.
timgMask: 0=excluded, otherwise included. Enter NULL to include all pixels in the average.
tacAllocated float array for the TAC.

Definition at line 34 of file imgeval.c.

42 {
43 unsigned long long int pxlNr;
44
45 if(img==NULL || img->status<IMG_STATUS_OCCUPIED) return(1);
46 if(tac==NULL) return(2);
47 if(timg!=NULL) {
48 if(timg->status<IMG_STATUS_OCCUPIED) return(3);
49 /* check that image dimensions are the same */
50 if(timg->dimz!=img->dimz || timg->dimz<1) return(5);
51 if(timg->dimy!=img->dimy || timg->dimy<1) return(5);
52 if(timg->dimx!=img->dimx || timg->dimx<1) return(5);
53 }
54 /* compute the nr of pixels in the mask */
55 if(timg==NULL) {
56 pxlNr=img->dimz*img->dimy*img->dimx;
57 } else {
58 pxlNr=0;
59 for(int zi=0; zi<timg->dimz; zi++)
60 for(int yi=0; yi<timg->dimy; yi++)
61 for(int xi=0; xi<timg->dimx; xi++)
62 if(timg->m[zi][yi][xi][0]!=0.0) pxlNr++;
63 }
64 if(pxlNr<1) return(4);
65
66 /* compute the TAC */
67 for(int fi=0; fi<img->dimt; fi++) {
68 tac[fi]=0.0;
69 for(int zi=0; zi<img->dimz; zi++) {
70 for(int yi=0; yi<img->dimy; yi++) {
71 for(int xi=0; xi<img->dimx; xi++) {
72 if(timg==NULL) tac[fi]+=img->m[zi][yi][xi][fi];
73 else if(timg->m[zi][yi][xi][0]!=0.0) tac[fi]+=img->m[zi][yi][xi][fi];
74 }
75 }
76 }
77 tac[fi]/=(float)pxlNr;
78 //printf("pxlNr=%llu/%llu\n", pxlNr, (unsigned long long)img->dimz*img->dimy*img->dimx);
79 }
80 return(0);
81}

Referenced by imgAverageTAC().

◆ imgAverageTAC()

int imgAverageTAC ( IMG * img,
float * tac )

Calculates an average time-activity curve of all pixels or bins in the specified IMG data.

See also
imgAverageMaskTAC, imgAverageAUC, imgRangeMinMax
Returns
Returns 0, if ok.
Parameters
img(Dynamic) IMG data from which TAC is computed.
tacAllocated float array for the TAC.

Definition at line 15 of file imgeval.c.

20 {
21 return(imgAverageMaskTAC(img, NULL, tac));
22}
int imgAverageMaskTAC(IMG *img, IMG *timg, float *tac)
Definition imgeval.c:34

Referenced by imgNoiseTemplate(), and sifAllocateWithIMG().

◆ imgMaskRoiNr()

int imgMaskRoiNr ( IMG * img,
INTEGER_LIST * list )

Get the list of ROIs in the mask image.

See also
imgMaskCount, imgThresholdMaskCount, imgThresholdMask
Returns
Returns 0 if successful.
Parameters
imgPointer to mask image. Pixel values <=0 represent pixels outside any ROI, and each rounded positive integer value represents one ROI.
listInitiated list of integers.

Definition at line 176 of file imgeval.c.

182 {
183 /* Check the input */
184 if(img==NULL || list==NULL) return 1;
185
186 /* Clear any previous list contents */
187 if(list->nr>0) {integerListEmpty(list);}
188
189 /* Go through the mask image */
190 for(int zi=0; zi<img->dimz; zi++)
191 for(int yi=0; yi<img->dimy; yi++)
192 for(int xi=0; xi<img->dimx; xi++) {
193 int v=temp_roundf(img->m[zi][yi][xi][0]);
194 if(v>0) {
195 int ret=integerListAdd(list, v, 1);
196 if(ret<0) return(100+ret);
197 }
198 }
199 /* Sort the list */
200 integerListSort(list);
201
202 return 0;
203}
int integerListEmpty(INTEGER_LIST *l)
Definition intex.c:175
int integerListSort(INTEGER_LIST *l)
Definition intex.c:219
int integerListAdd(INTEGER_LIST *l, int v, int ifnew)
Definition intex.c:190
int temp_roundf(float e)
Definition petc99.c:20

◆ imgMaskTAC()

int imgMaskTAC ( IMG * img,
IMG * mask,
double * tac,
int verbose )

Calculate TAC as weighted average of voxels in specified image data with relative weights given in a mask image.

See also
imgsegmThresholdMask, imgAverageMaskTAC, imgThresholdingLowHigh, imgThresholdMask
Returns
Returns 0 if successful.
Parameters
imgPointer to allocated image from which the weighted TAC is calculated.
maskPointer to mask image; x, y, and z dimensions must be the same as in the image to which the mask is applied.
tacPointer to an array where weighted pixel averages are written; it must be allocated for size >= dimt.
verboseVerbose level; set to <=0 to prevent all prints to stdout.

Definition at line 126 of file imgeval.c.

137 {
138 if(verbose>0) printf("imgMaskTAC()\n");
139
140 if(img->status<IMG_STATUS_OCCUPIED) return(1);
141 if(mask->status<IMG_STATUS_OCCUPIED) return(2);
142 if(mask->dimz!=img->dimz) return(3);
143 if(mask->dimy!=img->dimy) return(4);
144 if(mask->dimx!=img->dimx) return(5);
145 if(img->dimt<1 || mask->dimt<1) return(6);
146 if(tac==NULL) return(7);
147
148 /* Initiate TAC */
149 for(int fi=0; fi<img->dimt; fi++) tac[fi]=0.0;
150 /* Add weighted sum to TAC */
151 double w=0.0;
152 for(int zi=0; zi<mask->dimz; zi++)
153 for(int yi=0; yi<mask->dimy; yi++)
154 for(int xi=0; xi<mask->dimx; xi++) if(mask->m[zi][yi][xi][0]>0.0) {
155 for(int fi=0; fi<img->dimt; fi++)
156 tac[fi]+=mask->m[zi][yi][xi][0]*img->m[zi][yi][xi][fi];
157 w+=mask->m[zi][yi][xi][0];
158 }
159 /* Divide sum TAC by sum weights */
160 for(int fi=0; fi<img->dimt; fi++) tac[fi]/=w;
161 if(verbose>1) printf("mask_sum := %g\n", w);
162 if(w<=0.0 && verbose>0)
163 fprintf(stderr, "Warning: zero mask applied to image.\n");
164
165 return(0);
166}

◆ imgVoiMaskTAC()

int imgVoiMaskTAC ( IMG * img,
IMG * mask,
int mv,
double * tac,
int verbose )

Calculate TAC as average of voxels in image data, including only voxels which have specified value in the mask image.

See also
imgsegmThresholdMask, imgMaskTAC, imgMaskDilate, imgMaskErode
Returns
Returns the nr of voxels included, or <0 in case of an error.
Parameters
imgPointer to allocated image from which the mean TAC is calculated.
maskPointer to mask image; x, y, and z dimensions must be the same as in the image to which the mask is applied.
mvVoxels with this value in mask image are included in the average.
tacPointer to an array where weighted pixel averages are written; it must be allocated for size >= dimt.
verboseVerbose level; set to <=0 to prevent all prints to stdout.

Definition at line 214 of file imgeval.c.

227 {
228 if(verbose>0) printf("imgVoiMaskTAC(img, mask, %d, tac, %d)\n", mv, verbose);
229
230 if(img->status<IMG_STATUS_OCCUPIED) return(-1);
231 if(mask->status<IMG_STATUS_OCCUPIED) return(-2);
232 if(mask->dimz!=img->dimz) return(-3);
233 if(mask->dimy!=img->dimy) return(-4);
234 if(mask->dimx!=img->dimx) return(-5);
235 if(img->dimt<1 || mask->dimt<1) return(-6);
236 if(tac==NULL) return(-7);
237
238 /* Initiate TAC */
239 for(int fi=0; fi<img->dimt; fi++) tac[fi]=0.0;
240
241 /* Add the sum to TAC */
242 int pxlNr[img->dimt], badNr[img->dimt];
243 for(int fi=0; fi<img->dimt; fi++) pxlNr[fi]=badNr[fi]=0;
244 for(int zi=0; zi<mask->dimz; zi++) {
245 for(int yi=0; yi<mask->dimy; yi++) {
246 for(int xi=0; xi<mask->dimx; xi++) {
247 double mf=(int)temp_roundf(mask->m[zi][yi][xi][0]);
248 if(mf!=mv) continue;
249 for(int fi=0; fi<img->dimt; fi++) {
250 if(!isfinite(img->m[zi][yi][xi][fi])) {badNr[fi]++; continue;}
251 tac[fi]+=img->m[zi][yi][xi][fi]; pxlNr[fi]++;
252 }
253 }
254 }
255 }
256
257 /* Divide sum TAC by sum weights */
258 int minNr=pxlNr[0];
259 for(int fi=0; fi<img->dimt; fi++) {
260 if(badNr[fi]>0 && verbose>1) {
261 fprintf(stderr, "Warning: %d missing pixel values in frame %d.\n", badNr[fi], 1+fi);
262 fflush(stderr);
263 }
264 if(pxlNr[fi]==0) {
265 if(verbose>0) {
266 fprintf(stderr, "Warning: zero valid pixels in frame %d.\n", 1+fi); fflush(stderr);
267 }
268 tac[fi]=nan("");
269 }
270 if(verbose>1) {
271 printf("%d valid pixels inside mask in frame %d\n", pxlNr[fi], 1+fi); fflush(stdout);
272 }
273 tac[fi]/=(double)pxlNr[fi];
274 if(pxlNr[fi]<minNr) minNr=pxlNr[fi];
275 }
276
277 return(minNr);
278}