使用cv2.threshold函数时提示TypeError: Image data of dtype object cannot be converted to float

这篇博客主要介绍了在使用OpenCV进行图像处理时遇到的TypeError问题。在将灰度图像进行二值化处理时,由于忽视了`cv2.threshold`函数的返回值是一个元组,导致了错误。正确的做法是为返回值提供两个变量来分别接收。通过修正代码,成功展示了原始图像和二值化后的图像,对于理解和解决类似问题有很好的指导作用。
img_3 = cv2.imread(r'D:\Jupyter Notebook Files\cat.jpg')
img_gray = cv2.cvtColor(img_3, cv2.COLOR_BGR2GRAY)
img_threshold = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)
plt.subplot(1,2,1),plt.title('img_gray'), plt.axis('off'),plt.imshow(img_gray)
plt.subplot(1,2,2),plt.title('img_threshold'), plt.axis('off'),plt.imshow(img_threshold, 'gray')
plt.show()

原输入如上图所示,此时提示:

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

这是因为cv2.threshold函数的返回值有两个,而上图代码只给了一个变量接收,正确写法如下:

img_3 = cv2.imread(r'D:\Jupyter Notebook Files\cat.jpg')
img_gray = cv2.cvtColor(img_3, cv2.COLOR_BGR2GRAY)
-, img_threshold = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)
plt.subplot(1,2,1),plt.title('img_gray'), plt.axis('off'),plt.imshow(img_gray)
plt.subplot(1,2,2),plt.title('img_threshold'), plt.axis('off'),plt.imshow(img_threshold, 'gray')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值