Skip to content

Tutorial for Windows

Jiglxr edited this page Sep 22, 2022 · 6 revisions

日本語が読める方は、以下をどうぞ。

Installing OpenCV

Creating Visual Studio Project

Start Visual Studio (2005 or later). Create a new Windows Forms Application project (File -> New Project, Windows Form Application). Creating Visual Studio project

Adding References

Add a reference to OpenCvSharp.dll (Project -> Add Reference, Browse, find DLL). Adding References

Execution

Type in the following code to Program.cs and execute.

using System;
using System.Runtime.InteropServices;
using OpenCvSharp;

namespace WindowsFormsApplication1
{
    static class Program
    {
        static void Main()
        {
            using (IplImage image = new IplImage(128, 128, BitDepth.U8, 1)) 
            {
                image.Zero();
                for (int x = 0; x < image.Width; x++) 
                {
                    for (int y = 0; y < image.Height; y++) 
                    {
                        int offset = y * image.WidthStep + x;
                        byte value = (byte)(x + y);
                        Marshal.WriteByte(image.ImageData, offset, value);
                    }
                }
                using (CvWindow window = new CvWindow(image))
                {
                    CvWindow.WaitKey();
                }
            }
        }
    }
}

Execution Result

How to use MachineLearning and Blob

ML and cvblob(http://code.google.com/p/cvblob) are implemented with C++, therefore OpenCvSharp.Blob and OpenCvSharp.MachineLearning depend on the C++ wrapper OpenCvSharpExtern.dll. You need to put a OpenCvSharpExtern.dll in the same directory as the executable file (ex. bin/Debug).

Add the OpenCvSharpExtern.dll to your solution explorer, and set the copy property to "Copy if newer" or "Copy always". Copy always OpenCvSharpExtern.dll