Issue 1:
System information (version)

OpenCV => 4.2.0.34

Operating System / Platform => macOS 10.15.5

Compiler => Not sure, using Python 3.7.3 via Anaconda

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

Detailed description

Maybe related to #16030. I'm getting a segmentation fault, when calling warpAffine or warpPerspective from a background worker. Calling it from the foreground thread works fine, but the very same operation fails on the background thread.

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.

Steps to reproduce

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("Success warpPerspective " + 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("Success warpAffine " + 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)))

Issue submission checklist

I report the issue, it's not a question

I checked the problem with documentation, FAQ, open issues,

answers.opencv.org, Stack Overflow, etc and have not found solution

I updated to latest OpenCV version and the issue is still there

There is reproducer code and related data files: videos, images, onnx, etc

Issue 2:
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:

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__ == "__main__":

main_image = cv2.imread("./image.png")
main_image = cv2.cvtColor( main_image, cv2.COLOR_BGR2GRAY)

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

print 'Finished'

for value in result:
print value

If I remove main_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) script works, but with it in there it doesn't, even though that line shouldn't affect jobs processed by the pool.

All image paths lead to simple images, about ~20 of them in total.

Funny enough code works fine if I create images in memory with numpy instead of reading them with imread().

I'm guessing OpenCV uses some shared variables behind the scene that aren't protected from race conditions.

My environment: Mac OS X 10.10, OpenCV 3.0.0, Python 2.7.

The last few lines of stack trace are:

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&, cv::ParallelLoopBody const&, double) + 152
2 libopencv_imgproc.3.0.dylib 0x000000010c8b782e void cv::CvtColorLoop<cv::RGB2Gray<unsigned char> >(cv::Mat const&, cv::Mat&, cv::RGB2Gray<unsigned char> const&) + 134
3 libopencv_imgproc.3.0.dylib 0x000000010c8b1fd4 cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, 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\

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.

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.