UsTK : Ultrasound ToolKit  version 2.0.1 under development (2024-12-17)
usVolumeProcessing.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ustk software.
4  * Copyright (C) 2016 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ustk with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * This software was developed at:
17  * Inria Rennes - Bretagne Atlantique
18  * Campus Universitaire de Beaulieu
19  * 35042 Rennes Cedex
20  * France
21  *
22  * If you have questions regarding the use of this file, please contact
23  * Inria at ustk@inria.fr
24  *
25  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27  *
28  * Authors:
29  * Pierre Chatelain
30  * Jason Chevrie
31  *
32  *****************************************************************************/
33 
34 #include <visp3/core/vpException.h>
35 #include <visp3/ustk_volume_processing/usVolumeProcessing.h>
36 
43 {
44  dst.resize(src.getHeight(), src.getWidth(), src.getNumberOfFrames());
45 
46  for (unsigned int i = 0; i < src.getSize(); i++)
47  dst.getData()[i] = src.getConstData()[i].frobeniusNorm();
48 }
49 
56 {
57  if (size <= 0 || (size % 2) != 1) {
58  throw vpException(
59  vpException::badValue,
60  "usVolumeProcessing::generateGaussianDerivativeFilterI(): Bad filter size, should be positive and odd");
61  }
62 
63  double sigma2 = vpMath::sqr(sigma);
64  int m = (size - 1) / 2;
65  usImage3D<double> filter(size, size, size);
66 
67  double dgi, gj, gk;
68  for (int k = 0; k < size; k++) {
69  gk = exp(-vpMath::sqr(k - m) / (2.0 * sigma2)) / (sigma * sqrt(2.0 * M_PI));
70  for (int j = 0; j < size; j++) {
71  gj = exp(-vpMath::sqr(j - m) / (2.0 * sigma2)) / (sigma * sqrt(2.0 * M_PI));
72  for (int i = 0; i < size; i++) {
73  dgi = -(i - m) * exp(-vpMath::sqr(i - m) / (2.0 * sigma2)) / (2.0 * sigma * sigma2 * sqrt(2.0 * M_PI));
74  filter(i, j, k, dgi * gj * gk);
75  }
76  }
77  }
78  return filter;
79 }
80 
87 {
88  if (size <= 0 || (size % 2) != 1) {
89  throw vpException(
90  vpException::badValue,
91  "usVolumeProcessing::generateGaussianDerivativeFilterJ(): Bad filter size, should be positive and odd");
92  }
93 
94  double sigma2 = vpMath::sqr(sigma);
95  int m = (size - 1) / 2;
96  usImage3D<double> filter(size, size, size);
97 
98  double gi, dgj, gk;
99  for (int k = 0; k < size; k++) {
100  gk = exp(-vpMath::sqr(k - m) / (2.0 * sigma2)) / (sigma * sqrt(2.0 * M_PI));
101  for (int j = 0; j < size; j++) {
102  dgj = -(j - m) * exp(-vpMath::sqr(j - m) / (2.0 * sigma2)) / (2.0 * sigma * sigma2 * sqrt(2.0 * M_PI));
103  for (int i = 0; i < size; i++) {
104  gi = exp(-vpMath::sqr(i - m) / (2.0 * sigma2)) / (sigma * sqrt(2.0 * M_PI));
105  filter(i, j, k, gi * dgj * gk);
106  }
107  }
108  }
109  return filter;
110 }
111 
118 {
119  if (size <= 0 || (size % 2) != 1) {
120  throw vpException(
121  vpException::badValue,
122  "usVolumeProcessing::generateGaussianDerivativeFilterK(): Bad filter size, should be positive and odd");
123  }
124 
125  double sigma2 = vpMath::sqr(sigma);
126  int m = (size - 1) / 2;
127  usImage3D<double> filter(size, size, size);
128 
129  double gi, gj, dgk;
130  for (int k = 0; k < size; k++) {
131  dgk = -(k - m) * exp(-vpMath::sqr(k - m) / (2.0 * sigma2)) / (2.0 * sigma * sigma2 * sqrt(2.0 * M_PI));
132  for (int j = 0; j < size; j++) {
133  gj = exp(-vpMath::sqr(j - m) / (2.0 * sigma2)) / (sigma * sqrt(2.0 * M_PI));
134  for (int i = 0; i < size; i++) {
135  gi = exp(-vpMath::sqr(i - m) / (2.0 * sigma2)) / (sigma * sqrt(2.0 * M_PI));
136  filter(i, j, k, gi * gj * dgk);
137  }
138  }
139  }
140  return filter;
141 }
142 
149 {
150  if (size <= 0 || (size % 2) != 1) {
151  throw vpException(
152  vpException::badValue,
153  "usVolumeProcessing::generateGaussianDerivativeFilterII(): Bad filter size, should be positive and odd");
154  }
155 
156  double sigma2 = vpMath::sqr(sigma);
157  int m = (size - 1) / 2;
158  usImage3D<double> filter(size, size, size);
159 
160  double ddgi, gj, gk;
161  for (int k = 0; k < size; k++) {
162  gk = exp(-vpMath::sqr(k - m) / (2.0 * sigma2)) / (sigma * sqrt(2.0 * M_PI));
163  for (int j = 0; j < size; j++) {
164  gj = exp(-vpMath::sqr(j - m) / (2.0 * sigma2)) / (sigma * sqrt(2.0 * M_PI));
165  for (int i = 0; i < size; i++) {
166  ddgi = (vpMath::sqr(i - m) / (4.0 * vpMath::sqr(sigma2) * sqrt(2.0 * M_PI)) -
167  1.0 / (2.0 * sigma * sigma2 * sqrt(2.0 * M_PI))) *
168  exp(-vpMath::sqr(i - m) / (2.0 * sigma2));
169  filter(i, j, k, ddgi * gj * gk);
170  }
171  }
172  }
173  return filter;
174 }
175 
182 {
183  if (size <= 0 || (size % 2) != 1) {
184  throw vpException(
185  vpException::badValue,
186  "usVolumeProcessing::generateGaussianDerivativeFilterJJ(): Bad filter size, should be positive and odd");
187  }
188 
189  double sigma2 = vpMath::sqr(sigma);
190  int m = (size - 1) / 2;
191  usImage3D<double> filter(size, size, size);
192 
193  double gi, ddgj, gk;
194  for (int k = 0; k < size; k++) {
195  gk = exp(-vpMath::sqr(k - m) / (2.0 * sigma2)) / (sigma * sqrt(2.0 * M_PI));
196  for (int j = 0; j < size; j++) {
197  ddgj = (vpMath::sqr(j - m) / (4.0 * vpMath::sqr(sigma2) * sqrt(2.0 * M_PI)) -
198  1.0 / (2.0 * sigma * sigma2 * sqrt(2.0 * M_PI))) *
199  exp(-vpMath::sqr(j - m) / (2.0 * sigma2));
200  for (int i = 0; i < size; i++) {
201  gi = exp(-vpMath::sqr(i - m) / (2.0 * sigma2)) / (sigma * sqrt(2.0 * M_PI));
202  filter(i, j, k, gi * ddgj * gk);
203  }
204  }
205  }
206  return filter;
207 }
208 
215 {
216  if (size <= 0 || (size % 2) != 1) {
217  throw vpException(
218  vpException::badValue,
219  "usVolumeProcessing::generateGaussianDerivativeFilterKK(): Bad filter size, should be positive and odd");
220  }
221 
222  double sigma2 = vpMath::sqr(sigma);
223  int m = (size - 1) / 2;
224  usImage3D<double> filter(size, size, size);
225 
226  double gi, gj, ddgk;
227  for (int k = 0; k < size; k++) {
228  ddgk = (vpMath::sqr(k - m) / (4.0 * vpMath::sqr(sigma2) * sqrt(2.0 * M_PI)) -
229  1.0 / (2.0 * sigma * sigma2 * sqrt(2.0 * M_PI))) *
230  exp(-vpMath::sqr(k - m) / (2.0 * sigma2));
231  for (int j = 0; j < size; j++) {
232  gj = exp(-vpMath::sqr(j - m) / (2.0 * sigma2)) / (sigma * sqrt(2.0 * M_PI));
233  for (int i = 0; i < size; i++) {
234  gi = exp(-vpMath::sqr(i - m) / (2.0 * sigma2)) / (sigma * sqrt(2.0 * M_PI));
235  filter(i, j, k, gi * gj * ddgk);
236  }
237  }
238  }
239  return filter;
240 }
241 
248 {
249  if (size <= 0 || (size % 2) != 1) {
250  throw vpException(
251  vpException::badValue,
252  "usVolumeProcessing::generateGaussianDerivativeFilterIJ(): Bad filter size, should be positive and odd");
253  }
254 
255  double sigma2 = vpMath::sqr(sigma);
256  int m = (size - 1) / 2;
257  usImage3D<double> filter(size, size, size);
258 
259  double dgi, dgj, gk;
260  for (int k = 0; k < size; k++) {
261  gk = exp(-vpMath::sqr(k - m) / (2.0 * sigma2)) / (sigma * sqrt(2.0 * M_PI));
262  for (int j = 0; j < size; j++) {
263  dgj = -(j - m) * exp(-vpMath::sqr(j - m) / (2.0 * sigma2)) / (2.0 * sigma * sigma2 * sqrt(2.0 * M_PI));
264  for (int i = 0; i < size; i++) {
265  dgi = -(i - m) * exp(-vpMath::sqr(i - m) / (2.0 * sigma2)) / (2.0 * sigma * sigma2 * sqrt(2.0 * M_PI));
266  filter(i, j, k, dgi * dgj * gk);
267  }
268  }
269  }
270  return filter;
271 }
272 
279 {
280  if (size <= 0 || (size % 2) != 1) {
281  throw vpException(
282  vpException::badValue,
283  "usVolumeProcessing::generateGaussianDerivativeFilterIK(): Bad filter size, should be positive and odd");
284  }
285 
286  double sigma2 = vpMath::sqr(sigma);
287  int m = (size - 1) / 2;
288  usImage3D<double> filter(size, size, size);
289 
290  double dgi, gj, dgk;
291  for (int k = 0; k < size; k++) {
292  dgk = -(k - m) * exp(-vpMath::sqr(k - m) / (2.0 * sigma2)) / (2.0 * sigma * sigma2 * sqrt(2.0 * M_PI));
293  for (int j = 0; j < size; j++) {
294  gj = exp(-vpMath::sqr(j - m) / (2.0 * sigma2)) / (sigma * sqrt(2.0 * M_PI));
295  for (int i = 0; i < size; i++) {
296  dgi = -(i - m) * exp(-vpMath::sqr(i - m) / (2.0 * sigma2)) / (2.0 * sigma * sigma2 * sqrt(2.0 * M_PI));
297  filter(i, j, k, dgi * gj * dgk);
298  }
299  }
300  }
301  return filter;
302 }
303 
310 {
311  if (size <= 0 || (size % 2) != 1) {
312  throw vpException(
313  vpException::badValue,
314  "usVolumeProcessing::generateGaussianDerivativeFilterJK(): Bad filter size, should be positive and odd");
315  }
316 
317  double sigma2 = vpMath::sqr(sigma);
318  int m = (size - 1) / 2;
319  usImage3D<double> filter(size, size, size);
320 
321  double gi, dgj, dgk;
322  for (int k = 0; k < size; k++) {
323  dgk = -(k - m) * exp(-vpMath::sqr(k - m) / (2.0 * sigma2)) / (2.0 * sigma * sigma2 * sqrt(2.0 * M_PI));
324  for (int j = 0; j < size; j++) {
325  dgj = -(j - m) * exp(-vpMath::sqr(j - m) / (2.0 * sigma2)) / (2.0 * sigma * sigma2 * sqrt(2.0 * M_PI));
326  for (int i = 0; i < size; i++) {
327  gi = exp(-vpMath::sqr(i - m) / (2.0 * sigma2)) / (sigma * sqrt(2.0 * M_PI));
328  filter(i, j, k, gi * dgj * dgk);
329  }
330  }
331  }
332  return filter;
333 }
Representation of a physical image volume.
Definition: usImage3D.h:61
unsigned int getNumberOfFrames() const
Definition: usImage3D.h:137
unsigned int getSize() const
Definition: usImage3D.h:143
Type * getData()
Definition: usImage3D.h:110
void resize(unsigned int height, unsigned int width, unsigned int numberOfFrames)
Definition: usImage3D.h:376
Type * getConstData() const
Definition: usImage3D.h:104
unsigned int getHeight() const
Definition: usImage3D.h:131
unsigned int getWidth() const
Definition: usImage3D.h:125
static usImage3D< double > generateGaussianDerivativeFilterJJ(double sigma, int size)
static usImage3D< double > generateGaussianDerivativeFilterIJ(double sigma, int size)
static usImage3D< double > generateGaussianDerivativeFilterJ(double sigma, int size)
static usImage3D< double > generateGaussianDerivativeFilterI(double sigma, int size)
static usImage3D< double > generateGaussianDerivativeFilterIK(double sigma, int size)
static usImage3D< double > generateGaussianDerivativeFilterII(double sigma, int size)
static usImage3D< double > generateGaussianDerivativeFilterKK(double sigma, int size)
static void norm(const usImage3D< vpColVector > &src, usImage3D< double > &dst)
static usImage3D< double > generateGaussianDerivativeFilterK(double sigma, int size)
static usImage3D< double > generateGaussianDerivativeFilterJK(double sigma, int size)