ImportError:没有名为pyplot的模块 - Opencv

时间:2016-03-27 02:33:57

标签: python opencv matplotlib threshold

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

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

但是,它会返回错误:

  

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

我已经安装了matplotlibOpencvpython 2.7。 任何人都知道如何解决这个问题并继续?

3 个答案:

答案 0 :(得分:0)

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

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

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

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

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

答案 1 :(得分:0)

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

import sys

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

答案 2 :(得分:0)

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

相关问题