我在 Python 2.7.12 中使用 Canny 边缘检测算法和图像(.jpg)
我使用了以下代码:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('35.jpg')
edges = cv2.Canny(img,150,200)
plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(edges,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.show()
但这向我显示了错误,例如
Traceback (most recent call last):
File "D:/python/test4.py", line 11, in <module>
plt.subplot(121),plt.imshow(img,cmap = 'gray')
File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 3157, in imshow
**kwargs)
File "C:\Python27\lib\site-packages\matplotlib\__init__.py", line 1898, in inner
return func(ax, *args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\axes\_axes.py", line 5124, in imshow
im.set_data(X)
File "C:\Python27\lib\site-packages\matplotlib\image.py", line 596, in set_data
raise TypeError("Image data can not convert to float")
TypeError: Image data can not convert to float
我看到了很多与此相关的答案,但我无法纠正它。我还观察到,如果我将相同的图像保存为 .png,则代码运行完美。我需要在代码中进行哪些修改才能运行 .jpg 文件?谢谢。