Python OpenCV错误:"TypeError:图像数据无法转换为浮点数" [英] Python OpenCV Error: "TypeError: Image data cannot be converted to float"

查看:462
本文介绍了Python OpenCV错误:"TypeError:图像数据无法转换为浮点数"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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.

推荐答案

您实际上不是在读取图像.

You're not actually reading in an image.

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

Check out what happens if you try to display None in 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  

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

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

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

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

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 None and not reading properly.

这篇关于Python OpenCV错误:"TypeError:图像数据无法转换为浮点数"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆