user297850 user297850 - 3 months ago 24
Python Question

illegal instruction (core dumped) message when displaying generated image using numpy and matplotlib

When running the following code segment, the last two lines of code

plt.imshow(X[0,:,:]) plt.show()
keep generating the error message of
Illegal instruction (core dumped)
The X shape is
(1, 572, 572)
. May I know what can be the reason of this?

import os
import numpy as np
import matplotlib.pyplot as plt
import pylab
from scipy.ndimage.filters import gaussian_filter
from scipy import ndimage

np.random.seed(1234)
pylab.rcParams['figure.figsize'] = (10.0, 8.0)

nx = 572
ny = 572
sigma = 10

plateau_min = -2
plateau_max = 2

r_min = 1
r_max = 200

def create_image_and_label(nx,ny):
x = np.int(np.random.rand(1)[0]*nx)
y = np.int(np.random.rand(1)[0]*ny)

image = np.ones((nx,ny))
label = np.ones((nx,ny))
image[x,y] = 0
image_distance = ndimage.morphology.distance_transform_edt(image)

r = np.random.rand(1)[0]*(r_max-r_min)+r_min
plateau = np.random.rand(1)[0]*(plateau_max-plateau_min)+plateau_min

label[image_distance <= r] = 0
label[image_distance > r] = 1
label = (1 - label)

image_distance[image_distance <= r] = 0
image_distance[image_distance > r] = 1
image_distance = (1 - image_distance)*plateau

image = image_distance + np.random.randn(nx,ny)/sigma

return image, label[92:nx-92,92:nx-92]

def create_batch(nx,ny,n_image):

X = np.zeros((n_image,nx,ny))
Y = np.zeros((n_image,nx-184,ny-184,2))

for i in range(n_image):
X[i,:,:],Y[i,:,:,1] = create_image_and_label(nx,ny)
Y[i,:,:,0] = 1-Y[i,:,:,1]

return X,Y

X,Y = create_batch(nx,ny,1)

print(X.shape)
plt.imshow(X[0,:,:])
plt.show()

Answer

Works fine for me. Try upgrading and/or reinstalling your libraries and/or Python.

If it still fails, consider using gdb to get a stack trace (hint: gdb /path/to/your/python). Then submit a bug report to matplotlib, as suggested by @nneonneo.