这是我的python代码。它的目标是从文件系统中获取两个图像,然后根据图像分析生成图。
我安装了Anaconda软件包,我在Windows 10(Python 2.7)的iPython上工作。
当我运行我的代码时,我得到:AttributeError:' NoneType'对象没有属性' is_interactive'
我确保文件系统中存在包含我的图像的文件夹。
# Bring in necessary libraries
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
from skimage import color
import skimage.filters as filters
from skimage.transform import hough_circle
from skimage.feature import peak_local_max
from skimage import feature
from skimage import morphology
from skimage.draw import circle_perimeter
from skimage import img_as_float, img_as_ubyte
from skimage import segmentation as seg
from skimage.morphology import watershed
from scipy import ndimage as nd
from scipy.ndimage import convolve
from skimage import feature
import glob # for bulk file import
# Set defaults
plt.rcParams['image.cmap'] = 'gray' # Display grayscale images in... grayscale.
plt.rcParams['image.interpolation'] = 'none' # Use nearest-neighbour
plt.rcParams['figure.figsize'] = 10, 10
# Import test images
imgpaths = glob.glob("C:\\images\\ * .jpg") + glob.glob("C:\\images\\ * .png")
imgset = [img_as_ubyte(mpimg.imread(x)) for x in imgpaths]
# Display thumbnails of the images to ensure loading
plt.figure()
for i,img in enumerate(imgset):
plt.subplot(1, len(imgset), i+1)
plt.imshow(img, cmap = 'gray')
# Plots a histogram of the image, splitting into individual channels if necessary.
def plot_multichannel_histo(img):
if img.ndim == 2: # plot grayscale histo
plt.hist(img.flatten(), 256, range=(0,255), color='k', histtype='step')
elif img.ndim == 3: # print rgb histo
plt.hist(img.reshape(-1,3), 256, range=(0,255), color=['r','g','b'],histtype='step')
else: # Not an image
print("Must pass a valid RGB or grayscale image")
plt.xlim([0,255])
# Apply to test images
for i,img in enumerate(imgset):
plt.figure()
plt.subplot(1, 2, 1)
plt.imshow(img, cmap='gray')
plt.subplot(1, 2, 2)
plot_multichannel_histo(img)
iPython吐了出来:
---------------------------------------------------------------------------
UnknownBackend Traceback (most recent call last)
<ipython-input-1-bcf46d40fe9f> in <module>()
1 # Bring in necessary libraries
----> 2 get_ipython().magic(u'matplotlib inline')
3 import matplotlib.pyplot as plt
4 import matplotlib.image as mpimg
5 import numpy as np
C:\Users\Peter\Anaconda2\lib\site-packages\IPython\core\interactiveshell.pyc in magic(self, arg_s)
2156 magic_name, _, magic_arg_s = arg_s.partition(' ')
2157 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2158 return self.run_line_magic(magic_name, magic_arg_s)
2159
2160 #-------------------------------------------------------------------------
C:\Users\Peter\Anaconda2\lib\site-packages\IPython\core\interactiveshell.pyc in run_line_magic(self, magic_name, line)
2077 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
2078 with self.builtin_trap:
-> 2079 result = fn(*args,**kwargs)
2080 return result
2081
<decorator-gen-105> in matplotlib(self, line)
C:\Users\Peter\Anaconda2\lib\site-packages\IPython\core\magic.pyc in <lambda>(f, *a, **k)
186 # but it's overkill for just that one bit of state.
187 def magic_deco(arg):
--> 188 call = lambda f, *a, **k: f(*a, **k)
189
190 if callable(arg):
C:\Users\Peter\Anaconda2\lib\site-packages\IPython\core\magics\pylab.pyc in matplotlib(self, line)
98 print("Available matplotlib backends: %s" % backends_list)
99 else:
--> 100 gui, backend = self.shell.enable_matplotlib(args.gui)
101 self._show_matplotlib_backend(args.gui, backend)
102
C:\Users\Peter\Anaconda2\lib\site-packages\IPython\core\interactiveshell.pyc in enable_matplotlib(self, gui)
2945 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
2946
-> 2947 pt.activate_matplotlib(backend)
2948 pt.configure_inline_support(self, backend)
2949
C:\Users\Peter\Anaconda2\lib\site-packages\IPython\core\pylabtools.pyc in activate_matplotlib(backend)
292 matplotlib.rcParams['backend'] = backend
293
--> 294 import matplotlib.pyplot
295 matplotlib.pyplot.switch_backend(backend)
296
C:\Users\Peter\Anaconda2\lib\site-packages\matplotlib\pyplot.py in <module>()
2533 # are no-ops and the registered function respect `mpl.is_interactive()`
2534 # to determine if they should trigger a draw.
-> 2535 install_repl_displayhook()
2536
2537 ################# REMAINING CONTENT GENERATED BY boilerplate.py ##############
C:\Users\Peter\Anaconda2\lib\site-packages\matplotlib\pyplot.py in install_repl_displayhook()
164 ipython_gui_name = backend2gui.get(get_backend())
165 if ipython_gui_name:
--> 166 ip.enable_gui(ipython_gui_name)
167 else:
168 _INSTALL_FIG_OBSERVER = True
C:\Users\Peter\Anaconda2\lib\site-packages\IPython\terminal\interactiveshell.pyc in enable_gui(self, gui)
450 def enable_gui(self, gui=None):
451 if gui:
--> 452 self._inputhook = get_inputhook_func(gui)
453 else:
454 self._inputhook = None
C:\Users\Peter\Anaconda2\lib\site-packages\IPython\terminal\pt_inputhooks\__init__.pyc in get_inputhook_func(gui)
36
37 if gui not in backends:
---> 38 raise UnknownBackend(gui)
39
40 if gui in aliases:
UnknownBackend: No event loop integration for 'inline'. Supported event loops are: qt, qt4, qt5, gtk, gtk2, gtk3, tk, wx, pyglet, glut, osx
Error in callback <function post_execute at 0x0000000006E06908> (for post_execute):
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
C:\Users\Peter\Anaconda2\lib\site-packages\matplotlib\pyplot.py in post_execute()
146
147 def post_execute():
--> 148 if matplotlib.is_interactive():
149 draw_all()
150
AttributeError: 'NoneType' object has no attribute 'is_interactive'
Error in callback <function post_execute at 0x0000000006E06908> (for post_execute):
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
C:\Users\Peter\Anaconda2\lib\site-packages\matplotlib\pyplot.py in post_execute()
146
147 def post_execute():
--> 148 if matplotlib.is_interactive():
149 draw_all()
150
AttributeError: 'NoneType' object has no attribute 'is_interactive'