在matlab中访问特定文件夹中的图像

问题描述 投票:2回答:2

有人可以向我解释下面的代码不起作用吗?

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行)无法找到指定的文件:“Depth_003.png”。

我正在使用的目录是:C:\ Users \ owner \ Desktop

图片名称为Depth_001,Depth_002,Depth_003,......

奇怪的是,我有另一个包含图像的文件夹,如果我将'shower_depth'更改为另一个文件夹名称,它可以正常工作。

谢谢! P.S我做了一些进一步的实验,结果是因为图像的命名方式;如果它的Depth_01.png很好用,但是Depth_001.png不行

谁知道为什么?

matlab image-processing
2个回答
3
投票

以下命令:

Depth = dir (fullfile(myFolderdepth,'shower_depth','*.png'))

只获取文件的相对名称。这意味着仅检索文件名,而不是文件的完整路径。看看你得到的错误:

使用getImageFromFile时出错(第11行)

找不到指定的文件:"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

-1
投票

Depth_name只是图像名称。您必须在显示之前阅读图像。修改后的代码如下:

im = imread(Depth_name{3});
imshow(Depth_name{3});
© www.soinside.com 2019 - 2024. All rights reserved.