Visual Servoing Platform  version 3.6.1 under development (2025-02-19)
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 val_2 = 2;
127  const unsigned int stop = (size - 1) / 2;
128  const unsigned int width = I.getWidth();
129  double result = 0.;
130 
131  for (unsigned int i = 1; i <= stop; ++i) {
132  if ((c + i) < width) {
133  result += filter[i] * static_cast<double>(I[r][c + i].R + I[r][c - i].R);
134  }
135  else {
136  result += filter[i] * static_cast<double>(I[r][((val_2 * width) - c) - i - 1].R + I[r][c - i].R);
137  }
138  }
139  return result + (filter[0] * static_cast<double>(I[r][c].R));
140 }
141 
142 double vpImageFilter::filterXRightBorderG(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c,
143  const double *filter, unsigned int size)
144 {
145  const unsigned int val_2 = 2;
146  const unsigned int stop = (size - 1) / 2;
147  const unsigned int width = I.getWidth();
148  double result = 0.;
149 
150  for (unsigned int i = 1; i <= stop; ++i) {
151  if ((c + i) < width) {
152  result += filter[i] * static_cast<double>(I[r][c + i].G + I[r][c - i].G);
153  }
154  else {
155  result += filter[i] * static_cast<double>(I[r][((val_2 * width) - c) - i - 1].G + I[r][c - i].G);
156  }
157  }
158  return result + (filter[0] * static_cast<double>(I[r][c].G));
159 }
160 
161 double vpImageFilter::filterXRightBorderB(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c,
162  const double *filter, unsigned int size)
163 {
164  const unsigned int val_2 = 2;
165  const unsigned int stop = (size - 1) / 2;
166  const unsigned int width = I.getWidth();
167  double result = 0.;
168 
169  for (unsigned int i = 1; i <= stop; ++i) {
170  if ((c + i) < width) {
171  result += filter[i] * static_cast<double>(I[r][c + i].B + I[r][c - i].B);
172  }
173  else {
174  result += filter[i] * static_cast<double>(I[r][(val_2 * width) - c - i - 1].B + I[r][c - i].B);
175  }
176  }
177  return result + (filter[0] * static_cast<double>(I[r][c].B));
178 }
179 
180 double vpImageFilter::filterYR(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
181 {
182  const unsigned int stop = (size - 1) / 2;
183  double result = 0.;
184 
185  for (unsigned int i = 1; i <= stop; ++i) {
186  result += filter[i] * static_cast<double>(I[r + i][c].R + I[r - i][c].R);
187  }
188  return result + (filter[0] * static_cast<double>(I[r][c].R));
189 }
190 
191 double vpImageFilter::filterYG(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
192 {
193  const unsigned int stop = (size - 1) / 2;
194  double result = 0.;
195 
196  for (unsigned int i = 1; i <= stop; ++i) {
197  result += filter[i] * static_cast<double>(I[r + i][c].G + I[r - i][c].G);
198  }
199  return result + (filter[0] * static_cast<double>(I[r][c].G));
200 }
201 
202 double vpImageFilter::filterYB(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
203 {
204  const unsigned int stop = (size - 1) / 2;
205  double result = 0.;
206 
207  for (unsigned int i = 1; i <= stop; ++i) {
208  result += filter[i] * static_cast<double>(I[r + i][c].B + I[r - i][c].B);
209  }
210  return result + (filter[0] * static_cast<double>(I[r][c].B));
211 }
212 
213 double vpImageFilter::filterYTopBorderR(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
214 {
215  const unsigned int stop = (size - 1) / 2;
216  double result = 0.;
217 
218  for (unsigned int i = 1; i <= stop; ++i) {
219  if (r > i) {
220  result += filter[i] * static_cast<double>(I[r + i][c].R + I[r - i][c].R);
221  }
222  else {
223  result += filter[i] * static_cast<double>(I[r + i][c].R + I[i - r][c].R);
224  }
225  }
226  return result + (filter[0] * static_cast<double>(I[r][c].R));
227 }
228 
229 double vpImageFilter::filterYTopBorderG(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
230 {
231  const unsigned int stop = (size - 1) / 2;
232  double result = 0.;
233 
234  for (unsigned int i = 1; i <= stop; ++i) {
235  if (r > i) {
236  result += filter[i] * static_cast<double>(I[r + i][c].G + I[r - i][c].G);
237  }
238  else {
239  result += filter[i] * static_cast<double>(I[r + i][c].G + I[i - r][c].G);
240  }
241  }
242  return result + (filter[0] * static_cast<double>(I[r][c].G));
243 }
244 
245 double vpImageFilter::filterYTopBorderB(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
246 {
247  const unsigned int stop = (size - 1) / 2;
248  double result = 0.;
249 
250  for (unsigned int i = 1; i <= stop; ++i) {
251  if (r > i) {
252  result += filter[i] * static_cast<double>(I[r + i][c].B + I[r - i][c].B);
253  }
254  else {
255  result += filter[i] * static_cast<double>(I[r + i][c].B + I[i - r][c].B);
256  }
257  }
258  return result + (filter[0] * static_cast<double>(I[r][c].B));
259 }
260 
261 double vpImageFilter::filterYBottomBorderR(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c,
262  const double *filter, unsigned int size)
263 {
264  const unsigned int val_2 = 2;
265  const unsigned int height = I.getHeight();
266  const unsigned int stop = (size - 1) / 2;
267  double result = 0.;
268 
269  for (unsigned int i = 1; i <= stop; ++i) {
270  if ((r + i) < height) {
271  result += filter[i] * static_cast<double>(I[r + i][c].R + I[r - i][c].R);
272  }
273  else {
274  result += filter[i] * static_cast<double>(I[((val_2 * height) - r) - i - 1][c].R + I[r - i][c].R);
275  }
276  }
277  return result + (filter[0] * static_cast<double>(I[r][c].R));
278 }
279 
280 double vpImageFilter::filterYBottomBorderG(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c,
281  const double *filter, unsigned int size)
282 {
283  const unsigned int val_2 = 2;
284  const unsigned int height = I.getHeight();
285  const unsigned int stop = (size - 1) / 2;
286  double result = 0.;
287 
288  for (unsigned int i = 1; i <= stop; ++i) {
289  if ((r + i) < height) {
290  result += filter[i] * static_cast<double>(I[r + i][c].G + I[r - i][c].G);
291  }
292  else {
293  result += filter[i] * static_cast<double>(I[((val_2 * height) - r) - i - 1][c].G + I[r - i][c].G);
294  }
295  }
296  return result + (filter[0] * static_cast<double>(I[r][c].G));
297 }
298 
299 double vpImageFilter::filterYBottomBorderB(const vpImage<vpRGBa> &I, unsigned int r, unsigned int c,
300  const double *filter, unsigned int size)
301 {
302  const unsigned int val_2 = 2;
303  const unsigned int height = I.getHeight();
304  const unsigned int stop = (size - 1) / 2;
305  double result = 0.;
306 
307  for (unsigned int i = 1; i <= stop; ++i) {
308  if ((r + i) < height) {
309  result += filter[i] * static_cast<double>(I[r + i][c].B + I[r - i][c].B);
310  }
311  else {
312  result += filter[i] * static_cast<double>(I[((val_2 * height) - r) - i - 1][c].B + I[r - i][c].B);
313  }
314  }
315  return result + (filter[0] * static_cast<double>(I[r][c].B));
316 }
317 
318 #endif
319 
320 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