简体   繁体   English

不使用反斜杠时,cv2 imread 不返回任何值

[英]cv2 imread returns none when not using backslash

Hi I want to convert an image into grayscale based on alpha values but cv2 imread function returns following error:嗨,我想根据 alpha 值将图像转换为灰度,但 cv2 imread 函数返回以下错误:

TypeError: 'NoneType' object is not subscriptable TypeError: 'NoneType' 对象不可下标

import cv2
import matplotlib.pyplot as plt

img = cv2.imread('C:/Users/sgunes/Downloads/Cyrillic/Ж/5a793d87b7ef9.png', cv2.IMREAD_UNCHANGED)
img_gray = 255 - img[:, :, 3]

plt.imshow(img_gray, cmap='gray', vmin=0, vmax=255)
plt.show()

And when I debug it, my img objects gets None.当我调试它时,我的 img 对象没有。 How do I fix it?我如何解决它? Thanks for your attention.感谢您的关注。

According to OpenCV documentation :根据OpenCV 文档

The function imread loads an image from the specified file and returns it.函数 imread 从指定文件加载图像并返回它。 If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL ).如果无法读取图像(由于文件丢失、权限不正确、格式不受支持或无效),该函数将返回一个空矩阵 ( Mat::data==NULL )。

So that's mean that either you entered a wrong path, or that you don't have permissions, or that the image is not readable.这意味着您输入了错误的路径,或者您没有权限,或者图像不可读。

From your title, maybe it is a wrong path issue.从你的标题来看,可能是路径错误的问题。 If you want to use backslash, you can add an 'r' before your string (mean raw), or escaping each backslash (so '\\' become '\\\\').如果你想使用反斜杠,你可以在你的字符串之前添加一个 'r'(意思是原始的),或者转义每个反斜杠(所以 '\\' 变成 '\\\\')。 Note that if you use raw string (r''), you still can't finish your string by a backslash.请注意,如果您使用原始字符串 (r''),您仍然无法以反斜杠结束字符串。

img = cv2.imread(r'C:\Users\sgunes\Downloads\Cyrillic\Ж\5a793d87b7ef9.png', cv2.IMREAD_UNCHANGED)

or或者

img = cv2.imread('C:\\\\Users\\\\sgunes\\\\Downloads\\\\Cyrillic\\\\Ж\\\\5a793d87b7ef9.png', cv2.IMREAD_UNCHANGED)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM