UsTK : Ultrasound ToolKit  version 2.0.1 under development (2024-12-17)
usSignalProcessing.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  * Pedro Patlan Rosales
30  * Marc Pouliquen
31  *
32  *****************************************************************************/
33 
34 #include <visp3/ustk_elastography/usSignalProcessing.h>
35 
46 
56 vpMatrix usSignalProcessing::GaussianFilter(int height, int width, double sigma)
57 {
58  int i, j, a, b;
59  double mul;
60  vpMatrix t_out(height, width);
61  a = (width - 1) / 2;
62  b = (height - 1) / 2;
63  for (i = -a; i <= a; i++) {
64  for (j = -b; j <= b; j++) {
65  mul = (exp((-1.0 * ((i * i) + (j * j))) / (2.0 * sigma * sigma))) / (2 * M_PI * sigma);
66  t_out[j + b][i + a] = mul;
67  }
68  }
69  t_out = t_out / t_out.getMaxValue();
70  return t_out;
71 }
72 
81 {
82  vpMatrix t_out(image.getHeight(), image.getWidth());
83 
84  // manage first and last of each column
85  for (uint i = 0; i < image.getHeight(); i++) {
86  t_out[i][0] = image(i, 1) - image(i, 0);
87  t_out[i][image.getWidth() - 1] = image(i, image.getWidth() - 2) - image(i, image.getWidth() - 1);
88  }
89 
90  for (uint i = 0; i < image.getHeight(); i++)
91  for (uint j = 1; j < image.getWidth() - 1; j++)
92  t_out[i][j] = (image(i, j + 1) - image(i, j - 1)) * 0.5;
93 
94  return t_out;
95 }
96 
105 {
106  vpMatrix t_out(image.getHeight(), image.getWidth());
107 
108  // manage first and last of each row
109  for (uint j = 0; j < image.getWidth(); j++) {
110  t_out[0][j] = image(1, j) - image(0, j);
111  t_out[image.getHeight() - 1][j] = image(image.getHeight() - 1, j) - image(image.getHeight() - 2, j);
112  }
113 
114  for (uint i = 1; i < image.getHeight() - 1; i++)
115  for (uint j = 0; j < image.getWidth(); j++)
116  t_out[i][j] = (image(i + 1, j) - image(i - 1, j)) * 0.5;
117 
118  return t_out;
119 }
120 
131 {
132  if (A.getHeight() != B.getHeight() || A.getWidth() != B.getWidth())
133  throw(vpException(vpException::dimensionError,
134  "usSignalProcessing::Difference(), input matrices dimentions mismatch."));
135 
136  vpMatrix t_out(A.getHeight(), A.getWidth());
137 
138  for (uint i = 0; i < A.getHeight(); i++)
139  for (uint j = 0; j < A.getWidth(); j++)
140  t_out[i][j] = A(i, j) - B(i, j);
141  return t_out;
142 }
143 
152 vpMatrix usSignalProcessing::BilinearInterpolation(vpMatrix In, uint newW, uint newH)
153 {
154  vpMatrix ivec(newH, newW);
155  double tx = (double)(In.getCols() - 1.0) / newW;
156  double ty = (double)(In.getRows() - 1.0) / newH;
157  double x_diff, y_diff;
158  double A, B, C, D;
159 
160  for (uint i = 0; i < newH; i++) {
161  for (uint j = 0; j < newW; j++) {
162  int x = (int)(tx * j);
163  int y = (int)(ty * i);
164  x_diff = ((tx * j) - x);
165  y_diff = ((ty * i) - y);
166 
167  A = In[y][x];
168  B = In[y][x + 1];
169  C = In[y + 1][x];
170  D = In[y + 1][x + 1];
171 
172  double vi = (double)(A * (1.0 - x_diff) * (1.0 - y_diff) + B * (x_diff) * (1.0 - y_diff) +
173  C * (y_diff) * (1.0 - x_diff) + D * (x_diff * y_diff));
174  ivec.data[j + i * newW] = vi;
175  }
176  }
177  return ivec;
178 }
179 
188 vpMatrix usSignalProcessing::HadamardProd(vpMatrix matrix1, vpMatrix matrix2)
189 {
190  assert(matrix1.getRows() == matrix2.getRows());
191  assert(matrix1.getCols() == matrix2.getCols());
192  uint t_rows = matrix2.getRows();
193  uint t_cols = matrix2.getCols();
194  vpMatrix t_out(t_rows, t_cols);
195  for (uint i = 0; i < t_rows; i++)
196  for (uint j = 0; j < t_cols; j++)
197  t_out[i][j] = matrix1[i][j] * matrix2[i][j];
198  return t_out;
199 }
unsigned int getHeight() const
Definition: usImageRF2D.h:412
unsigned int getWidth() const
Definition: usImageRF2D.h:424
static vpMatrix BilinearInterpolation(vpMatrix In, uint newW, uint newH)
Bilinear Interpolation.
usSignalProcessing()
Constructor.
static vpMatrix GaussianFilter(int height, int width, double sigma)
Gaussian Kernel generator.
static vpMatrix Difference(const usImageRF2D< short int > &A, const usImageRF2D< short int > &B)
static vpMatrix HadamardProd(vpMatrix matrix1, vpMatrix matrix2)
Element-wise product.
static vpMatrix getXGradient(const usImageRF2D< short > &image)
Computation of gradients.
virtual ~usSignalProcessing()
Virtual destructor.
static vpMatrix getYGradient(const usImageRF2D< short int > &image)