title: OpenCV shape matching between two similar shapes
body: <p>I'm trying to match a slightly irregular shape to a database of shapes. For example, here the contour I'm trying to match:</p>
  
  <p><a href="https://i.stack.imgur.com/JBd7t.png" rel="noreferrer"><img src="https://i.stack.imgur.com/JBd7t.png" alt="enter image description here"></a></p>
  
  <p>For more information, this is an outline of an HDMI connector, represented as a contour. It is slightly rough as this was taken with a phone while holding the HDMI.</p>
  
  <p>This is my database of connectors:</p>
  
  <p>HDMI: <a href="https://i.stack.imgur.com/rNyDr.png" rel="noreferrer"><img src="https://i.stack.imgur.com/rNyDr.png" alt="enter image description here"></a></p>
  
  <p>DVI: <a href="https://i.stack.imgur.com/b03sA.png" rel="noreferrer"><img src="https://i.stack.imgur.com/b03sA.png" alt="enter image description here"></a></p>
  
  <p>5PinDIN: <a href="https://i.stack.imgur.com/yIzfl.png" rel="noreferrer"><img src="https://i.stack.imgur.com/yIzfl.png" alt="enter image description here"></a></p>
  
  <p>DB25: <a href="https://i.stack.imgur.com/hAHHW.png" rel="noreferrer"><img src="https://i.stack.imgur.com/hAHHW.png" alt="enter image description here"></a></p>
  
  <p>These are a lot clearer as these are contours gathered from connector images from the internet.</p>
  
  <p>For what I have tried:</p>
  
  <p>cv2.matchShapes()</p>
  
  <p>Since these are all just contours, I tried directly comparing them using the matchShapes() method, and it failed to produce good results. The similarities between the irregular contour, and my database was:</p>
  
  <p>HDMI: 0.90</p>
  
  <p>DB25: 0.84</p>
  
  <p>5 Pin DIN: 0.5</p>
  
  <p>DVI: 0.21</p>
  
  <p>Since contours are more similar the closer to 0 the match result is, the algorithm completely failed. I tried the other methods of matching by changing the third parameter and was still unsuccessful.</p>
  
  <p>ORB:</p>
  
  <p>Being similar to SIFT, I tried keypoint matching. Averaging the distance between the different matches in my database (after finding the top 15% of matches):</p>
  
  <pre><code>mean([m.distance for m in matches])
  </code></pre>
  
  <p>The distances came up as:</p>
  
  <p>Five Pin DIN: 7.6</p>
  
  <p>DB25: 11.7</p>
  
  <p>DVI: 12.1</p>
  
  <p>HDMI: 19.6</p>
  
  <p>As this classified a circle as the shape most like my contour, this has failed as well.</p>
  
  <p>Here are the matching key points from ORB of the actual HDMI slot vs my example HDMI slot for more information:
  <a href="https://i.stack.imgur.com/PMND5.png" rel="noreferrer"><img src="https://i.stack.imgur.com/PMND5.png" alt="enter image description here"></a></p>
  
  <p>Are there any ideas/other algorithms I should try? Or is a CNN my only choice (which I would rather avoid as I don't have the appropriate amount of data).</p>