shimat / opencvsharp

OpenCV wrapper for .NET
Apache License 2.0
5.42k stars 1.15k forks source link

AccessViolationException in DetectAndCompute() #340

Closed Shkarlatov closed 4 years ago

Shkarlatov commented 7 years ago

Using version 3.2.0.20170324 via NuGet

In function DetectAndCompute() I get an exception

   in OpenCvSharp.Feature2D.DetectAndCompute(InputArray image, InputArray mask, KeyPoint[]& keypoints, OutputArray descriptors, Boolean useProvidedKeypoints) in C:\projects\opencvsharp\src\OpenCvSharp\Modules\features2d\Feature2D.cs:string 315
   in cv_test.Program.Main(String[] args) in C:\Users\User\Desktop\cv_test\cv_test\Program.cs:string 30
   in System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   in System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   in Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   in System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   in System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   in System.Threading.ThreadHelper.ThreadStart()

Small test code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenCvSharp;
using OpenCvSharp.Flann;
using OpenCvSharp.XFeatures2D;

namespace cv_test
{
    class Program
    {
        static void Main(string[] args)
        {   
        KeyPoint[] keyPoints;
            Mat descriptor = new Mat();
            SURF detector = SURF.Create(500);
            //FastFeatureDetector detector = FastFeatureDetector.Create(); 
            detector.DetectAndCompute(image_1,null,out keyPoints,descriptor);
        }
    }
}

But if you use the ORB orb=ORB.Create() orb.DetectAndCompute(image_1,null,out keyPoints,descriptor); then there is no exception

shimat commented 7 years ago

It works for me when the image_1 is successfully initialized.

using (var gray = Cv2.ImRead("lenna.png", ImreadModes.GrayScale))
using (var surf = SURF.Create(500))
using (Mat descriptor = new Mat())
{
    KeyPoint[] keyPoints;
    surf.DetectAndCompute(gray, null, out keyPoints, descriptor);

    Console.WriteLine($"keyPoints has {keyPoints.Length} items.");
    Console.WriteLine($"descriptor has {descriptor.Rows} items.");
}
Shkarlatov commented 7 years ago

It's not work. The same exception default test_

static void Main(string[] args)
{
    using (var gray = Cv2.ImRead(@"C:\Users\User\Desktop\test_photo\test_.jpg", ImreadModes.GrayScale))
    using (var surf = SURF.Create(500))
    using (Mat descriptor = new Mat())
    {
        KeyPoint[] keyPoints;
        surf.DetectAndCompute(gray, null, out keyPoints, descriptor);

        Console.WriteLine($"keyPoints has {keyPoints.Length} items.");
        Console.WriteLine($"descriptor has {descriptor.Rows} items.");
    }
}

I attach test image.

shimat commented 7 years ago

Can you show me an InnerException of the thrown exception?

Shkarlatov commented 7 years ago

InnerException in null

Shkarlatov commented 7 years ago

Orb and SIFT work Surf and FastFeatureDetector do not work

shimat commented 7 years ago

please try the latest nuget https://www.nuget.org/packages/OpenCvSharp3-AnyCPU/3.2.0.20170419

Shkarlatov commented 7 years ago
            using (var gray = Cv2.ImRead(@"C:\Users\User\Desktop\test_photo\1.jpg", ImreadModes.GrayScale))
            using (var surf = SURF.Create(500))
            using (Mat descriptor = new Mat())
            {
                KeyPoint[] keyPoints;
                surf.DetectAndCompute(gray, null, out keyPoints, descriptor);

                Console.WriteLine($"keyPoints has {keyPoints.Length} items.");
                Console.WriteLine($"descriptor has {descriptor.Rows} items.");
            }
OpenCvSharp.OpenCVException не обработано
  ErrMsg=u != 0
  FileName=C:\libs\opencv\3.2\sources\modules\core\src\matrix.cpp
  FuncName=cv::Mat::create
  HResult=-2146233088
  Line=433
  Message=u != 0
  Source=OpenCvSharp
  StackTrace:
       в OpenCvSharp.NativeMethods.<>c.<.cctor>b__7_0(ErrorCode status, String funcName, String errMsg, String fileName, Int32 line, IntPtr userdata) в C:\projects\opencvsharp\src\OpenCvSharp\PInvoke\NativeMethods.cs:строка 206
       в OpenCvSharp.NativeMethods.features2d_Feature2D_detectAndCompute(IntPtr detector, IntPtr image, IntPtr mask, IntPtr keypoints, IntPtr descriptors, Int32 useProvidedKeypoints)
       в OpenCvSharp.Feature2D.DetectAndCompute(InputArray image, InputArray mask, KeyPoint[]& keypoints, OutputArray descriptors, Boolean useProvidedKeypoints) в C:\projects\opencvsharp\src\OpenCvSharp\Modules\features2d\Feature2D.cs:строка 270
       в cv_test.Program.Main(String[] args) в C:\Users\User\Desktop\cv_test\cv_test\Program.cs:строка 30
       в System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       в System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       в Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       в System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       в System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       в System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
shimat commented 7 years ago

It works for me.

It seems to fail memory allocation of Mat. Please check your available memory. Can you run a simpler OpenCvSharp program that does not use SURF?

See https://github.com/opencv/opencv/issues/6595.

Shkarlatov commented 7 years ago
        static void Main(string[] args)
        {
            Mat m1 = Cv2.ImRead(@"C:\Users\User\Desktop\test_photo\1.jpg", ImreadModes.GrayScale);
            //Mat m2 = Cv2.ImRead(@"C:\Users\User\Desktop\test_photo\2.jpg", ImreadModes.GrayScale);
            using (var img = Cv2.ImRead(@"C:\Users\User\Desktop\test_photo\1.jpg", ImreadModes.GrayScale))
            using (var detector = FastFeatureDetector.Create(500))
            using (Mat descriptor = new Mat())
            {
                KeyPoint[] keyPoints;
                detector.DetectAndCompute(img, null, out keyPoints, descriptor);

                Console.WriteLine($"keyPoints has {keyPoints.Length} items.");
                Console.WriteLine($"descriptor has {descriptor.Rows} items.");
            }
            Console.Read();
        }
        System.AccessViolationException не обработано
  HResult=-2147467261
  Message=Попытка чтения или записи в защищенную память. Это часто свидетельствует о том, что другая память повреждена.
  Source=OpenCvSharp
  StackTrace:
       в OpenCvSharp.NativeMethods.features2d_Feature2D_detectAndCompute(IntPtr detector, IntPtr image, IntPtr mask, IntPtr keypoints, IntPtr descriptors, Int32 useProvidedKeypoints)
       в OpenCvSharp.Feature2D.DetectAndCompute(InputArray image, InputArray mask, KeyPoint[]& keypoints, OutputArray descriptors, Boolean useProvidedKeypoints) в C:\projects\opencvsharp\src\OpenCvSharp\Modules\features2d\Feature2D.cs:строка 270
       в cv_test.Program.Main(String[] args) в C:\Users\User\Desktop\cv_test\cv_test\Program.cs:строка 23
       в System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       в System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       в Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       в System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       в System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       в System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Its work
        static void Main(string[] args)
        {
            Mat m1 = Cv2.ImRead(@"C:\Users\User\Desktop\test_photo\1.jpg", ImreadModes.GrayScale);
            //Mat m2 = Cv2.ImRead(@"C:\Users\User\Desktop\test_photo\2.jpg", ImreadModes.GrayScale);
            using (var img = Cv2.ImRead(@"C:\Users\User\Desktop\test_photo\1.jpg", ImreadModes.GrayScale))
            using (var detector = ORB.Create(500))
            using (Mat descriptor = new Mat())
            {
                KeyPoint[] keyPoints;
                detector.DetectAndCompute(img, null, out keyPoints, descriptor);

                Console.WriteLine($"keyPoints has {keyPoints.Length} items.");
                Console.WriteLine($"descriptor has {descriptor.Rows} items.");
            }
            Console.Read();
        }

static void Main(string[] args)
        {
            Mat m1 = Cv2.ImRead(@"C:\Users\User\Desktop\test_photo\1.jpg", ImreadModes.GrayScale);
            //Mat m2 = Cv2.ImRead(@"C:\Users\User\Desktop\test_photo\2.jpg", ImreadModes.GrayScale);
            using (var img = Cv2.ImRead(@"C:\Users\User\Desktop\test_photo\1.jpg", ImreadModes.GrayScale))
            using (var detector = SIFT.Create(500))
            using (Mat descriptor = new Mat())
            {
                KeyPoint[] keyPoints;
                detector.DetectAndCompute(img, null, out keyPoints, descriptor);

                Console.WriteLine($"keyPoints has {keyPoints.Length} items.");
                Console.WriteLine($"descriptor has {descriptor.Rows} items.");
            }
            Console.Read();
        }
OpenCvSharp.OpenCVException не обработано
  ErrMsg=u != 0
  FileName=C:\libs\opencv\3.2\sources\modules\core\src\matrix.cpp
  FuncName=cv::Mat::create
  HResult=-2146233088
  Line=433
  Message=u != 0
  Source=OpenCvSharp
  StackTrace:
       в OpenCvSharp.NativeMethods.<>c.<.cctor>b__7_0(ErrorCode status, String funcName, String errMsg, String fileName, Int32 line, IntPtr userdata) в C:\projects\opencvsharp\src\OpenCvSharp\PInvoke\NativeMethods.cs:строка 206
       в OpenCvSharp.NativeMethods.features2d_Feature2D_detectAndCompute(IntPtr detector, IntPtr image, IntPtr mask, IntPtr keypoints, IntPtr descriptors, Int32 useProvidedKeypoints)
       в OpenCvSharp.Feature2D.DetectAndCompute(InputArray image, InputArray mask, KeyPoint[]& keypoints, OutputArray descriptors, Boolean useProvidedKeypoints) в C:\projects\opencvsharp\src\OpenCvSharp\Modules\features2d\Feature2D.cs:строка 270
       в cv_test.Program.Main(String[] args) в C:\Users\User\Desktop\cv_test\cv_test\Program.cs:строка 23
       в System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       в System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       в Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       в System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       в System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       в System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

1.jpg it is grayscale JPG 6779 x 8530

If i change target build to x64, when

        static void Main(string[] args)
        {
            Thread.Sleep(2000);
            //Mat m1 = Cv2.ImRead(@"C:\Users\User\Desktop\test_photo\1.jpg", ImreadModes.GrayScale);
            //Mat m2 = Cv2.ImRead(@"C:\Users\User\Desktop\test_photo\2.jpg", ImreadModes.GrayScale);

            using (var img = Cv2.ImRead(@"C:\Users\User\Desktop\test_photo\test_.jpg", ImreadModes.GrayScale))
            using (var detector = SIFT.Create(500))
            using (Mat descriptor = new Mat())
            {
                KeyPoint[] keyPoints;
                detector.DetectAndCompute(img, null, out keyPoints, descriptor);

                Console.WriteLine($"keyPoints has {keyPoints.Length} items.");
                Console.WriteLine($"descriptor has {descriptor.Rows} items.");
            }
            Console.Read();
        }

eat all my memory (4Gb) and I can only kill process. If set small image (as first test image 480 x 328) its work,but keyPoints.Length and descriptor.Rows = 502 (why if i set 500)

takuya-takeuchi commented 7 years ago

Hi CrazyAlex25

FastFeatureDetector.Create().DetectAndCompute should throw exception because this function is not implemented in native opencv 3.2.0.

I tried the following code in VC++ 14.

#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include <opencv2/features2d/features2d.hpp>

int _tmain(int argc, _TCHAR* argv[])
{
    // This image is retrieved from https://dwimaging.files.wordpress.com/2013/03/big-ben-panorama-old-s.jpg
    cv::Mat m1 = cv::imread("F:\big-ben-panorama-old-s.jpg", CV_LOAD_IMAGE_GRAYSCALE);
    cv::Mat img = cv::imread("F:\big-ben-panorama-old-s.jpg", CV_LOAD_IMAGE_GRAYSCALE);
    auto detector = cv::FastFeatureDetector::create(500);
    cv::Mat descriptor;
    std::vector<cv::KeyPoint> keyPoints;
    detector->detectAndCompute(img, NULL, keyPoints, descriptor);

    printf("keyPois has %llu items\n", keyPoints.size());
    printf("descriptor  has %d items\n", descriptor.rows);

    return 0;
}

And throw

OpenCV Error: The function/feature is not implemented () in cv::Feature2D::detectAndCompute, file C:\build\master_winpack-build-win64-vc14\opencv\modules\features2d\src\feature2d.cpp, line 154

So I guess that what FastFeatureDetector.Create().DetectAndCompute failed is not OpenCVSharp issue.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.