opencv: Python bindings: ORB is broken

Following code fails:

import cv2
img = cv2.imread('some_image.jpg')
orb = cv2.ORB_create(nfeatures=5000)
ps, descs = orb.detectAndCompute(img, None)

Error output:

OpenCV Error: Assertion failed (The data should normally be NULL!) in allocate, file /home/polarnick/coding/forks/opencv_3.1/modules/python/src2/cv2.cpp, line 163
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-4-03ae3bc0ea38> in <module>()
----> 1 ps, descs = orb.detectAndCompute(img, None)

error: /home/polarnick/coding/forks/opencv_3.1/modules/python/src2/cv2.cpp:163: error: (-215) The data should normally be NULL! in function allocate

Environment: Python 3.4, Ubuntu

Affects master and 3.1.0.

Workaround: if passing cv2.UMat(img) (see https://github.com/Itseez/opencv/pull/6078) - everything works

Can I pull-request this test case?

UPD: The better workaround is cv2.ocl.setUseOpenCL(False) before ORB execution

The bug was introduced in commit 190d00ea3e46feedd38a7417c0a5ab08b900a56b (Pull request: https://github.com/Itseez/opencv/pull/5317)

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 23 (8 by maintainers)

Most upvoted comments

As a temporary working stop-gap till the UMat / T-API issue is resolved, just disabling OpenCL support in run-time works for me: cv2.ocl.setUseOpenCL(False)

Same problem with OpenCV 3.1, Python 2.7, Windows 10. This seems to be a general problem with the Python bindings. I have seen other chatter on the internet about a similar type of error message when using Python, but unrelated to usage of ORB.

# -*- coding: utf-8 -*-
import numpy as np
import cv2
import matplotlib
from matplotlib import pyplot as plt

img = cv2.imread('C:/Dev/bs/data-tools/TestCollateral/Mars3/final0.jpg',cv2.IMREAD_GRAYSCALE)
mask = np.ones(img.shape, np.uint8);

orb = cv2.ORB_create()

# ERROR OCCURS ON NEXT LINE:
kp, des = orb.detectAndCompute(img,None)

In the IPython console I get:

Traceback (most recent call last):

  File "<ipython-input-3-906d4ad4cd53>", line 1, in <module>
    runfile('C:/Users/davidi/Desktop/cvplay.py', wdir='C:/Users/davidi/Desktop')

  File "C:\Programs\Anaconda-py-2.7\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
    execfile(filename, namespace)

  File "C:\Programs\Anaconda-py-2.7\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "C:/Users/davidi/Desktop/cvplay.py", line 13, in <module>
    kp, des = orb.detectAndCompute(img,None)

error: C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\python\src2\cv2.cpp:163: error: (-215) The data should normally be NULL! in function NumpyAllocator::allocate

I followed tzickel’s suggestion (above). I added the following line of code to the top of my script and it fixed the problem.

cv2.ocl.setUseOpenCL(False)

@Rajashalini , It’s ‘oc1’ not ‘ocl’ . Replace ‘L’ by ‘ONE’ 1 .

hi. im very new to image processing. When i execute the below line,

cv2.ocl.setUseOpenCL(False)

it shows ‘module’ object has no attribute ‘ocl’ . could anyone please help me…thank you…

Ok, what happens here (when OpenCL is enabled), the code flow is:

  1. The python wrapper function (auto generated) for ORB’s detectAndCompute defines an Mat called descriptors (in C++) and calls pyopencv_to on an input argument for it (which is usually not defined) and thus it set’s descriptors allocator to numpy allocator and returns: https://github.com/Itseez/opencv/blob/master/modules/python/src2/cv2.cpp#L229 afterwards it calls ORB_Impl::detectAndCompute with descriptors as OutputArray.
  2. Because the Mat is empty, it creates it (allocates) inorder to save the data: https://github.com/Itseez/opencv/blob/master/modules/features2d/src/orb.cpp#L1129
  3. ocl_computeOrbDescriptors needs the input as an UMat, not a Mat, so the error described here occurs when _descriptors is converted to an UMat: https://github.com/Itseez/opencv/blob/master/modules/features2d/src/orb.cpp#L1170
  4. the Mat::getUMat function calls the Mat allocator (but now since the Mat is created in step 2 already, it has an allocated data pointer): https://github.com/Itseez/opencv/blob/master/modules/core/src/umatrix.cpp#L292
  5. Remember in the start the Mat allocator is set to be NumpyAllocator, but it does not accept an allocate call with allocated data, and aborts: https://github.com/Itseez/opencv/blob/master/modules/python/src2/cv2.cpp#L163

Now someone who understands UMat and Python bindings should figure out how to fix this 😃

iirc #6078 is supposed to fix this issues.