title: Recognize small objects with OpenCV
body: <p>I would like to be able to recognize the position (center) and the angle of some small components with openCV with C#. To achieve that, I am grabbing pictures from a webcam and try to process them with the Canny algorithm. Unfortunately, the results are not that good as expected. Sometimes it is ok sometimes it is not. </p>
  
  <p>I have attached an example image from the cam and the corresponding output of OpenCV. </p>
  
  <p>I hope that someone could give me hints or maybe some code snippets, how to achieve my desired results. Is this something that is usually done with AI?</p>
  
  <p>Example images:</p>
  
  <p>Input:</p>
  
  <p><a href="https://i.stack.imgur.com/3ysjem.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3ysjem.jpg" alt="Input image"></a></p>
  
  <p>Output 1:</p>
  
  <p><a href="https://i.stack.imgur.com/ViHyCm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ViHyCm.png" alt="enter image description here"></a></p>
  
  <p>Output 2:</p>
  
  <p><a href="https://i.stack.imgur.com/kfZZVm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kfZZVm.png" alt="enter image description here"></a></p>
  
  <p>Expected:</p>
  
  <p><a href="https://i.stack.imgur.com/RkeX4m.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RkeX4m.png" alt="enter image description here"></a></p>
  
  <p>Thanks.</p>
  
  <p>Actual code:</p>
  
  <pre><code>        Mat src;
          src = BitmapConverter.ToMat(lastFrame);
          Mat dst = new Mat();
          Mat dst2 = new Mat();
  
          Cv2.Canny(src, dst, hScrollBar1.Value, hScrollBar2.Value);
  
          // Find contours
          OpenCvSharp.Point[][] contours; //vector&lt;vector&lt;Point&gt;&gt; contours;
          HierarchyIndex[] hierarchyIndexes; //vector&lt;Vec4i&gt; hierarchy;
  
          Cv2.FindContours(dst, out contours, out hierarchyIndexes, RetrievalModes.External, ContourApproximationModes.ApproxTC89L1);
  
          foreach (OpenCvSharp.Point[] element in contours)
          {
              var biggestContourRect = Cv2.BoundingRect(element);
  
              Cv2.Rectangle(dst,
                  new OpenCvSharp.Point(biggestContourRect.X, biggestContourRect.Y),
                  new OpenCvSharp.Point(biggestContourRect.X + biggestContourRect.Width, biggestContourRect.Y + biggestContourRect.Height),
                  new Scalar(255, 0, 0), 3);
          }
  
  
  
          using (new Window("dst image", dst)) ;
          using (new Window("src image", src)) ;
  </code></pre>