title: How to crop away convexity defects?
body: <p>I'm trying to detect and fine-locate some objects in images from contours. The contours that I get often include some noise (maybe form the background, I don't know). The objects should look similar to rectangles or squares like:</p>
  
  <p><a href="https://i.stack.imgur.com/C6NRR.png"><img src="https://i.stack.imgur.com/C6NRR.png" alt="enter image description here"></a> </p>
  
  <p>I get very good results with shape matching (<code>cv::matchShapes</code>) to detect contours with those objects in them, with and without noise, but I have problems with the fine-location in case of noise.</p>
  
  <p>Noise looks like:</p>
  
  <p><a href="https://i.stack.imgur.com/F2Xv3.png"><img src="https://i.stack.imgur.com/F2Xv3.png" alt="enter image description here"></a> or <a href="https://i.stack.imgur.com/1xeKN.png"><img src="https://i.stack.imgur.com/1xeKN.png" alt="enter image description here"></a> for example.</p>
  
  <p>My idea was to find convexity defects and if they become too strong, somehow crop away the part that leads to concavity. Detecting the defects is ok, typically I get two defects per "unwanted structure", but I'm stuck on how to decide what and where I should remove points from the contours.</p>
  
  <p>Here are some contours, their masks (so you can extract the contours easily) and the convex hull including thresholded convexity defects:</p>
  
  <p><a href="https://i.stack.imgur.com/8G14e.png"><img src="https://i.stack.imgur.com/8G14e.png" alt="enter image description here"></a><a href="https://i.stack.imgur.com/KLVem.png"><img src="https://i.stack.imgur.com/KLVem.png" alt="enter image description here"></a><a href="https://i.stack.imgur.com/ErwpG.png"><img src="https://i.stack.imgur.com/ErwpG.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/YD6Kn.png"><img src="https://i.stack.imgur.com/YD6Kn.png" alt="enter image description here"></a><a href="https://i.stack.imgur.com/stLdf.png"><img src="https://i.stack.imgur.com/stLdf.png" alt="enter image description here"></a><a href="https://i.stack.imgur.com/3Mx6w.png"><img src="https://i.stack.imgur.com/3Mx6w.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/zSTCu.png"><img src="https://i.stack.imgur.com/zSTCu.png" alt="enter image description here"></a><a href="https://i.stack.imgur.com/kTouF.png"><img src="https://i.stack.imgur.com/kTouF.png" alt="enter image description here"></a><a href="https://i.stack.imgur.com/58FoY.png"><img src="https://i.stack.imgur.com/58FoY.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/MRsr3.png"><img src="https://i.stack.imgur.com/MRsr3.png" alt="enter image description here"></a><a href="https://i.stack.imgur.com/cigBO.png"><img src="https://i.stack.imgur.com/cigBO.png" alt="enter image description here"></a><a href="https://i.stack.imgur.com/lFoa7.png"><img src="https://i.stack.imgur.com/lFoa7.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/90DVn.png"><img src="https://i.stack.imgur.com/90DVn.png" alt="enter image description here"></a><a href="https://i.stack.imgur.com/InJNh.png"><img src="https://i.stack.imgur.com/InJNh.png" alt="enter image description here"></a><a href="https://i.stack.imgur.com/V6QHn.png"><img src="https://i.stack.imgur.com/V6QHn.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/HMOrr.png"><img src="https://i.stack.imgur.com/HMOrr.png" alt="enter image description here"></a><a href="https://i.stack.imgur.com/TTARo.png"><img src="https://i.stack.imgur.com/TTARo.png" alt="enter image description here"></a><a href="https://i.stack.imgur.com/N6bpe.png"><img src="https://i.stack.imgur.com/N6bpe.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/KjW0X.png"><img src="https://i.stack.imgur.com/KjW0X.png" alt="enter image description here"></a><a href="https://i.stack.imgur.com/G4F5A.png"><img src="https://i.stack.imgur.com/G4F5A.png" alt="enter image description here"></a><a href="https://i.stack.imgur.com/mIztA.png"><img src="https://i.stack.imgur.com/mIztA.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/d8wz8.png"><img src="https://i.stack.imgur.com/d8wz8.png" alt="enter image description here"></a><a href="https://i.stack.imgur.com/vIkRl.png"><img src="https://i.stack.imgur.com/vIkRl.png" alt="enter image description here"></a><a href="https://i.stack.imgur.com/Ic8z1.png"><img src="https://i.stack.imgur.com/Ic8z1.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/xO8Zx.png"><img src="https://i.stack.imgur.com/xO8Zx.png" alt="enter image description here"></a><a href="https://i.stack.imgur.com/B93t6.png"><img src="https://i.stack.imgur.com/B93t6.png" alt="enter image description here"></a><a href="https://i.stack.imgur.com/jbWSS.png"><img src="https://i.stack.imgur.com/jbWSS.png" alt="enter image description here"></a></p>
  
  <p>Could I just walk through the contour and locally decide whether a "left turn" is performed by the contour (if walking clockwise) and if so, remove contour points until the next left turn is taken? Maybe starting at a convexity defect?</p>
  
  <p>I'm looking for algorithms or code, programming language should not be important, algorithm is more important.</p>