Visual Servoing Platform  version 3.6.1 under development (2024-10-18)
vpImageFilter_xy.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 ViSP 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  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Image XY filtering.
32  */
33 
34 #include <visp3/core/vpConfig.h>
35 #include <visp3/core/vpImageFilter.h>
36 
37 BEGIN_VISP_NAMESPACE
38 
39 #ifndef DOXYGEN_SHOULD_SKIP_THIS
40 double vpImageFilter::filterXR(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
41 {
42  const unsigned int stop = (size - 1) / 2;
43  double result = 0.;
44  for (unsigned int i = 1; i <= stop; ++i) {
45  result += filter[i] * static_cast<double>(I[r][c + i].R + I[r][c - i].R);
46  }
47  return result + (filter[0] * static_cast<double>(I[r][c].R));
48 }
49 
50 double vpImageFilter::filterXG(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
51 {
52  const unsigned int stop = (size - 1) / 2;
53  double result = 0.;
54 
55  for (unsigned int i = 1; i <= stop; ++i) {
56  result += filter[i] * static_cast<double>(I[r][c + i].G + I[r][c - i].G);
57  }
58  return result + (filter[0] * static_cast<double>(I[r][c].G));
59 }
60 
61 double vpImageFilter::filterXB(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
62 {
63  const unsigned int stop = (size - 1) / 2;
64  double result = 0.;
65 
66  for (unsigned int i = 1; i <= stop; ++i) {
67  result += filter[i] * static_cast<double>(I[r][c + i].B + I[r][c - i].B);
68  }
69  return result + (filter[0] * static_cast<double>(I[r][c].B));
70 }
71 
72 double vpImageFilter::filterXLeftBorderR(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c,
73  const double *filter, unsigned int size)
74 {
75  const unsigned int stop = (size - 1) / 2;
76  double result = 0.;
77 
78  for (unsigned int i = 1; i <= stop; ++i) {
79  if (c > i) {
80  result += filter[i] * static_cast<double>(I[r][c + i].R + I[r][c - i].R);
81  }
82  else {
83  result += filter[i] * static_cast<double>(I[r][c + i].R + I[r][i - c].R);
84  }
85  }
86  return result + (filter[0] * static_cast<double>(I[r][c].R));
87 }
88 
89 double vpImageFilter::filterXLeftBorderG(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c,
90  const double *filter, unsigned int size)
91 {
92  const unsigned int stop = (size - 1) / 2;
93  double result = 0.;
94 
95  for (unsigned int i = 1; i <= stop; ++i) {
96  if (c > i) {
97  result += filter[i] * static_cast<double>(I[r][c + i].G + I[r][c - i].G);
98  }
99  else {
100  result += filter[i] * static_cast<double>(I[r][c + i].G + I[r][i - c].G);
101  }
102  }
103  return result + (filter[0] * static_cast<double>(I[r][c].G));
104 }
105 
106 double vpImageFilter::filterXLeftBorderB(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c,
107  const double *filter, unsigned int size)
108 {
109  const unsigned int stop = (size - 1) / 2;
110  double result = 0.;
111 
112  for (unsigned int i = 1; i <= stop; ++i) {
113  if (c > i) {
114  result += filter[i] * static_cast<double>(I[r][c + i].B + I[r][c - i].B);
115  }
116  else {
117  result += filter[i] * static_cast<double>(I[r][c + i].B + I[r][i - c].B);
118  }
119  }
120  return result + (filter[0] * static_cast<double>(I[r][c].B));
121 }
122 
123 double vpImageFilter::filterXRightBorderR(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c,
124  const double *filter, unsigned int size)
125 {
126  const unsigned int stop = (size - 1) / 2;
127  const unsigned int width = I.getWidth();
128  double result = 0.;
129 
130  for (unsigned int i = 1; i <= stop; ++i) {
131  if ((c + i) < width) {
132  result += filter[i] * static_cast<double>(I[r][c + i].R + I[r][c - i].R);
133  }
134  else {
135  result += filter[i] * static_cast<double>(I[r][((2 * width) - c) - i - 1].R + I[r][c - i].R);
136  }
137  }
138  return result + (filter[0] * static_cast<double>(I[r][c].R));
139 }
140 
141 double vpImageFilter::filterXRightBorderG(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c,
142  const double *filter, unsigned int size)
143 {
144  const unsigned int stop = (size - 1) / 2;
145  const unsigned int width = I.getWidth();
146  double result = 0.;
147 
148  for (unsigned int i = 1; i <= stop; ++i) {
149  if ((c + i) < width) {
150  result += filter[i] * static_cast<double>(I[r][c + i].G + I[r][c - i].G);
151  }
152  else {
153  result += filter[i] * static_cast<double>(I[r][((2 * width) - c) - i - 1].G + I[r][c - i].G);
154  }
155  }
156  return result + (filter[0] * static_cast<double>(I[r][c].G));
157 }
158 
159 double vpImageFilter::filterXRightBorderB(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c,
160  const double *filter, unsigned int size)
161 {
162  const unsigned int stop = (size - 1) / 2;
163  const unsigned int width = I.getWidth();
164  double result = 0.;
165 
166  for (unsigned int i = 1; i <= stop; ++i) {
167  if ((c + i) < width) {
168  result += filter[i] * static_cast<double>(I[r][c + i].B + I[r][c - i].B);
169  }
170  else {
171  result += filter[i] * static_cast<double>(I[r][(2 * width) - c - i - 1].B + I[r][c - i].B);
172  }
173  }
174  return result + (filter[0] * static_cast<double>(I[r][c].B));
175 }
176 
177 double vpImageFilter::filterYR(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
178 {
179  const unsigned int stop = (size - 1) / 2;
180  double result = 0.;
181 
182  for (unsigned int i = 1; i <= stop; ++i) {
183  result += filter[i] * static_cast<double>(I[r + i][c].R + I[r - i][c].R);
184  }
185  return result + (filter[0] * static_cast<double>(I[r][c].R));
186 }
187 
188 double vpImageFilter::filterYG(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
189 {
190  const unsigned int stop = (size - 1) / 2;
191  double result = 0.;
192 
193  for (unsigned int i = 1; i <= stop; ++i) {
194  result += filter[i] * static_cast<double>(I[r + i][c].G + I[r - i][c].G);
195  }
196  return result + (filter[0] * static_cast<double>(I[r][c].G));
197 }
198 
199 double vpImageFilter::filterYB(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
200 {
201  const unsigned int stop = (size - 1) / 2;
202  double result = 0.;
203 
204  for (unsigned int i = 1; i <= stop; ++i) {
205  result += filter[i] * static_cast<double>(I[r + i][c].B + I[r - i][c].B);
206  }
207  return result + (filter[0] * static_cast<double>(I[r][c].B));
208 }
209 
210 double vpImageFilter::filterYTopBorderR(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
211 {
212  const unsigned int stop = (size - 1) / 2;
213  double result = 0.;
214 
215  for (unsigned int i = 1; i <= stop; ++i) {
216  if (r > i) {
217  result += filter[i] * static_cast<double>(I[r + i][c].R + I[r - i][c].R);
218  }
219  else {
220  result += filter[i] * static_cast<double>(I[r + i][c].R + I[i - r][c].R);
221  }
222  }
223  return result + (filter[0] * static_cast<double>(I[r][c].R));
224 }
225 
226 double vpImageFilter::filterYTopBorderG(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
227 {
228  const unsigned int stop = (size - 1) / 2;
229  double result = 0.;
230 
231  for (unsigned int i = 1; i <= stop; ++i) {
232  if (r > i) {
233  result += filter[i] * static_cast<double>(I[r + i][c].G + I[r - i][c].G);
234  }
235  else {
236  result += filter[i] * static_cast<double>(I[r + i][c].G + I[i - r][c].G);
237  }
238  }
239  return result + (filter[0] * static_cast<double>(I[r][c].G));
240 }
241 
242 double vpImageFilter::filterYTopBorderB(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
243 {
244  const unsigned int stop = (size - 1) / 2;
245  double result = 0.;
246 
247  for (unsigned int i = 1; i <= stop; ++i) {
248  if (r > i) {
249  result += filter[i] * static_cast<double>(I[r + i][c].B + I[r - i][c].B);
250  }
251  else {
252  result += filter[i] * static_cast<double>(I[r + i][c].B + I[i - r][c].B);
253  }
254  }
255  return result + (filter[0] * static_cast<double>(I[r][c].B));
256 }
257 
258 double vpImageFilter::filterYBottomBorderR(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c,
259  const double *filter, unsigned int size)
260 {
261  const unsigned int height = I.getHeight();
262  const unsigned int stop = (size - 1) / 2;
263  double result = 0.;
264 
265  for (unsigned int i = 1; i <= stop; ++i) {
266  if ((r + i) < height) {
267  result += filter[i] * static_cast<double>(I[r + i][c].R + I[r - i][c].R);
268  }
269  else {
270  result += filter[i] * static_cast<double>(I[((2 * height) - r) - i - 1][c].R + I[r - i][c].R);
271  }
272  }
273  return result + (filter[0] * static_cast<double>(I[r][c].R));
274 }
275 
276 double vpImageFilter::filterYBottomBorderG(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c,
277  const double *filter, unsigned int size)
278 {
279  const unsigned int height = I.getHeight();
280  const unsigned int stop = (size - 1) / 2;
281  double result = 0.;
282 
283  for (unsigned int i = 1; i <= stop; ++i) {
284  if ((r + i) < height) {
285  result += filter[i] * static_cast<double>(I[r + i][c].G + I[r - i][c].G);
286  }
287  else {
288  result += filter[i] * static_cast<double>(I[((2 * height) - r) - i - 1][c].G + I[r - i][c].G);
289  }
290  }
291  return result + (filter[0] * static_cast<double>(I[r][c].G));
292 }
293 
294 double vpImageFilter::filterYBottomBorderB(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c,
295  const double *filter, unsigned int size)
296 {
297  const unsigned int height = I.getHeight();
298  const unsigned int stop = (size - 1) / 2;
299  double result = 0.;
300 
301  for (unsigned int i = 1; i <= stop; ++i) {
302  if ((r + i) < height) {
303  result += filter[i] * static_cast<double>(I[r + i][c].B + I[r - i][c].B);
304  }
305  else {
306  result += filter[i] * static_cast<double>(I[((2 * height) - r) - i - 1][c].B + I[r - i][c].B);
307  }
308  }
309  return result + (filter[0] * static_cast<double>(I[r][c].B));
310 }
311 
312 #endif
313 
314 END_VISP_NAMESPACE
static void filter(const vpImage< ImageType > &I, vpImage< FilterType > &If, const vpArray2D< FilterType > &M, bool convolve=false, const vpImage< bool > *p_mask=nullptr)
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181