14 #ifndef LIBALVISION_ALVISION_ALIMAGE_OPENCV_H_
15 #define LIBALVISION_ALVISION_ALIMAGE_OPENCV_H_
18 #include <opencv2/core.hpp>
19 #include <opencv2/imgcodecs.hpp>
21 #include <alerror/alerror.h>
34 static inline cv::Mat aLImageToCvMat(
const AL::ALImage & al_image,
int cv_type = -1)
38 unsigned char *data =
const_cast<unsigned char*
>(al_image.
getData());
46 if ((n_layers < 0) || (n_channels < 0)) {
48 ss <<
"Color space \"" << al_image.
getColorSpace() <<
"\" not supported";
49 throw AL::ALError(
"ALVision",
"aLImageToCvMat", ss.str());
52 switch (n_layers / n_channels)
65 ss <<
"Depth \"" << (n_layers / n_channels) <<
"\" not supported";
66 throw AL::ALError(
"ALVision",
"aLImageToCvMat", ss.str());
70 CV_MAKETYPE(cv_depth, n_channels), data);
79 static inline AL::ALImage* cvMatToALImage(
const cv::Mat & img,
81 float pLeftAngle = 0.0f,
float pTopAngle = 0.0f,
82 float pRightAngle = 0.0f,
float pBottomAngle = 0.0f)
86 throw AL::ALError(
"ALVision",
"cvMatToALImage",
87 "Channel number incompatibility between cv::Mat and colorSpace");
97 throw AL::ALError(
"ALVision",
"cvMatToALImage",
98 "Type incompatibility between cv::Mat and colorSpace");
103 pLeftAngle, pTopAngle, pRightAngle, pBottomAngle);
114 static inline AL::ALImage* cvMatToALImageCopy(
const cv::Mat & img,
116 float pLeftAngle = 0.0f,
float pTopAngle = 0.0f,
117 float pRightAngle = 0.0f,
float pBottomAngle = 0.0f)
121 throw AL::ALError(
"ALVision",
"cvMatToALImage",
122 "Channel number incompatibility between cv::Mat and colorSpace");
132 throw AL::ALError(
"ALVision",
"cvMatToALImage",
133 "Type incompatibility between cv::Mat and colorSpace");
138 pLeftAngle, pTopAngle, pRightAngle, pBottomAngle);
139 std::memcpy(al_image->
getData(), img.data, img.cols * img.rows * img.elemSize());
152 static inline bool writeALImageToFile(
const std::string& path,
const ALImage& al_image)
154 if(!cv::imwrite(path, aLImageToCvMat(al_image)))
156 throw AL::ALError(
"ALVision",
"writeALImageToFile",
157 "Can't write image to "+path);
160 const std::string path_data = path+
".yml";
161 cv::FileStorage fs(path_data, cv::FileStorage::WRITE);
164 throw AL::ALError(
"ALVision",
"writeALImageToFile",
165 "Can't open "+path_data+
" to write metadata");
168 fs <<
"width" << al_image.getWidth();
169 fs <<
"height" << al_image.getHeight();
171 fs <<
"colorspace" << al_image.getColorSpace();
172 long long timestamp = al_image.getTimeStamp();
173 fs <<
"timestamp_s" <<
static_cast<int>(timestamp/1000000ll);
174 fs <<
"timestamp_us" <<
static_cast<int>(timestamp%1000000ll);
175 fs <<
"camera_id" <<
static_cast<int>(al_image.getCameraId());
176 fs <<
"fov_left" << al_image.getLeftAngle();
177 fs <<
"fov_top" << al_image.getTopAngle();
178 fs <<
"fov_right" << al_image.getRightAngle();
179 fs <<
"fov_bottom" << al_image.getBottomAngle();
180 fs <<
"roi_enabled" << al_image.isROIEnabled();
182 for(
int i = 0; i < al_image.getNumOfROIs(); ++i)
184 const ALImage::ROI* roi = al_image.getROI(i);
189 <<
"height" << roi->h
190 <<
"leftAngle" << roi->leftAngle
191 <<
"topAngle" << roi->topAngle
192 <<
"rightAngle" << roi->rightAngle
193 <<
"bottomAngle" << roi->bottomAngle
198 }
catch(
const cv::Exception& e)
200 throw AL::ALError(
"ALVision",
"writeALImageToFile",
201 "Exception caught when writing metadata to "+path_data+
": "+e.what());
214 static inline ALImage readALImageFromFile(
const std::string& path,
int pColorspace = -1)
216 const cv::Mat img = cv::imread(path, cv::IMREAD_ANYCOLOR);
218 const std::string path_data = path+
".yml";
219 cv::FileStorage fs(path_data, cv::FileStorage::READ);
223 if(pColorspace == -1)
225 throw AL::ALError(
"ALVision",
"readALImageFromFile",
226 "Can't open "+path_data+
" to read metadata");
230 ALImage al_image(img.cols, img.rows, pColorspace,
false);
231 unsigned char* data = al_image.getData();
232 const size_t data_size = img.cols * img.rows * img.elemSize();
233 std::memcpy(data, img.data, data_size);
238 int width, height, colorspace, timestamp_s, timestamp_us, camera_id;
239 float fov_left, fov_top, fov_right, fov_bottom;
241 std::vector<ALImage::ROI> rois;
243 fs[
"width"] >> width;
244 fs[
"height"] >> height;
246 fs[
"colorspace"] >> colorspace;
247 fs[
"timestamp_s"] >> timestamp_s;
248 fs[
"timestamp_us"] >> timestamp_us;
249 fs[
"camera_id"] >> camera_id;
250 fs[
"fov_left"] >> fov_left;
251 fs[
"fov_top"] >> fov_top;
252 fs[
"fov_right"] >> fov_right;
253 fs[
"fov_bottom"] >> fov_bottom;
254 fs[
"roi_enabled"] >> roi_enabled;
255 cv::FileNode fn_rois = fs[
"rois"];
256 cv::FileNodeIterator it = fn_rois.begin();
257 for(; it != fn_rois.end(); ++it)
259 const int roi_x = (*it)[
"x"];
260 const int roi_y = (*it)[
"y"];
261 const int roi_w = (*it)[
"width"];
262 const int roi_h = (*it)[
"height"];
263 const float roi_leftAngle = (*it)[
"leftAngle"];
264 const float roi_topAngle = (*it)[
"topAngle"];
265 const float roi_rightAngle = (*it)[
"rightAngle"];
266 const float roi_bottomAngle = (*it)[
"bottomAngle"];
267 const ALImage::ROI roi(roi_x, roi_y, roi_w, roi_h,
268 roi_leftAngle, roi_topAngle, roi_rightAngle, roi_bottomAngle);
272 }
catch(
const cv::Exception& e)
274 throw AL::ALError(
"ALVision",
"readALImageFromFile",
275 "Exception caught when reading metadata from "+path_data+
": "+e.what());
278 if(width != img.cols || height != img.rows)
280 throw AL::ALError(
"ALVision",
"readALImageFromFile",
281 "Image dimensions do not match metadata");
284 ALImage al_image(img.cols, img.rows, colorspace,
false);
285 unsigned char* data = al_image.getData();
286 const size_t data_size = img.cols * img.rows * img.elemSize();
287 std::memcpy(data, img.data, data_size);
288 al_image.setTimeStamp(timestamp_s, timestamp_us);
289 al_image.setCameraId(static_cast<char>(camera_id));
290 al_image.setAngles(fov_left, fov_top, fov_right, fov_bottom);
291 al_image.setEnableROIs(roi_enabled);
292 for(
size_t i = 0; i < rois.size(); ++i)
294 al_image.addROI(rois[i]);
int getNumChannelsInColorSpace(const int colorSpace)
Utility function that returns the number of channels for a given color space.
int getNumLayersInColorSpace(const int colorSpace)
Utility function that returns the number of layers for a given color space.
void setData(unsigned char *pData)
set the image data pointer to point to the specified buffer.
const unsigned char * getData() const
return the reference to the image data.
int getHeight(void) const
int getColorSpace(void) const