title: Obstacle detection for ground robot using aerial image
body: <p><a href="https://i.stack.imgur.com/XLBVB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XLBVB.png" alt="Result of the code"></a>I want to perform obstacle detection for a ground robot by using a picture taken by a drone of the area the ground robot will cover. Since I have limited background in image processing I am not sure how to carry this out. I tried using the following method but the result is not very accurate. It detects very small edges also and it does not work well with aerial images. </p>
  
  <pre><code>#pragma once
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include "opencv2/opencv.hpp"
  
  using namespace std;
  using namespace cv;
  
  //----------------------------------------------------------
  // MAIN
  //----------------------------------------------------------
  int main(int argc, char* argv[])
  {
  // src image
      Mat src;
  //grayscale image
      Mat gray;
  // edges image
      Mat edges;
  //dst image
      Mat dst;
  //eroded image
      Mat erosion;
  //smoothed result
      Mat result;
  
  //----------------------------------------------------------
  // Image loading
  //----------------------------------------------------------
      namedWindow("result");
      namedWindow("src");
      namedWindow("edges");
      src = imread("C:/Users/HP/Desktop/SDP/obstacle detection/obstacle detection/obstacle detection/shapes.jpg");
  
  //----------------------------------------------------------
  //Specifying size and type of image
  //----------------------------------------------------------
      edges = Mat::zeros(src.size(), CV_8UC1);
      dst = Mat::zeros(src.size(), CV_8UC1);
      gray= Mat::zeros(src.size(), CV_8UC1);
      erosion = Mat::zeros(src.size(), CV_8UC1);
      result = Mat::zeros(src.size(), CV_8UC1);
  
  //----------------------------------------------------------
  //Converting from RGB to grayscale
  //----------------------------------------------------------
      cvtColor(src, gray, COLOR_BGR2GRAY);
  
  //----------------------------------------------------------
  //Edge Detetcion using OpenCV Canny Edge Detector function
  //----------------------------------------------------------
      Canny(gray, edges, 80, 255);
  
  //----------------------------------------------------------
  //Filling in the non-obstacle areas with white
  //----------------------------------------------------------
      for (int i = 0; i&lt;edges.cols; ++i)
      {
          int j = edges.rows - 1;
          for (j = edges.rows - 1; j&gt;0; --j)
          {
              if (edges.at&lt;uchar&gt;(j, i)&gt;0)
              {
                  break;
              }
          }
          dst(Range(j, dst.rows - 1), Range(i, i + 1)) = 255;
      }
  
  //----------------------------------------------------------
  // Appying erosion function to remove noise 
  //----------------------------------------------------------
      Mat element = getStructuringElement(MORPH_RECT, Size(10, 10));
     erode(dst,erosion,element);
  
   //----------------------------------------------------------
   //Smoothing the edges to get result
   //----------------------------------------------------------
  
  
      GaussianBlur(erosion, result, Size(5,5), 4);
  
  
  //----------------------------------------------------------    
  // Displaying the intermediate and final resulting images
  //----------------------------------------------------------
      namedWindow("src", WINDOW_NORMAL);
      imshow("src", src);
  
      namedWindow("edges", WINDOW_NORMAL);
      imshow("edges", edges);
  
      namedWindow("dst", WINDOW_NORMAL);
      imshow("dst", dst);
  
      namedWindow("erosion", WINDOW_NORMAL);
      imshow("erosion", erosion);
  
      namedWindow("result", WINDOW_NORMAL);
      imshow("result", result);
  
  
  
  
  //----------------------------------------------------------
  // Wait key press
  //----------------------------------------------------------
      waitKey(0);
      destroyAllWindows();
      return 0;
  }
  </code></pre>
  
  <p>The code takes in an image, converts it to grayscale. Then canny edge detection is used to detect edges of all the objects in the image. This edge detected image s filled with white color starting from the bottom until an edge is detected. The process continues until the whole image is covered. The result is a binary image with white color for areas without obstacles and and black color for obstacles. The opencv function erode is then used to remove unnecessary noise.</p>
  
  <p>I would really appreciate it if I get suggestions on how to improve this or use any other technique.</p>