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

<ul dir="auto">
<li>OpenCV =&gt; 3.4 branch ( <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/opencv/opencv/commit/0101fa789cde6c03bc58288a56e1d6ce7175cd48/hovercard" href="https://github.com/opencv/opencv/commit/0101fa789cde6c03bc58288a56e1d6ce7175cd48"><tt>0101fa7</tt></a> )</li>
<li>Operating System / Platform =&gt; Windows 10 64bit</li>
<li>Compiler =&gt; Visual Studio 2015</li>
<li>CUDA =&gt; 10.0</li>
</ul>
<h5 dir="auto">Summarized description</h5>
<ul dir="auto">
<li>The <code class="notranslate">opencv_test_imgproc</code> passes, but when I ran it from target <code class="notranslate">RUN_TESTS</code>, it says <code class="notranslate">Segfault</code></li>
</ul>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="    Start 9: opencv_test_imgproc
1/1 Test #9: opencv_test_imgproc ..............***Exception: SegFault137.81 sec"><pre class="notranslate"><code class="notranslate">    Start 9: opencv_test_imgproc
1/1 Test #9: opencv_test_imgproc ..............***Exception: SegFault137.81 sec
</code></pre></div>
<ul dir="auto">
<li>I ran <code class="notranslate">opencv_test_imgproc</code> separately, but all the test passes (!)</li>
<li>To reproduce what gtest was seeing, I need to ran it in Debug mode</li>
<li>When running <code class="notranslate">opencv_test_imgproc</code> with "Start debug" on Visual Studio 2015, the test passed but at the end, it did cause an <code class="notranslate">Segfault</code> at <a href="https://github.com/opencv/opencv/blob/0101fa789cde6c03bc58288a56e1d6ce7175cd48/modules/core/src/ocl.cpp#L4328">ocl.cpp</a><br>
<div class="Box Box--condensed my-2">
  <div class="Box-header f6">
    <p class="mb-0 text-bold">
      <a href="https://github.com/opencv/opencv/blob/0101fa789cde6c03bc58288a56e1d6ce7175cd48/modules/core/src/ocl.cpp#L4322-L4329">opencv/modules/core/src/ocl.cpp</a>
    </p>
    <p class="mb-0 color-fg-muted">
        Lines 4322 to 4329
      in
      <a data-pjax="true" class="commit-tease-sha" href="/opencv/opencv/commit/0101fa789cde6c03bc58288a56e1d6ce7175cd48">0101fa7</a>
    </p>
  </div>
  <div itemprop="text" class="Box-body p-0 blob-wrapper blob-wrapper-embedded data">
    <table class="highlight tab-size mb-0 js-file-line-container" data-tab-size="8" data-paste-markdown-skip="">

        <tbody><tr class="border-0">
          <td id="L4322" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="4322"></td>
          <td id="LC4322" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-k">void</span> <span class="pl-en">_releaseBufferEntry</span>(<span class="pl-k">const</span> BufferEntry&amp; entry) </td>
        </tr>

        <tr class="border-0">
          <td id="L4323" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="4323"></td>
          <td id="LC4323" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> { </td>
        </tr>

        <tr class="border-0">
          <td id="L4324" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="4324"></td>
          <td id="LC4324" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">     <span class="pl-c1">CV_Assert</span>(entry.<span class="pl-smi">capacity_</span> != <span class="pl-c1">0</span>); </td>
        </tr>

        <tr class="border-0">
          <td id="L4325" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="4325"></td>
          <td id="LC4325" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">     <span class="pl-c1">CV_Assert</span>(entry.<span class="pl-smi">clBuffer_</span> != <span class="pl-c1">NULL</span>); </td>
        </tr>

        <tr class="border-0">
          <td id="L4326" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="4326"></td>
          <td id="LC4326" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">     <span class="pl-c1">LOG_BUFFER_POOL</span>(<span class="pl-s"><span class="pl-pds">"</span>OpenCL release buffer: %p, %lld (0x%llx) bytes<span class="pl-cce">\n</span><span class="pl-pds">"</span></span>, </td>
        </tr>

        <tr class="border-0">
          <td id="L4327" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="4327"></td>
          <td id="LC4327" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">             entry.<span class="pl-smi">clBuffer_</span>, (<span class="pl-k">long</span> <span class="pl-k">long</span>)entry.<span class="pl-smi">capacity_</span>, (<span class="pl-k">long</span> <span class="pl-k">long</span>)entry.<span class="pl-smi">capacity_</span>); </td>
        </tr>

        <tr class="border-0">
          <td id="L4328" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="4328"></td>
          <td id="LC4328" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">     <span class="pl-c1">CV_OCL_DBG_CHECK</span>(<span class="pl-c1">clReleaseMemObject</span>(entry.<span class="pl-smi">clBuffer_</span>)); </td>
        </tr>

        <tr class="border-0">
          <td id="L4329" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="4329"></td>
          <td id="LC4329" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> } </td>
        </tr>
    </tbody></table>
  </div>
</div>
</li>
</ul>
<h5 dir="auto">Detailed description</h5>
<ul dir="auto">
<li>
<p dir="auto">I narrowed down the test case, and the test case was from <code class="notranslate">OCL_ImgProc/CvtColor8u32f</code></p>
</li>
<li>
<p dir="auto">Furthermore, it was related to test of either HSV/Lab/Luv conversion.</p>
</li>
<li>
<p dir="auto">Also, although I put a break point at where it causes <code class="notranslate">Segfault</code>, it doesn't reproduce the situation.</p>
</li>
<li>
<p dir="auto">I have to exit from the <a href="https://github.com/opencv/opencv/blob/0101fa789cde6c03bc58288a56e1d6ce7175cd48/modules/imgproc/test/test_main.cpp">main routine call</a> and it was happening after that exit call.  Mysterious...</p>
</li>
<li>
<p dir="auto">After watching the source code, I realized that some of the static <code class="notranslate">UMat</code> was used in a wrong scope, such like <a href="https://github.com/opencv/opencv/blob/0101fa789cde6c03bc58288a56e1d6ce7175cd48/modules/imgproc/src/color_hsv.cpp#L1483-L1486">here </a><br>
</p><div class="Box Box--condensed my-2">
  <div class="Box-header f6">
    <p class="mb-0 text-bold">
      <a href="https://github.com/opencv/opencv/blob/0101fa789cde6c03bc58288a56e1d6ce7175cd48/modules/imgproc/src/color_hsv.cpp#L1483-L1486">opencv/modules/imgproc/src/color_hsv.cpp</a>
    </p>
    <p class="mb-0 color-fg-muted">
        Lines 1483 to 1486
      in
      <a data-pjax="true" class="commit-tease-sha" href="/opencv/opencv/commit/0101fa789cde6c03bc58288a56e1d6ce7175cd48">0101fa7</a>
    </p>
  </div>
  <div itemprop="text" class="Box-body p-0 blob-wrapper blob-wrapper-embedded data">
    <table class="highlight tab-size mb-0 js-file-line-container" data-tab-size="8" data-paste-markdown-skip="">

        <tbody><tr class="border-0">
          <td id="L1483" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="1483"></td>
          <td id="LC1483" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> { </td>
        </tr>

        <tr class="border-0">
          <td id="L1484" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="1484"></td>
          <td id="LC1484" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">     <span class="pl-k">static</span> UMat sdiv_data; </td>
        </tr>

        <tr class="border-0">
          <td id="L1485" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="1485"></td>
          <td id="LC1485" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">     <span class="pl-k">static</span> UMat hdiv_data180; </td>
        </tr>

        <tr class="border-0">
          <td id="L1486" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="1486"></td>
          <td id="LC1486" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">     <span class="pl-k">static</span> UMat hdiv_data256; </td>
        </tr>
    </tbody></table>
  </div>
</div>
<p></p>
</li>
<li>
<p dir="auto">These <code class="notranslate">UMat</code> were passed as a <a href="https://github.com/opencv/opencv/blob/0101fa789cde6c03bc58288a56e1d6ce7175cd48/modules/imgproc/src/color_hsv.cpp#L1516-L1521">parameter of the OpenCL kernel</a> but the scope <a href="https://github.com/opencv/opencv/blob/0101fa789cde6c03bc58288a56e1d6ce7175cd48/modules/imgproc/src/color_hsv.cpp#L1519">was closed before</a> it was <a href="https://github.com/opencv/opencv/blob/0101fa789cde6c03bc58288a56e1d6ce7175cd48/modules/imgproc/src/color_hsv.cpp#L1521">actually used</a><br>
</p><div class="Box Box--condensed my-2">
  <div class="Box-header f6">
    <p class="mb-0 text-bold">
      <a href="https://github.com/opencv/opencv/blob/0101fa789cde6c03bc58288a56e1d6ce7175cd48/modules/imgproc/src/color_hsv.cpp#L1516-L1521">opencv/modules/imgproc/src/color_hsv.cpp</a>
    </p>
    <p class="mb-0 color-fg-muted">
        Lines 1516 to 1521
      in
      <a data-pjax="true" class="commit-tease-sha" href="/opencv/opencv/commit/0101fa789cde6c03bc58288a56e1d6ce7175cd48">0101fa7</a>
    </p>
  </div>
  <div itemprop="text" class="Box-body p-0 blob-wrapper blob-wrapper-embedded data">
    <table class="highlight tab-size mb-0 js-file-line-container" data-tab-size="8" data-paste-markdown-skip="">

        <tbody><tr class="border-0">
          <td id="L1516" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="1516"></td>
          <td id="LC1516" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">     h.<span class="pl-c1">setArg</span>(<span class="pl-c1">ocl::KernelArg::PtrReadOnly</span>(sdiv_data)); </td>
        </tr>

        <tr class="border-0">
          <td id="L1517" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="1517"></td>
          <td id="LC1517" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">     h.<span class="pl-c1">setArg</span>(hrange == <span class="pl-c1">256</span> ? <span class="pl-c1">ocl::KernelArg::PtrReadOnly</span>(hdiv_data256) : </td>
        </tr>

        <tr class="border-0">
          <td id="L1518" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="1518"></td>
          <td id="LC1518" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">                              <span class="pl-c1">ocl::KernelArg::PtrReadOnly</span>(hdiv_data180)); </td>
        </tr>

        <tr class="border-0">
          <td id="L1519" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="1519"></td>
          <td id="LC1519" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> } </td>
        </tr>

        <tr class="border-0">
          <td id="L1520" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="1520"></td>
          <td id="LC1520" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">  </td>
        </tr>

        <tr class="border-0">
          <td id="L1521" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="1521"></td>
          <td id="LC1521" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-k">return</span> h.<span class="pl-c1">run</span>(); </td>
        </tr>
    </tbody></table>
  </div>
</div>
<p></p>
</li>
<li>
<p dir="auto">Looking back the history, it seems that color convert was splitted in <a href="https://github.com/opencv/opencv/commit/0b900b54e54e8cbd19a65cfe69d20838f142e6b3">early 2018</a>, and before the split, the scope was aligned with the actual use.</p>
</li>
<li>
<p dir="auto">I moved the <code class="notranslate">UMat</code> declarations and also, I removed the <code class="notranslate">static</code></p>
</li>
<li>
<p dir="auto">Now the test passed without <code class="notranslate">Segfault</code></p>
</li>
</ul>
<h5 dir="auto">Steps to reproduce</h5>
<ul dir="auto">
<li>
<p dir="auto">Build under environment explained above.</p>
</li>
<li>
<p dir="auto">Run with CUDA 10.0 and run the OpenCL kernel on dGPU</p>
</li>
<li>
<p dir="auto"><code class="notranslate">OPENCV_OPENCL_DEVICE=:dgpu opencv_test_imgproc --gtest_filter=OCL_ImgProc/CvtColor8u32f*</code></p>
</li>
<li>
<p dir="auto">Also, there I'm not sure why this doesn't happen on <code class="notranslate">CPU</code> and <code class="notranslate">igpu</code> on my laptop.</p>
</li>
<li>
<p dir="auto">Still, the scope difference seems bad, so I think it has meaning to fix the scope.</p>
</li>
</ul>
<h5 dir="auto">System information (version)</h5>

<ul dir="auto">
<li>OpenCV =&gt; recent 3.4 ( <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/opencv/opencv/commit/c2096771cb705097d2203146838bf4ff71a9c774/hovercard" href="https://github.com/opencv/opencv/commit/c2096771cb705097d2203146838bf4ff71a9c774"><tt>c209677</tt></a> )</li>
<li>Operating System / Platform =&gt; Jetson Xavier, Jetson TX1, Jetson TX2, Jetson Nano, Firefly RK3399, ODROID-N2 (All Aarch64)</li>
<li>Compiler =&gt; GCC 7.4.0</li>
</ul>
<h5 dir="auto">Abstract</h5>
<ul dir="auto">
<li>Another rounding error causes missing count in histogram</li>
<li>This leads to test failure in xphoto module</li>
</ul>
<h5 dir="auto">Detailed description</h5>
<p dir="auto">The original issue comes from <code class="notranslate">opencv_test_xphoto</code> which is contrib module.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ ./build/bin/opencv_test_xphoto --gtest_filter=*float_min_value
CTEST_FULL_OUTPUT
OpenCV version: 3.4.8-pre
OpenCV VCS version: 3.4.7-189-gc209677
Build type: Release
Compiler: /usr/bin/c++  (ver 7.4.0)
Parallel framework: pthreads
CPU features: NEON FP16
OpenCL is disabled
TEST: Skip tests with tags: 'mem_6gb', 'verylong'
Note: Google Test filter = *float_min_value
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from xphoto_simplecolorbalance
[ RUN      ] xphoto_simplecolorbalance.float_min_value
/home/nvidia/opencv_contrib/modules/xphoto/test/simple_color_balance.cpp:197: Failure
The difference between minDst and newMin is 65468.87890625, which exceeds 65536*1e-4, where
minDst evaluates to -65468.87890625,
newMin evaluates to 0, and
65536*1e-4 evaluates to 6.5536000000000003.
[  FAILED  ] xphoto_simplecolorbalance.float_min_value (1 ms)
[----------] 1 test from xphoto_simplecolorbalance (1 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (2 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] xphoto_simplecolorbalance.float_min_value

 1 FAILED TEST"><pre class="notranslate"><code class="notranslate">$ ./build/bin/opencv_test_xphoto --gtest_filter=*float_min_value
CTEST_FULL_OUTPUT
OpenCV version: 3.4.8-pre
OpenCV VCS version: 3.4.7-189-gc209677
Build type: Release
Compiler: /usr/bin/c++  (ver 7.4.0)
Parallel framework: pthreads
CPU features: NEON FP16
OpenCL is disabled
TEST: Skip tests with tags: 'mem_6gb', 'verylong'
Note: Google Test filter = *float_min_value
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from xphoto_simplecolorbalance
[ RUN      ] xphoto_simplecolorbalance.float_min_value
/home/nvidia/opencv_contrib/modules/xphoto/test/simple_color_balance.cpp:197: Failure
The difference between minDst and newMin is 65468.87890625, which exceeds 65536*1e-4, where
minDst evaluates to -65468.87890625,
newMin evaluates to 0, and
65536*1e-4 evaluates to 6.5536000000000003.
[  FAILED  ] xphoto_simplecolorbalance.float_min_value (1 ms)
[----------] 1 test from xphoto_simplecolorbalance (1 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (2 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] xphoto_simplecolorbalance.float_min_value

 1 FAILED TEST
</code></pre></div>
<p dir="auto">Not only <code class="notranslate">float_min_value</code> but total 3 tests fail.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="[==========] 15 tests from 4 test cases ran. (1171 ms total)
[  PASSED  ] 12 tests.
[  FAILED  ] 3 tests, listed below:
[  FAILED  ] xphoto_simplecolorbalance.float_min_value
[  FAILED  ] xphoto_simplecolorbalance.float_p
[  FAILED  ] xphoto_simplecolorbalance.float_c3"><pre class="notranslate"><code class="notranslate">[==========] 15 tests from 4 test cases ran. (1171 ms total)
[  PASSED  ] 12 tests.
[  FAILED  ] 3 tests, listed below:
[  FAILED  ] xphoto_simplecolorbalance.float_min_value
[  FAILED  ] xphoto_simplecolorbalance.float_p
[  FAILED  ] xphoto_simplecolorbalance.float_c3
</code></pre></div>
<h5 dir="auto">Steps to reproduce</h5>
<p dir="auto">These tests were added by <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="493812478" data-permission-text="Title is private" data-url="https://github.com/opencv/opencv_contrib/issues/2263" data-hovercard-type="pull_request" data-hovercard-url="/opencv/opencv_contrib/pull/2263/hovercard" href="https://github.com/opencv/opencv_contrib/pull/2263">opencv/opencv_contrib#2263</a><br>
<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="493812478" data-permission-text="Title is private" data-url="https://github.com/opencv/opencv_contrib/issues/2263" data-hovercard-type="pull_request" data-hovercard-url="/opencv/opencv_contrib/pull/2263/hovercard" href="https://github.com/opencv/opencv_contrib/pull/2263">opencv/opencv_contrib#2263</a> also <a href="https://github.com/opencv/opencv_contrib/pull/2263/files#diff-4865499a75d5cb18c0afb5d1bf869124R92">modified balanceWhiteSimple to use calcHist</a></p>
<p dir="auto">In <a href="https://github.com/opencv/opencv/blob/c2096771cb705097d2203146838bf4ff71a9c774/modules/imgproc/src/histogram.cpp#L862">calcHist</a>, it's computing the bin number <a href="https://github.com/opencv/opencv/blob/c2096771cb705097d2203146838bf4ff71a9c774/modules/imgproc/src/histogram.cpp#L251">here</a><br>
</p><div class="Box Box--condensed my-2">
  <div class="Box-header f6">
    <p class="mb-0 text-bold">
      <a href="https://github.com/opencv/opencv/blob/c2096771cb705097d2203146838bf4ff71a9c774/modules/imgproc/src/histogram.cpp#L251-L253">opencv/modules/imgproc/src/histogram.cpp</a>
    </p>
    <p class="mb-0 color-fg-muted">
        Lines 251 to 253
      in
      <a data-pjax="true" class="commit-tease-sha" href="/opencv/opencv/commit/c2096771cb705097d2203146838bf4ff71a9c774">c209677</a>
    </p>
  </div>
  <div itemprop="text" class="Box-body p-0 blob-wrapper blob-wrapper-embedded data">
    <table class="highlight tab-size mb-0 js-file-line-container" data-tab-size="8" data-paste-markdown-skip="">

        <tbody><tr class="border-0">
          <td id="L251" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="251"></td>
          <td id="LC251" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-k">int</span> idx = <span class="pl-c1">cvFloor</span>(*p0*a + b); </td>
        </tr>

        <tr class="border-0">
          <td id="L252" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="252"></td>
          <td id="LC252" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-k">if</span>( (<span class="pl-k">unsigned</span>)idx &lt; (<span class="pl-k">unsigned</span>)sz ) </td>
        </tr>

        <tr class="border-0">
          <td id="L253" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="253"></td>
          <td id="LC253" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">     ((<span class="pl-k">int</span>*)H)[idx]++; </td>
        </tr>
    </tbody></table>
  </div>
</div>
<p></p>
<p dir="auto">In Release mode + Aarch64, this <code class="notranslate">multiply and add</code> operations are tend to be compiled as <code class="notranslate">fmadd</code> instruction</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="fmadd  d0, d2, d0, d1"><pre class="notranslate"><code class="notranslate">fmadd  d0, d2, d0, d1
</code></pre></div>
<p dir="auto">And this <code class="notranslate">fmadd</code> causes digit loss when the result becomes near to zero.<br>
Following text shows that <code class="notranslate">*p0*a+b</code> is supposed to become 0.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="(gdb) p  *p0
$22 = 24000
(gdb) p a
$23 = 0.098610877049377668
(gdb) p b
$24 = -2366.6610491850643
(gdb) p *p0*a+b
$25 = 0"><pre class="notranslate"><code class="notranslate">(gdb) p  *p0
$22 = 24000
(gdb) p a
$23 = 0.098610877049377668
(gdb) p b
$24 = -2366.6610491850643
(gdb) p *p0*a+b
$25 = 0
</code></pre></div>
<p dir="auto">when the actual computation result becomes negative, not 0.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="(gdb) p value
$20 = -2.2115642650533118e-13"><pre class="notranslate"><code class="notranslate">(gdb) p value
$20 = -2.2115642650533118e-13
</code></pre></div>
<p dir="auto">Since we are doing <code class="notranslate">cvFloor</code> on negative value, the result becomes <code class="notranslate">-1</code> and it won't pass the following <code class="notranslate">if</code> condition<br>
</p><div class="Box Box--condensed my-2">
  <div class="Box-header f6">
    <p class="mb-0 text-bold">
      <a href="https://github.com/opencv/opencv/blob/c2096771cb705097d2203146838bf4ff71a9c774/modules/imgproc/src/histogram.cpp#L252-L253">opencv/modules/imgproc/src/histogram.cpp</a>
    </p>
    <p class="mb-0 color-fg-muted">
        Lines 252 to 253
      in
      <a data-pjax="true" class="commit-tease-sha" href="/opencv/opencv/commit/c2096771cb705097d2203146838bf4ff71a9c774">c209677</a>
    </p>
  </div>
  <div itemprop="text" class="Box-body p-0 blob-wrapper blob-wrapper-embedded data">
    <table class="highlight tab-size mb-0 js-file-line-container" data-tab-size="8" data-paste-markdown-skip="">

        <tbody><tr class="border-0">
          <td id="L252" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="252"></td>
          <td id="LC252" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-k">if</span>( (<span class="pl-k">unsigned</span>)idx &lt; (<span class="pl-k">unsigned</span>)sz ) </td>
        </tr>

        <tr class="border-0">
          <td id="L253" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="253"></td>
          <td id="LC253" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">     ((<span class="pl-k">int</span>*)H)[idx]++; </td>
        </tr>
    </tbody></table>
  </div>
</div>
<br>
This will lead to missing count of pixel.<br>
The cause is same as <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="154590824" data-permission-text="Title is private" data-url="https://github.com/opencv/opencv/issues/6536" data-hovercard-type="issue" data-hovercard-url="/opencv/opencv/issues/6536/hovercard" href="https://github.com/opencv/opencv/issues/6536">#6536</a> , it's another headache rounding error.<p></p>
<p dir="auto">Now, my best solution I can imagine is to apply <code class="notranslate">round-to-zero</code> instead of <code class="notranslate">cvFloor</code> inside <code class="notranslate">calcHist</code><br>
Another solution is to modify the parameter of <code class="notranslate">xphoto</code> so the test passes.<br>
I just like to know which way to fix this issue, or if there are any already implemented <code class="notranslate">round-to-zero</code> function in OpenCV (AFAIK, there are only cvFloor, cvRuond and cvCeil).</p>