title: How to determine rotation of a shape?
body: <p>I have following shape.</p>
  
  <p><a href="https://i.stack.imgur.com/YUK1c.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YUK1c.png" alt="enter image description here"></a></p>
  
  <p><strong>It may be rotated by unknown angle.</strong> I want to determine its rotation in reference to horizontal axis (so shape above would have rotation equal to 0). Best idea I have come up so far is to determine contours of the shape, find minimum area rectangle and then take its rotation as rotation of shape itself.</p>
  
  <pre><code>Mat mask = imread("path_to_image");
  vector&lt;vector&lt;Point&gt;&gt; contours;
  vector&lt;Vec4i&gt; hierarchy;
  vector&lt;RotatedRect&gt; rotatedRects;
  
  findContours(mask, contours, hierarchy, RetrievalModes::RETR_TREE, ContourApproximationModes::CHAIN_APPROX_SIMPLE);
  
  const auto&amp; largestContour = max_element(contours.begin(), contours.end(),
      [](const auto&amp; e1, const auto&amp; e2) { return e1.size() &lt; e2.size(); });
  RotatedRect rotatedRect = minAreaRect(*largestContour);
  </code></pre>
  
  <p>The problem is that rectangle doesn't border the shape in expected way.</p>
  
  <p><a href="https://i.stack.imgur.com/eniZj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eniZj.png" alt="enter image description here"></a></p>
  
  <p>I'm not sure if I can go with that and simply calculate rotation from it anyway, because shape comes from other image processing and I don't know if rectangle would not laid on a different diagonal.</p>
  
  <p>Is there more reliable and better way of finding rotation of this shape?</p>
  
  <p><strong>Edit:</strong> 
  Image with shape can be in different scale. </p>