libalvision  2.1.4.13
 All Classes Namespaces Files Functions Variables Macros Pages
alimage_opencv.h
Go to the documentation of this file.
1 
10 #ifndef LIBALVISION_ALVISION_ALIMAGE_OPENCV_H_
11 #define LIBALVISION_ALVISION_ALIMAGE_OPENCV_H_
12 
13 #include <cstring> // for std::memcpy
14 #include <opencv2/core/core.hpp>
15 
16 #include <alerror/alerror.h>
17 
18 #include "alimage.h"
19 #include "alvisiondefinitions.h"
20 
21 namespace AL
22 {
29 static inline cv::Mat aLImageToCvMat(const AL::ALImage & al_image, int cv_type = -1)
30 {
31  // OpenCV does not take a const void* because the object could be modified later. This pointer will only be used
32  // in a tmp local cv::Mat so it's fine
33  unsigned char *data = const_cast<unsigned char*>(al_image.getData());
34 
35  if (cv_type >= 0)
36  return cv::Mat(al_image.getHeight(), al_image.getWidth(), cv_type, data);
37 
38  // Check the types
39  int n_layers = AL::getNumLayersInColorSpace(al_image.getColorSpace());
40  int n_channels = AL::getNumChannelsInColorSpace(al_image.getColorSpace());
41  if ((n_layers < 0) || (n_channels < 0)) {
42  std::stringstream ss;
43  ss << "Color space \"" << al_image.getColorSpace() << "\" not supported";
44  throw AL::ALError("ALVision", "aLImageToCvMat", ss.str());
45  }
46  int cv_depth;
47  switch (n_layers / n_channels)
48  {
49  case 4:
50  cv_depth = CV_32F;
51  break;
52  case 2:
53  cv_depth = CV_16U;
54  break;
55  case 1:
56  cv_depth = CV_8U;
57  break;
58  default:
59  std::stringstream ss;
60  ss << "Depth \"" << (n_layers / n_channels) << "\" not supported";
61  throw AL::ALError("ALVision", "aLImageToCvMat", ss.str());
62  }
63 
64  return cv::Mat(al_image.getHeight(), al_image.getWidth(),
65  CV_MAKETYPE(cv_depth, n_channels), data);
66 }
67 
74 static inline AL::ALImage* cvMatToALImage(const cv::Mat & img, int colorSpace,
75  float pLeftAngle = 0.0f, float pTopAngle = 0.0f,
76  float pRightAngle = 0.0f, float pBottomAngle = 0.0f)
77 {
78  // Check the number of channels
79  if (AL::getNumChannelsInColorSpace(colorSpace) != img.channels())
80  throw AL::ALError("ALVision", "cvMatToALImage",
81  "Channel number incompatibility between cv::Mat and colorSpace");
82 
83  // Check the type
84  switch (img.depth())
85  {
86  case CV_8U:
87  case CV_16U:
88  case CV_32F:
89  break;
90  default:
91  throw AL::ALError("ALVision", "cvMatToALImage",
92  "Type incompatibility between cv::Mat and colorSpace");
93  }
94 
95  // Create an ALImage with the buffer
96  AL::ALImage *al_image = new AL::ALImage(img.cols, img.rows, colorSpace, true,
97  pLeftAngle, pTopAngle, pRightAngle, pBottomAngle);
98  al_image->setData(img.data);
99 
100  return al_image;
101 }
102 
108 static inline AL::ALImage* cvMatToALImageCopy(const cv::Mat & img,
109  int colorSpace,
110  float pLeftAngle = 0.0f, float pTopAngle = 0.0f,
111  float pRightAngle = 0.0f, float pBottomAngle = 0.0f)
112 {
113  // Check the number of channels
114  if (AL::getNumChannelsInColorSpace(colorSpace) != img.channels())
115  throw AL::ALError("ALVision", "cvMatToALImage",
116  "Channel number incompatibility between cv::Mat and colorSpace");
117 
118  // Check the type
119  switch (img.type())
120  {
121  case CV_8U:
122  case CV_16U:
123  case CV_32F:
124  break;
125  default:
126  throw AL::ALError("ALVision", "cvMatToALImage",
127  "Type incompatibility between cv::Mat and colorSpace");
128  }
129 
130  // Copy the image data in a buffer
131  size_t data_size = img.cols * img.rows * img.elemSize();
132  unsigned char *data = new unsigned char[data_size];
133  std::memcpy(data, img.data, data_size);
134 
135  // Create an ALImage with the buffer
136  AL::ALImage *al_image = new AL::ALImage(img.cols, img.rows, colorSpace, false,
137  pLeftAngle, pTopAngle, pRightAngle, pBottomAngle);
138  al_image->setData(data);
139 
140  return al_image;
141 }
142 }
143 
144 #endif /* LIBALVISION_ALVISION_ALIMAGE_OPENCV_H_ */
int getNumLayersInColorSpace(const int colorSpace)
Utility function that returns the number of layers for a given color space.
int getNumChannelsInColorSpace(const int colorSpace)
Utility function that returns the number of channels for a given color space.
const unsigned char * getData() const
return the reference to the image data.
Definition: alimage.h:162
void setData(unsigned char *pData)
set the image data pointer to point to the specified buffer.
Definition: alimage.h:187
int getColorSpace(void) const
Definition: alimage.h:243
int getWidth(void) const
Definition: alimage.h:240
vision defines
int getHeight(void) const
Definition: alimage.h:241
handle image