opencv / opencv

Open Source Computer Vision Library
https://opencv.org
Apache License 2.0
75.95k stars 55.62k forks source link

OpenCV3 python calls to FlannBasedMatcher::knnMatch fail with error #5667

Closed Algomorph closed 8 years ago

Algomorph commented 8 years ago

The following code returns an error.

    sift = x2d.SIFT_create(1000)
    features_l, des_l = sift.detectAndCompute(im_l, None)
    features_r, des_r = sift.detectAndCompute(im_r, None)

    FLANN_INDEX_KDTREE = 1
    index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
    search_params = dict(checks=50)
    flann = cv2.FlannBasedMatcher(index_params,search_params)

The error returned:

opencv/modules/python/src2/cv2.cpp:161: error: (-215) The data should normally be NULL! in function allocate

I tried this with Python 2.7. FLANN_INDEX_KDTREE is set to 1 unlike here, since in modules/flann/include/opencv2/flann/defines.h I found it set to 1 on line 84.

irwincollin commented 8 years ago

I managed to temporarily fix this for my simple project by downgrading from openCV 3.1 to openCV 2.4.

Have you found a fix to this other than downgrading? I am new to not only openCV but also to python. I am using python 2.7.11, numpy 1.10.2 superpack for python 2.7, scipy 0.16.1 superpack for python 2.7, and openCV 3.1.

I am trying to run a simple script as follows:

import numpy as np
import cv2

cap = cv2.VideoCapture(0)
cv2.namedWindow("Original",1)
cv2.namedWindow("Foreground",1)
bgs = cv2.createBackgroundSubtractorMOG2()

while(1):
    img = cap.read()[1]
    if img is not None:
        img = cv2.resize(img, (0,0), fx=0.1, fy=0.1)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        img = cv2.equalizeHist(img)
        cv2.imshow("Original",img)
        fgmask = bgs.apply(img)
        foreground = cv2.bitwise_and(img,img,mask=fgmask)
        cv2.imshow("Foreground",foreground)
        k = cv2.waitKey(30) & 0xff
        if k == 27:
            break

cap.release()
cv2.destroyAllWindows()

The error is as follows:

OpenCV Error: Assertion failed (The data should normally be NULL!) in NumpyAllocator::allocate, file C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\python\src2\cv2.cpp, line 163
Traceback (most recent call last):
  File "backgroundsub.py", line 16, in <module>
    fgmask = bgs.apply(img)
cv2.error: C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\python\src2\cv2.cpp:163: error: (-215) The data should normally be NULL! in function NumpyAllocator::allocate

Thank you in advance!

Algomorph commented 8 years ago

@irwincollin I'm aftraid the only way to fix this is to tinker with opencv's python bindings. For now, if you are not OK with downgrading, I suggest writing your app in C++: looking at the number of open C++ bugs for OpenCV 3, I doubt anyone will start addressing binding problems any time soon. Alternatively, you can tinker OpenCV 3 source to try to find the source of the error using printf / cout statements and recompiling every time (I'm unaware of any other way to do it when you're calling the code from python). Looking at cv2.cpp, line 163 is a good way to start.

webdevbyjoss commented 8 years ago

subscribing, got the same error today on Ubuntu, Python 2.7, OpenCV 3.1: OpenCV Error: Assertion failed (The data should normally be NULL!) in allocate, file /home/username/opt/opencv/modules/python/src2/cv2.cpp, line 163

thelunararmy commented 8 years ago

Getting the same problem. OS X, Python 2.7.11, OpenCV 3.1.0:

OpenCV Error: Assertion failed (The data should normally be NULL!) in allocate, file /Users/ooo/opencv/modules/python/src2/cv2.cpp, line 163 Traceback (most recent call last): File "/Users/ooo/Documents/EclipseWorkspace/BackgroundKiller2/main.py", line 139, in flannMatches = flann.knnMatch(des,descam,k=2) cv2.error: /Users/ooo/opencv/modules/python/src2/cv2.cpp:163: error: (-215) The data should normally be NULL! in function allocate

hmischel commented 8 years ago

I too am getting this error :( I will try the downgrade to opencv 2.4

ehrenbrav commented 8 years ago

Same issue... Linux, 2.7.9, openCV 3.1.0-dev

hmischel commented 8 years ago

This bug looks like it's been around for a couple months.

I guess that does not bode well.

I'll have to find that line of code and try commenting it out.

I guess that's the downside of open source.

Is there a bug fix roadmap to see an estimated time to fix?

alalek commented 8 years ago

Could someone check OpenCV build with -DWITH_OPENCL=OFF CMake parameter?

hmischel commented 8 years ago

I am using cmake gui.

I tried this. I am not sure if this is what you were asking for since I am fairly new to C++/Python.

No luck though.

From: Alexander Alekhin [mailto:notifications@github.com] Sent: Wednesday, January 13, 2016 6:02 AM To: Itseez/opencv opencv@noreply.github.com Cc: hmischel hmischel@diligentsystems.com Subject: Re: [opencv] OpenCV3 python calls to FlannBasedMatcher::knnMatch fail with error (#5667)

Could someone check OpenCV build with -DWITH_OPENCL=OFF CMake parameter?

— Reply to this email directly or view it on GitHub https://github.com/Itseez/opencv/issues/5667#issuecomment-171254684 . http://cdn.mailscanner.info/1x1spacer.gif

ehrenbrav commented 8 years ago

I built with -DWITH_OPENCL=OFF and confirmed this does not solve the issue.

hmischel commented 8 years ago

I took the earlier advice of commenting out that bracket of code cv2.cpp, and have been running successfully since then.

It’s a hack, but my project is moving along.

From: ehrenbrav [mailto:notifications@github.com] Sent: Wednesday, January 13, 2016 1:01 PM To: Itseez/opencv opencv@noreply.github.com Cc: hmischel hmischel@diligentsystems.com Subject: Re: [opencv] OpenCV3 python calls to FlannBasedMatcher::knnMatch fail with error (#5667)

I built with -DWITH_OPENCL=OFF and confirmed this does not solve the issue.

— Reply to this email directly or view it on GitHub https://github.com/Itseez/opencv/issues/5667#issuecomment-171380546 . http://cdn.mailscanner.info/1x1spacer.gif

ehrenbrav commented 8 years ago

That's what I did too.

I maintain this is a perfectly valid engineering solution: when you're getting a strange error message that is disrupting your work...disable the error message!

Algomorph commented 8 years ago

@ehrenbrav What you guys did was not just disabling the error message, but also disabling the check whose failure was causing the message. The check was probably there for a reason, it is still unclear to me who put it there or why (would need to dig through git commit history), but "data not being null" is potentially causing a memory leak somewhere, so watch out for that.

patricksnape commented 8 years ago

That particular Numpy allocation commit (the data != 0 check) is from this commit and has been there since November 30, 2013 and was merged in #1901 - so it isn't actually because of that commit and you definitely shouldn't be disabling it.

It looks more likely that the Python bindings have been changed and this has caused some memory to get allocated that previously wasn't.

hmischel commented 8 years ago

Hi Patrick,

So that leaves us between a rock and hard place as the saying goes.

If we don’t comment out that line of code, then it is not possible to use Flann with python.

The alternative would be to use SURF, which rumor has it costs 20K to license.

Or to use C++ or EMGU. That’s not really viable when the application requirements from the client specify Python.

Perhaps I am missing something obvious, but unless there is another option, I don’t really see an alternative to commenting out that line of code.

Herb

From: Patrick Snape [mailto:notifications@github.com] Sent: Friday, January 22, 2016 4:33 AM To: Itseez/opencv opencv@noreply.github.com Cc: hmischel hmischel@diligentsystems.com Subject: Re: [opencv] OpenCV3 python calls to FlannBasedMatcher::knnMatch fail with error (#5667)

That particular Numpy allocation commit (the data != 0 check) is from this commit https://github.com/Itseez/opencv/commit/6da5d2133180e3a819e64dcec37692db2309d757 and has been there since November 30, 2013 and was merged in #1901 https://github.com/Itseez/opencv/pull/1901 - so it isn't actually because of that commit and you definitely shouldn't be disabling it.

It looks more likely that the FLANN bindings have been changed and this has caused some memory to get allocated that previously wasn't.

— Reply to this email directly or view it on GitHub https://github.com/Itseez/opencv/issues/5667#issuecomment-173860770 . http://cdn.mailscanner.info/1x1spacer.gif

patricksnape commented 8 years ago

@hmischel I'm just saying - don't be surprised if you have a massive memory leak or something isn't computing what you think it is! I think that the offending line is being hit because something weird is happening. I can't replicate the issue for createBackgroundSubtractorMOG2, but I can replicate it for FlannBasedMatcher.

Something fishy is happening though - for example, the code passes if you do matches = flann.knnMatch(des1, 2) rather than matches = flann.knnMatch(des1, des2, 2).

My gut feeling is that something weird is happening with the bindings and the fact that knnMatch is overloaded is the problem. Perhaps passing the second argument, which in the C++ API could map to the output matches vector, is somehow getting interpreted as the output vector in Python (which is expected to be an empty array?).

Dikay900 commented 8 years ago

just wanna make sure this bug was introduced on the 3.1 release since everyone is talking about 3.1? 3.0 works fine?

Algomorph commented 8 years ago

@Dikay900 , I reported it back in 3.0, so I don't think 3.0 (or 3.0 beta) will work.

Dikay900 commented 8 years ago

using latest master and windows 10, VS2015 with Python 3.5 and Python 2.7.11 with latest numpy compiled and installed.

Edit: doubled checking now: python 2.7.11 & python 3.5 fgmask = bgs.apply(img) worked for me from the second snippet matches = flann.knnMatch(des1,des2,k=2) does throw the error mentioned from the first snippet

patricksnape commented 8 years ago

I believe that the patch here fixes this issue for the FlannBasedMatcher. It would be great if someone could verify. This is obviously ugly code but I just wanted to try get something for @hmischel et. al. who just want to get on with their work. If someone can confirm this works for me I will create a clean PR.

My instinct was that BFMatcher worked, but FlannBasedMatcher did not (and only in the case of passing training descriptors). So, in looking at the differences between their code, I saw that add was overloaded for FlannBasedMatcher and that the accessing of the descriptors_ was following a different pattern - merely following the pattern seemed to fix it!

Dikay900 commented 8 years ago

I applied this commit here 26d9a7cd400921b4c2b2bc6ecafd30873d848084 which was already rejected in #4111 so you can simply cherry-pick this commit.

This works as intended and does not seem to break functionality or T-API compatibility in general so i would think this is a hacky but valid fix. Would like to see what the opencv devs are saying. :+1:

patricksnape commented 8 years ago

@dikay900 ah right, that is essentially exactly the same as my fix! Some variant of this definitely needs to be merged in. I'm fine to do a PR if an opencv dev can weigh in.

patricksnape commented 8 years ago

Actually, @Dikay900, 26d9a7c seems wrong to me, given the DescriptorMatcher::add logic. From what I can understand from browsing the source code, if you call getMatVector on an InputArray (which ultimately is what is passed to add via knnMatch), and then InputArray is actually of type MAT, then each column of the Matrix is added as an element to the vector. Whereas, if you call getMatVector and InputArray represents a STD_VECTOR_MAT then each element represents an entire Matrix, which seems more correct. Given the logic of the DescriptorMatcher::add, it seems that either a vector of Matrices or a single Matrix is expected, and thus you can't just call getMatVector as is done in that commit. You need to do as I did in my gist, which replicates the logic from DescriptorMatcher::add.

colinmccormick commented 8 years ago

Getting the same error: OSX 10.11.13, Python 2.7.11, OpenCV 3.1. @patricksnape I tried your patch to matchers.cpp and rebuilt, but still got the same error.

patricksnape commented 8 years ago

@colinmccormick Which patch - the one provided by my PR #6009? I tried my patch on OSX and Linux, 3.5 and 2.7 and it seemed to work for me :worried:

colinmccormick commented 8 years ago

@patricksnape I tried your patch (PR #6009) again and rebuilt, and this time it worked. (Not sure what I did wrong the first time.) Error is resolved, so thanks!

alalek commented 8 years ago

Thanks @patricksnape !

hmischel commented 8 years ago

Thank you!

From: Alexander Alekhin [mailto:notifications@github.com] Sent: Tuesday, February 2, 2016 11:06 AM To: Itseez/opencv opencv@noreply.github.com Cc: hmischel hmischel@diligentsystems.com Subject: Re: [opencv] OpenCV3 python calls to FlannBasedMatcher::knnMatch fail with error (#5667)

Closed #5667 https://github.com/Itseez/opencv/issues/5667 .

— Reply to this email directly or view it on GitHub https://github.com/Itseez/opencv/issues/5667#event-536053718 . http://cdn.mailscanner.info/1x1spacer.gif

ro-mathew commented 8 years ago

Thanks @patricksnape

NEMANJA-ILIC commented 8 years ago

Thanks @patricksnape

shu1995shijia commented 8 years ago

Try use other descriptor other feature descriptor called "AKAZE" (from Opencv 3.0.0 I believe) which could do the same job as the ORB. I am using opencv 3.1.0 by the way.

ZhengRui commented 8 years ago

Thanks @patricksnape the fix works :+1:

jpxrc commented 8 years ago

Tried the above code on Python 2.7.9, OpenCV 3.1.0 and it generates the NULL error. I reverted back to OpenCV 3.0 and it is working again.

patricksnape commented 8 years ago

@junostar - which code generated the error? And did you apply my patch?

jpxrc commented 8 years ago

@patricksnape - Sorry I didn't see there were two different code snippets. I'm talking about calling fgmask = bgs.apply(img) with the cv2.createBackgroundSubtractorMOG2(). I didn't apply the fix because I have to deploy my software that uses OpenCV to people and I can't expect them to manually go in and fix the bug. I will have to wait for the next version and hopefully it gets picked up...

patricksnape commented 8 years ago

@junostar Right. Well given that this thread is closed you should probably create a new issue showing that you still seeing cv2.createBackgroundSubtractorMOG2() as failing (I could never actually replicate this). You will also probably want to give detailed information about all the flags that were on during building as well as your operating system and compiler so that we can try and replicate your error. It's essentially impossible to fix these things without being able to replicate them!

jpxrc commented 8 years ago

@patricksnape - Ok, thanks I will look into doing that! FYI, I didn't build OpenCV 3.1 from source, it was the pre-compiled binary from the official repo that fails...

benjimin commented 7 years ago

I've replicated this error, using opencv 3.1.0 on Linux.

OpenCV Error: Assertion failed (The data should normally be NULL!) in allocate, file /.../opencv-3.1.0/modules/python/src2/cv2.cpp, line 163

Additionally, I found that calling cv2.FlannBasedMatcher() without arguments produces a segfault (I was expecting it to instead choose default parameters).

mobcdi commented 7 years ago

Is there a workaround for this error so FLANN matching can be used in python for those not able to edit the source code and need to stick on version 3.1 not nightly builds?

scimetfoo commented 7 years ago

Has the PR been merged?

adityapatadia commented 7 years ago

Any update on this?

NEMANJA-ILIC commented 7 years ago

Did you build from source? It should be fixed as I have used it on one of my projects.

adityapatadia commented 7 years ago

I applied patch in homebrew but new version release to fix this will be appreciated.

fmw42 commented 7 years ago

Does any one know if this has been fixed yet in a downloadable version of OpenCV 3.1.0_x and if so what is the minor version number?

benjimin commented 7 years ago

@fmw42, following the links here, it looks like a fix was merged in April, but there has not been any new release tagged since December (3.1.0), so I think you're asking for 3.1.1 to be created.

fmw42 commented 7 years ago

I get my downloads from MacPorts and it has OpenCV 3.1.0_3 (only). So I was hoping there might have been a newer minor release that would include that patch. How often are versions released from OpenCV? What are the policies that drive when a new version is released?

moof2k commented 7 years ago

Also seeing this with the ORB feature compute function @ c48d7f86b:

orb = cv2.ORB_create()
kp = orb.detect(image, None)
kp, des = orb.compute(image, kp)
...
error: (-215) The data should normally be NULL! in function allocate
djshaji commented 7 years ago

Can confirm, error occurs with ORB as well: matches = flann.knnMatch(des1,des2, k=2) cv2.error: /builddir/build/BUILD/opencv-3.1.0/modules/python/src2/cv2.cpp:163: error: (-215) The data should normally be NULL! in function allocate

mpmX commented 7 years ago

still no official fix? :( Edit: cv2.ocl.setUseOpenCL(False) seems to work

slimway commented 7 years ago

hello sir, i used your solution and the error is fixed, however the flann can't find any matches even in the same images