GithubHelp home page GithubHelp logo

shimat / opencvsharp Goto Github PK

View Code? Open in Web Editor NEW
5.1K 229.0 1.1K 70.06 MB

OpenCV wrapper for .NET

License: Apache License 2.0

C# 85.27% CMake 0.04% C++ 10.55% C 3.30% Dockerfile 0.81% PowerShell 0.04%
opencv wrapper dotnet nuget opencvsharp image-processing machine-learning computer-vision dotnetstandard nuget-packages

opencvsharp's Introduction

opencvsharp

Github Actions Windows Status Github Actions Ubuntu Status GitHub license

Old versions of OpenCvSharp are stored in opencvsharp_2410.

NuGet

Managed libraries

Package Description Link
OpenCvSharp4 OpenCvSharp core libraries NuGet version
OpenCvSharp4.Extensions GDI+ Extensions NuGet version
OpenCvSharp4.WpfExtensions WPF Extensions NuGet version
OpenCvSharp4.Windows All-in-one package for Windows (except UWP) NuGet version

Native bindings

Package Description Link
OpenCvSharp4.runtime.win Native bindings for Windows x64/x86 (except UWP) NuGet version
OpenCvSharp4.runtime.uwp Native bindings for UWP (Universal Windows Platform) x64/x86/ARM NuGet version
OpenCvSharp4_.runtime.ubuntu.20.04-x64 Native bindings for Ubuntu 20.04 x64 NuGet version
OpenCvSharp4.runtime.linux-arm Native bindings for Linux Arm NuGet version
OpenCvSharp4.runtime.wasm Native bindings for WebAssembly NuGet version

Native binding (OpenCvSharpExtern.dll / libOpenCvSharpExtern.so) is required to work OpenCvSharp. To use OpenCvSharp, you should add both OpenCvSharp4 and OpenCvSharp4.runtime.* packages to your project. Currently, native bindings for Windows, UWP and Ubuntu are released.

Packages named OpenCvSharp3-* and OpenCvSharp-* are deprecated.

OpenCvSharp3-AnyCPU / OpenCvSharp3-WithoutDll / OpenCvSharp-AnyCPU / OpenCvSharp-WithoutDll

Docker images

Installation

Windows (except UWP)

Add OpenCvSharp4 and OpenCvSharp4.runtime.win NuGet packages to your project. You can use OpenCvSharp4.Windows instead.

UWP

Add OpenCvSharp4 and OpenCvSharp4.runtime.uwp NuGet packages to your project. Note that OpenCvSharp4.runtime.win and OpenCvSharp4.Windows don't work for UWP.

Ubuntu 20.04

Add OpenCvSharp4 and OpenCvSharp4.runtime.ubuntu.20.04.x64 NuGet packages to your project.

dotnet new console -n ConsoleApp01
cd ConsoleApp01
dotnet add package OpenCvSharp4
dotnet add package OpenCvSharp4.runtime.ubuntu.20.04-x64
# -- edit Program.cs --- # 
dotnet run

Downloads

If you do not use NuGet, get DLL files from the release page.

Target OpenCV

Requirements

PS1> Install-WindowsFeature Server-Media-Foundation

OpenCvSharp won't work on Unity and Xamarin platform. For Unity, please consider using OpenCV for Unity or some other solutions.

OpenCvSharp does not support CUDA. If you want to use the CUDA features, you need to customize the native bindings yourself.

Usage

For more details, see samples and Wiki pages.

Always remember to release Mat instances! The using syntax is useful.

// C# 8
// Edge detection by Canny algorithm
using OpenCvSharp;

class Program 
{
    static void Main() 
    {
        using var src = new Mat("lenna.png", ImreadModes.Grayscale);
        using var dst = new Mat();
        
        Cv2.Canny(src, dst, 50, 200);
        using (new Window("src image", src)) 
        using (new Window("dst image", dst)) 
        {
            Cv2.WaitKey();
        }
    }
}

As mentioned above, objects of classes, such as Mat and MatExpr, have unmanaged resources and need to be manually released by calling the Dispose() method. Worst of all, the +, -, *, and other operators create new objects each time, and these objects need to be disposed, or there will be memory leaks. Despite having the using syntax, the code still looks very verbose.

Therefore, a ResourcesTracker class is provided. The ResourcesTracker implements the IDisposable interface, and when the Dispose() method is called, all resources tracked by the ResourcesTracker are disposed. The T() method of ResourcesTracker can trace an object or an array of objects, and the method NewMat() is like T(new Mat(...). All the objects that need to be released can be wrapped with T().For example: t.T(255 - t.T(picMat * 0.8)) . Example code is as following:

using (var t = new ResourcesTracker())
{
    Mat mat1 = t.NewMat(new Size(100, 100), MatType.CV_8UC3, new Scalar(0));
    Mat mat3 = t.T(255-t.T(mat1*0.8));
    Mat[] mats1 = t.T(mat3.Split());
    Mat mat4 = t.NewMat();
    Cv2.Merge(new Mat[] { mats1[0], mats1[1], mats1[2] }, mat4);
}

using (var t = new ResourcesTracker())
{
    var src = t.T(new Mat(@"lenna.png", ImreadModes.Grayscale));
    var dst = t.NewMat();
    Cv2.Canny(src, dst, 50, 200);
    var blurredDst = t.T(dst.Blur(new Size(3, 3)));
    t.T(new Window("src image", src));
    t.T(new Window("dst image", blurredDst));
    Cv2.WaitKey();
}      

Features

  • OpenCvSharp is modeled on the native OpenCV C/C++ API style as much as possible.
  • Many classes of OpenCvSharp implement IDisposable. There is no need to manage unsafe resources.
  • OpenCvSharp does not force object-oriented programming style on you. You can also call native-style OpenCV functions.
  • OpenCvSharp provides functions for converting from Mat into Bitmap(GDI+) or WriteableBitmap(WPF).

Code samples

https://github.com/shimat/opencvsharp_samples/

API Documents

http://shimat.github.io/opencvsharp/api/OpenCvSharp.html

OpenCvSharp Build Instructions

Windows

  • Install Visual Studio 2022 or later
    • VC++ features are required.
  • Run download_opencv_windows.ps1 to download OpenCV libs and headers from https://github.com/shimat/opencv_files. Those lib files are precompiled by the owner of OpenCvSharp using GitHub Actions.
.\download_opencv_windows.ps1
  • Build OpenCvSharp
    • Open OpenCvSharp.sln and build

How to customize OpenCV binaries yourself

If you want to use some OpenCV features that are not provided by default in OpenCvSharp (e.g. GPU), you will have to build OpenCV yourself. The binary files of OpenCV for OpenCvSharp for Windows are created in the opencv_files repository. See the README.

  • git clone --recursive https://github.com/shimat/opencv_files
  • Edit build_windows.ps1 or build_uwp.ps1 to customize the CMake parameters .
  • Run the PowerShell script.

Ubuntu

git clone https://github.com/shimat/opencvsharp.git
cd opencvsharp
git fetch --all --tags --prune && git checkout ${OPENCVSHARP_VERSION}
  • Build native wrapper OpenCvSharpExtern
cd opencvsharp/src
mkdir build
cd build
cmake -D CMAKE_INSTALL_PREFIX=${YOUR_OPENCV_INSTALL_PATH} ..
make -j 
make install

You should add reference to opencvsharp/src/build/OpenCvSharpExtern/libOpenCvSharpExtern.so

export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:/home/shimat/opencvsharp/src/build/OpenCvSharpExtern"
  • Add OpenCvSharp4 NuGet package to your project
dotnet new console -n ConsoleApp01
cd ConsoleApp01
dotnet add package OpenCvSharp4
# -- edit Program.cs --- # 
dotnet run

Donations

If you find the OpenCvSharp library useful and would like to show your gratitude by donating, here are some donation options. Thank you.

https://github.com/sponsors/shimat

opencvsharp's People

Contributors

avensun avatar cjvaughter avatar codingtornado avatar cryocz avatar dkpark89 avatar grigoryanartem avatar hez2010 avatar inohiroki avatar jcapona avatar jiuyong avatar jtone123 avatar kojikobayashi avatar lwqwag avatar m-roberts avatar matthias-bl avatar mherrmannsolid avatar n0099 avatar neoxeo avatar peterwishart avatar randallmlf avatar ravazquez avatar shimat avatar takuya-takeuchi avatar themetalmonkey avatar tknhs avatar tsoft-lab avatar twhidden avatar yamachu avatar yangzhongke avatar yukoba avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

opencvsharp's Issues

CvMat default constructor produces (natively) non-initialized object

CvMat and all other DisposableCVObject-derived objects when created with default constructor (or the one with single bool parameter) use DisposableObject.AllocMemory to allocate memory for native structure. That method in turn uses Marshal.AllocHGlobal method which produces non-initialized memory. As a consequence code like "new CvMat().Dispose()" crashes.
My suggestion would be to zero-initialize allocated memory inside DisposableObject.AllocMemory.
But that alone won't be sufficient as OpenCV expects some magic value to be put in the first field of every structure (like cvCreateMat or cv::Mat::Mat() does by putting CV_MAT_MAGIC_VAL (0x42FF0000) to the type/flags field) and that is what CvMat's (and other objects') constructor should be doing.

surfについて質問

こんにちは。opencvsharpでsurfを使おうとしているところですが、わからないことが2つ出たので質問させていただきます。
1つ目ですが、surfを行おうとしたところ、surf.runのところで”保護されているメモリに読み取りまたは書き込み操作を行おうとしました。他のメモリが壊れていることが考えられます。"というエラーが出ます。いろいろ原因を調べていたところですが、わかりませんでした。どうか知恵をお貸しください。
コードはこちらとなっています。

 static void SurfRun(out CvMat point1,out CvMat point2,IplImage resize1,IplImage resize2)
        {
       Mat src = new Mat(resize1, true);
            Mat src2 = new Mat(resize2, true);
            Mat src = new Mat(resize1, true);
            Mat src2 = new Mat(resize2, true);
          //Detect the keypoints and generate their descriptors using SURF
            SURF surf = new SURF(500, 4, 2, true);
            KeyPoint[] keypoints1, keypoints2;
            MatOfFloat descriptors1 = new MatOfFloat();
            MatOfFloat descriptors2 = new MatOfFloat();
            surf.Run(src, null, out keypoints1, descriptors1);//ここでエラーが出ます
            surf.Run(src2, null, out keypoints2, descriptors2);
            //Ptr<DescriptorExtractor> descriptorExtractor = DescriptorExtractor.Create( "SURF" );
            // Matching descriptor vectors with a brute force matcher
            BFMatcher matcher = new BFMatcher(NormType.L2, false);
            DMatch[] matches = matcher.Match(descriptors1, descriptors2);
            Mat view = new Mat();
            Cv2.DrawMatches(src, keypoints1, src2, keypoints2, matches, view);

初心者な質問かもしれないですが、よろしくお願いします。

Cv.GetTextSize invalid argument

Hi

I am trying to make shape detection and apply text in front of the objects but I keep getting invalid arguments from Cv.GetTextSize... I have been looking for a few hours for answers and samples but non has been able to solve my problem

Could this be a bug or am i doing it wrong?

A Snippet of my code

void setLabel(IplImage im, string label, CvSeq<CvPoint> contours)
{
    CvFont fontface = new CvFont(FontFace.HersheySimplex,1.0,1.0);
    double scale = 0.4;
    int thickness = 1;
    int baseline = 0;
    CvSize text = Cv.GetTextSize(label,fontface,12,0); 
    CvRect r = Cv.BoundingRect(contours);
    CvPoint pt = new CvPoint(r.X + ((r.Width - text.Width) / 2), r.Y + ((r.Height + text.Height) / 2));
    Cv.Rectangle(im, pt + CvPoint(0, baseline), pt + CvPoint(text.Width, -text.Height), Cv.RGB(255,255,255),thickness);
    Cv.PutText(im, label, pt, fontface,Cv.RGB(0,0,0));
}

I have also tried to do it like this but then i get implicit conversion from void to CvSize error

            CvSize size = new CvSize(12,12);
    CvSize text = Cv.GetTextSize(label,fontface,out size, out baseline)

I am using OSX 10.9.5 and OpenCVSharp 2.4.9

Thanks in advance

opencvsharp 2.4.9 Sep06 exception bug

Hi,

I am switch from 248 version to 249 version because it support image stitching. I have a very simple test code works fine with 248, but throw exceptions after I shift to 249. After finish all the image process and wait key to exit program, it crash as:

OpenCvSharp.OpenCVException was unhandled
HResult=-2146232832
Message=unrecognized or unsupported array type
Source=OpenCvSharp
ErrMsg=unrecognized or unsupported array type
FileName=C:\builds\2_4_PackSlave-win64-vc11-shared\opencv\modules\core\src\array.cpp
FuncName=cvReleaseData
Line=996
StackTrace:
at OpenCvSharp.NativeMethods.<.cctor>b__0(CvStatus status, String funcName, String errMsg, String fileName, Int32 line, IntPtr userdata)
at OpenCvSharp.NativeMethods.cvReleaseImage(IntPtr& image)
at OpenCvSharp.IplImage.Dispose(Boolean disposing)
at OpenCvSharp.DisposableObject.Finalize()
InnerException:

Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using OpenCvSharp;

namespace Panorama
{
class PanoStitching
{
const int ImgNum = 8;
const string Name_pathName = "../../../data/Input/images/samples_Sep18/";
const string Name_imgPrefix = "IMG_";
const string Name_format = ".jpg";
const int Name_startNum = 3263;

    public PanoStitching()
    {
        List<IplImage> vImage;
        loadImages(out vImage);
        stitchImages(vImage);
        vImage.Clear();
       // init();

    }
    ~PanoStitching()
    {
    }
    private void loadImages(out List<IplImage> _imgList)
    {
        _imgList = new List<IplImage>();
        IplImage tempImage = new IplImage();
        CvWindow windowImage = new CvWindow("image", WindowMode.StretchImage);
        int ResizeScale = 4;
        for (int i = 0; i < ImgNum; i++)
        {
            int index=Name_startNum+i;
            string filename = Path.Combine(Name_pathName, Name_imgPrefix+index.ToString("D4")+Name_format);
            tempImage = new IplImage(filename);
            _imgList.Add(tempImage);

            Cv.ResizeWindow("image", tempImage.Width / ResizeScale, tempImage.Height / ResizeScale);
            windowImage.ShowImage(_imgList[i]);
            Cv.WaitKey(0);
        }
        windowImage.Close();
        Cv.ReleaseImage(tempImage);
    }
    private void stitchImages(List<IplImage> _imgList)
    {
        for (int i = 0; i < _imgList.Count; i++)
        {
            Console.WriteLine("Success load image: " + i.ToString());
            _imgList[i].Dispose();
        }

    }

    private void init()
    {
        int ResizeScale = 4;
        IplImage image_original = new IplImage("../../../data/Input/images/samples_Sep18/IMG_3263.jpg");
        //IplImage image_resize = new IplImage(new CvSize(image_original.Width/ResizeScale, image_original.Height/ResizeScale), BitDepth.U8, image_original.NChannels);
        CvWindow windowImage = new CvWindow("image", WindowMode.StretchImage);
        Cv.ResizeWindow("image", image_original.Width / ResizeScale, image_original.Height / ResizeScale);
        windowImage.ShowImage(image_original);
        Cv.WaitKey(0);
        windowImage.Close();
    }

}

}

Actually you see the init() function, it will run correct even without dispose any image, but with List of image load, it will crash even I try all the way to dispose every single image in the List.

It will be really helpful if this been figure out.

Thanks

Ruizhi

last dll build with vs2008

seems last dll build don't work with vs2008.

Only solution is to compile project myself or I can use some older prebuild dlls?

Squares detecting

I use Squares example for find rectangle object on scanned image. But this example not work if i put my picture in corner of scanner! I need cut only scanned photo, whithout white area.
Can anybody advise, how i can find this photo on scanned image? any examples, methods?

Memory leak on IplImage

while(true){
    IplImage img = new IplImage("test.jpg");
    Cv.ReleaseImage(img);
    //img.Dispose(); does not work either.
}

I'm having memory leak issues on IplImage.(seems that it doesn't release memory)
Using .NET Framework 4.5 and newest opencvsharp on nuget.

what is the last version of opencvsharp that works with vc2010?

おはよう,

I need to develop the software using vc2010 and I cannot change to vc2012 because of the company policy, still I want to use your wonderful library and opencv routines in my program. Could you please tell me what is the last version of opencvsharp that supports vc2010 and what will happen if I try to use the latest 2.4.9 with vc2010 instead of vc2012 ?

Thanks!

AccessViolation on Cv2.WarpPerspective

Cv2.WarpPerspective() で AccessViolationException が発生します。
なお、Cv.WarpPerspective() は問題なく動作します。

Issue #35 で修正していただいたDLLを使用しています。

使用したチェスボードを含む画像 (11x8マス、10x7コーナー、Jpeg形式)
situation_vga

検証のためのコード

//Cv
using (CvMat chessboard = new CvMat(filename))
{
    Cv.ShowImage("Input_Cv", chessboard);

    CvPoint2D32f[] corners;
    if (Cv.FindChessboardCorners(chessboard, new Size(10, 7), out corners))
    {
        using (CvMat dest = new CvMat(chessboard.Rows, chessboard.Cols, MatrixType.U8C3))
        {
            CvPoint2D32f[] a = 
            {
                corners[0],
                corners[60],
                corners[69],
                corners[9]
            };

            foreach (var corner in a)
            {
                Cv.Circle(chessboard, corner, 5, new CvScalar(0, 0, 255));
            }
            Cv.ShowImage("RectangleVertices_Cv", chessboard);

            CvPoint2D32f[] b =
            {
                new CvPoint2D32f(0, 0),
                new CvPoint2D32f(0, chessboard.Height),
                new CvPoint2D32f(chessboard.Width, chessboard.Height),
                new CvPoint2D32f(chessboard.Width, 0)
            };

            CvMat map_matrix;
            Cv.GetPerspectiveTransform(a, b, out map_matrix);
            Cv.WarpPerspective(chessboard, dest, map_matrix, Interpolation.Linear | Interpolation.FillOutliers, CvScalar.ScalarAll(255)); //Succeed
            Cv.ReleaseMat(map_matrix);

            Cv.ShowImage("Output_Cv", dest);
        }

        Cv.WaitKey();
    }
}

//Cv2
using (Mat chessboard = new Mat(filename))
{
    Point2f[] corners;
    if (Cv2.FindChessboardCorners(chessboard, new Size(10, 7), out corners))
    {
        Point2f[] a =
        {
            corners[0],
            corners[60],
            corners[69],
            corners[9]
        };

        foreach (var corner in a)
        {
            chessboard.Circle(corner, 5, new Scalar(0, 0, 255));
        }
        Cv2.ImShow("RectangleVertices_Cv2", chessboard);
        Cv2.WaitKey();

        Point2f[] b =
        {
            new Point2f(0, 0),
            new Point2f(0, chessboard.Height),
            new Point2f(chessboard.Width, chessboard.Height),
            new Point2f(chessboard.Width, 0)
        };

        using (Mat map_matrix = Cv2.GetPerspectiveTransform(a, b))
        using (Mat dest = new Mat(new Size(640, 480), MatType.CV_8UC3))
        {
            Cv2.WarpPerspective(chessboard, dest, map_matrix, dest.Size(), Interpolation.Linear | Interpolation.FillOutliers, BorderType.Default, Scalar.All(255)); //AccessViolation
            Cv2.ImShow("Output_Cv2", dest);
        }

        ////Another way (Using Mat.WarpPerspective())
        //using (Mat map_matrix = Cv2.GetPerspectiveTransform(a, b))
        //using (Mat dest = chessboard.WarpPerspective(map_matrix, new Size(640, 480), Interpolation.Linear | Interpolation.FillOutliers, BorderType.Default, Scalar.All(255))) //AccessViolation
        //{
        //    Cv2.ImShow("Output_Cv2", dest);
        //}

        Cv2.WaitKey();
    }
}

IplImage.InitImageHeader and Dispose

The last commit on DisposableObject.cs (67dcd6a#diff-8) introduced a bug by not initializing AllocatedMemorySize property on IplImage.InitImageHeader -> IplImage.ctor -> DisposableObject.AllocMemory -> DisposableObject.NootifyMemoryPressure code path. That results in cvReleaseImage beeing called in IplImage.Dispose what in turn results in crash if CvArr.SetData is used to set managed array on the created IplImage afterwards (as the native code tries to free the pinned managed memory).

Cv.PyrSegmentationからCvConnectedCompを出せない

いつもopencvsharpでお世話になっています。

今回はCv.PyrSegmentationを用いて画像の分割を行おうとしていますが、NativeのOpenCVのサンプルが以下のようになっていて

CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* comp = NULL;
cvPyrSegmentation(src, dst, storage, &comp, 4, 200, 50);
int ncomp = comp->total;
for(int i = 0; i < ncomp; i++)
{
CvConnectedComp* cc = (CvConnectedComp*)cvGetSeqElem(comp, i);
}

opencvsharpを用いて

CvSeq comp;
Cv.PyrSegmentation(source, destination, storage, out comp, 2, threshold1, threshold2);
for (int i = 0; i < comp.Total; i++)
{
CvConnectedComp cc = cc.GetSeqElem(i);
}

のように書きました。しかし CvConnectedComp cc = cc.GetSeqElem(i); のことろで次のようなコンパイルエラーが発生しました。

Error 1 'OpenCvSharp.CvConnectedComp' does not contain a definition for 'GetSeqElem' and no extension method 'GetSeqElem' accepting a first argument of type 'OpenCvSharp.CvConnectedComp' could be found (are you missing a using directive or an assembly reference?

Cv.PyrSegmentationからCvConnectedCompを取得するためにはどうすればいいでしょうか。よろしくお願いします。

OpenCvSharp - Code is giving different results while debugging and in release mode.

I am trying to compare two images using the following example.

https://github.com/shimat/opencvsharp/blob/master/sample/CStyleSamplesCS/Samples/SURFSample.cs

I am comparing the distance between two images using the following modified function. (See I have added a dis variable for calculating the similarity between two images).

Now the result of dis is different when I run the program pressing F5 and when I run the program using the exe directly.

I think this has some thing to do with floating point numbers as they behave weirdly as mentioned in this article.

http://stackoverflow.com/questions/2342396/why-does-this-floating-point-calculation-give-different-results-on-different-mac?lq=1

How to resolve this problem?

static double dis = 0;//For calculating the distance
private static int NaiveNearestNeighbor(float[] vec, int laplacian, CvSURFPoint[] modelKeypoints, float[][] modelDescriptors)
        {
            int neighbor = -1;
            double dist1 = 1e6, dist2 = 1e6;

            for (int i = 0; i < modelDescriptors.Length; i++)
            {
                // const CvSURFPoint* kp = (const CvSURFPoint*)kreader.ptr;
                CvSURFPoint kp = modelKeypoints[i];

                if (laplacian != kp.Laplacian)
                    continue;

                double d = CompareSurfDescriptors(vec, modelDescriptors[i], dist2);
                if (d < dist1)
                {
                    dist2 = dist1;
                    dist1 = d;
                    neighbor = i;
                }
                else if (d < dist2)
                    dist2 = d;
            }
            dis = dis + dist1;
            if (dist1 < 0.6 * dist2)
                return neighbor;
            else
                return -1;
        }
        private static int[] FindPairs(CvSURFPoint[] objectKeypoints, float[][] objectDescriptors, CvSURFPoint[] imageKeypoints, float[][] imageDescriptors)
        {
            List<int> ptPairs = new List<int>();

            for (int i = 0; i < objectDescriptors.Length; i++)
            {
                CvSURFPoint kp = objectKeypoints[i];
                int nearestNeighbor = NaiveNearestNeighbor(objectDescriptors[i], kp.Laplacian, imageKeypoints, imageDescriptors);
                if (nearestNeighbor >= 0)
                {
                    ptPairs.Add(i);
                    ptPairs.Add(nearestNeighbor);
                }
            }
            if (objectDescriptors.Length != 0)
            {
                dis = (100 - (dis / objectDescriptors.Length) * 100);
            }
            else
            {
                dis = 0;
            }
            return ptPairs.ToArray();
        }

GPU CascadeClassifier

Hello, could you add the wrap for gpu::CascadeClassifier_GPU, gpu::MOG_GPU and gpu::MOG2_GPU?
Thank you very much

Operators implementation

Hi,
in OpenCVSharp the operators are not implemented for Mat type. if I have this code:

Mat mask = mat1 > mat2; // where mat1 and mat2 are Mat variables

The exception will be "An unhandled exception of type 'System.NotImplementedException' occurred in OpenCvSharp.CPlusPlus.dll"

The only solution is to check (in loops) if one element in mat1 is > than one element in mat2 at the same position. Is there a simpler and faster solution in OpenCVSharp than the loops?

Thanks :)

AccessViolation on Cv2.FindChessboardCorners

日本語で失礼します。
Cv2.FindChessboardCorners() で AccessViolationException が発生します。
Cv.FindChessboardCorners() でも試したところ、こちらは問題ありませんでした。

Nuget から入手した OpenCvSharp 2.4.8.20140609 を使用しています。
使用したチェスボード(11x8マス、10x7コーナー、アップロードのためbmp->png変換)
chessboard

検証のためのコード

using (CvMat chessboard = new CvMat(filename))
{
    Cv.ShowImage("Input", chessboard);

    CvPoint2D32f[] corners;
    if (Cv.FindChessboardCorners(chessboard, new Size(10, 7), out corners))
    {
        foreach (var corner in corners)
        {
            Cv.Circle(chessboard, corner, 5, new CvScalar(0, 0, 255));
        }
        Cv.ShowImage("Output_Cv", chessboard);
        Cv.WaitKey();
    }
}

using (Mat chessboard = new Mat(filename))
{
    Point2f[] corners;
    if (Cv2.FindChessboardCorners(chessboard, new Size(10, 7), out corners)) //AccessViolation
    {
        foreach (var corner in corners)
        {
            Cv2.Circle(chessboard, corner, 5, new Scalar(0, 0, 255));
        }
        Cv2.ImShow("Output_Cv2", chessboard);
        Cv2.WaitKey();
    }
}

Question regarding type conversion

Hey shimat,

I've got a question regarding converting types using opencvsharp. I am using the threshold function and my data by default comes in CV_16UC1 but if I understand correctly I need it to be in either CV_32FC1 or CV_8UC1. In opencv I would use cv::Mat::convertTo, I'm wondering what the equivalent would be in OpenCVSharp?

Looking forward to hearing back!

Greetings from Brazil!

Hey, shimat,

I'm here just to say I just downloaded your latest release of opencvsharp, tested it under Mono/Linux, and it works flawlessly!

I'm going to port a Chess Tracking app I've written in C to C# thanks to your great library!

Regards,

William

Cv2.MeanStdDev bug?

Hi,
I have a Mat type of CV_32FC1 (resolution of 960x1280).

When I am using Cv2.MeanStdDev to compute the mean and STD, the mean value is wrong, I am guessing that there is an overflow in the computation because the final value is always -1.999159E+37. Another strange behaviour it is that sometimes the STD is negative.

If I am using the classic CV2.Mean the value is right.

GetBackgroundImage bug

When attempting to use the GetBackgroundImage() method in order to get the background model generated by BackgroundSubtractorMOG2 I get the following exception

OpenCvSharp.OpenCVException was unhandled by user code
  HResult=-2146232832
  Message=nchannels == 3
  Source=OpenCvSharp
  ErrMsg=nchannels == 3
  FileName=C:\buildslave64\win64_amdocl\2_4_PackSlave-win32-vc11-shared\opencv\modules\video\src\bgfg_gaussmix2.cpp
  FuncName=cv::BackgroundSubtractorMOG2::getBackgroundImage
  Line=580
  StackTrace:
       at OpenCvSharp.NativeMethods.<.cctor>b__0(CvStatus status, String funcName, String errMsg, String fileName, Int32 line, IntPtr userdata)
       at OpenCvSharp.CPlusPlus.NativeMethods.video_BackgroundSubtractorMOG2_getBackgroundImage(IntPtr self, IntPtr backgroundImage)
       at OpenCvSharp.CPlusPlus.BackgroundSubtractorMOG2.GetBackgroundImage(OutputArray backgroundImage)
       at placeWhereTheMethodIsUsed
  InnerException: 

Which, as far as I understand, means that the output image has the wrong number of channels; this is not the case, the output image is set to CV_8UC3 which should fix the problem.
Could you please fix this?

Performance of 64 bit 2.4.9

I have been using OpenCVSharp for a number of years. For a number of reasons I have continued to use version 2.3 even thought it is 32 bit and I work with 64 bit machines now days. I decided to try the latest, 64 bit 2.4.9 , using NuGet to load the package. Things worked well until I ran the results. In my application I am using both video and depth from a Kinect. The video data is processed with a "blue filter" and then passed on to OpenCvSharpBlob in order to find a "target". The 2.3 version had 24% better performance then the 2.4.9 version. In both cases I am using the OpenCv dll's provided with the OpenCvSharp package and the computer used was the same 64 bit Windows 7 machine. Any idea why the performance is so poor? Is there anything I can do to improved the performance? The application directs an autonomous robot and the performance hit makes the new version unusable.

surfについて質問

こんにちは。surfについて以前何度か質問させていただいたのですが、また質問させていただきます。以前にMatの変換の質問をさせていただき、画像ファイルからMatに変換したときは何も問題なくsurfを行えていたのですが、iplimageからMatへ変換したときにマッチング?のところでqueryDescriptors.type() == trainDescCollection[0].type()という例外が発生しました。
以下はソースコードです。

 static void SurfRun(out CvMat point1,out CvMat point2,IplImage ipl1,IplImage ipl2)
        {
       Mat src = new Mat(ipl1, true);
            Mat src2 = new Mat(ipl2, true);
            //Detect the keypoints and generate their descriptors using SURF
            SURF surf = new SURF(500, 4, 2, true);
            KeyPoint[] keypoints1, keypoints2;
            MatOfFloat descriptors1 = new MatOfFloat();
            MatOfFloat descriptors2 = new MatOfFloat();
            surf.Run(src, null, out keypoints1, descriptors1);
            surf.Run(src2, null, out keypoints2, descriptors2);
            // Matching descriptor vectors with a brute force matcher
            BFMatcher matcher = new BFMatcher(NormType.L2, false);
            DMatch[] matches = matcher.Match(descriptors1, descriptors2);//例外が発生する箇所
            Mat view = new Mat();
            Cv2.DrawMatches(src, keypoints1, src2, keypoints2, matches, view);

となっております。
原因がよくわからずこまっており、いろいろ調べたのですが解決できません。どうかよろしくお願いします。

DebuggerVisualizers.dll for Visual Studio 2013

Hi,
I tried the compiled version of DebuggerVisualizers.dll (2.4.8 10 May, 2014) and one compiled by me but no one is working (when I am debugging my applicaiton) with Visual Studio 2013 Pro.

Is there a workaourd to fix this?

Thx :)

Bug in Mat constructor

Hi,
if I run the following code the two Mat objects have different sizes (i.e. if inputFrame size is 1773x2367 then outMat size is 2367x1773, outMat is transposed)

Mat outMat =
new Mat(inputFrame.Size(), MatType.CV_8UC1, Scalar.All(255));

If I use the foloowing code, the sizes are the same

Mat outMat =
new Mat(inputFrame.Rows, inputFrame.Cols, MatType.CV_8UC1, Scalar.All(255));

Memory Leak on IplImage.ToBitmap()

Seems to be a memory leak on

IplImage.ToBitmap()

while(true)
{
    using(var image = new IplImage(@"C:\temp\LicensePlate.png"))
    {
        using(var bitmap = image.ToBitmap())
        {
            // Use bitmap
        } 
     }
}

using OpenCvSharp.MachineLearning

Hello
first of all thank you for your great work, 2nd please advice me how to use OpenCvSharp.MachineLearning ? i try to use CvKNearest but without success
warm regards

OpencvSharp ver 2.4.8.20140523 (Any CPU build) not supported in mono for android

Hi,

Thank you for providing a free C# binding for OpenCV. I tried integrating the latest(Any CPU build) version in to my Xamarin.Android project. The home page of this project says OpenCVsharp can run on any platform which supports mono. But when the assemblies are loaded on compilation, it is returning a FileNotFoundException- "Could not load assembly System.Drawing. What is the work around to get it working in mono fo android ?

FAST and ORB detection for GPU

Hello, could you please add the methods listed here? They are the GPU implementations of FAST and ORB detection.

Thank you very much, keep up the amazing work.

Can`t find morphology method in opencvsharp

I use Morphological methods, erode, dilate, but can`t find morphologyEx method in opencvsharp

In OpenCV for example:
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)

Unable to construct a background substractor

Hello, I'm attempting to get a mask of the difference between a before and after picture, but when I try to create a new BackgroundSubtractorMOG2 I get the following error:

Unable to find an entry point named 'video_BackgroundSubtractorMOG2_new2' in DLL 'OpenCvSharpExtern'.

From what I've read from the internet, it's something relating to your wrapper's entry point for the function
Since I've been unable to find this specific issue being mentioned I guess that might not the case, could it be something relating to my installation? I used NuGet to install the full packaged opencvsharp with dlls on VS2012

A cross-platform AnyCPU approach

Hi, in #26 we discussed a problem of AnyCPU support for Windows and Unix. Unfortunately, I could not find a beautiful solution based on the DllImport attribute because of Mono and Microsoft .NET Runtime have different algorithms that work with native libraries. So, I developed own approach: we can generate calls of native method on the fly by signatures from some interface. Please take, a look at my solution: https://github.com/AndreyAkinshin/DotNetRuntimeImplementer
If you like this approach, I can modify it to your needs.

2.4.8 throws dll not found exception on 32bit machines

We have updated from 2.4.5 to 2.4.8... replaced all dlls added the new Extenion-dll and everything seems to be fine. but now we got an error Report from one of our customers who use a 32bit Windows. on that machine we get an dll not found exception for the core-dll (first call in TryPInvoke).

Get the pixel value of inverse fft image problem

Hi, I have another problem about fft :) I hope you can suggest something. I tried to solve this problem seriously but I couldn't found the reason.

I have a test image. I apply it fft then I make something with the fft image. After that I make the inverse fft with the code you write previously. When I try to see the inverse fft result image, it seems true.

The problem starts here. I get pixel values ​​and import them a new image , the new image is really different from the inverseimage. But I have to get the pixel values truly for my project. Can you help me again, please?

The test image:
testyenicam

İnversİmage:
inverseimage

Newİmage:
newmage

And my code is here:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenCvSharp;
using System.Runtime.InteropServices;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using OpenCvSharp.CPlusPlus;


namespace fftandcodes
{
    class Program
    {

        static void Main(string[] args)
        {

            Mat img = Cv2.ImRead("Testyenicam.png", LoadMode.GrayScale);// the test image

            Mat padded = new Mat();

            int m = (img.Rows);
            int n = (img.Cols);

            Cv2.CopyMakeBorder(img, padded, 0, m - img.Rows, 0, n - img.Cols, BorderType.Constant, Scalar.All(0));
            Mat paddedF32 = new Mat();

            padded.ConvertTo(paddedF32, MatType.CV_32F);
            Mat[] planes = { paddedF32, Mat.Zeros(padded.Size(), MatType.CV_32F) };


            Mat complex = new Mat();
            Cv2.Merge(planes, complex);
            Mat dft = new Mat();
            Cv2.Dft(complex, dft);

            for (int i = 0; i < 851; i++)//I try to make a filtering process here
            {
                for (int j = 2822; j < 4323; j++)
                {

                    dft.Set<double>(i, j, 0);
                }
                for (int j = 550; j < 2051; j++)
                {
                    dft.Set<double>(i, j, 0);

                }
            }

            for (int i = 2598; i < 3248; i++)
            {
                for (int j = 2822; j < 4323; j++)
                {
                    dft.Set<double>(i, j, 0);

                }
                for (int j = 550; j < 2051; j++)
                {
                    dft.Set<double>(i, j, 0);

                }
            }

            Mat inverseTransform = new Mat();

            Cv2.Dft(dft, inverseTransform, DftFlag2.Inverse | DftFlag2.RealOutput);
            Cv2.Normalize(inverseTransform, inverseTransform, 0, 1, NormType.MinMax);

           using (new CvWindow("InverseImage", WindowMode.StretchImage, (IplImage)inverseTransform))   // İnverse fft result seems true
            {
                Cv.WaitKey(0);
            }

           IplImage NewImage = Cv.CreateImage(new CvSize(dft.Width, dft.Height), BitDepth.U8, 1);// I try to get the the pixcel value 
           IntPtr ptr11 = NewImage.ImageData;

            int offsett;
            for (int i = 0; i < dft.Height; i++)
            {
                for (int j = 0; j < dft.Width; j++)
                {
                    offsett = (NewImage.WidthStep * i) + (j);

                    Marshal.WriteByte(ptr11, offsett, Convert.ToByte((inverseTransform.Get<Vec3b>(i, j)[0])));// the pixel values aren't true

                }
            }


            using (new CvWindow("NewImage", WindowMode.StretchImage, NewImage))// New image is completly different from the İnverseİmage
            {
                Cv.WaitKey(0);
            }


        }

    }
}

OpenCvSharp - Descriptor Extractor class missing

I have been trying to find out descriptor extractor class for computing descriptors for BRISK, ORB & FREAK algorithm. Unfortunately it seems as if implementation of extracting descriptors is missing. I can extract SURF features by using CV.ExtractSurf method but not for other algorithms.

Can you please include descriptor extraction for other algorithm also.

Transform Cv2 methods to extension methods

Transform methods of class Cv2 to extension methods.
Example. Change method signature
public static void CvtColor(InputArray src, OutputArray dst, ColorConversion code, int dstCn = 0);
to
public static void CvtColor(this InputArray src, OutputArray dst, ColorConversion code, int dstCn = 0);

Usage:
var dst = new Mat();
new Mat("test.bmp").CvtColor(dst, ColorConversion.RgbToGray);

Trying to add Face Recognizer

Hi,
I'm trying to add support for the FaceRecognizer API. I was able to configure and build the project from source but now I'm stuck on how to pass the parameters for train method:
http://docs.opencv.org/modules/contrib/doc/facerec/facerec_api.html#facerecognizer-train

It has signature

void FaceRecognizer::train(InputArrayOfArrays src, InputArray labels) 

But first argument is expected as vector and second is vector

So I came up with following code in OpenCvSharpExtern

CVAPI(void) objdetect_FaceRecognizer_train(cv::FaceRecognizer *obj, cv::Mat** src, int srcLength, int* labels, int labelsLength)
{
    std::vector<cv::Mat> srcVec(srcLength);
    for (int i = 0; i < srcLength; i++)
        srcVec[i] = *src[i];

    std::vector<int> labelsVec = std::vector<int>(labels, labels + labelsLength);
    obj->train(srcVec, labelsVec);
}

Then on C# side I have declared this in native methods:

        [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)]
        public static extern void objdetect_FaceRecognizer_train(IntPtr obj, IntPtr[] src, int srcLength, 
            [MarshalAs(UnmanagedType.LPArray)] int[] labels, int labelsLength);

Finally this is the wrapper code:

        public void Train(Mat[] src, int[] labels)
        {
            IntPtr[] srcPtrs = EnumerableEx.SelectPtrs(src);

            NativeMethods.objdetect_FaceRecognizer_train(ptr, srcPtrs, src.Length, labels, labels.Length);
        }

I get AccessViolationException so clearly I must have done something stupid.

var dm = DescriptorMatcher.Create("BruteForce"); returns " Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

Hi.

I don't know if this is an error or I'm just new to opencv, but when i write:

var dm = DescriptorMatcher.Create("BruteForce");
dm.Clear();

I get:

An unhandled exception of type 'System.AccessViolationException' occurred in OpenCvSharp.CPlusPlus.dll

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

i use visual studio 2013

Best regards
Morten

[question] Why CvMemStorage.IsEnabledDispose is false when we create that by Cv.CreateMemStorage?

When we create CvMemStorage by Cv.CreateMemStorage, IsEnabledDispose is disabled.

using (var storage = Cv.CreateMemStorage())
using (var storage2 = new CvMemStorage(0))
{
   storage.IsEnabledDispose;  // => false
   storage2.IsEnabledDispose;  // => true
}

The memory usage will be increased even if we use using statement.
Is there a specific reason we have to disable IsEnabledDispose in this case?

iplimage→Matの変換について

カメラからリアルタイムで画像を取得し、それをsurfで処理しようとしているのですが、
iplimage→Matに変換するところ( Mat src = Cv2.CvArrToMat(Img1); Img1が画像のiplimage)で、
'OpenCvSharp.CPlusPlus.NativeMethods' のタイプ初期化子が例外をスローしました。
と出ます。いろいろ原因を調べたのですが私にはわからなかったので、考えられる原因をいただけなせんでしょうか?
よろしくお願いします。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.