C# call OpenCV to develop a simple version of beauty drawing tool [recommended]

Time:2021-7-25

preface

Calling opencv in C # is actually very simple, because there are many open source libraries of OpenCV in C #.

This paper mainly introduces the application in WPF projectOpenCVSharp3-AnyCPUThe open source class library handles pictures. Let’s do the preparations before development.

preparation

First, let’s create a WPF project.

Then, search opencvsharp on nuget, as shown below:

Next, we select the opencvsharp3 anycpu option for installation.

After installing opencvsharp3 anycpu, our project will automatically introduce four class libraries, as shown in the following figure:

Here, our preparations are completed, very simple.

Application of OpenCV in c#

Now, let’s enter the project and call OpenCV.

Let’s first introduce opencv related namespaces, as follows:


using OpenCvSharp;
using OpenCvSharp.Extensions;

Then we use the mat class in the project to operate the picture.

Red and blue color channel exchange

In opencv, the mat class is very important. It is the entrance to deal with pictures.

Now, let’s do a simple picture operation. The colors of red and blue channels are exchanged. The code is as follows:

Mat mat = new Mat(@"..\..\Images\ocv02.jpg", ImreadModes.Unchanged);
for (var y = 0; y < mat.Height; y++)
{
 for (var x = 0; x < mat.Width; x++)
 {
  Vec3b color = mat.Get<Vec3b>(y, x);
  var temp = color.Item0;
  color.Item0 = color.Item2; // B to R
  color.Item2 = temp;  // R to B
  mat.Set(y, x, color);
 }
}
var mem = mat.ToMemoryStream();
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.StreamSource = mem;
bmp.EndInit();
imgOutput.Source = bmp;
mat.Dispose();///  This method has been rewritten in mat, which can release resources and be called safely

The renderings are as follows:

It can be seen that we have successfully handled the color exchange of the red and blue channels of the picture.

Now let’s do more opencv operations.

corrosion

The code is as follows:


using (var src = new Mat(@"..\..\Images\ocv02.jpg", ImreadModes.AnyDepth | ImreadModes.AnyColor))
{
 Cv2.Erode(src, src, new Mat());
 var mem = src.ToMemoryStream();
 BitmapImage bmp = new BitmapImage();
 bmp.BeginInit();
 bmp.StreamSource = mem;
 bmp.EndInit();
 imgOutput.Source = bmp;
}

The renderings are as follows:

reversal

The code is as follows:

using (var src = new Mat(@"..\..\Images\ocv02.jpg", ImreadModes.AnyDepth | ImreadModes.AnyColor))
{
 Using (VaR DST = new mat()) // processing after copying
 {
  Cv2.BitwiseNot(src, dst, new Mat());
  var mem = dst.ToMemoryStream();
  BitmapImage bmp = new BitmapImage();
  bmp.BeginInit();
  bmp.StreamSource = mem;
  bmp.EndInit();
  imgOutput.Source = bmp;
 }
}

The renderings are as follows:

Brightness – dimming

The code is as follows:

BitmapImage bmpSource = new BitmapImage(new Uri("pack://application:,,,/images/ocv02.jpg" ));
Mat mat = bmpSource.ToMat();
for (var y = 0; y < mat.Height; y++)
{
 for (var x = 0; x < mat.Width; x++)
 {
  Vec3b color = mat.Get<Vec3b>(y, x);
  int item0 = color.Item0;
  int item1 = color.Item1;
  int item2 = color.Item2;
  #Region darkens
  item0 -= 60;
  item1 -= 60;
  item2 -= 60;
  if (item0 < 0)
   item0 = 0;
  if (item1 < 0)
   item1 = 0;
  if (item2 < 0)
   item2 = 0;
  #endregion
  #Region lights up
  //item0 += 80;
  //item1 += 80;
  //item2 += 80;
  //if (item0 > 255)
  // item0 = 255;
  //if (item1 > 255)
  // item1 = 255;
  //if (item2 > 255)
  // item2 = 255;
  #endregion
 
  color.Item0 = (byte)item0;
  color.Item1 = (byte)item1;
  color.Item2 = (byte)item2;
  mat.Set(y, x, color);
 }
}
var mem = mat.ToMemoryStream();
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.StreamSource = mem;
bmp.EndInit();
imgOutput.Source = bmp;
mat.Dispose();///  This method has been rewritten in mat, which can release resources and be called safely

As you can see, the code here is a little special.

After importing pictures through bitmapimage, we directly use the tomat method of bitmapimage object to convert bitmapimage into an object of mat class.

We need to pay special attention to this tomat method, because it is not a method of bitmapimage class. It is an extension method. The extension method tomat will be added to bitmapimage and bitmap objects only if we reference the opencvsharp namespace.

The renderings are as follows:

Vertex change

The code is as follows:

using (var src = new Mat(@"..\..\Images\ocv02.jpg", ImreadModes.AnyDepth | ImreadModes.AnyColor))
{
 Using (VaR DST = new mat()) // processing after copying
 { 
  //Set original transform vertex
  List< Point2f> AffinePoints0 =new List<Point2f>() { new Point2f(100, 50), new Point2f(100, 390), new Point2f(600, 50) };
  //Set target image transform vertices
  List<Point2f> AffinePoints1 = new List<Point2f>() { new Point2f(200, 100), new Point2f(200, 330), new Point2f(500, 50) };
  //Calculate transformation matrix
  Mat Trans =Cv2.GetAffineTransform(AffinePoints0, AffinePoints1);
  //Matrix affine transformation
  Cv2.WarpAffine(src, dst, Trans,new OpenCvSharp.Size() { Height= src.Cols, Width= src.Rows });
  var mem = dst.ToMemoryStream();
  BitmapImage bmp = new BitmapImage();
  bmp.BeginInit();
  bmp.StreamSource = mem;
  bmp.EndInit();
  imgOutput.Source = bmp;
 }
}

The renderings are as follows:

Beauty skin grinding

The code is as follows:

using (var src = new Mat(@"..\..\Images\ocv02.jpg", ImreadModes.AnyDepth | ImreadModes.AnyColor))
{
 Using (VaR DST = new mat()) // processing after copying
 {
  Cv2.BilateralFilter(src, dst, 15, 35d, 35d);
  var mem = dst.ToMemoryStream();
  BitmapImage bmp = new BitmapImage();
  bmp.BeginInit();
  bmp.StreamSource = mem;
  bmp.EndInit();
  imgOutput.Source = bmp;
 }
}

The renderings are as follows:

As you can see, there are many opencv special effects in the picture, but only a few are introduced in the article. This is because the methods of other special effects are similar to those in the article, so only these are listed.

Interested friends can download the code to learn by themselves.

Now c# call OpenCV to develop a simple version of the beautiful drawing tool.

The code has been transferred to GitHub. Welcome to download it.

GitHub address: https://github.com/kiba518/WpfOpenCV

summary

The above is what Xiaobian introduced to you. C# call OpenCV to develop a simple version of beautiful drawing tool. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support to the developeppaer website!
If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!

Recommended Today

OC basis

IOS development interview essential skills chart.png What are objects and what are the objects in OC? An object is an instance of a class; Is an instance created through a class, which is generally called an instance object; Common objects in OC include instance objects, class objects, and metaclass objects; What is a class? What […]