python opencv (cv2) in blender I am using stock Ubuntu 16.04, blender 2.76 install. I am trying to use the python opencv (cv2) module from within blender to do some image processing on a texture.

I have installed opencv with the python 3 cv2 library. The following executes without error

python3 -c "import cv2; cv2.imread('img.png')"


But if I enter the same thing into the blender python console, or call it from within a script and run that script using 

blender --python &lt;script.py&gt; 


Then blender immediately segfaults and dumps a crash report file with the following backtrace


# backtrace
blender(BLI_system_backtrace+0x30) [0x1361b10]
blender() [0x97a98e]
/lib/x86_64-linux-gnu/libc.so.6(+0x354b0) [0x7fc443cb94b0]
/usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4(_ZNK2cv3Mat6copyToERKNS_12_OutputArrayE+0x35) [0x7fc435ab8865]
/usr/local/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so(+0x813b6) [0x7fc4085373b6]
/usr/local/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so(+0x81e84) [0x7fc408537e84]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyCFunction_Call+0xe9) [0x7fc44aca51b9]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyEval_EvalFrameEx+0x7555) [0x7fc44adbf085]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyEval_EvalFrameEx+0x79d9) [0x7fc44adbf509]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(+0x24ac0c) [0x7fc44ae4fc0c]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyEval_EvalCodeEx+0x23) [0x7fc44ae4fce3]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(+0xd2a98) [0x7fc44acd7a98]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyObject_Call+0x6e) [0x7fc44ad7e8ee]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(+0x225a4c) [0x7fc44ae2aa4c]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyObject_Call+0x6e) [0x7fc44ad7e8ee]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(+0x7e279) [0x7fc44ac83279]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(+0x83276) [0x7fc44ac88276]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyObject_Call+0x6e) [0x7fc44ad7e8ee]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyEval_EvalFrameEx+0x1ccf) [0x7fc44adb97ff]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(+0x24ac0c) [0x7fc44ae4fc0c]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyEval_EvalCodeEx+0x23) [0x7fc44ae4fce3]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyEval_EvalCode+0x1b) [0x7fc44adb789b]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyRun_FileExFlags+0xb2) [0x7fc44add4bc2]
blender() [0xd786b5]
blender() [0x97b1d5]
blender(BLI_argsParse+0xe5) [0x1315e85]
blender(main+0xd77) [0x95cc87]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0) [0x7fc443ca4830]
blender(_start+0x29) [0x97a2b9]


This is the log file if run using LD_DEBUG=files blender --python script.py &gt; blender.log 2&gt;&amp;1 as I saw on another link about segfaults in blender that I have now lost


32577:  opening file=/lib/x86_64-linux-gnu/libgcc_s.so.1 [0]; direct_opencount=2
32577:  


After reading https://developer.blender.org/T43869 and https://developer.blender.org/T43491#288351 I wrote a little python script to diff the output of ldd for both the cv2.so and the blender binary to see if I could see any miss matched shared libraries ... but both cv2.so and the blender binary seem to use the same .so files where there are commonalities

So I am a bit out of ideas at this point. Also I wondering why both blender and opencv rely on libgcc_s.so, is it something to do with pthreads? ... Anyway if anyone has got this working or has made more progress than me it would be great to hear from you .. any help would be much appreciated :)
Today I found an easy way to start using OpenCV in Blender without any (manual) compilation steps. My OS is Ubuntu 18.04 (64bit) and I run Blender 2.79b. 

It may be also important to say, that I did not installed Blender from repository (e.g. apt), but simply downloaded the archive and extracted in a sub-folder of my home directory.

I performed following steps to start using OpenCV:


Open Terminal
Change directory (cd) to [blender]/2.79/python/bin
Executed ./python3.5m -m ensurepip (to become pip available)
Executed ./python3.5m -m pip install --upgrade pip (upgrade pip)
Executed ./python3.5m -m pip install opencv-python (Though an unofficial build the version I got was 3.4.2.17)
Executed ./python3.5m -m pip install opencv-contrib-python (I needed SIFT features and they are only available in the contrib package)


After these commands were executed I started Blender and performed a test where I extracted SIFT features from one Image and wrote an augmented result image to filesystem. Worked without issues.

It is that easy, that I would expect it somewhere to be written down in the internet, but unfortunately I have found only instructions which are far more complicated.

Hope this help someone!