Deep learning in C # (I): using OpenCV to recognize coins

Time:2021-7-27

In this series of articles, we will use deep neural network (DNN) to perform coin recognition. Specifically, we will train a DNN to recognize coins in the image.

In this article, we will describe an opencv application that will detect coins in images. Coin detection is a common stage before complete coin recognition. It includes detecting and extracting coins from a given image.
Deep learning in C # (I): using OpenCV to recognize coins
The code accompanying this series will be implemented in C # using keras. In the last article in this series, we will briefly use ml.net. Among the many choices, why use keras. Net? Keras.NET   It’s easy to learn because it’s basically a direct mapping from the classic tensorflow written in Python to c#. For readers unfamiliar with machine learning, this is much easier than creating examples in other ways.

The coin detection process is divided into three stages:

  1. Convert image to grayscale. Color increases the complexity of detection tasks, and in many cases, they can not convey any relevant information that can be obtained from image brightness.
  2. Apply Gaussian blur. Because coins usually contain an inner circle, we apply this transformation to blur the image. This ensures that any inner circles are ignored by the operation in the next step, so our algorithm will not accidentally think of them as a separate coin.
  3. Apply Hough transform. This is to detect circles.

First, let’s create a. Net framework 4.7.2 console application in Visual Studio community 2019. We will name our solution and project “coinrecognitionexample” and create a detection folder and a coindetector class in it.
Deep learning in C # (I): using OpenCV to recognize coins
We will use opencvsharp, so we can continue to install dependencies from nuget package manager in visual studio. To do this, click Tools > nuget package manager

We can see the dependencies that need to install opencvsharp.
Deep learning in C # (I): using OpenCV to recognize coins
The specific implementation occurs in the coindetector class:

public class CoinDetector
  {
      private Mat _image;
      private Mat _originalImage;
      private string _pathToFile;

      public CoinDetector(string pathToFile)
      {
          _pathToFile = pathToFile;
      }

      public void ImagePreprocessing()
      {
          _image = new Mat(_pathToFile, ImreadModes.Color);
          _originalImage = _image.Clone();
          TransformGrayScale();
          TransformGaussianBlur();
          HoughSegmentation();
      }

      private void TransformGrayScale()
      {
          _image = _originalImage.CvtColor(ColorConversionCodes.BGR2GRAY);
          new Window("Grayed Coins", WindowMode.Normal, _image);
          Cv2.WaitKey();
      }

      private void TransformGaussianBlur()
      {
          Cv2.GaussianBlur(_image, _image, new Size(0, 0), 1);
          new Window("Blurred Coins", WindowMode.Normal, _image);
          //Cv2.WaitKey();
      }

      private void HoughSegmentation()
      {
          Mat result = _image.Clone();

          var circleSegments = Cv2.HoughCircles(_image, HoughMethods.Gradient, 1.02, 40);
          for (int i = 0; i < circleSegments.Length; i++)
          {
              Cv2.Circle(result, (Point) circleSegments[i].Center, (int)circleSegments[i].Radius, new Scalar(255, 255, 0), 2);
          }

          using (new Window("Circles", result))
          {
              Cv2.WaitKey();
          }
      }
  }

In the constructor of the class, we receive the path of the coin image. This method and imagepreprocessing method are the only two common entities in the coindetector class. All other methods are private and related to the three phases listed above. In imageprocessing   Method, we save an image of the original mat (pixel matrix) object and generate a copy of the upcoming transformation. The mat class and all calls to the CV2 class come from opencvsharp. After each transformation, we call new window to visually display the transformation. The parameters of CV2. Houghcircles depend on the problem you are facing, that is, the image you are processing.

The parameters shown in the code match our example.

To complete the coin detection example, we can add the following line of code to the main method of the console application project and execute it.

string filePath = @"C:/Users/arnal/Documents/coins.jpg";
var coinDetector = new CoinDetector(filePath);
coinDetector.ImagePreprocessing();

This is the image we will use for the test. These include Serbian coins:
Deep learning in C # (I): using OpenCV to recognize coins
The final result will be the image we saw earlier:
Deep learning in C # (I): using OpenCV to recognize coins
As we can see, the white circle corresponding to Hough transform is used in the middle and recognized.

This concludes the first article in this series. In the next article, we will preprocess the data set input into the machine learning model.

Welcome to official account official account. If you love foreign technical articles, you can recommend it to me through public comments.
Deep learning in C # (I): using OpenCV to recognize coins