使用numpy和matplotlib显示生成的图像时出现非法指令(core dumped)消息

时间:2016-08-22 15:46:18

标签: python numpy image-processing matplotlib scipy

运行以下代码段时,最后两行代码plt.imshow(X[0,:,:]) plt.show()会一直生成错误消息Illegal instruction (core dumped) X形状为(1, 572, 572)。我可以知道这可能是什么原因吗?

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()

1 个答案:

答案 0 :(得分:0)

对我来说很好。尝试升级和/或重新安装库和/或Python。

如果仍然失败,请考虑使用gdb获取堆栈跟踪(提示:gdb /path/to/your/python)。然后按照@nneonneo的建议,向matplotlib提交bug report