lamiastella

DepthCamera.cpp

May 30th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.55 KB | None | 0 0
  1. #include "DepthCamera.h"
  2. #include "Visualizer.h"
  3.  
  4. void DepthCamera::computeClusters(double max_distance, double min_size)
  5. {
  6.     clusters.clear();
  7.     cv::Mat depthMap = cv::Mat::zeros(depthMap.rows, depthMap.cols, depthMap.type());
  8.     /*cv::Mat gaussian_depthMap = cv::Mat::zeros(gaussian_depthMap.rows, gaussian_depthMap.cols, gaussian_depthMap.type());
  9.     cv::GaussianBlur(xyzMap, gaussian_depthMap, cv::Size(3,3), 0, 0);
  10.     cv::namedWindow("gaussian blur");
  11.     cv::imshow("gaussian blur", gaussian_depthMap);
  12.     cvWaitKey(1);*/
  13.     cv::medianBlur(xyzMap, depthMap, 3);
  14.     cv::namedWindow("median blur", CV_WINDOW_AUTOSIZE);
  15.     cv::imshow("median blur", depthMap);
  16.     cvWaitKey(1);
  17.     cv::Mat channels[3];
  18.     cv::Mat mask = cv::Mat::zeros(depthMap.rows, depthMap.cols, depthMap.type());
  19.     /*cv::split(mask, channels);
  20.     cv::namedWindow("channel 2");
  21.     cv::imshow("channel 2", channels[0]);
  22.     cvWaitKey(1); */
  23.    
  24.     for (int r = depthMap.rows - 1; r >= 0; r--) {
  25.         for (int c = 0; c < depthMap.cols; c++) {
  26.             if (depthMap.at<cv::Vec3f>(r, c)[2] > 0.2) {
  27.                 mask = cv::Mat::zeros(depthMap.rows, depthMap.cols, depthMap.type());
  28.                 //mona why not to use the OpenCV floodFill?
  29.                 floodFill(c, r, depthMap, mask, max_distance);
  30.                 cv::Mat channels[3];
  31.                 cv::split(mask, channels);
  32.                 if (cv::countNonZero(channels[2]) > min_size) {
  33.                     cv::medianBlur(mask, mask, 3);
  34.                     clusters.push_back(mask.clone());
  35.                 }
  36.             }
  37.         }
  38.     }
  39. }
  40.  
  41. /***
  42. Recursively performs floodfill on depthMap
  43. ***/
  44. void DepthCamera::floodFill(int x, int y, cv::Mat& depthMap, cv::Mat& mask, double max_distance)
  45. {
  46.     if (x < 0 || x >= depthMap.cols || y < 0 || y >= depthMap.rows || depthMap.at<cv::Vec3f>(y, x)[2] == 0.0)
  47.         return;
  48.     if (closeEnough(x, y, depthMap, 4, max_distance)) {
  49.         mask.at<cv::Vec3f>(y, x) = depthMap.at<cv::Vec3f>(y, x);
  50.         depthMap.at<cv::Vec3f>(y, x)[0] = 0;
  51.         depthMap.at<cv::Vec3f>(y, x)[1] = 0;
  52.         depthMap.at<cv::Vec3f>(y, x)[2] = 0;
  53.     }
  54.     else {
  55.         return;
  56.     }
  57.  
  58.     floodFill(x + 1, y, depthMap, mask, max_distance);
  59.     floodFill(x - 1, y, depthMap, mask, max_distance);
  60.     floodFill(x, y + 1, depthMap, mask, max_distance);
  61.     floodFill(x, y - 1, depthMap, mask, max_distance);
  62. }
  63.  
  64. /***
  65. Check whether candidate point is close enough to neighboring points
  66. ***/
  67. bool DepthCamera::closeEnough(int x, int y, cv::Mat& depthMap, int num_neighbors, double max_distance)
  68. {
  69.     int num_close = 0;
  70.     if (x - 1 < 0 || depthMap.at<cv::Vec3f>(y, x - 1)[2] == 0 || Util::euclidianDistance3D(depthMap.at<cv::Vec3f>(y, x), depthMap.at<cv::Vec3f>(y, x - 1)) < max_distance) {
  71.         num_close++;
  72.     }
  73.     if (x + 1 >= depthMap.cols || depthMap.at<cv::Vec3f>(y, x + 1)[2] == 0 || Util::euclidianDistance3D(depthMap.at<cv::Vec3f>(y, x), depthMap.at<cv::Vec3f>(y, x + 1)) < max_distance) {
  74.         num_close++;
  75.     }
  76.     if (y - 1 < 0 || depthMap.at<cv::Vec3f>(y - 1, x)[2] == 0 || Util::euclidianDistance3D(depthMap.at<cv::Vec3f>(y, x), depthMap.at<cv::Vec3f>(y - 1, x)) < max_distance) {
  77.         num_close++;
  78.     }
  79.     if (y + 1 >= depthMap.rows || depthMap.at<cv::Vec3f>(y + 1, x)[2] == 0 || Util::euclidianDistance3D(depthMap.at<cv::Vec3f>(y, x), depthMap.at<cv::Vec3f>(y + 1, x)) < max_distance) {
  80.         num_close++;
  81.     }
  82.  
  83.     if (num_close >= num_neighbors) {
  84.         return true;
  85.     }
  86.  
  87.     return false;
  88. }
  89.  
  90. /***
  91. Remove noise on zMap and xyzMap based on INVALID_FLAG_VALUE and CONFIDENCE_THRESHOLD
  92. ***/
  93. void DepthCamera::removeNoise() {
  94.  
  95.     for (int y = 0; y < xyzMap.rows; y++) {
  96.         for (int x = 0; x < xyzMap.cols; x++) {
  97.             if (xyzMap.at<cv::Vec3f>(y, x)[2] > 0.9f || ampMap.at<float>(y, x) < CONFIDENCE_THRESHHOLD)
  98.             //mona if (xyzMap.at<cv::Vec3f>(y, x)[2] > 0.9f)
  99.             //if (xyzMap.at<cv::Vec3f>(y, x)[2] > 0.2)
  100.             {
  101.                 xyzMap.at<cv::Vec3f>(y, x)[0] = 0;
  102.                 xyzMap.at<cv::Vec3f>(y, x)[1] = 0;
  103.                 xyzMap.at<cv::Vec3f>(y, x)[2] = 0;
  104.             }
  105.         }
  106.     }
  107.  
  108.     cv::Mat channels[3];
  109.     cv::split(xyzMap, channels);
  110.     if ((float)cv::countNonZero(channels[2]) / (xyzMap.rows*xyzMap.cols) > 0.5) {
  111.         badInput = true;
  112.     } else {
  113.         badInput = false;
  114.     }
  115.  
  116.     return;
  117. }
  118.  
  119. void DepthCamera::removePoints(std::vector<cv::Point2i> points)
  120. {
  121.     for (int i = 0; i < points.size();i++) {
  122.         int x = points[i].x;
  123.         int y = points[i].y;
  124.         xyzMap.at<cv::Vec3f>(y, x)[0] = 0;
  125.         xyzMap.at<cv::Vec3f>(y, x)[1] = 0;
  126.         xyzMap.at<cv::Vec3f>(y, x)[2] = 0;
  127.     }
  128.     return;
  129. }
  130.  
  131. int DepthCamera::getWidth()
  132. {
  133.     return X_DIMENSION;
  134. }
  135.  
  136. int DepthCamera::getHeight()
  137. {
  138.     return Y_DIMENSION;
  139. }
  140.  
  141. cv::Mat DepthCamera::getXYZMap()
  142. {
  143.     return xyzMap;
  144. }
  145.  
  146. cv::Mat DepthCamera::getAmpMap()
  147. {
  148.     return ampMap;
  149. }
  150.  
  151. cv::Mat DepthCamera::getFlagMap()
  152. {
  153.     return flagMap;
  154. }
  155.  
  156. std::vector<cv::Mat> DepthCamera::getClusters()
  157. {
  158.     return clusters;
  159. }
  160.  
  161. void DepthCamera::initilizeImages()
  162. {
  163.     cv::Size dimension = cv::Size(X_DIMENSION, Y_DIMENSION);
  164.     ampMap = cv::Mat(dimension, CV_32FC1);
  165.     xyzMap = cv::Mat(dimension, CV_32FC3);
  166.     flagMap = cv::Mat(dimension, CV_8UC1);
  167.     clusters.clear();
  168.  
  169.     return;
  170. }
  171.  
  172. /***
  173. write a frame into file located at "destination"
  174. ***/
  175. bool DepthCamera::writeImage(std::string destination) {
  176.     cv::FileStorage fs(destination, cv::FileStorage::WRITE);
  177.  
  178.     fs << "xyzMap" << xyzMap;
  179.     fs << "ampMap" << ampMap;
  180.     fs << "flagMap" << flagMap;
  181.  
  182.     fs.release();
  183.     return true;
  184. }
  185.  
  186. /***
  187. Reads a frame from file located at "source"
  188. ***/
  189. bool DepthCamera::readImage(std::string source) {
  190.     cv::FileStorage fs;
  191.     fs.open(source, cv::FileStorage::READ);
  192.  
  193.     initilizeImages();
  194.     fs["xyzMap"] >> xyzMap;
  195.     fs["ampMap"] >> ampMap;
  196.     fs["flagMap"] >> flagMap;
  197.  
  198.     fs.release();
  199.  
  200.     if (xyzMap.rows == 0 || ampMap.rows == 0 || flagMap.rows == 0) {
  201.         return false;
  202.     }
  203.     else {
  204.         return true;
  205.     }
  206. }
Add Comment
Please, Sign In to add comment