I want to know why you are giving this error in the variable?

0
import numpy as np      # conda install numpy
import cv2 as cv        # conda install opencv
import matplotlib.pyplot as plt
#from PIL import Image  # para rodar no console
def showfig(image, ucmap):
    imgplot=plt.imshow(image, ucmap)

    img = cv.imread("imgs/pfig01.jpg",0)
hf = cv.calcHist([img],[0],None,[256],[0,256])

plt.figure(figsize=(13,5)) 
plt.subplot(121),plt.imshow(img,"gray"),plt.title('Input')
plt.subplot(122),plt.plot(hf),plt.title('Hist')
plt.show()

You're giving this error

NameError                                 Traceback (most recent call last)
<ipython-input-15-03db2af2cf65> in <module>()
      7 
      8     img = cv.imread("imgs/pfig01.jpg",0)
----> 9 hf = cv.calcHist([img],[0],None,[256],[0,256])
     10 
     11 plt.figure(figsize=(13,5))

NameError: name 'img' is not defined
    
asked by anonymous 23.10.2018 / 16:43

1 answer

0

img is a local variable, that is, it exists only within the function that implements it. It is created when the showfig function is called and ceases to exist when the function is terminated. Declare the variable out of the scope of the function so that it can be used globally.

    
23.10.2018 / 16:54