【问题标题】:ImportError: No module named pyplot - OpencvImportError:没有名为 pyplot 的模块 - Opencv
【发布时间】:2016-07-14 14:02:07
【问题描述】:

我正在尝试执行以下代码:

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('Paw01.png',0)
img = cv2.medianBlur(img,5)

ret,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
                        cv2.THRESH_BINARY,11,2)
th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
                        cv2.THRESH_BINARY,11,2)
titles = ['Original Image', 'Global Thresholding (v = 127)',
      'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [img, th1, th2, th3]
for i in xrange(4):
    plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])
    plt.show()`

但是,它返回错误:

Traceback(最近一次通话最后):文件“/home/lrcorre/Desktop/Paper SIBGRAPI/OpenCV_Programs/Thresholding.py",第 3 行,在 将 matplotlib.pyplot 导入为 plt 文件“/home/lrcorre/Desktop/Paper SIBGRAPI/OpenCV_Programs/matplotlib.py”, 第 3 行,在 将 matplotlib.pyplot 导入为 plt ImportError: 没有名为 pyplot 的模块

我已经安装了matplotlib、Opencv 和python 2.7。 有谁知道我该如何解决这个问题并继续?

【问题讨论】:

    标签: python opencv matplotlib threshold


    【解决方案1】:

    matplotlib 是 python 绘图库目录名。在那个目录下有一个像这样的文件pylot.py。

    /dir1/dir2/.../site-packages/matplotlib/pyplot.py
    

    在您的情况下,您在 PYTHONPATH 目录的某处定义了 matplotlib.py。

     /home/lrcorre/Desktop/PaperSIBGRAPI/OpenCV_Programs/matplotlib.py
    

    这禁止导入你想要的 pyplot.py 文件。因此,将文件名更改为不同的名称。

    【讨论】:

    • 感谢答案!它现在运行!
    【解决方案2】:

    确保您使用的是 python 2.7 版本:

    import sys
    
    print(sys.version)  # parentheses necessary in python 3. 
    

    【讨论】:

      【解决方案3】:

      确保您要导入模块的文件未命名为 matplotlib.py。它为我解决了这个问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-11
        • 2012-12-07
        • 2012-05-23
        • 2019-07-29
        • 2015-07-04
        • 2014-03-15
        • 2017-12-29
        • 2014-09-12
        相关资源
        最近更新 更多