<p dir="auto">I have compiled OpenCV 3.0.0 for python on Windows.<br>
When I tried to run the code bellow I got this error message: "The data should normally be NULL!"</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
import cv2

img1 = cv2.imread('C:\\Users\\soboh\\Documents\\pcb.jpg',0)          # queryImage
img2 = cv2.imread('C:\\Users\\soboh\\Documents\\pcb.jpg',0) # trainImage

sift = cv2.xfeatures2d.SURF_create()

kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)


FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50)   # or pass empty dictionary

flann = cv2.FlannBasedMatcher(index_params,search_params)

matches = flann.knnMatch(des1,des2,k=2)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">import</span> <span class="pl-s1">cv2</span>

<span class="pl-s1">img1</span> <span class="pl-c1">=</span> <span class="pl-s1">cv2</span>.<span class="pl-en">imread</span>(<span class="pl-s">'C:<span class="pl-cce">\\</span>Users<span class="pl-cce">\\</span>soboh<span class="pl-cce">\\</span>Documents<span class="pl-cce">\\</span>pcb.jpg'</span>,<span class="pl-c1">0</span>)          <span class="pl-c"># queryImage</span>
<span class="pl-s1">img2</span> <span class="pl-c1">=</span> <span class="pl-s1">cv2</span>.<span class="pl-en">imread</span>(<span class="pl-s">'C:<span class="pl-cce">\\</span>Users<span class="pl-cce">\\</span>soboh<span class="pl-cce">\\</span>Documents<span class="pl-cce">\\</span>pcb.jpg'</span>,<span class="pl-c1">0</span>) <span class="pl-c"># trainImage</span>

<span class="pl-s1">sift</span> <span class="pl-c1">=</span> <span class="pl-s1">cv2</span>.<span class="pl-s1">xfeatures2d</span>.<span class="pl-v">SURF_create</span>()

<span class="pl-s1">kp1</span>, <span class="pl-s1">des1</span> <span class="pl-c1">=</span> <span class="pl-s1">sift</span>.<span class="pl-en">detectAndCompute</span>(<span class="pl-s1">img1</span>,<span class="pl-c1">None</span>)
<span class="pl-s1">kp2</span>, <span class="pl-s1">des2</span> <span class="pl-c1">=</span> <span class="pl-s1">sift</span>.<span class="pl-en">detectAndCompute</span>(<span class="pl-s1">img2</span>,<span class="pl-c1">None</span>)


<span class="pl-v">FLANN_INDEX_KDTREE</span> <span class="pl-c1">=</span> <span class="pl-c1">0</span>
<span class="pl-s1">index_params</span> <span class="pl-c1">=</span> <span class="pl-en">dict</span>(<span class="pl-s1">algorithm</span> <span class="pl-c1">=</span> <span class="pl-v">FLANN_INDEX_KDTREE</span>, <span class="pl-s1">trees</span> <span class="pl-c1">=</span> <span class="pl-c1">5</span>)
<span class="pl-s1">search_params</span> <span class="pl-c1">=</span> <span class="pl-en">dict</span>(<span class="pl-s1">checks</span><span class="pl-c1">=</span><span class="pl-c1">50</span>)   <span class="pl-c"># or pass empty dictionary</span>

<span class="pl-s1">flann</span> <span class="pl-c1">=</span> <span class="pl-s1">cv2</span>.<span class="pl-v">FlannBasedMatcher</span>(<span class="pl-s1">index_params</span>,<span class="pl-s1">search_params</span>)

<span class="pl-s1">matches</span> <span class="pl-c1">=</span> <span class="pl-s1">flann</span>.<span class="pl-en">knnMatch</span>(<span class="pl-s1">des1</span>,<span class="pl-s1">des2</span>,<span class="pl-s1">k</span><span class="pl-c1">=</span><span class="pl-c1">2</span>)</pre></div>
<p dir="auto">The error happens when calling the function flann.knnMatch(). The full error message is:<br>
"cv2.error: cv2.cpp:161: error: (-215) The data should normally be NULL! in function allocate"</p>
<p dir="auto">So I have opened the file cv2.cpp and looked at line 161 and found these lines of code:</p>
<div class="highlight highlight-source-c++ notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="if( data != 0 )
{
CV_Error(Error::StsAssert, &quot;The data should normally be NULL!&quot;);
// probably this is safe to do in such extreme case
return stdAllocator-&gt;allocate(dims0, sizes, type, data, step, flags, usageFlags);
}"><pre class="notranslate"><span class="pl-k">if</span>( data != <span class="pl-c1">0</span> )
{
<span class="pl-c1">CV_Error</span>(Error::StsAssert, <span class="pl-s"><span class="pl-pds">"</span>The data should normally be NULL!<span class="pl-pds">"</span></span>);
<span class="pl-c"><span class="pl-c">//</span> probably this is safe to do in such extreme case</span>
<span class="pl-k">return</span> stdAllocator-&gt;<span class="pl-c1">allocate</span>(dims0, sizes, type, data, step, flags, usageFlags);
}</pre></div>
<p dir="auto">so I have put the line that starts with "CV_Error" in a comment and compiled again the OpenCV and tried to run the same code and everything worked fine, but I am not sure that what I did is ok!!! Any help?</p>
<p dir="auto">The following code returns an error.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="    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)"><pre class="notranslate">    <span class="pl-s1">sift</span> <span class="pl-c1">=</span> <span class="pl-s1">x2d</span>.<span class="pl-v">SIFT_create</span>(<span class="pl-c1">1000</span>)
    <span class="pl-s1">features_l</span>, <span class="pl-s1">des_l</span> <span class="pl-c1">=</span> <span class="pl-s1">sift</span>.<span class="pl-en">detectAndCompute</span>(<span class="pl-s1">im_l</span>, <span class="pl-c1">None</span>)
    <span class="pl-s1">features_r</span>, <span class="pl-s1">des_r</span> <span class="pl-c1">=</span> <span class="pl-s1">sift</span>.<span class="pl-en">detectAndCompute</span>(<span class="pl-s1">im_r</span>, <span class="pl-c1">None</span>)

    <span class="pl-v">FLANN_INDEX_KDTREE</span> <span class="pl-c1">=</span> <span class="pl-c1">1</span>
    <span class="pl-s1">index_params</span> <span class="pl-c1">=</span> <span class="pl-en">dict</span>(<span class="pl-s1">algorithm</span><span class="pl-c1">=</span><span class="pl-v">FLANN_INDEX_KDTREE</span>, <span class="pl-s1">trees</span><span class="pl-c1">=</span><span class="pl-c1">5</span>)
    <span class="pl-s1">search_params</span> <span class="pl-c1">=</span> <span class="pl-en">dict</span>(<span class="pl-s1">checks</span><span class="pl-c1">=</span><span class="pl-c1">50</span>)
    <span class="pl-s1">flann</span> <span class="pl-c1">=</span> <span class="pl-s1">cv2</span>.<span class="pl-v">FlannBasedMatcher</span>(<span class="pl-s1">index_params</span>,<span class="pl-s1">search_params</span>)</pre></div>
<p dir="auto">The error returned:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="opencv/modules/python/src2/cv2.cpp:161: error: (-215) The data should normally be NULL! in function allocate"><pre class="notranslate"><code class="notranslate">opencv/modules/python/src2/cv2.cpp:161: error: (-215) The data should normally be NULL! in function allocate
</code></pre></div>
<p dir="auto">I tried this with Python 2.7.<br>
FLANN_INDEX_KDTREE is set to 1 unlike <a href="http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_matcher/py_matcher.html#matcher" rel="nofollow">here</a>,<br>
since in modules/flann/include/opencv2/flann/defines.h I found it set to 1 on line 84.</p>