module 'cv2' has no attribute 'hist'
时间: 2023-09-17 22:10:20 浏览: 79
This error usually occurs when you try to use the function 'hist' from the OpenCV (cv2) library, but the function is not recognized.
There could be a few reasons for this error, such as:
1. You might have misspelled the function name.
2. The function might not be available in the version of OpenCV you are using.
3. The function might not be imported correctly.
To resolve this error, you can try the following solutions:
1. Double-check the spelling of the function name and make sure it is correct.
2. Upgrade to a newer version of OpenCV that includes the 'hist' function.
3. Import the function correctly by using the following code:
```python
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('image.jpg', 0)
hist = cv2.calcHist([img],[0],None,[256],[0,256])
plt.hist(img.ravel(),256,[0,256])
plt.show()
```
This code imports the 'cv2' library and the 'pyplot' module from matplotlib. It then reads an image and calculates the histogram using the 'calcHist' function. Finally, it displays the histogram using the 'hist' function from pyplot.
阅读全文