<h5 dir="auto">System information (version)</h5>

<ul dir="auto">
<li>OpenCV =&gt; <g-emoji class="g-emoji" alias="grey_question" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/2754.png">❔</g-emoji></li>
<li>Operating System / Platform =&gt; ❔</li>
<li>Compiler =&gt; <g-emoji class="g-emoji" alias="grey_question" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/2754.png">❔</g-emoji></li>
</ul>
<h5 dir="auto">Detailed description</h5>

<h5 dir="auto">Steps to reproduce</h5>

<h5 dir="auto">Issue submission checklist</h5>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> I report the issue, it's not a question

</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> I checked the problem with documentation, FAQ, open issues,<br>
answers.opencv.org, Stack Overflow, etc and have not found solution

</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> I updated to latest OpenCV version and the issue is still there

</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> There is reproducer code and related data files: videos, images, onnx, etc

</li>
</ul>
<h5 dir="auto">System information (version)</h5>
<ul dir="auto">
<li>OpenCV: 3.3</li>
<li>Operating System / Platform: ubuntu16</li>
<li>Compiler: cmake &amp; gcc5.4</li>
</ul>
<h5 dir="auto">Detailed description</h5>
<p dir="auto">opencv knn: dead loop</p>
<h5 dir="auto">Steps to reproduce</h5>
<div class="highlight highlight-source-c++ notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="#include &lt;opencv2/imgproc.hpp&gt;
#include &lt;opencv2/ml.hpp&gt;
#include &lt;vector&gt;
#include &quot;opencv2/core/core_c.h&quot;

using namespace std;
using namespace cv;
using namespace cv::ml;

using cv::Ptr;

int main(){

    Mat xTrainData = (Mat_&lt;float&gt;(5,2) &lt;&lt; 1, 1, 1, 1, 2, 2, 2, 2, 2, 2);
    Mat yTrainLabels = (Mat_&lt;float&gt;(5,1) &lt;&lt; 1, 1, 2, 2, 2);

    cout &lt;&lt; &quot;===============xTrainData====================&quot;&lt;&lt; endl;
    cout &lt;&lt; &quot;xTrainData: &quot;  &lt;&lt; xTrainData&lt;&lt; endl;

    cout &lt;&lt; &quot;===============yTrainLabels====================&quot;&lt;&lt; endl;
    cout &lt;&lt; &quot;yTrainLabels: &quot;  &lt;&lt; yTrainLabels &lt;&lt; endl;

    // KNearest KDTree implementation
    Ptr&lt;KNearest&gt; knnKdt = KNearest::create();
    knnKdt-&gt;setAlgorithmType(KNearest::KDTREE);
    knnKdt-&gt;setIsClassifier(true);

   // When runs to this step, it does not run any more.
    knnKdt-&gt;train(xTrainData, ml::ROW_SAMPLE, yTrainLabels);

    float d_test[2][2] = {{1.3, 1.3}, {2.2, 2.4}};

    Mat xTestData = (Mat_&lt;float&gt;(2,1) &lt;&lt; 1.1, 1.1, 2, 2.2);
    Mat zBestLabels;
    cout &lt;&lt; &quot;===============xTestData====================&quot;&lt;&lt; endl;
    cout &lt;&lt; &quot;xTestData: &quot;  &lt;&lt; xTestData &lt;&lt; endl;

    knnKdt-&gt;findNearest(xTestData, 1, zBestLabels);
    cout &lt;&lt; &quot;===============zBestLabels====================&quot;&lt;&lt; endl;
    cout &lt;&lt; &quot;zBestLabels: &quot;  &lt;&lt; zBestLabels &lt;&lt; endl;
}
"><pre class="notranslate">#<span class="pl-k">include</span> <span class="pl-s"><span class="pl-pds">&lt;</span>opencv2/imgproc.hpp<span class="pl-pds">&gt;</span></span>
#<span class="pl-k">include</span> <span class="pl-s"><span class="pl-pds">&lt;</span>opencv2/ml.hpp<span class="pl-pds">&gt;</span></span>
#<span class="pl-k">include</span> <span class="pl-s"><span class="pl-pds">&lt;</span>vector<span class="pl-pds">&gt;</span></span>
#<span class="pl-k">include</span> <span class="pl-s"><span class="pl-pds">"</span>opencv2/core/core_c.h<span class="pl-pds">"</span></span>

<span class="pl-k">using</span> <span class="pl-k">namespace</span> <span class="pl-en">std</span><span class="pl-k">;</span>
<span class="pl-k">using</span> <span class="pl-k">namespace</span> <span class="pl-en">cv</span><span class="pl-k">;</span>
<span class="pl-k">using</span> <span class="pl-k">namespace</span> <span class="pl-en">cv</span>::ml<span class="pl-k">;</span>

<span class="pl-k">using</span> cv::<span class="pl-c1">Ptr</span>;

<span class="pl-k">int</span> <span class="pl-en">main</span>(){

    Mat xTrainData = (Mat_&lt;<span class="pl-k">float</span>&gt;(<span class="pl-c1">5</span>,<span class="pl-c1">2</span>) &lt;&lt; <span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>);
    Mat yTrainLabels = (Mat_&lt;<span class="pl-k">float</span>&gt;(<span class="pl-c1">5</span>,<span class="pl-c1">1</span>) &lt;&lt; <span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>);

    cout &lt;&lt; <span class="pl-s"><span class="pl-pds">"</span>===============xTrainData====================<span class="pl-pds">"</span></span>&lt;&lt; endl;
    cout &lt;&lt; <span class="pl-s"><span class="pl-pds">"</span>xTrainData: <span class="pl-pds">"</span></span>  &lt;&lt; xTrainData&lt;&lt; endl;

    cout &lt;&lt; <span class="pl-s"><span class="pl-pds">"</span>===============yTrainLabels====================<span class="pl-pds">"</span></span>&lt;&lt; endl;
    cout &lt;&lt; <span class="pl-s"><span class="pl-pds">"</span>yTrainLabels: <span class="pl-pds">"</span></span>  &lt;&lt; yTrainLabels &lt;&lt; endl;

    <span class="pl-c"><span class="pl-c">//</span> KNearest KDTree implementation</span>
    <span class="pl-c1">Ptr</span>&lt;KNearest&gt; knnKdt = <span class="pl-c1">KNearest::create</span>();
    knnKdt-&gt;<span class="pl-c1">setAlgorithmType</span>(KNearest::KDTREE);
    knnKdt-&gt;<span class="pl-c1">setIsClassifier</span>(<span class="pl-c1">true</span>);

   <span class="pl-c"><span class="pl-c">//</span> When runs to this step, it does not run any more.</span>
    knnKdt-&gt;<span class="pl-c1">train</span>(xTrainData, ml::ROW_SAMPLE, yTrainLabels);

    <span class="pl-k">float</span> d_test[<span class="pl-c1">2</span>][<span class="pl-c1">2</span>] = {{<span class="pl-c1">1.3</span>, <span class="pl-c1">1.3</span>}, {<span class="pl-c1">2.2</span>, <span class="pl-c1">2.4</span>}};

    Mat xTestData = (Mat_&lt;<span class="pl-k">float</span>&gt;(<span class="pl-c1">2</span>,<span class="pl-c1">1</span>) &lt;&lt; <span class="pl-c1">1.1</span>, <span class="pl-c1">1.1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2.2</span>);
    Mat zBestLabels;
    cout &lt;&lt; <span class="pl-s"><span class="pl-pds">"</span>===============xTestData====================<span class="pl-pds">"</span></span>&lt;&lt; endl;
    cout &lt;&lt; <span class="pl-s"><span class="pl-pds">"</span>xTestData: <span class="pl-pds">"</span></span>  &lt;&lt; xTestData &lt;&lt; endl;

    knnKdt-&gt;<span class="pl-c1">findNearest</span>(xTestData, <span class="pl-c1">1</span>, zBestLabels);
    cout &lt;&lt; <span class="pl-s"><span class="pl-pds">"</span>===============zBestLabels====================<span class="pl-pds">"</span></span>&lt;&lt; endl;
    cout &lt;&lt; <span class="pl-s"><span class="pl-pds">"</span>zBestLabels: <span class="pl-pds">"</span></span>  &lt;&lt; zBestLabels &lt;&lt; endl;
}
</pre></div>
<p dir="auto">When I runs it in clion:</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/16144935/42207391-61e4a88a-7edc-11e8-8eb9-7338ae273342.png"><img src="https://user-images.githubusercontent.com/16144935/42207391-61e4a88a-7edc-11e8-8eb9-7338ae273342.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto">What's wrong?</p>