3
votes

I am trying to execute the following code:

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

But, It returns the error:

Traceback (most recent call last): File "/home/lrcorre/Desktop/Paper SIBGRAPI/OpenCV_Programs/Thresholding.py", line 3, in import matplotlib.pyplot as plt File "/home/lrcorre/Desktop/Paper SIBGRAPI/OpenCV_Programs/matplotlib.py", line 3, in import matplotlib.pyplot as plt ImportError: No module named pyplot

I already installed matplotlib, Opencv and python 2.7. Anyone know how can I fix this problem and proceed?

3

3 Answers

0
votes

matplotlib is python plotting library directory name. Under that directory there is a file pylot.py like this.

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

In your case you have matplotlib.py defined somewhere along your PYTHONPATH directory.

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

This prohibits importing the pyplot.py file you want. So change the file name to something different.

0
votes

Make sure that you use python 2.7 version:

import sys

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

Make sure the file you are importing the module in, is not named matplotlib.py. It solved the problem for me.