Bsoft  1.9
Bernard's software package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
img_edit.cpp
Go to the documentation of this file.
1 
9 #include "img_edit.h"
10 #include "Complex.h"
11 #include "Matrix.h"
12 #include "matrix_linear.h"
13 #include "utilities.h"
14 
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17 
31 int img_delete_range(Bimage* p, double min, double max, double fill, int fill_type)
32 {
33  if ( fill_type == FILL_AVERAGE ) fill = p->average();
34 
35  unsigned long i, j, n, nvox = 0;
36  unsigned long imgsize = p->sizeX()*p->sizeY()*p->sizeZ()*p->channels();
37 
38  if ( verbose & VERB_PROCESS ) {
39  cout << "Filling a range:" << endl;
40  cout << "Range: " << min << " - " << max << endl;
41  cout << "Fill value: " << fill << endl << endl;
42  } else if ( verbose & VERB_LABEL )
43  cout << "Filling a range" << endl << endl;
44 
45  for ( n=0; n<p->images(); n++ ) {
46  if ( fill_type == FILL_BACKGROUND ) fill = p->image[n].background();
47  for ( i=n*imgsize, j=i+imgsize; i<j; i++ ) if ( (*p)[i] >= min && (*p)[i] <= max ) {
48  p->set(i, fill);
49  nvox++;
50  }
51  }
52 
53  if ( verbose & VERB_PROCESS )
54  cout << "Number of voxels changed: " << nvox << endl;
55 
56  return nvox;
57 }
58 
70 {
71  p->change_type(Float);
72 
73  unsigned long i, j, n, x, y, z;
74  double b[4], v;
75  Matrix a(4,4), a2(4,4);
76 
77  if ( verbose & VERB_PROCESS ) {
78  cout << "Correcting the image linear gradient:" << endl;
79  cout << "Image\tb0\tb1\tb2\tb3" << endl;
80  } else if ( verbose & VERB_LABEL )
81  cout << "Correcting the image linear gradient" << endl << endl;
82 
83  // Precalculate the a matrix which depends only on the size
84  // Keep it like this in case we want to use a mask later
85  for ( z=0; z<p->sizeZ(); z++ ) {
86  for ( y=0; y<p->sizeY(); y++ ) {
87  for ( x=0; x<p->sizeX(); x++ ) {
88  a[0][0] += 1;
89  a[0][1] += x;
90  a[0][2] += y;
91  a[0][3] += z;
92  a[1][1] += x*x;
93  a[1][2] += x*y;
94  a[1][3] += x*z;
95  a[2][2] += y*y;
96  a[2][3] += y*z;
97  a[3][3] += z*z;
98  }
99  }
100  }
101  for ( i=1; i<4; i++ ) {
102  for ( j=0; j<i; j++ )
103  a[i][j] = a[j][i];
104  if ( a[i][i] < 1 ) a[i][i] = 1; // The diagonals cannot be zero
105  }
106 
107  for ( n=0; n<p->images(); n++ ) {
108  for ( i=0; i<4; i++ ) b[i] = 0;
109  a2 = a;
110  for ( i = n*p->sizeZ()*p->sizeY()*p->sizeX(), z=0; z<p->sizeZ(); z++ ) {
111  for ( y=0; y<p->sizeY(); y++ ) {
112  for ( x=0; x<p->sizeX(); x++, i++ ) {
113  v = (*p)[i];
114  b[0] += v;
115  b[1] += v*x;
116  b[2] += v*y;
117  b[3] += v*z;
118  }
119  }
120  }
121  a2.LU_decomposition(b);
122  if ( verbose & VERB_PROCESS )
123  cout << n << tab << b[0] << tab << b[1] << tab << b[2] << tab << b[3] << endl;
124  for ( i = n*p->sizeZ()*p->sizeY()*p->sizeX(), z=0; z<p->sizeZ(); z++ ) {
125  for ( y=0; y<p->sizeY(); y++ ) {
126  for ( x=0; x<p->sizeX(); x++, i++ ) {
127  v = (*p)[i] - b[0] + b[1]*x + b[2]*y + b[3]*z;
128  p->set(i, v);
129 // data[i] -= b[0] + b[1]*x + b[2]*y + b[3]*z;
130  }
131  }
132  }
133  }
134 
135  return 0;
136 }
137 
151 int img_extract_tetrahedron(Bimage* p, Vector3<float>* tet, double fill, int fill_type)
152 {
153  if ( fill_type == FILL_AVERAGE ) fill = p->average();
154 
155  unsigned long i, j, k, n, x, y, z, c;
156 
157  Vector3<float> center;
158  Vector3<float> normal[4];
159  float origin[4];
160 
161  for ( i=0; i<4; i++ ) center = center + tet[i];
162  center /= 4.0;
163 
164  normal[0] = tet[0].normal(tet[1], tet[2]);
165  normal[1] = tet[1].normal(tet[2], tet[3]);
166  normal[2] = tet[2].normal(tet[3], tet[0]);
167  normal[3] = tet[3].normal(tet[0], tet[1]);
168 
169  // The distance of a point from a plane is given by the inner product sum of
170  // the normal to the plane and the vector from that point to any point on the plane
171  for ( i=0; i<4; i++ ) {
172  origin[i] = normal[i].scalar(tet[i]);
173  if ( normal[i].scalar(center) < origin[i] ) {
174  normal[i] = -normal[i];
175  origin[i] = -origin[i];
176  }
177  }
178 
179  if ( verbose & VERB_PROCESS ) {
180  cout << "Extracting a tetrahedron:" << endl;
181  cout << "Vertices: " << tet[0] << endl;
182  cout << " " << tet[1] << endl;
183  cout << " " << tet[2] << endl;
184  cout << " " << tet[3] << endl;
185  cout << "Center: " << center << endl;
186  cout << "Normals: " << normal[0] << " (" << origin[0] << ")" << endl;
187  cout << " " << normal[1] << " (" << origin[1] << ")" << endl;
188  cout << " " << normal[2] << " (" << origin[2] << ")" << endl;
189  cout << " " << normal[3] << " (" << origin[3] << ")" << endl;
190  cout << endl;
191  } else if ( verbose & VERB_LABEL )
192  cout << "Extracting a tetrahedron" << endl << endl;
193 
194  for ( i=n=0; n<p->images(); n++ ) {
195  if ( fill_type == FILL_BACKGROUND ) fill = p->image[n].background();
196  for ( z=0; z<p->sizeZ(); z++ ) {
197  for ( y=0; y<p->sizeY(); y++ ) {
198  for ( x=0; x<p->sizeX(); x++, i++ ) {
199  for ( j=0; j<4; j++ ) {
200  if ( normal[j].x()*x + normal[j].y()*y + normal[j].z()*z < origin[j] )
201  for ( c=0, k=i*p->channels(); c<p->channels(); c++, k++ ) p->set(k, fill);
202  }
203  }
204  }
205  }
206  }
207 
208  return 0;
209 }
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1408
Type z()
Definition: Vector3.h:141
void sizeZ(size_t nz)
Definition: Bimage.h:320
#define FILL_BACKGROUND
Definition: utilities.h:141
Header file for general utilities.
double background()
Definition: Bimage.h:126
Class for complex numbers.
Definition: Bimage.h:59
#define FILL_AVERAGE
Definition: utilities.h:140
Generalized matrix class.
#define VERB_PROCESS
Definition: utilities.h:99
double average(size_t cc, double xf, double yf, double zf, size_t nn, double iscale)
Averages in the xy plane when scaling is less than 1.
Definition: Bimage.cpp:1118
long b
Definition: jpegint.h:371
General image parameter class.
Definition: Bimage.h:152
int img_delete_range(Bimage *p, double min, double max, double fill, int fill_type)
Replaces values in a given range with the fill value.
Definition: img_edit.cpp:31
double LU_decomposition()
Matrix inversion by LU decomposition.
Definition: matrix.cpp:60
size_t channels()
Definition: Bimage.h:313
Vector3 normal(Vector3 &v1, Vector3 &v2)
Definition: Vector3.h:239
double scalar(const Vector3 &v) const
Definition: Vector3.h:219
int verbose
void set(size_t j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:884
Header file for functions for editing image contents.
size_t images()
Definition: Bimage.h:400
Definition: Matrix.h:21
void sizeX(size_t nx)
Definition: Bimage.h:318
Header file for matrix functions.
void sizeY(size_t ny)
Definition: Bimage.h:319
int img_gradient_correction(Bimage *p)
Calculates and corrects for a linear gradient across an image.
Definition: img_edit.cpp:69
int img_extract_tetrahedron(Bimage *p, Vector3< float > *tet, double fill, int fill_type)
Extracts a tetrahedral part of the image.
Definition: img_edit.cpp:151
#define VERB_LABEL
Definition: utilities.h:98
Type y()
Definition: Vector3.h:140
ostream & tab(ostream &out)
Definition: utilities.cpp:21
Bsub_image * image
Definition: Bimage.h:183