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

<h5 dir="auto">Detailed description</h5>
<p dir="auto">While testing turing.py, cv.imshow led to assertion failure in cvShowImage</p>
<p dir="auto">Assertion is</p>
<p dir="auto">CV_Assert(dst.data == (uchar*)dst_ptr);</p>
<p dir="auto">It seems the code is assuming that dst.data points to the window-&gt;image bits. Although dst.data equals dst_ptr and the window-&gt;image buffer, cv::cvtColor, can trigger a reallocation of the dst.data array.</p>
<p dir="auto">It looks like it works in cases where the size of the elements of the arrays don't change, but turing.py is converting from a gray scale float32 and cvtColor is converting it to RGB with a float for each value. The orginal buffer from</p>
<p dir="auto">window-&gt;image = SelectObject( window-&gt;dc, CreateDIBSection(window-&gt;dc, binfo,<br>
DIB_RGB_COLORS, &amp;dst_ptr, 0, 0));<br>
contains RGB with * bits per color.</p>
<p dir="auto">It might be a problem in cvtColor, since in the end windows probably needs a 8bit RGB value.</p>
<p dir="auto">This is my first day looking at opencv code, so I'm hoping someone more familiar with the code, has a better idea.</p>
<h5 dir="auto">Steps to reproduce</h5>
<p dir="auto">run python sample turing.py</p>
<p dir="auto">Open CV 3.1<br>
Windows 10<br>
Visual Studio 2015</p>
<p dir="auto">This bug is in cv::ml:: RTrees.</p>
<p dir="auto">I am writing a trained RTrees out using FileStorage, which appears to work correctly.  Doing a read() to deserialize causes a crash in In rtrees.cpp, line 335, in the read() method of DTreesImplForRTrees:<br>
<code class="notranslate">oobError = (double)fn["oob_error"];</code></p>
<p dir="auto">This happens regardless of whether I am serializing using a MEMORY- or file-based FileStorage object.  The FileNode produced by getFirstTopLevelNode() does not appear to contain the expected nodes in it.</p>
<p dir="auto">Here is code which produces the issue for memory FileStorage:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="cv::FileStorage fsWrite(&quot;my_tree&quot;, cv::FileStorage::WRITE + cv::FileStorage::MEMORY + FileStorage::FORMAT_XML);
Ptr&lt;cv::ml::Rtrees&gt; model;
[code to train model]
model-&gt;write(fsWrite);
std::string modelString = fsWrite.releaseAndGetString(); // produces proper-looking string with xml in it

Ptr&lt;cv::ml::RTrees&gt; resultModel = Algorithm::read&lt;ml::RTrees&gt;(fsWrite.getFirstTopLevelNode());"><pre class="notranslate"><code class="notranslate">cv::FileStorage fsWrite("my_tree", cv::FileStorage::WRITE + cv::FileStorage::MEMORY + FileStorage::FORMAT_XML);
Ptr&lt;cv::ml::Rtrees&gt; model;
[code to train model]
model-&gt;write(fsWrite);
std::string modelString = fsWrite.releaseAndGetString(); // produces proper-looking string with xml in it

Ptr&lt;cv::ml::RTrees&gt; resultModel = Algorithm::read&lt;ml::RTrees&gt;(fsWrite.getFirstTopLevelNode());
</code></pre></div>
<p dir="auto">And here for file-based:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="cv::FileStorage fsWrite(&quot;D:\\test.xml&quot;, cv::FileStorage::WRITE + FileStorage::FORMAT_XML);
m_ptrModel-&gt;write(fsWrite);
cv::FileStorage fsRead(&quot;D:\\test.xml&quot;, FileStorage::READ + FileStorage::FORMAT_XML);
m_ptrModel = cv::Algorithm::read&lt;ml::RTrees&gt;(fsRead.getFirstTopLevelNode());"><pre class="notranslate"><code class="notranslate">cv::FileStorage fsWrite("D:\\test.xml", cv::FileStorage::WRITE + FileStorage::FORMAT_XML);
m_ptrModel-&gt;write(fsWrite);
cv::FileStorage fsRead("D:\\test.xml", FileStorage::READ + FileStorage::FORMAT_XML);
m_ptrModel = cv::Algorithm::read&lt;ml::RTrees&gt;(fsRead.getFirstTopLevelNode());
</code></pre></div>
<p dir="auto">Here is what the modelString and file that are produced by the write() look like:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;opencv_storage&gt;
&lt;is_classifier&gt;0&lt;/is_classifier&gt;
&lt;var_all&gt;13&lt;/var_all&gt;
&lt;var_count&gt;12&lt;/var_count&gt;
&lt;ord_var_count&gt;13&lt;/ord_var_count&gt;
&lt;cat_var_count&gt;0&lt;/cat_var_count&gt;
&lt;training_params&gt;
  &lt;use_surrogates&gt;0&lt;/use_surrogates&gt;
  &lt;max_categories&gt;10&lt;/max_categories&gt;
  &lt;regression_accuracy&gt;9.9999997764825821e-03&lt;/regression_accuracy&gt;
  &lt;max_depth&gt;25&lt;/max_depth&gt;
  &lt;min_sample_count&gt;10&lt;/min_sample_count&gt;
  &lt;cross_validation_folds&gt;0&lt;/cross_validation_folds&gt;
  &lt;nactive_vars&gt;4&lt;/nactive_vars&gt;&lt;/training_params&gt;
&lt;global_var_idx&gt;1&lt;/global_var_idx&gt;
&lt;var_idx&gt;
  0 1 2 3 4 5 6 7 8 9 10 11&lt;/var_idx&gt;
&lt;var_type&gt;
  0 0 0 0 0 0 0 0 0 0 0 0 0&lt;/var_type&gt;
&lt;cat_ofs&gt;
  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0&lt;/cat_ofs&gt;
&lt;missing_subst&gt;
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.&lt;/missing_subst&gt;
&lt;oob_error&gt;0.&lt;/oob_error&gt;
&lt;ntrees&gt;1&lt;/ntrees&gt;
&lt;trees&gt;
  &lt;_&gt;
    &lt;nodes&gt;
      &lt;_&gt;
        &lt;depth&gt;0&lt;/depth&gt;
        &lt;value&gt;2.7725000000000000e+00&lt;/value&gt;
        &lt;splits&gt;
          &lt;_&gt;&lt;var&gt;7&lt;/var&gt;
            &lt;quality&gt;1.2565179687500000e+04&lt;/quality&gt;
            &lt;le&gt;5.8210000991821289e+00&lt;/le&gt;&lt;/_&gt;&lt;/splits&gt;&lt;/_&gt;
      &lt;_&gt;
        &lt;depth&gt;1&lt;/depth&gt;
        &lt;value&gt;2.9880095923261392e+00&lt;/value&gt;
        &lt;splits&gt;
          &lt;_&gt;&lt;var&gt;7&lt;/var&gt;
            &lt;quality&gt;1.1172947265625000e+04&lt;/quality&gt;
            &lt;le&gt;1.2557047605514526e+00&lt;/le&gt;&lt;/_&gt;&lt;/splits&gt;&lt;/_&gt;
      &lt;_&gt;
        &lt;depth&gt;2&lt;/depth&gt;
        &lt;value&gt;3.&lt;/value&gt;&lt;/_&gt;
      &lt;_&gt;
        &lt;depth&gt;2&lt;/depth&gt;
        &lt;value&gt;2.7368421052631580e+00&lt;/value&gt;
        &lt;splits&gt;
          &lt;_&gt;&lt;var&gt;3&lt;/var&gt;
            &lt;quality&gt;438.&lt;/quality&gt;
            &lt;le&gt;-1.2632201194763184e+01&lt;/le&gt;&lt;/_&gt;&lt;/splits&gt;&lt;/_&gt;
      &lt;_&gt;
        &lt;depth&gt;3&lt;/depth&gt;
        &lt;value&gt;3.&lt;/value&gt;&lt;/_&gt;
      &lt;_&gt;
        &lt;depth&gt;3&lt;/depth&gt;
        &lt;value&gt;2.&lt;/value&gt;&lt;/_&gt;
      &lt;_&gt;
        &lt;depth&gt;1&lt;/depth&gt;
        &lt;value&gt;2.&lt;/value&gt;&lt;/_&gt;&lt;/nodes&gt;&lt;/_&gt;&lt;/trees&gt;
&lt;/opencv_storage&gt;"><pre class="notranslate"><code class="notranslate">&lt;?xml version="1.0"?&gt;
&lt;opencv_storage&gt;
&lt;is_classifier&gt;0&lt;/is_classifier&gt;
&lt;var_all&gt;13&lt;/var_all&gt;
&lt;var_count&gt;12&lt;/var_count&gt;
&lt;ord_var_count&gt;13&lt;/ord_var_count&gt;
&lt;cat_var_count&gt;0&lt;/cat_var_count&gt;
&lt;training_params&gt;
  &lt;use_surrogates&gt;0&lt;/use_surrogates&gt;
  &lt;max_categories&gt;10&lt;/max_categories&gt;
  &lt;regression_accuracy&gt;9.9999997764825821e-03&lt;/regression_accuracy&gt;
  &lt;max_depth&gt;25&lt;/max_depth&gt;
  &lt;min_sample_count&gt;10&lt;/min_sample_count&gt;
  &lt;cross_validation_folds&gt;0&lt;/cross_validation_folds&gt;
  &lt;nactive_vars&gt;4&lt;/nactive_vars&gt;&lt;/training_params&gt;
&lt;global_var_idx&gt;1&lt;/global_var_idx&gt;
&lt;var_idx&gt;
  0 1 2 3 4 5 6 7 8 9 10 11&lt;/var_idx&gt;
&lt;var_type&gt;
  0 0 0 0 0 0 0 0 0 0 0 0 0&lt;/var_type&gt;
&lt;cat_ofs&gt;
  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0&lt;/cat_ofs&gt;
&lt;missing_subst&gt;
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.&lt;/missing_subst&gt;
&lt;oob_error&gt;0.&lt;/oob_error&gt;
&lt;ntrees&gt;1&lt;/ntrees&gt;
&lt;trees&gt;
  &lt;_&gt;
    &lt;nodes&gt;
      &lt;_&gt;
        &lt;depth&gt;0&lt;/depth&gt;
        &lt;value&gt;2.7725000000000000e+00&lt;/value&gt;
        &lt;splits&gt;
          &lt;_&gt;&lt;var&gt;7&lt;/var&gt;
            &lt;quality&gt;1.2565179687500000e+04&lt;/quality&gt;
            &lt;le&gt;5.8210000991821289e+00&lt;/le&gt;&lt;/_&gt;&lt;/splits&gt;&lt;/_&gt;
      &lt;_&gt;
        &lt;depth&gt;1&lt;/depth&gt;
        &lt;value&gt;2.9880095923261392e+00&lt;/value&gt;
        &lt;splits&gt;
          &lt;_&gt;&lt;var&gt;7&lt;/var&gt;
            &lt;quality&gt;1.1172947265625000e+04&lt;/quality&gt;
            &lt;le&gt;1.2557047605514526e+00&lt;/le&gt;&lt;/_&gt;&lt;/splits&gt;&lt;/_&gt;
      &lt;_&gt;
        &lt;depth&gt;2&lt;/depth&gt;
        &lt;value&gt;3.&lt;/value&gt;&lt;/_&gt;
      &lt;_&gt;
        &lt;depth&gt;2&lt;/depth&gt;
        &lt;value&gt;2.7368421052631580e+00&lt;/value&gt;
        &lt;splits&gt;
          &lt;_&gt;&lt;var&gt;3&lt;/var&gt;
            &lt;quality&gt;438.&lt;/quality&gt;
            &lt;le&gt;-1.2632201194763184e+01&lt;/le&gt;&lt;/_&gt;&lt;/splits&gt;&lt;/_&gt;
      &lt;_&gt;
        &lt;depth&gt;3&lt;/depth&gt;
        &lt;value&gt;3.&lt;/value&gt;&lt;/_&gt;
      &lt;_&gt;
        &lt;depth&gt;3&lt;/depth&gt;
        &lt;value&gt;2.&lt;/value&gt;&lt;/_&gt;
      &lt;_&gt;
        &lt;depth&gt;1&lt;/depth&gt;
        &lt;value&gt;2.&lt;/value&gt;&lt;/_&gt;&lt;/nodes&gt;&lt;/_&gt;&lt;/trees&gt;
&lt;/opencv_storage&gt;
</code></pre></div>
<p dir="auto">You should be able to duplicate the problem by taking the model string here, sticking it in a file, and simply doing a read() on it, something like:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="#include &quot;opencv2\core\core.hpp&quot;
#include &quot;opencv2\ml\ml.hpp&quot;
cv::FileStorage fsRead(&quot;D:\\test.xml&quot;, FileStorage::READ + FileStorage::FORMAT_XML);
Ptr&lt;cv::ml::RTrees&gt; resultModel = Algorithm::read&lt;ml::RTrees&gt;(fsWrite.getFirstTopLevelNode());"><pre class="notranslate"><code class="notranslate">#include "opencv2\core\core.hpp"
#include "opencv2\ml\ml.hpp"
cv::FileStorage fsRead("D:\\test.xml", FileStorage::READ + FileStorage::FORMAT_XML);
Ptr&lt;cv::ml::RTrees&gt; resultModel = Algorithm::read&lt;ml::RTrees&gt;(fsWrite.getFirstTopLevelNode());
</code></pre></div>