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
(Undo revision 986 by Inuxejiq (talk))
Line 1: Line 1:
----
 
<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
 
----
 
=[http://erihybomex.co.cc Page Is Unavailable Due To Site Maintenance, Please Visit Reserve Copy Page]=
 
----
 
=[http://erihybomex.co.cc CLICK HERE]=
 
----
 
</div>
 
 
'''This example requires [[Version_History#Emgu.CV-1.5.0.0|Emgu CV 1.5.0.0]]'''
 
'''This example requires [[Version_History#Emgu.CV-1.5.0.0|Emgu CV 1.5.0.0]]'''
  
Line 12: Line 4:
 
We will start by the Hello World sample, written in C#
 
We will start by the Hello World sample, written in C#
  
&lt;source lang="csharp">
+
<source lang="csharp">
 
using Emgu.CV;
 
using Emgu.CV;
 
using Emgu.CV.CvEnum;
 
using Emgu.CV.CvEnum;
Line 27: Line 19:
  
 
//Create an image of 400x200 of Blue color
 
//Create an image of 400x200 of Blue color
using (Image&lt;Bgr, Byte> img = new Image&lt;Bgr, byte>(400, 200, new Bgr(255, 0, 0)))  
+
using (Image<Bgr, Byte> img = new Image<Bgr, byte>(400, 200, new Bgr(255, 0, 0)))  
 
{
 
{
 
   //Create the font
 
   //Create the font
Line 41: Line 33:
 
   CvInvoke.cvDestroyWindow(win1);  
 
   CvInvoke.cvDestroyWindow(win1);  
 
}
 
}
&lt;/source>
+
</source>
  
 
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".
 
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".
Line 50: Line 42:
 
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.  
 
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.  
  
&lt;source lang="csharp">
+
<source lang="csharp">
 
using Emgu.CV;
 
using Emgu.CV;
 
using Emgu.CV.CvEnum;
 
using Emgu.CV.CvEnum;
Line 60: Line 52:
  
 
//Create an image of 400x200 of Blue color
 
//Create an image of 400x200 of Blue color
using (Image&lt;Bgr, Byte> img = new Image&lt;Bgr, byte>(400, 200, new Bgr(255, 0, 0)))  
+
using (Image<Bgr, Byte> img = new Image<Bgr, byte>(400, 200, new Bgr(255, 0, 0)))  
 
{
 
{
 
   //Create the font
 
   //Create the font
Line 71: Line 63:
 
   ImageViewer.Show(img, "Test Window");
 
   ImageViewer.Show(img, "Test Window");
 
}
 
}
&lt;/source>
+
</source>

Revision as of 03:44, 24 November 2010

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;
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.

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