<h5 dir="auto">System information (version)</h5>
<ul dir="auto">
<li>OpenCV =&gt; 4.2.0.34</li>
<li>Operating System / Platform =&gt; macOS 10.15.5</li>
<li>Compiler =&gt; Not sure, using Python 3.7.3 via Anaconda</li>
</ul>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin"><pre lang="none" class="notranslate"><code class="notranslate">gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
</code></pre></div>
<h5 dir="auto">Detailed description</h5>
<p dir="auto">Maybe related to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="530878383" data-permission-text="Title is private" data-url="https://github.com/opencv/opencv/issues/16030" data-hovercard-type="issue" data-hovercard-url="/opencv/opencv/issues/16030/hovercard" href="https://github.com/opencv/opencv/issues/16030">#16030</a>. I'm getting a segmentation fault, when calling <code class="notranslate">warpAffine</code> or <code class="notranslate">warpPerspective</code> from a background worker. Calling it from the foreground thread works fine, but the very same operation fails on the background thread.</p>
<p dir="auto">Keep in mind, that this code fails silently, because the problematic operation happens on the background thread - it simply does not succeed. I guess there is a way to read the actual stacktrace, but I haven't figured that out for the multiprocessing library.</p>
<h5 dir="auto">Steps to reproduce</h5>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from multiprocessing import Pool

import cv2
import numpy as np


def f(x):
    cv2.warpPerspective(np.ones((707, 500)), np.asarray([[1, 0, 0], [0, 1, 0], [0, 0, 1]]).astype(np.float32), (707, 500))
    print(&quot;Success warpPerspective &quot; + str(x))


def g(x):
    cv2.warpAffine(np.ones((221, 221, 34)).astype(np.float32), np.asarray([[4, 0, 0], [0, 4, 0]]).astype(np.float32), (881, 881))
    print(&quot;Success warpAffine &quot; + str(x))


if __name__ == '__main__':
    f(10)  # Running on the foreground thread works
    g(10)  # Running on the foreground thread works

    pool = Pool(processes=4)  # Running on the background threads does not
    print(pool.map(f, range(5)))

    pool2 = Pool(processes=4)  # Running on the background threads does not
    print(pool2.map(g, range(5)))"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">multiprocessing</span> <span class="pl-k">import</span> <span class="pl-v">Pool</span>

<span class="pl-k">import</span> <span class="pl-s1">cv2</span>
<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">def</span> <span class="pl-en">f</span>(<span class="pl-s1">x</span>):
    <span class="pl-s1">cv2</span>.<span class="pl-en">warpPerspective</span>(<span class="pl-s1">np</span>.<span class="pl-en">ones</span>((<span class="pl-c1">707</span>, <span class="pl-c1">500</span>)), <span class="pl-s1">np</span>.<span class="pl-en">asarray</span>([[<span class="pl-c1">1</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>], [<span class="pl-c1">0</span>, <span class="pl-c1">1</span>, <span class="pl-c1">0</span>], [<span class="pl-c1">0</span>, <span class="pl-c1">0</span>, <span class="pl-c1">1</span>]]).<span class="pl-en">astype</span>(<span class="pl-s1">np</span>.<span class="pl-s1">float32</span>), (<span class="pl-c1">707</span>, <span class="pl-c1">500</span>))
    <span class="pl-en">print</span>(<span class="pl-s">"Success warpPerspective "</span> <span class="pl-c1">+</span> <span class="pl-en">str</span>(<span class="pl-s1">x</span>))


<span class="pl-k">def</span> <span class="pl-en">g</span>(<span class="pl-s1">x</span>):
    <span class="pl-s1">cv2</span>.<span class="pl-en">warpAffine</span>(<span class="pl-s1">np</span>.<span class="pl-en">ones</span>((<span class="pl-c1">221</span>, <span class="pl-c1">221</span>, <span class="pl-c1">34</span>)).<span class="pl-en">astype</span>(<span class="pl-s1">np</span>.<span class="pl-s1">float32</span>), <span class="pl-s1">np</span>.<span class="pl-en">asarray</span>([[<span class="pl-c1">4</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>], [<span class="pl-c1">0</span>, <span class="pl-c1">4</span>, <span class="pl-c1">0</span>]]).<span class="pl-en">astype</span>(<span class="pl-s1">np</span>.<span class="pl-s1">float32</span>), (<span class="pl-c1">881</span>, <span class="pl-c1">881</span>))
    <span class="pl-en">print</span>(<span class="pl-s">"Success warpAffine "</span> <span class="pl-c1">+</span> <span class="pl-en">str</span>(<span class="pl-s1">x</span>))


<span class="pl-k">if</span> <span class="pl-s1">__name__</span> <span class="pl-c1">==</span> <span class="pl-s">'__main__'</span>:
    <span class="pl-en">f</span>(<span class="pl-c1">10</span>)  <span class="pl-c"># Running on the foreground thread works</span>
    <span class="pl-en">g</span>(<span class="pl-c1">10</span>)  <span class="pl-c"># Running on the foreground thread works</span>

    <span class="pl-s1">pool</span> <span class="pl-c1">=</span> <span class="pl-v">Pool</span>(<span class="pl-s1">processes</span><span class="pl-c1">=</span><span class="pl-c1">4</span>)  <span class="pl-c"># Running on the background threads does not</span>
    <span class="pl-en">print</span>(<span class="pl-s1">pool</span>.<span class="pl-en">map</span>(<span class="pl-s1">f</span>, <span class="pl-en">range</span>(<span class="pl-c1">5</span>)))

    <span class="pl-s1">pool2</span> <span class="pl-c1">=</span> <span class="pl-v">Pool</span>(<span class="pl-s1">processes</span><span class="pl-c1">=</span><span class="pl-c1">4</span>)  <span class="pl-c"># Running on the background threads does not</span>
    <span class="pl-en">print</span>(<span class="pl-s1">pool2</span>.<span class="pl-en">map</span>(<span class="pl-s1">g</span>, <span class="pl-en">range</span>(<span class="pl-c1">5</span>)))</pre></div>
<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" checked=""> 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" checked=""> 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" checked=""> 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" checked=""> There is reproducer code and related data files: videos, images, onnx, etc

</li>
</ul>
<p dir="auto">I'm trying to use OpenCV with Python's multiprocessing module, but it breaks on me even with very simple code. Here is an example:</p>
<div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import cv2
import multiprocessing
import glob
import numpy

def job(path):

    image = cv2.imread(path)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    return path

if __name__ == &quot;__main__&quot;:

    main_image = cv2.imread(&quot;./image.png&quot;)
    main_image = cv2.cvtColor( main_image, cv2.COLOR_BGR2GRAY)

    paths = glob.glob(&quot;./data/*&quot;)
    pool = multiprocessing.Pool()
    result = pool.map(job, paths)

    print 'Finished'

    for value in result:
        print value"><pre class="notranslate">import cv2
import multiprocessing
import glob
import numpy

def job(path):

    image = cv2.imread(path)
    image = cv2.cvtColor(image, cv2.<span class="pl-c1">COLOR_BGR2GRAY</span>)

    return path

<span class="pl-k">if</span> __name__ == "<span class="pl-s">__main__</span>":

    main_image = cv2.imread("<span class="pl-s">./image.png</span>")
    main_image = cv2.cvtColor( main_image, cv2.<span class="pl-c1">COLOR_BGR2GRAY</span>)

    paths = glob.glob("<span class="pl-s">./data/*</span>")
    pool = multiprocessing.<span class="pl-v">Pool</span>()
    result = pool.map(job, paths)

    print <span class="pl-s">'Finished'</span>

    for value in result:
        print value</pre></div>
<p dir="auto">If I remove <code class="notranslate">main_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)</code> script works, but with it in there it doesn't, even though that line shouldn't affect jobs processed by the pool.<br>
All image paths lead to simple images, about ~20 of them in total.<br>
Funny enough code works fine if I create images in memory with <code class="notranslate">numpy</code> instead of reading them with <code class="notranslate">imread()</code>.<br>
I'm guessing OpenCV uses some shared variables behind the scene that aren't protected from race conditions.</p>
<p dir="auto">My environment: Mac OS X 10.10, OpenCV 3.0.0, Python 2.7.<br>
The last few lines of stack trace are:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Application Specific Information:
crashed on child side of fork pre-exec

Thread 0 Crashed:: Dispatch queue: com.apple.root.default-qos
0   libdispatch.dylib               0x00007fff8668913f dispatch_apply_f + 769
1   libopencv_core.3.0.dylib        0x000000010ccebd14 cv::parallel_for_(cv::Range const&amp;, cv::ParallelLoopBody const&amp;, double) + 152
2   libopencv_imgproc.3.0.dylib     0x000000010c8b782e void cv::CvtColorLoop&lt;cv::RGB2Gray&lt;unsigned char&gt; &gt;(cv::Mat const&amp;, cv::Mat&amp;, cv::RGB2Gray&lt;unsigned char&gt; const&amp;) + 134
3   libopencv_imgproc.3.0.dylib     0x000000010c8b1fd4 cv::cvtColor(cv::_InputArray const&amp;, cv::_OutputArray const&amp;, int, int) + 23756
4   cv2.so                          0x000000010c0ed439 pyopencv_cv_cvtColor(_object*, _object*, _object*) + 687
5   org.python.python               0x000000010bc64968 PyEval_EvalFrameEx + 19480
6   org.python.python               0x000000010bc5fa42 PyEval_EvalCodeEx + 1538\"><pre class="notranslate"><code class="notranslate">Application Specific Information:
crashed on child side of fork pre-exec

Thread 0 Crashed:: Dispatch queue: com.apple.root.default-qos
0   libdispatch.dylib               0x00007fff8668913f dispatch_apply_f + 769
1   libopencv_core.3.0.dylib        0x000000010ccebd14 cv::parallel_for_(cv::Range const&amp;, cv::ParallelLoopBody const&amp;, double) + 152
2   libopencv_imgproc.3.0.dylib     0x000000010c8b782e void cv::CvtColorLoop&lt;cv::RGB2Gray&lt;unsigned char&gt; &gt;(cv::Mat const&amp;, cv::Mat&amp;, cv::RGB2Gray&lt;unsigned char&gt; const&amp;) + 134
3   libopencv_imgproc.3.0.dylib     0x000000010c8b1fd4 cv::cvtColor(cv::_InputArray const&amp;, cv::_OutputArray const&amp;, int, int) + 23756
4   cv2.so                          0x000000010c0ed439 pyopencv_cv_cvtColor(_object*, _object*, _object*) + 687
5   org.python.python               0x000000010bc64968 PyEval_EvalFrameEx + 19480
6   org.python.python               0x000000010bc5fa42 PyEval_EvalCodeEx + 1538\
</code></pre></div>
<p dir="auto">BTW - I got other OpenCV functions to crash when used with Python multiprocessing too, above is just the smallest example I could produce that reflects the problem.</p>
<p dir="auto">Also, I got above algorithm (and much more complicated ones) to work in multithreaded C++ programs, using same OpenCV build on same machine, so I guess the issue lies on Python bindings side.</p>