title: Detecting object regions in image opencv
body: <p>We're currently trying to detect the object regions in medical instruments images  using the methods available in OpenCV, C++ version.  An example image is shown below:
  <img src="https://i.stack.imgur.com/X2kkz.jpg" alt="1"></p>
  
  <p>Here are the steps we're following:</p>
  
  <ul>
  <li>Converting the image to gray scale</li>
  <li>Applying median filter </li>
  <li>Find edges using sobel filter</li>
  <li>Convert the result to binary image using a threshold of 25</li>
  <li>Skeletonize the image to make sure we have neat edges</li>
  <li>Finding X largest connected components</li>
  </ul>
  
  <p>This approach works perfectly for the image <code>1</code> and here is the result:</p>
  
  <p><img src="https://i.stack.imgur.com/FI5md.jpg" alt="1-result"></p>
  
  <ul>
  <li>The yellow borders are the connected components detected.</li>
  <li>The rectangles are just to highlight the presence of a connected component.</li>
  <li>To get understandable results, we just removed the connected components that are completely inside any another one, so the end result is something like this:</li>
  </ul>
  
  <p><img src="https://i.stack.imgur.com/wfZqO.jpg" alt="1-endResult"></p>
  
  <p>So far, everything was fine but another sample of image complicated our work shown below. 
  <img src="https://i.stack.imgur.com/bFly6.jpg" alt="2"></p>
  
  <p>Having a small light green towel under the objects results this image:</p>
  
  <p><img src="https://i.stack.imgur.com/bXmDa.jpg" alt="2-result"></p>
  
  <p>After filtering the regions as we did earlier, we got this:</p>
  
  <p><img src="https://i.stack.imgur.com/NsF14.png" alt="2-endresult"></p>
  
  <p>Obviously, it is not what we need..we're excepting something like this:</p>
  
  <p><img src="https://i.stack.imgur.com/c4aHh.jpg" alt="2-ExpectedResult"></p>
  
  <p>I'm thinking about clustering the closest connected components found(somehow!!) so we can minimize the impact of the presence of the towel, but don't know yet if it's something doable or someone has tried something like this before? Also, does anyone have any better idea to overcome this kind of problems?</p>
  
  <p>Thanks in advance.</p>