-
Notifications
You must be signed in to change notification settings - Fork 1
/
roi.c
172 lines (159 loc) · 4.79 KB
/
roi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* roi.c
*
* ROI proceesing routines for Xdisp.
*
* image_stats()
* roi_stats()
* draw_roi()
*
* Copyright (c) Bruce Pike 1993-1997
*
*/
#include "xdisp.h"
#include <sys/stat.h>
/*---------------------------- image_stats -----------------------------*/
int image_stats()
{
roi_stats(0,0,Width-1,Height-1,0);
}
/*---------------------------- roi_stats -------------------------------*/
int roi_stats(int x1, int y1, int x2, int y2, int Quiet )
{
double roi_sum, roi_mean, roi_variance,
rw_roi_min, rw_roi_max,
rw_roi_mean, rw_roi_variance, tmp;
int i, j, index, roi_max, roi_min, roi_count, i_offset;
/* initialize a few things */
roi_sum = 0.0;
roi_variance = 0.0;
rw_roi_variance = 0.0;
roi_max = SHRT_MIN;
roi_min = SHRT_MAX;
if (x2 < x1) {
i = x1;
x1 = x2;
x2 = i;
}
if (y2 < y1) {
i = y1;
y1 = y2;
y2 = i;
}
roi_count = (x2-x1+1)*(y2-y1+1);
i_offset = load_all_images ? image_number*Width*Height : 0;
/* first loop through roi data to get mean */
for (i=x1; i<=x2; i++) {
for (j=y1; j<=y2; j++) {
index = j*Width+i + i_offset;
roi_sum += short_Image[index];
roi_min = (short_Image[index] < roi_min) ?
short_Image[index] : roi_min;
roi_max = (short_Image[index] > roi_max) ?
short_Image[index] : roi_max;
}
}
/* calculate roi_mean */
roi_mean = (float) roi_sum / roi_count;
/* perform scale correction if needed */
if ((file_format==ACR_NEMA_FORMAT)||
(file_format==MINC_FORMAT) ||
(Input_Data_Type > SHORT_DATA)) {
rw_roi_min = (double) (rw_offset +
((double)(roi_min-I_Min)*rw_scale));
rw_roi_max = (double) (rw_offset +
((double)(roi_max-I_Min)*rw_scale));
rw_roi_mean = (double) (rw_offset +
((roi_mean-(double)I_Min)*rw_scale));
}
/* second loop through roi data to get variance */
for (i=x1; i<=x2; i++) {
for (j=y1; j<=y2; j++) {
roi_variance += ((short_Image[j*Width+i+i_offset] - roi_mean) *
(short_Image[j*Width+i+i_offset] - roi_mean)) /
(roi_count-1);
if ((file_format==ACR_NEMA_FORMAT)||
(file_format==MINC_FORMAT) ||
(Input_Data_Type > SHORT_DATA)) {
tmp = (double)(rw_offset +
((double)(short_Image[j*Width+i+i_offset]-I_Min)*rw_scale));
rw_roi_variance += ((tmp - rw_roi_mean) *
(tmp - rw_roi_mean)) /
(roi_count-1);
}
}
}
/* set return values */
if ((file_format==ACR_NEMA_FORMAT)||
(file_format==MINC_FORMAT) ||
(Input_Data_Type > SHORT_DATA)) {
tic_mean = rw_roi_mean;
if (roi_count<2)
tic_std = 0;
else
tic_std = sqrt(rw_roi_variance);
}
else {
tic_mean = roi_mean;
if (roi_count<2)
tic_std = 0;
else
tic_std = sqrt(roi_variance);
}
if (roi_count>2 && !Quiet) {
/* display the results */
if (num_images>1)
fprintf(stderr,"\nROI data from source image %d:\n",image_number);
else
fprintf(stderr,"\nROI data from source image:\n");
fprintf(stderr,"------------------------------\n");
fprintf(stderr,"upper left = %d, %d\n", x1, y1);
fprintf(stderr,"lower right = %d, %d\n", x2, y2);
fprintf(stderr,"pixel count = %d\n", roi_count);
if ((file_format==ACR_NEMA_FORMAT) ||
(file_format==MINC_FORMAT) ||
(Input_Data_Type >= FLOAT_DATA)) {
fprintf(stderr,"roi min = %f\n", rw_roi_min);
fprintf(stderr,"roi max = %f\n", rw_roi_max);
fprintf(stderr,"roi mean = %f\n", rw_roi_mean);
fprintf(stderr,"roi variance = %f\n\n", rw_roi_variance);
}
else if (Input_Data_Type==USHORT_DATA || Input_Data_Type==ULONG_DATA) {
fprintf(stderr,"roi min = %u\n", (ulong) floor(rw_roi_min+0.5));
fprintf(stderr,"roi max = %u\n", (ulong) floor(rw_roi_max+0.5));
fprintf(stderr,"roi mean = %f\n", rw_roi_mean);
fprintf(stderr,"roi variance = %f\n\n", rw_roi_variance);
}
else if (Input_Data_Type==LONG_DATA) {
fprintf(stderr,"roi min = %d\n", (long) floor(rw_roi_min+0.5));
fprintf(stderr,"roi max = %d\n", (long) floor(rw_roi_max+0.5));
fprintf(stderr,"roi mean = %f\n", rw_roi_mean);
fprintf(stderr,"roi variance = %f\n\n", rw_roi_variance);
}
else {
fprintf(stderr,"roi min = %d\n", roi_min);
fprintf(stderr,"roi max = %d\n", roi_max);
fprintf(stderr,"roi mean = %f\n", roi_mean);
fprintf(stderr,"roi variance = %f\n\n", roi_variance);
}
}
}
/*----------------------- draw_roi -----------------------------*/
int draw_roi(int x1, int y1, int x2, int y2)
{
int i;
/* check corners */
if (x2 < x1) {
i = x1;
x1 = x2;
x2 = i;
}
if (y2 < y1) {
i = y1;
y1 = y2;
y2 = i;
}
/* draw the roi */
XDrawRectangle(theDisp, mainW, roiGC, x1, y1, x2-x1+1, y2-y1+1);
roi_present = 1;
}