title: OpenCV Canny + Watershed
body: <p>I'm using a canny edge detection and a finding contours function (both OpenCV) to create markers for the watershed transform. Everything works fine but I'm not 100% satisfied with the results. The reason is that some edges are missing and therefore important information is lost. In more detail, I got a bunch of windows (front views), which are rectangles, after the watershed transform I end up with something like this:</p>
  
  <p><img src="https://i.stack.imgur.com/JSkzB.png" alt="enter image description here"> <img src="https://i.stack.imgur.com/2xpwG.png" alt="enter image description here">
  but I would rather have nice rectangles, that are complete and not open to one side. While maintaining irregular shapes (bushes in front of the house, cars..) Any ideas how I could solve this problem?I thought about overlaying the whole image with a grid, but I can't make it work. </p>
  
  <p>Thank you very much.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>Mat gray;
  cvtColor(im, gray, CV_BGR2GRAY);
  
  // Use Canny instead of threshold to catch squares with gradient shading
  Mat bw;
  Canny(gray, bw, 0, 100, 5, true);
  
  // Find contours
  vector&lt;vector&lt;Point&gt; &gt; contours;
  vector&lt;Vec4i&gt; hierarchy;
  
  findContours( bw, contours, hierarchy,
      CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
  
  
  // watershed
  Mat markers(bw.size(), CV_32S);
  markers = Scalar::all(0);
  int idx = 0;
  int compCount = 0;
  for( ; idx &gt;= 0; idx = hierarchy[idx][0], compCount++ ) {
      if (fabs(contourArea(contours[compCount])) &lt; min_size )
          continue;
      drawContours(markers, contours, idx, Scalar::all(compCount+1), 1, 8, hierarchy, INT_MAX);
  }
  watershed( im, markers );
  </code></pre>
  
  <p>As requested, here is the original image, the image I would like to get and my output:
  <img src="https://i.stack.imgur.com/8ubS9.jpg" alt="enter image description here"></p>
  
  <p>And I would like to have a segmentation like this (although over segmentation does not hurt, I just need to make sure, I get all the details):</p>
  
  <p><img src="https://i.stack.imgur.com/eg4qo.png" alt="enter image description here"></p>
  
  <p>While I get something like this:
  <img src="https://i.stack.imgur.com/VYQmT.png" alt="enter image description here">
  (please ignore the colours, they are not important for this question and are just a result of my overall program). This is only one example, if you want, I can show you more, also please have a look at the etrims dataset, all my pictures are from there.</p>