Python OpenCV 错误:“类型错误:图像数据无法转换为浮点数”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47704895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:19:47  来源:igfitidea点击:

Python OpenCV Error: "TypeError: Image data cannot be converted to float"

pythonopencv

提问by alkasm

So I am trying to create a Python Program to detect similar details in two images using Python's OpenCV. I have the two images and they are in my current directory, and they exist (see the code in lines 6-17). But I am getting the following error when I try running it.

所以我试图创建一个 Python 程序来使用 Python 的 OpenCV 检测两个图像中的相似细节。我有两个图像,它们在我的当前目录中,并且它们存在(参见第 6-17 行中的代码)。但是当我尝试运行它时出现以下错误。

import numpy as np
import matplotlib.pyplot as plt
import cv2
import os

path1 = "WIN_20171207_13_51_33_Pro.jpg"
path2 = "WIN_20171207_13_51_43_Pro.jpg"

if os.path.isfile(path1):
    img1 = cv2.imread('WIN_20171207_13_51_33_Pro.jpeg',0)
else:
    print ("The file " + path1 + " does not exist.")

if os.path.isfile(path2):
    img2 = cv2.imread('WIN_20171207_13_51_43_Pro.jpeg',0)
else:
    print ("The file " + path2 + " does not exist.")

orb = cv2.ORB_create()

kpl1, des1 = orb.detectAndCompute(img1,None)
kpl2, des2 = orb.detectAndCompute(img2,None)

bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

matches = bf.match(des1, des2)
matches = sorted(matches, key=lambda x:x.distance)

img3 = cv2.drawMatches(img1,kpl1,img2,kpl2,matches[:10],None, flags=2)

plt.imshow (img3)
plt.show()

Here is the error I keep on getting...

这是我不断收到的错误...

Traceback (most recent call last):


File "C:\Users\jweir\source\repos\BruteForceFeatureDetection\BruteForceFeatureDetection\BruteForceFeatureDetection.py", line 31, in <module>
    plt.imshow (img3)
  File "C:\Program Files\Python36\lib\site-packages\matplotlib\pyplot.py", line 3080, in imshow
    **kwargs)
  File "C:\Program Files\Python36\lib\site-packages\matplotlib\__init__.py", line 1710, in inner
    return func(ax, *args, **kwargs)
  File "C:\Program Files\Python36\lib\site-packages\matplotlib\axes\_axes.py", line 5194, in imshow
    im.set_data(X)
  File "C:\Program Files\Python36\lib\site-packages\matplotlib\image.py", line 600, in set_data
    raise TypeError("Image data cannot be converted to float")
TypeError: Image data cannot be converted to float

Can someone please explpain to me why I am getting this error, what it means, and how to fix it.

有人可以向我解释为什么我会收到这个错误,它意味着什么,以及如何解决它。

回答by alkasm

You're not actually reading in an image.

您实际上并不是在阅读图像。

Check out what happens if you try to display Nonein matplotlib:

查看如果您尝试None在 matplotlib 中显示会发生什么:

plt.imshow(None)
Traceback (most recent call last):  

 File ".../example.py", line 16, in <module>  
   plt.imshow(None)  
 File ".../matplotlib/pyplot.py", line 3157, in imshow  
   **kwargs)  
 File ".../matplotlib/__init__.py", line 1898, in inner  
   return func(ax, *args, **kwargs)  
 File ".../matplotlib/axes/_axes.py", line 5124, in imshow  
   im.set_data(X)  
 File ".../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  
Traceback (most recent call last):  

 File ".../example.py", line 16, in <module>  
   plt.imshow(None)  
 File ".../matplotlib/pyplot.py", line 3157, in imshow  
   **kwargs)  
 File ".../matplotlib/__init__.py", line 1898, in inner  
   return func(ax, *args, **kwargs)  
 File ".../matplotlib/axes/_axes.py", line 5124, in imshow  
   im.set_data(X)  
 File ".../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  

You're reading WIN_20171207_13_51_33_Pro.jpegbut you're checking if WIN_20171207_13_51_33_Pro.jpgexists. Note the different extensions. Why do you have the filename written twice (and differently)? Just simply write:

您正在阅读,WIN_20171207_13_51_33_Pro.jpeg但正在检查是否WIN_20171207_13_51_33_Pro.jpg存在。注意不同的扩展名。为什么你把文件名写了两次(而且不同)?只需简单地写:

if os.path.isfile(path1):
    img1 = cv2.imread(path1, 0)
else:
    print ("The file " + path1 + " does not exist.")

Note that even if you put a bogus file into cv2.imread(), the resulting image will just be None, which doesn't error in any of the subsequent function calls until matplotlib tries to draw it. If you print(img1)after reading, you'll see it's Noneand not reading properly.

请注意,即使您将伪造的文件放入cv2.imread(),生成的图像也只是None,在 matplotlib 尝试绘制它之前,在任何后续函数调用中都不会出错。如果你print(img1)在阅读后,你会看到它,None而不是正确阅读。

回答by vedant dave

Please check your file name or path. Some time during uploading image or at the time of saving the extension .jpg or .png automatically choosen by system. so also include it in imshow() command.

请检查您的文件名或路径。在上传图像或保存扩展名时的某些时间,系统会自动选择 .jpg 或 .png。所以也将它包含在 imshow() 命令中。