有人可以向我解释一下下面的代码不起作用吗?
myFolderdepth = 'C:\Users\owner\Desktop'; %Specify Directory to get image from
Depth = dir (fullfile(myFolderdepth,'shower_depth','*.png'))%%Get images from file named shower_depth
Depth_name = {Depth.name}'; %gets the name
figure;
imshow(Depth_name{3})
我收到的错误消息如下: 使用 getImageFromFile 时出错(第 11 行) 找不到指定的文件: “深度_003.png”。
我正在工作的目录是:C:\Users\owner\Desktop
图片名称为Depth_001,Depth_002,Depth_003,……
奇怪的是,我有另一个包含图像的文件夹,如果我将“shower_depth”更改为其他文件夹名称,它就可以正常工作。
谢谢! P.S 我做了一些进一步的实验,事实证明这是因为图像的命名方式;如果它的 Depth_01.png 没问题,它可以工作,但 Depth_001.png 不行
有人知道为什么吗?
最佳答案
以下命令:
Depth = dir (fullfile(myFolderdepth,'shower_depth','*.png'))
仅获取文件的相对名称。这意味着仅检索文件名,而不是文件的完整路径。查看您收到的错误:
Error using
getImageFromFile
(line 11)Cannot find the specified file:
"Depth_003.png"
.
你看到上面文件名中你的图片所在的路径了吗?没有!您只能看到存储在目录中的文件。您需要指定图像所在的完整路径。
您需要做的是将目录以及图像本身作为您提供给 imshow
的字符串附加:
myFolderdepth = 'C:\Users\owner\Desktop'; %Specify Directory to get image from
Depth = dir (fullfile(myFolderdepth,'shower_depth','*.png'))%%Get images from file named shower_depth
Depth_name = {Depth.name}'; %gets the name
figure;
imshow(fullfile(myFolderDepth, Depth_name{3})); %// CHANGE HERE
关于matlab - 从matlab中的特定文件夹访问图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27852507/