【问题标题】:dtype object cannot be converted to float matplotlib [closed]dtype 对象无法转换为浮点 matplotlib [关闭]
【发布时间】:2021-08-13 00:23:57
【问题描述】:

我一直在尝试运行以下代码

import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread("G:\myfiles\frames\frame1.jpg",0)
image = [img]
for i in range(1):
    plt.subplot(1, 1, i+1), plt.imshow(image[i], 'gray')
    plt.xticks([]),plt.yticks([])

plt.show()

我收到以下错误

    TypeError                                 Traceback (most recent call last)
<ipython-input-29-b2519be4ee9a> in <module>
      1 image = [img]
      2 for i in range(1):
----> 3     plt.subplot(1, 1, i+1), plt.imshow(image[i], 'gray')
      4     plt.xticks([]),plt.yticks([])
      5 

E:\anaconda\programme files\lib\site-packages\matplotlib\pyplot.py in imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, filternorm, filterrad, resample, url, data, **kwargs)
   2728         filternorm=filternorm, filterrad=filterrad, resample=resample,
   2729         url=url, **({"data": data} if data is not None else {}),
-> 2730         **kwargs)
   2731     sci(__ret)
   2732     return __ret

E:\anaconda\programme files\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
   1445     def inner(ax, *args, data=None, **kwargs):
   1446         if data is None:
-> 1447             return func(ax, *map(sanitize_sequence, args), **kwargs)
   1448 
   1449         bound = new_sig.bind(ax, *args, **kwargs)

E:\anaconda\programme files\lib\site-packages\matplotlib\axes\_axes.py in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, filternorm, filterrad, resample, url, **kwargs)
   5521                               resample=resample, **kwargs)
   5522 
-> 5523         im.set_data(X)
   5524         im.set_alpha(alpha)
   5525         if im.get_clip_path() is None:

E:\anaconda\programme files\lib\site-packages\matplotlib\image.py in set_data(self, A)
    701                 not np.can_cast(self._A.dtype, float, "same_kind")):
    702             raise TypeError("Image data of dtype {} cannot be converted to "
--> 703                             "float".format(self._A.dtype))
    704 
    705         if self._A.ndim == 3 and self._A.shape[-1] == 1:

TypeError: Image data of dtype object cannot be converted to float

这是具体的错误,好像是matplotlib导致的错误

TypeError: Image data of dtype object cannot be converted to float

为什么会出现此错误,对此有什么可能的解决方法?

【问题讨论】:

  • 这种问题已经问过好几次了,但都是零回答,如果有人提供解决方案会有所帮助
  • 文件路径中未转义的反斜杠,没有测试imread是否成功。

标签: python opencv matplotlib


【解决方案1】:

您需要转义路径中的反斜杠,因为反斜杠是用于转义特殊字符的特殊字符。

你可以试试:

img = cv2.imread("G:\\myfiles\\frames\\frame1.jpg", 0)

或者你可以使用r-string:

img = cv2.imread(r"G:\myfiles\frames\frame1.jpg", 0)

最后,您可以尝试使用正斜杠而不是反斜杠:

img = cv2.imread("G:/myfiles/frames/frame1.jpg", 0)

【讨论】:

    猜你喜欢
    • 2014-11-15
    • 2017-09-15
    • 2018-02-28
    • 2020-09-06
    相关资源
    最近更新 更多