14 #ifndef LIBALVISION_ALVISION_ALIMAGE_OPENCV_H_ 
   15 #define LIBALVISION_ALVISION_ALIMAGE_OPENCV_H_ 
   18 #include <opencv2/core/core.hpp> 
   19 #include <opencv2/highgui/highgui.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, 
int colorSpace,
 
   80                                           float pLeftAngle  = 0.0f, 
float pTopAngle    = 0.0f,
 
   81                                           float pRightAngle = 0.0f, 
float pBottomAngle = 0.0f)
 
   85       throw AL::ALError(
"ALVision", 
"cvMatToALImage",
 
   86           "Channel number incompatibility between cv::Mat and colorSpace");
 
   96       throw AL::ALError(
"ALVision", 
"cvMatToALImage",
 
   97           "Type incompatibility between cv::Mat and colorSpace");
 
  102                                           pLeftAngle, pTopAngle, pRightAngle, pBottomAngle);
 
  113 static inline AL::ALImage* cvMatToALImageCopy(
const cv::Mat & img,
 
  115     float pLeftAngle  = 0.0f, 
float pTopAngle    = 0.0f,
 
  116     float pRightAngle = 0.0f, 
float pBottomAngle = 0.0f)
 
  120       throw AL::ALError(
"ALVision", 
"cvMatToALImage",
 
  121           "Channel number incompatibility between cv::Mat and colorSpace");
 
  131       throw AL::ALError(
"ALVision", 
"cvMatToALImage",
 
  132           "Type incompatibility between cv::Mat and colorSpace");
 
  136   size_t data_size = img.cols * img.rows * img.elemSize();
 
  137   unsigned char *data = 
new unsigned char[data_size];
 
  138   std::memcpy(data, img.data, data_size);
 
  142                                           pLeftAngle, pTopAngle, pRightAngle, pBottomAngle);
 
  156 static bool writeALImageToFile(
const std::string& path, 
const ALImage& al_image)
 
  158   if(!cv::imwrite(path, aLImageToCvMat(al_image)))
 
  160     throw AL::ALError(
"ALVision", 
"writeALImageToFile",
 
  161                       "Can't write image to "+path);
 
  164   const std::string path_data = path+
".yml";
 
  165   cv::FileStorage fs(path_data, cv::FileStorage::WRITE);
 
  168     throw AL::ALError(
"ALVision", 
"writeALImageToFile",
 
  169                       "Can't open "+path_data+
" to write metadata");
 
  172     fs << 
"width"      << al_image.getWidth();
 
  173     fs << 
"height"     << al_image.getHeight();
 
  175     fs << 
"colorspace" << al_image.getColorSpace();
 
  176     long long timestamp = al_image.getTimeStamp();
 
  177     fs << 
"timestamp_s"  << 
static_cast<int>(timestamp/1000000ll);
 
  178     fs << 
"timestamp_us"  << 
static_cast<int>(timestamp%1000000ll);
 
  179     fs << 
"camera_id"  << 
static_cast<int>(al_image.getCameraId());
 
  180     fs << 
"fov_left"   << al_image.getLeftAngle();
 
  181     fs << 
"fov_top"    << al_image.getTopAngle();
 
  182     fs << 
"fov_right"  << al_image.getRightAngle();
 
  183     fs << 
"fov_bottom" << al_image.getBottomAngle();
 
  184     fs << 
"roi_enabled" << al_image.isROIEnabled();
 
  186     for(
int i = 0; i < al_image.getNumOfROIs(); ++i)
 
  188       const ALImage::ROI* roi = al_image.getROI(i);
 
  193          << 
"height" << roi->h
 
  194          << 
"leftAngle" << roi->leftAngle
 
  195          << 
"topAngle" << roi->topAngle
 
  196          << 
"rightAngle" << roi->rightAngle
 
  197          << 
"bottomAngle" << roi->bottomAngle
 
  202   } 
catch(
const cv::Exception& e)
 
  204     throw AL::ALError(
"ALVision", 
"writeALImageToFile",
 
  205                       "Exception caught when writing metadata to "+path_data+
": "+e.what());
 
  218 static ALImage readALImageFromFile(
const std::string& path, 
int pColorspace = -1)
 
  220   const cv::Mat img = cv::imread(path, cv::IMREAD_ANYCOLOR);
 
  222   const std::string path_data = path+
".yml";
 
  223   cv::FileStorage fs(path_data, cv::FileStorage::READ);
 
  227     if(pColorspace == -1)
 
  229       throw AL::ALError(
"ALVision", 
"readALImageFromFile",
 
  230                         "Can't open "+path_data+
" to read metadata");
 
  234       ALImage al_image(img.cols, img.rows, pColorspace, 
false);
 
  235       unsigned char* data = al_image.getData();
 
  236       const size_t data_size = img.cols * img.rows * img.elemSize();
 
  237       std::memcpy(data, img.data, data_size);
 
  242   int width, height, colorspace, timestamp_s, timestamp_us, camera_id;
 
  243   float fov_left, fov_top, fov_right, fov_bottom;
 
  245   std::vector<ALImage::ROI> rois;
 
  247     fs[
"width"]        >> width;
 
  248     fs[
"height"]       >> height;
 
  250     fs[
"colorspace"]   >> colorspace;
 
  251     fs[
"timestamp_s"]  >> timestamp_s;
 
  252     fs[
"timestamp_us"] >> timestamp_us;
 
  253     fs[
"camera_id"]    >> camera_id;
 
  254     fs[
"fov_left"]     >> fov_left;
 
  255     fs[
"fov_top"]      >> fov_top;
 
  256     fs[
"fov_right"]    >> fov_right;
 
  257     fs[
"fov_bottom"]   >> fov_bottom;
 
  258     fs[
"roi_enabled"]  >> roi_enabled;
 
  259     cv::FileNode fn_rois = fs[
"rois"];
 
  260     cv::FileNodeIterator it = fn_rois.begin();
 
  261     for(; it != fn_rois.end(); ++it)
 
  263       const int roi_x = (*it)[
"x"];
 
  264       const int roi_y = (*it)[
"y"];
 
  265       const int roi_w = (*it)[
"width"];
 
  266       const int roi_h = (*it)[
"height"];
 
  267       const float roi_leftAngle = (*it)[
"leftAngle"];
 
  268       const float roi_topAngle = (*it)[
"topAngle"];
 
  269       const float roi_rightAngle = (*it)[
"rightAngle"];
 
  270       const float roi_bottomAngle = (*it)[
"bottomAngle"];
 
  271       const ALImage::ROI roi(roi_x, roi_y, roi_w, roi_h,
 
  272                              roi_leftAngle, roi_topAngle, roi_rightAngle, roi_bottomAngle);
 
  276   } 
catch(
const cv::Exception& e)
 
  278     throw AL::ALError(
"ALVision", 
"readALImageFromFile",
 
  279                       "Exception caught when reading metadata from "+path_data+
": "+e.what());
 
  282   if(width != img.cols || height != img.rows)
 
  284     throw AL::ALError(
"ALVision", 
"readALImageFromFile",
 
  285                       "Image dimensions do not match metadata");
 
  288   ALImage al_image(img.cols, img.rows, colorspace, 
false);
 
  289   unsigned char* data = al_image.getData();
 
  290   const size_t data_size = img.cols * img.rows * img.elemSize();
 
  291   std::memcpy(data, img.data, data_size);
 
  292   al_image.setTimeStamp(timestamp_s, timestamp_us);
 
  293   al_image.setCameraId(static_cast<char>(camera_id));
 
  294   al_image.setAngles(fov_left, fov_top, fov_right, fov_bottom);
 
  295   al_image.setEnableROIs(roi_enabled);
 
  296   for(
size_t i = 0; i < rois.size(); ++i)
 
  298     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