Difference between revisions of "Hello World in CSharp"

From Emgu CV: OpenCV in .NET (C#, VB, C++ and more)
Jump to navigation Jump to search
(New page: We will start by the Hello World sample, written in C# <source lang="csharp">using Emgu.CV; using Emgu.CV.CvEnum; ... String win1 = "Test Window"; //The name of the window CvInvoke.cvNa...)
 
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 +
'''This example requires [[Version_History#Emgu.CV-1.5.0.0|Emgu CV 1.5.0.0]] and up'''
 +
 +
== Hello World - Version 1==
 
We will start by the Hello World sample, written in C#
 
We will start by the Hello World sample, written in C#
  
<source lang="csharp">using Emgu.CV;
+
=== Emgu CV 3.0 ===
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
Click to view source code
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using System;
 +
using Emgu.CV;
 
using Emgu.CV.CvEnum;
 
using Emgu.CV.CvEnum;
 +
using Emgu.CV.Structure;
  
 
...
 
...
  
 
String win1 = "Test Window"; //The name of the window
 
String win1 = "Test Window"; //The name of the window
CvInvoke.cvNamedWindow(win1); //Create the window using the specific name
+
CvInvoke.NamedWindow(win1); //Create the window using the specific name
 +
 
 +
Mat img = new Mat(200, 400, DepthType.Cv8U, 3); //Create a 3 channel image of 400x200
 +
img.SetTo(new Bgr(255, 0, 0).MCvScalar); // set it to Blue color
 +
 
 +
//Draw "Hello, world." on the image using the specific font
 +
CvInvoke.PutText(
 +
  img,
 +
  "Hello, world",
 +
  new System.Drawing.Point(10, 80),
 +
  FontFace.HersheyComplex,
 +
  1.0,
 +
  new Bgr(0, 255, 0).MCvScalar);
 +
       
  
Image<Bgr, Byte> img = new Image<Bgr, byte>(400, 200, new Bgr(255, 0, 0)); //Create an image of 400x200 of Blue color
+
CvInvoke.Imshow(win1, img); //Show the image
MCvFont f = new MCvFont(FONT.CV_FONT_HERSHEY_COMPLEX, 1.0, 1.0); //Create the font
+
CvInvoke.WaitKey(0);  //Wait for the key pressing event
 +
CvInvoke.DestroyWindow(win1); //Destroy the window if key is pressed
 +
</syntaxhighlight>
 +
</div>
 +
</div>
  
img.Draw("Hello, world", ref f, new Point2D<int>(10, 80), new Bgr(0, 255, 0)); //Draw "Hello, world." on the image using the specific font
+
=== Emgu CV 1.5 ===
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
Click to view source code
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using Emgu.CV;
 +
using Emgu.CV.CvEnum;
 +
using Emgu.CV.Structure;
 +
using System.Drawing;
  
CvInvoke.cvShowImage(win1, img); //Show the image
+
...
CvInvoke.cvWaitKey(0);  //Wait for the key pressing event
 
CvInvoke.cvDestroyWindow(win1); //Destory the window
 
</source>
 
  
The above code will create an image of 400x200 with blue background color and the String "Hello, world" in green on the foreground. The image will be displayed a window named "Test Window".
+
//The name of the window
 +
String win1 = "Test Window";
 +
 
 +
//Create the window using the specific name
 +
CvInvoke.cvNamedWindow(win1);
 +
 
 +
//Create an image of 400x200 of Blue color
 +
using (Image<Bgr, Byte> img = new Image<Bgr, byte>(400, 200, new Bgr(255, 0, 0)))
 +
{
 +
  //Create the font
 +
  MCvFont f = new MCvFont(CvEnum.FONT.CV_FONT_HERSHEY_COMPLEX, 1.0, 1.0);
 +
  //Draw "Hello, world." on the image using the specific font
 +
  img.Draw("Hello, world", ref f, new Point(10, 80), new Bgr(0, 255, 0));
 +
 
 +
  //Show the image
 +
  CvInvoke.cvShowImage(win1, img.Ptr);
 +
  //Wait for the key pressing event
 +
  CvInvoke.cvWaitKey(0);
 +
  //Destory the window
 +
  CvInvoke.cvDestroyWindow(win1);
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 
 +
The above code will create an image of 400x200 with blue background color and the text "Hello, world" in green on the foreground. The image will be displayed a window named "Test Window".
  
 
[[image:HelloWorldExample.GIF]]
 
[[image:HelloWorldExample.GIF]]
 +
 +
== Hello World - Version 2==
 +
Showing image using cvNamedWindow is good, but [[Emgu CV]] has an event better tool for the same purpose, that is, the ImageViewer class under Emgu.CV.UI namespace.
 +
 +
=== Emgu CV 3.0 ===
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
Click to view source code
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using Emgu.CV;
 +
using Emgu.CV.CvEnum;
 +
using Emgu.CV.Structure;
 +
using Emgu.CV.UI;
 +
using System.Drawing;
 +
 +
...
 +
 +
//Create a 3 channel image of 400x200
 +
using (Mat img = new Mat(200, 400, DepthType.Cv8U, 3))
 +
{
 +
  img.SetTo(new Bgr(255, 0, 0).MCvScalar); // set it to Blue color
 +
 +
  //Draw "Hello, world." on the image using the specific font
 +
  CvInvoke.PutText(
 +
      img,
 +
      "Hello, world",
 +
      new System.Drawing.Point(10, 80),
 +
      FontFace.HersheyComplex,
 +
      1.0,
 +
      new Bgr(0, 255, 0).MCvScalar);
 +
 
 +
  //Show the image using ImageViewer from Emgu.CV.UI
 +
  ImageViewer.Show(img, "Test Window");
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 +
=== Emgu CV 2.x ===
 +
<div class="toccolours mw-collapsible mw-collapsed">
 +
Click to view source code
 +
<div class="mw-collapsible-content">
 +
<syntaxhighlight lang="csharp">
 +
using Emgu.CV;
 +
using Emgu.CV.CvEnum;
 +
using Emgu.CV.Structure;
 +
using Emgu.CV.UI;
 +
using System.Drawing;
 +
 +
...
 +
 +
//Create an image of 400x200 of Blue color
 +
using (Image<Bgr, Byte> img = new Image<Bgr, byte>(400, 200, new Bgr(255, 0, 0)))
 +
{
 +
  //Create the font
 +
  MCvFont f = new MCvFont(CvEnum.FONT.CV_FONT_HERSHEY_COMPLEX, 1.0, 1.0);
 +
 +
  //Draw "Hello, world." on the image using the specific font
 +
  img.Draw("Hello, world", ref f, new Point(10, 80), new Bgr(0, 255, 0));
 +
 
 +
  //Show the image using ImageViewer from Emgu.CV.UI
 +
  ImageViewer.Show(img, "Test Window");
 +
}
 +
</syntaxhighlight>
 +
</div>
 +
</div>

Latest revision as of 16:19, 18 February 2021

This example requires Emgu CV 1.5.0.0 and up

Hello World - Version 1

We will start by the Hello World sample, written in C#

Emgu CV 3.0

Click to view source code

using System;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;

...

String win1 = "Test Window"; //The name of the window
CvInvoke.NamedWindow(win1); //Create the window using the specific name

Mat img = new Mat(200, 400, DepthType.Cv8U, 3); //Create a 3 channel image of 400x200
img.SetTo(new Bgr(255, 0, 0).MCvScalar); // set it to Blue color

//Draw "Hello, world." on the image using the specific font
CvInvoke.PutText(
   img, 
   "Hello, world", 
   new System.Drawing.Point(10, 80), 
   FontFace.HersheyComplex, 
   1.0, 
   new Bgr(0, 255, 0).MCvScalar);
         

CvInvoke.Imshow(win1, img); //Show the image
CvInvoke.WaitKey(0);  //Wait for the key pressing event
CvInvoke.DestroyWindow(win1); //Destroy the window if key is pressed

Emgu CV 1.5

Click to view source code

using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using System.Drawing;

...

//The name of the window
String win1 = "Test Window";

//Create the window using the specific name
CvInvoke.cvNamedWindow(win1);

//Create an image of 400x200 of Blue color
using (Image<Bgr, Byte> img = new Image<Bgr, byte>(400, 200, new Bgr(255, 0, 0))) 
{
   //Create the font
   MCvFont f = new MCvFont(CvEnum.FONT.CV_FONT_HERSHEY_COMPLEX, 1.0, 1.0);
   //Draw "Hello, world." on the image using the specific font
   img.Draw("Hello, world", ref f, new Point(10, 80), new Bgr(0, 255, 0));

   //Show the image
   CvInvoke.cvShowImage(win1, img.Ptr);
   //Wait for the key pressing event
   CvInvoke.cvWaitKey(0);
   //Destory the window
   CvInvoke.cvDestroyWindow(win1); 
}

The above code will create an image of 400x200 with blue background color and the text "Hello, world" in green on the foreground. The image will be displayed a window named "Test Window".

HelloWorldExample.GIF

Hello World - Version 2

Showing image using cvNamedWindow is good, but Emgu CV has an event better tool for the same purpose, that is, the ImageViewer class under Emgu.CV.UI namespace.

Emgu CV 3.0

Click to view source code

using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.UI;
using System.Drawing;

...

//Create a 3 channel image of 400x200
using (Mat img = new Mat(200, 400, DepthType.Cv8U, 3)) 
{
   img.SetTo(new Bgr(255, 0, 0).MCvScalar); // set it to Blue color

   //Draw "Hello, world." on the image using the specific font
   CvInvoke.PutText(
      img, 
      "Hello, world", 
      new System.Drawing.Point(10, 80), 
      FontFace.HersheyComplex, 
      1.0, 
      new Bgr(0, 255, 0).MCvScalar);
   
   //Show the image using ImageViewer from Emgu.CV.UI
   ImageViewer.Show(img, "Test Window");
}

Emgu CV 2.x

Click to view source code

using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.UI;
using System.Drawing;

...

//Create an image of 400x200 of Blue color
using (Image<Bgr, Byte> img = new Image<Bgr, byte>(400, 200, new Bgr(255, 0, 0))) 
{
   //Create the font
   MCvFont f = new MCvFont(CvEnum.FONT.CV_FONT_HERSHEY_COMPLEX, 1.0, 1.0);

   //Draw "Hello, world." on the image using the specific font
   img.Draw("Hello, world", ref f, new Point(10, 80), new Bgr(0, 255, 0)); 
   
   //Show the image using ImageViewer from Emgu.CV.UI
   ImageViewer.Show(img, "Test Window");
}