|
Tutorial
This is a basic tutorial on how to use OpenCVDotNet
Hello, OpenCVDotNetWe'll create a simple C# project that uses OpenCVDotNet to perform a simple image-wise operation on a video (AVI) file (show only the red color channel instead of all three color channels). You will first need to Install OpenCVDotNet (and all needed components). Creating the Project
CVImage.ToBitmap()OpenCVDotNet's CVImage object has a wondrous method called ToBitmap(). This method turns an OpenCV image into a .NET Bitmap object, which can be used whenever a Bitmap object is accepted. A common use of this method is to place a PictureBox on your form, and assign the resulting Bitmap into the Image of the PictureBox. This way, a CV image can be displayed on a Windows Form.
CVCaptureCVCapture can be used to capture images from a video - it can either be constructed to capture images from a Web Cam, or from an AVI file. We will use a CVCapture and a .NET Timer object to play a video stream. The Timer object will tick every 40 milliseconds (25 times per second), and in every tick, we will capture the next frame of the video, analyze it and display it into the picture box.
Your file should look like this: using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenCVDotNet;
namespace HelloOpenCVDotNet
{
public partial class Form1 : Form
{
private CVCapture capture;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
}
}
}Opening the Video FileType in the following code under Form1_Load handler (under <path to AVI file>, put a path to the AVI file that you wish to display). private void Form1_Load(object sender, EventArgs e)
{
capture = new CVCapture("<path to AVI file>");
}QueryFrameUse the CVCapture.QueryFrame() method to receive the next frame every tick, convert it to a Bitmap and assign it as the Picture Box's image. private void timer1_Tick(object sender, EventArgs e)
{
using (CVImage nextFrame = capture.QueryFrame())
{
pictureBox1.Image = nextFrame.ToBitmap();
}
}Note that we use the using statement here, to make sure the CVImage object is disposed at the end of the method. Without 'using', we would have to explicitly call nextFrame.Release() at the end of the method. Congrats! You can run your application and check it out. It should show the video you referenced in the picture box, and it should be playing! Split and MergeNow, we will do some changes to the image before it's displayed. We'll flip the color channels of the image, so that we will use the red channel instead of the green channel, the blue channel instead of the red and the green channel instead of the blue. To do that, we will use the Split and Merge methods of CVImage. The Split method returns an array of 3 CVImage objects, corresponding to the blue, green and red channels (in that order). The Merge takes an array of three CVImage objects and merges them into the three channels (BGR order as well). using (CVImage nextFrame = capture.QueryFrame())
{
using (CVImage emptyImage = new CVImage(nextFrame.Width, nextFrame.Height, CVDepth.Depth8U, 1))
{
// set entire image to "0".
emptyImage.Zero();
// split the image into three channels
CVImage[] bgrChannels = nextFrame.Split();
nextFrame.Merge(new CVImage[] { emptyImage, emptyImage, bgrChannels[2] });
pictureBox1.Image = nextFrame.ToBitmap();
// release channel images.
foreach (CVImage ch in bgrChannels)
ch.Release();
}
} Notes:
|
Sign in to add a comment
I think that you didn't consider of the end of the avi playing.It will lead to "System.NullReferenceException?".To avoid this ,we could easily add these codes: if (nextFrame != null) { capture = new CVCapture("<path to AVI file>");} else { capture = new CVCapture("g:\\1.AVI");//reload} But,many avi files couldn't be opened: unsettled “OpenCVDotNet.CVException” type of exceptional occured in OpenCVDotNet.dll Unable to open file 'g:\1.AVI' for capture
Why?
can you show the use of CVCapture function to show a webcam output??
@simone.laurenzano: doing "capture = new CVCapture();" in the load method will tend to get any attached camera. "capture = new CVCapture(0);" gets the zeroth webcam and so forth.
Can you show how to capture images using a web cam in C? If somebody can I will be so much thankful to him/her.
@friism: How could I get the 2nd camera ? I use "capture = new CVCapture(1);" or other number but I can't get anything .
to charkac, here is some code from vc++. you have to use some lib. of openCV.
i get the following when trying to run in VS 2005 studio with C#. it compiles fine, but i get the following at run time
System.IO.FileNotFoundException? was unhandled
This does not seem to work for me. I am getting a Bad Image Header error from cvCloneImage function. Does anyone know what the problem could be?
hello
when I try the project I got this error message: FileNotFoundException? Exception from HRESULT: 0x8007007E
I think it is a opencvdotnet registration or something. can you help. regards
Hello all,
so I try to fix a problem with error signature and I make it, I download another opencvdotnet dll libraries and program working very well.
>>>> But I want to use tracking for webcam not only for movie file. Please help me somebody.<<<<<<
Bizzzar
If somebody want please write down your email adress and I send to you.
When i try to run the sample code that i got from the googles tutorial page ... here is the code
using System; using System.Collections.Generic; using System.ComponentModel?; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using OpenCVDotNet.UI; using OpenCVDotNet.Algorithms; using OpenCVDotNet;
namespace WindowsFormsApplication1? {
I got the error message " Could not load file or Assembly 'OPenCVDotNet, Version=1.0.2615.24735, Culture=neutral, PublicKeyToken?=null' or one of its dependencies . This application has failed to start because the application configuration is incorrect . HRESULT:0x800736B1 See the end of this message for details on invoking just-in-time (JIT) debugging instead of this dialog box.
Exception Text System.IO.FileLoadException?: Could not load file or assembly 'OpenCVDotNet, Version=1.0.2615.24735, Culture=neutral, PublicKeyToken?=null' or one of its dependencies. This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem. (Exception from HRESULT: 0x800736B1) File name: 'OpenCVDotNet, Version=1.0.2615.24735, Culture=neutral, PublicKeyToken?=null' ---> System.Runtime.InteropServices?.COMException (0x800736B1): This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem. (Exception from HRESULT: 0x800736B1)
EventArgs? e) msg, IntPtr? wparam, IntPtr? lparam)Loaded Assemblies mscorlib
WindowsFormsApplication1? file:///C:/Documents%20and%20Settings/Anoop%20Krishnan%20G/My%20Documents/Visual%20Studio%202008/Projects/WindowsFormsApplication1/WindowsFormsApplication1/bin/Debug/WindowsFormsApplication1.exe
System.Windows.Forms
System
System.Drawing
System.Configuration
System.Xml
JIT Debugging To enable just-in-time (JIT) debugging, the .config file for this application or computer (machine.config) must have the jitDebugging value set in the system.windows.forms section. The application must also be compiled with debugging enabled.
For example:
<configuration>
</configuration>When JIT debugging is enabled, any unhandled exception will be sent to the JIT debugger registered on the computer rather than be handled by this dialog box.
This is the error message i got when running , the application compiles but run time errors are occuring am using Visual Studio 2008 with OPENCV 1.0 with the OPENCVDOTNET.
Regards
Anoop Krishnan G
Hi
I've been trying to use OpenCVDotNet for more than two weeks however I keep on getting the same error:
Could not load file or Assembly 'OPenCVDotNet, Version=1.0.2615.24735, Culture=......
I also added <system.windows.forms jitDebugging="true" /> to the c# config file but this did not solve the problem.
I think that the problem is related to the location of the DLLs. I added a referance to the OpenCVDotNet.dll in folder "C:\Program Files\OpenCVDotNet". Should I do something else?
I need to use OpenCV as part of my thesis project where i would need to use the face detection part. Your help would be greatly appreciated.
Thanks in advance
James
Hi I've been trying to use OpenCVDotNet but I've got this : 'System.IO.FileNotFoundException?' when I want to load the path of the .avi what can I do ?
Thanks
I'm getting this error, anyone have any ideas?
System.BadImageFormatException? was unhandled
thx
Why the program or debug it when running normal but when you click button to load the avi file do not see anything even load This is code:
using System; using System.Collections.Generic; using System.ComponentModel?; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using OpenCVDotNet; using OpenCVDotNet.UI; using OpenCVDotNet.Algorithms;
namespace WindowsApplication1? {
}
I have the same issue and am wondering if it is related to the fact that I am running a 64 bit OS and I suspect the OpenCVDotNet is a 32 bit one. I am looking at the project settings to force it to compile a 32 bit solution etc.
I have this error: L'assembly référencé 'OpenCVDotNet' n'a pas un nom fort Do you have an idea please????