Hello World in CSharp: Difference between revisions

From EMGU
Jump to navigation Jump to search
m HelloWorldCSharp moved to Hello World in CSharp: Better naming convention
No edit summary
Line 1: Line 1:
'''This example requires [[Version_History#Emgu.CV-1.5.0.0|Emgu CV 1.5.0.0]]'''
== 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;
<source lang="csharp">
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;


...
...


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


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


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
//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));


CvInvoke.cvShowImage(win1, img); //Show the image
  //Show the image
CvInvoke.cvWaitKey(0); //Wait for the key pressing event
  CvInvoke.cvShowImage(win1, img.Ptr);
CvInvoke.cvDestroyWindow(win1); //Destory the window
  //Wait for the key pressing event
  CvInvoke.cvWaitKey(0);
  //Destory the window
  CvInvoke.cvDestroyWindow(win1);  
}
</source>
</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 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.
<source lang="csharp">
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.UI;
...
//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");
}
</source>

Revision as of 21:34, 11 February 2009

This example requires Emgu CV 1.5.0.0

Hello World - Version 1

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

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

...

//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".

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.

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

...

//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");
}