You are not logged in.

#1 2013-02-12 19:27:50

Bladtman242
Member
Registered: 2012-02-14
Posts: 127

python segmentation fault with matplotlib and opencv2

Once again fellow archers, I am in need of your help.
I have a file, Test1.py, which uses cv2, pylab, and numpy.
Excluding either pylab or cv2 functions, I can make the rest of the script work, but when pylab is imported, calling cv2's imshow() function causes a "segmentation fault (core dumped)".

By the sound of it, I thought it might come from the underlying C stuff, rather than python.
So I ran it through gdb.
This is where I need help. I'm not sure how to interprete the backtrace, and I probably wouldn't know what to to if I were.
Perhaps a problem with glib or GObject?
Below are the python script in question (it a template for a school assignment), and the gdb trace.

Test1.py

import sys
sys.settrace
import numpy as np
import cv2 
import math
import pylab

#----------------------------Functions

def show1_OpenCV(image):
#    ##This function define a window by namedWindow() and then show the image in that window
    image=np.array(image)
#    cv2.namedWindow('ByOpenCV', cv2.WINDOW_AUTOSIZE)## create a window called 
#    #+'By OpenCV'Using the \nw{cv2.WINDOW_AUTOSIZE} parameter when defining a window display the image with its actual size in the window.
    cv2.imshow('ByOpenCV', image) ## show the image in 'By OpenCV' window
#    cv2.waitKey(0) ## the window will be closed with a (any)key press
def show2_OpenCV(*image):
    ## Showing the image using OpenCV
    im=[]
    for i in image:
        im.append(array(i))
    
    cv2.namedWindow('1', cv2.WINDOW_AUTOSIZE)## create a window called 'By OpenCV' Using the \nw{cv2.WINDOW_AUTOSIZE} parameter when defining a window display the image with its actual size in the window.
    cv2.imshow('1', im[0]) ## show the image in 'By OpenCV' window
    cv2.namedWindow('2', cv2.WINDOW_AUTOSIZE)
    cv2.imshow('2', im[1]) 
    cv2.waitKey(0) ## the window will be closed with a (any)key press

def showAll_OpenCV(**image):
	im = []
	for (k,v) in image.items():
		cv2.namedWindow(str(k), cv2.WINDOW_AUTOSIZE)
		cv2.imshow(k, np.array(v))
	cv2.waitKey()

def show1_pylab(image):
    ## Showing the image using pylab
    figure("By pylab")## Create a figure
    gray()
    title("1"); imshow(image)
    show()

def show2_pylab(*images):
    ## Showing the image using pylab
    figure("By pylab")## Create a figure
    gray()
    subplot(1,2,1);title("1"); imshow(images[0])## more about 'subplot()' : <http://www.scipy.org/Cookbook/Matplotlib/Multiple_Subplots_with_One_Axis_Label>
    subplot(1,2,2);title("2"); imshow(images[1])
    show()  

def grayLevelMap2(I,vector):
	I = np.copy(I)
	(x,y) = np.shape(I)
	for i in range(x):
		for j in range(y):
			I[i,j] = vector[I[i,j]]
	return I

def grayLevelMap(I,a,b):
	I = np.copy(I)
	(x,y) = np.shape(I)
	for i in range(x):
		for j in range(y):
			I[i,j] = max(0,min(255, a* I[i,j] +b))
	return I

def grayLevelMapF(I,f):
	I = np.copy(I)
	(x,y) = np.shape(I)
	for i in range(x):
		I[i] = map(f, I[i])
	return I

def f(x):
	func=(255-x)*math.sqrt(100/+1)
	return func

def displayVect(v):
	points=[]
	for i in range(len(v)):
		points.append((i,v[i]))
	bins = range(256)
	n = v
	grid(None, 'major', 'both')
	plot(bins, n, 'k-', linewidth=5)
	axis([-2, 256, -2, 256])
	show()


##------------------------------Main body

# Loading an image using openCV
I1=cv2.imread("children.tif") 
I2=cv2.imread("Eye1.jpg") 
I3=cv2.imread("Marker1.jpg") 
I4=cv2.imread("GreenTest.jpg") 
I1=cv2.cvtColor(I1, cv2.COLOR_RGB2GRAY)
I2=cv2.cvtColor(I2, cv2.COLOR_RGB2GRAY)
I3=cv2.cvtColor(I3, cv2.COLOR_RGB2GRAY)
I4=cv2.cvtColor(I4, cv2.COLOR_RGB2GRAY)

show1_OpenCV(grayLevelMapF(I4, lambda x: 2*x))
show1_OpenCV(I4)

gdb output:

Reading symbols from /usr/bin/python2...(no debugging symbols found)...done.
(gdb) run Test1.py
Starting program: /usr/bin/python2 Test1.py
warning: Could not load shared library symbols for linux-vdso.so.1.
Do you need "set solib-search-path" or "set sysroot"?
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
Traceback (most recent call last):
  File "/usr/share/gdb/auto-load/usr/lib/libgobject-2.0.so.0.3400.3-gdb.py", line 9, in <module>
    from gobject import register
  File "/usr/share/glib-2.0/gdb/gobject.py", line 3, in <module>
    import gdb.backtrace
ImportError: No module named backtrace
[New Thread 0x7fffdf259700 (LWP 3981)]
[ 
Program received signal SIGSEGV, Segmentation fault.
0x00000036cb6bc785 in do_warn () from /usr/lib/libpython2.7.so.1.0
(gdb) backtrace
#0  0x00000036cb6bc785 in do_warn () from /usr/lib/libpython2.7.so.1.0
#1  0x00000036cb6bcc4c in PyErr_WarnEx () from /usr/lib/libpython2.7.so.1.0
#2  0x00007fffe10153b5 in ?? ()
   from /usr/lib/python2.7/site-packages/gobject/_gobject.so
#3  0x00000036cde4ea00 in g_logv () from /usr/lib/libglib-2.0.so.0
#4  0x00000036cde4ebf2 in g_log () from /usr/lib/libglib-2.0.so.0
#5  0x00007fffef8e009e in gtk_disable_setlocale ()
   from /usr/lib/libgtk-x11-2.0.so.0
#6  0x00007ffff21ca8a8 in cvInitSystem ()
   from /usr/lib/libopencv_highgui.so.2.4
#7  0x00007ffff21cafb3 in cvNamedWindow ()
   from /usr/lib/libopencv_highgui.so.2.4
#8  0x00007ffff352dfe4 in ?? () from /usr/lib/python2.7/site-packages/cv2.so
#9  0x00000036cb6dc05a in PyEval_EvalFrameEx ()
   from /usr/lib/libpython2.7.so.1.0
#10 0x00000036cb6dba83 in PyEval_EvalFrameEx ()
   from /usr/lib/libpython2.7.so.1.0
#11 0x00000036cb6dcedd in PyEval_EvalCodeEx ()
---Type <return> to continue, or q <return> to quit---j
   from /usr/lib/libpython2.7.so.1.0
#12 0x00000036cb6dcfb2 in PyEval_EvalCode () from /usr/lib/libpython2.7.so.1.0
#13 0x00000036cb6f5eea in run_mod () from /usr/lib/libpython2.7.so.1.0
#14 0x00000036cb6f6ce2 in PyRun_FileExFlags ()
   from /usr/lib/libpython2.7.so.1.0
#15 0x00000036cb6f76fb in PyRun_SimpleFileExFlags ()
   from /usr/lib/libpython2.7.so.1.0
#16 0x00000036cb7089f2 in Py_Main () from /usr/lib/libpython2.7.so.1.0
#17 0x00007ffff7833a15 in __libc_start_main () from /usr/lib/libc.so.6
#18 0x0000000000400741 in _start ()
(gdb) run Test1.py

Last edited by Bladtman242 (2013-02-12 19:30:55)

Offline

#2 2013-02-15 07:43:06

szym
Member
Registered: 2007-11-16
Posts: 14

Re: python segmentation fault with matplotlib and opencv2

Similar (same?) issue here.

The crasher:

from matplotlib import pyplot as plt
import cv2
cv2.namedWindow('bam', 0)  # CRASH!

Workaround:

import cv2
cv2.namedWindow('bam', 0)
from matplotlib import pyplot as plt
cv2.namedWindow('bam bam', 0)  # No crash

Offline

#3 2013-02-15 16:13:55

Bladtman242
Member
Registered: 2012-02-14
Posts: 127

Re: python segmentation fault with matplotlib and opencv2

Thank you so much!
This (ugly) workaround might have saved me from a lot of trouble with this semester's exercises.
Just to be clear, the solution is:

  • Import opencv

  • Call namedWindow

  • Do the rest of the imports

  • Do the rest of the script

Corect?

I'm hesitant to mark the thread as solved, as the problem is still very much there, this workaround just allows us to ignore it.

Offline

Board footer

Powered by FluxBB