Visual Servoing Platform  version 3.6.1 under development (2024-09-07)
vpFeatureLuminanceMapping.h
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  * Luminance based feature.
32  */
33 
34 #ifndef VP_FEATURE_LUMINANCE_MAPPING_H
35 #define VP_FEATURE_LUMINANCE_MAPPING_H
36 #include <visp3/core/vpConfig.h>
37 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
38 #include <array>
39 #include <memory>
40 
41 #include <visp3/core/vpImage.h>
42 #include <visp3/core/vpMatrix.h>
43 #include <visp3/visual_features/vpBasicFeature.h>
44 #include <visp3/visual_features/vpFeatureLuminance.h>
45 
47 
57 class VISP_EXPORT vpLuminanceMapping
58 {
59 public:
65  vpLuminanceMapping(unsigned int mappingSize) : m_mappingSize(mappingSize) { }
66 
70  virtual ~vpLuminanceMapping() { }
71 
82  virtual void map(const vpImage<unsigned char> &I, vpColVector &s) = 0;
83 
92  virtual void interaction(const vpImage<unsigned char> &I, const vpMatrix &LI, const vpColVector &s, vpMatrix &L) = 0;
93 
100  virtual void inverse(const vpColVector &s, vpImage<unsigned char> &I) = 0;
101 
107  unsigned int getProjectionSize() const { return m_mappingSize; }
108 
114  unsigned int getBorder() const { return m_border; }
115 
122  void setBorder(unsigned border) { m_border = border; }
123 
124  static void imageAsVector(const vpImage<unsigned char> &I, vpColVector &Ivec, unsigned border);
125  static void imageAsMatrix(const vpImage<unsigned char> &I, vpMatrix &Imat, unsigned border);
126 
127 protected:
128  unsigned m_mappingSize;
129  unsigned m_border;
130 };
131 
150 class VISP_EXPORT vpLuminancePCA : public vpLuminanceMapping
151 {
152 public:
153  vpLuminancePCA() : vpLuminanceMapping(0), m_basis(nullptr), m_mean(nullptr), m_Ivec(0), m_Ih(0), m_Iw(0) { }
154 
158  virtual ~vpLuminancePCA() { }
159 
167  vpLuminancePCA(const std::shared_ptr<vpMatrix> &basis, const std::shared_ptr<vpColVector> &mean, const vpColVector &explainedVariance);
168 
172  vpLuminancePCA(const vpLuminancePCA &other);
173 
174  vpLuminancePCA &operator=(const vpLuminancePCA &other);
175 
184  void init(const std::shared_ptr<vpMatrix> &basis, const std::shared_ptr<vpColVector> &mean, const vpColVector &variance);
185 
191  std::shared_ptr<vpMatrix> getBasis() const { return m_basis; }
196  std::shared_ptr<vpColVector> getMean() const { return m_mean; }
197 
205  vpColVector getExplainedVariance() const { return m_explainedVariance; }
206 
207  void map(const vpImage<unsigned char> &I, vpColVector &s) VP_OVERRIDE;
208  void inverse(const vpColVector &s, vpImage<unsigned char> &I) VP_OVERRIDE;
209  void interaction(const vpImage<unsigned char> &I, const vpMatrix &LI, const vpColVector &s, vpMatrix &L) VP_OVERRIDE;
210 
220  void save(const std::string &basisFilename, const std::string &meanFileName, const std::string &explainedVarianceFile) const;
221 
231  static vpLuminancePCA load(const std::string &basisFilename, const std::string &meanFileName, const std::string &explainedVarianceFile);
232 
233 #ifdef VISP_HAVE_MODULE_IO
245  static vpLuminancePCA learn(const std::vector<std::string> &imageFiles, const unsigned int projectionSize, const unsigned int imageBorder = 0);
246 #endif
247 
259  static vpLuminancePCA learn(const std::vector<vpImage<unsigned char>> &images, const unsigned int projectionSize, const unsigned int imageBorder = 0);
267  static vpLuminancePCA learn(const vpMatrix &images, const unsigned int projectionSize);
268 
269 
270 private:
271  std::shared_ptr<vpMatrix> m_basis;
272  std::shared_ptr<vpColVector> m_mean;
273  vpColVector m_explainedVariance;
274  vpColVector m_Ivec;
275  unsigned int m_Ih, m_Iw;
276 };
277 
286 class VISP_EXPORT vpLuminanceDCT : public vpLuminanceMapping
287 {
288 public:
293  class VISP_EXPORT vpMatrixZigZagIndex
294  {
295  public:
303  void init(unsigned rows, unsigned cols);
312  void getValues(const vpMatrix &m, unsigned int start, unsigned int end, vpColVector &s) const;
313 
321  void setValues(const vpColVector &s, unsigned int start, vpMatrix &m) const;
322 
323  private:
324  std::vector<unsigned> m_rowIndex; // Contains the row index of the nth value of the zigzag indexing
325  std::vector<unsigned> m_colIndex; // Contains the row index of the nth value of the zigzag indexing
326  unsigned m_rows;
327  unsigned m_cols;
328  };
329 
335  vpLuminanceDCT(const unsigned int k) : vpLuminanceMapping(k)
336  {
337  init(k);
338  }
339 
343  void init(const unsigned int k)
344  {
345  m_mappingSize = k;
347  m_Ih = m_Iw = 0;
348  }
349 
353  vpLuminanceDCT(const vpLuminanceDCT &other);
354 
355  vpLuminanceDCT &operator=(const vpLuminanceDCT &other) = default;
356 
357  void map(const vpImage<unsigned char> &I, vpColVector &s) VP_OVERRIDE;
358  void inverse(const vpColVector &s, vpImage<unsigned char> &I) VP_OVERRIDE;
359  void interaction(const vpImage<unsigned char> &I, const vpMatrix &LI, const vpColVector &s, vpMatrix &L) VP_OVERRIDE;
360 
361 private:
362  void computeDCTMatrix(vpMatrix &D, unsigned int n) const;
363  void computeDCTMatrices(unsigned int rows, unsigned int cols);
364 
365 protected:
366  unsigned m_Ih, m_Iw;
369  vpMatrix m_Dcols, m_Drows;
370  std::array<vpMatrix, 6> m_dIdrPlanes;
372 };
373 
387 class VISP_EXPORT vpFeatureLuminanceMapping : public vpBasicFeature
388 {
389 public:
390  vpFeatureLuminanceMapping(const vpCameraParameters &cam, unsigned int h, unsigned int w, double Z, const std::shared_ptr<vpLuminanceMapping> mapping);
391  vpFeatureLuminanceMapping(const vpFeatureLuminance &luminance, std::shared_ptr<vpLuminanceMapping> mapping);
392  void init() VP_OVERRIDE;
393  void init(const vpCameraParameters &cam, unsigned int h, unsigned int w, double Z, std::shared_ptr<vpLuminanceMapping> mapping);
394  void init(const vpFeatureLuminance &luminance, std::shared_ptr<vpLuminanceMapping> mapping);
395 
398  vpFeatureLuminanceMapping *duplicate() const VP_OVERRIDE;
399 
400  virtual ~vpFeatureLuminanceMapping() = default;
401 
402  void build(vpImage<unsigned char> &I);
403 
405  unsigned int = 1) const VP_OVERRIDE
406  { }
408  unsigned int = 1) const VP_OVERRIDE
409  { }
410 
411  vpColVector error(const vpBasicFeature &s_star, unsigned int select = FEATURE_ALL) VP_OVERRIDE;
412  void error(const vpBasicFeature &s_star, vpColVector &e);
413 
414  vpMatrix interaction(unsigned int select = FEATURE_ALL) VP_OVERRIDE;
415  void interaction(vpMatrix &L);
416 
417  void print(unsigned int select = FEATURE_ALL) const VP_OVERRIDE;
418 
420  std::shared_ptr<vpLuminanceMapping> &getMapping() { return m_mapping; }
421 
422 private:
423  std::shared_ptr<vpLuminanceMapping> m_mapping;
424  vpFeatureLuminance m_featI;
425  vpMatrix m_LI;
427 };
428 END_VISP_NAMESPACE
429 #endif
430 #endif
class that defines what is a visual feature
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:157
static const vpColor green
Definition: vpColor.h:220
Class to combine luminance features (photometric servoing)
std::shared_ptr< vpLuminanceMapping > & getMapping()
void display(const vpCameraParameters &, const vpImage< unsigned char > &, const vpColor &=vpColor::green, unsigned int=1) const VP_OVERRIDE
void display(const vpCameraParameters &, const vpImage< vpRGBa > &, const vpColor &=vpColor::green, unsigned int=1) const VP_OVERRIDE
virtual ~vpFeatureLuminanceMapping()=default
vpFeatureLuminance & getLuminanceFeature()
Class that defines the image luminance visual feature.
static const int DEFAULT_BORDER
Helper class to iterate and get/set the values from a matrix, following a zigzag pattern.
Implementation of .
vpMatrix m_Dcols
DCT representation of the image.
vpLuminanceDCT::vpMatrixZigZagIndex m_zigzag
Luminance interaction matrix, seen as six image planes.
vpMatrix m_dct
Image as a matrix.
void init(const unsigned int k)
Initialize the DCT object with the number of required components.
vpLuminanceDCT & operator=(const vpLuminanceDCT &other)=default
vpMatrix m_Imat
image dimensions (without borders)
std::array< vpMatrix, 6 > m_dIdrPlanes
the computed DCT matrices. The separable property of DCt is used so that a 1D DCT is computed on rows...
vpLuminanceDCT(const unsigned int k)
Build a new DCT object.
Base class for functions that map an image and its interaction matrix to a different domain.
unsigned int getProjectionSize() const
Returns the size of the space to which an image is mapped to.
virtual void interaction(const vpImage< unsigned char > &I, const vpMatrix &LI, const vpColVector &s, vpMatrix &L)=0
Compute the interaction matrix associated with the representation s.
unsigned int getBorder() const
Returns the number of pixels that are removed by the photometric VS computation.
virtual void inverse(const vpColVector &s, vpImage< unsigned char > &I)=0
Reconstruct I from a representation s.
vpLuminanceMapping(unsigned int mappingSize)
Construct a new vp Luminance Mapping object.
void setBorder(unsigned border)
Set the number of pixels that are removed by the photometric VS computation This function should be c...
virtual void map(const vpImage< unsigned char > &I, vpColVector &s)=0
Map an image I to a representation s. This representation s has getProjectionSize() rows.
unsigned m_border
Final vector size.
Implementation of .
std::shared_ptr< vpColVector > getMean() const
Get , the mean image computed from the dataset.
std::shared_ptr< vpMatrix > getBasis() const
Get , the subspace projection matrix ( )
vpColVector getExplainedVariance() const
Get the values of explained variance by each of the eigen vectors.
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169