Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 从MATLAB中的文件夹中读取多个.img文件时出错_Image_Matlab_Image Processing - Fatal编程技术网

Image 从MATLAB中的文件夹中读取多个.img文件时出错

Image 从MATLAB中的文件夹中读取多个.img文件时出错,image,matlab,image-processing,Image,Matlab,Image Processing,我有100个.img文件,我正试图使用以下代码从目录中读取这些文件: srcFiles = dir('/Users/Adrian/Documents/Foam_Data/ssd0/2013-10-25_09-01-12/000000/*.img'); % the folder in which ur images exists for i = 1:100 % length(srcFiles) filename = srcFiles(i).name; fid = fope

我有100个.img文件,我正试图使用以下代码从目录中读取这些文件:

srcFiles = dir('/Users/Adrian/Documents/Foam_Data/ssd0/2013-10-25_09-01-12/000000/*.img'); % the folder in which ur images exists

for i = 1:100   % length(srcFiles)

     filename = srcFiles(i).name;
    fid = fopen(filename);
    image = fread(fid, 2048*2048, 'uint8=>uint8');
    fclose(fid);
    image = reshape(image, 2048, 2048);
    figure;
    imshow(image);

end
'/Users/Adrian/Documents/Foam_Data/ssd0/2013-10-25_09-01-12/000000/'
是my.img文件所在目录的路径。似乎我在定义文件标识符时出错了,但我不知道我遗漏了什么:

Error using fread
Invalid file identifier.  Use fopen to generate a valid file identifier.

Error in sequenceimage (line 32)
    image = fread(fid, 2048*2048, 'uint8=>uint8');

有人能帮我纠正这个错误吗

出现该错误的原因是
dir
为列出的每个文件返回相对的名称,而不是每个文件的绝对路径。同样地,通过执行
srcFiles(i).name
,您将只获得文件名本身,而不是文件的完整路径

因此,在调用
fopen
时,需要将目录附加到文件本身的顶部

为了使事情更灵活,将目录放在一个单独的字符串中,这样您只需在一个地方而不是两个地方修改代码

非常简单:

%// Change here
loc = '/Users/Adrian/Documents/Foam_Data/ssd0/2013-10-25_09-01-12/000000/';

%// Change here
srcFiles = dir([loc '*.img']); % the folder in which ur images exists

for i = 1:100   % length(srcFiles)

     filename = srcFiles(i).name;

    %// Change here!
    fid = fopen([loc filename]);
    image = fread(fid, 2048*2048, 'uint8=>uint8');
    fclose(fid);
    image = reshape(image, 2048, 2048);
    figure;
    imshow(image);

end

出现该错误的原因是
dir
为列出的每个文件返回相对的名称,而不是每个文件的绝对路径。同样地,通过执行
srcFiles(i).name
,您将只获得文件名本身,而不是文件的完整路径

因此,在调用
fopen
时,需要将目录附加到文件本身的顶部

为了使事情更灵活,将目录放在一个单独的字符串中,这样您只需在一个地方而不是两个地方修改代码

非常简单:

%// Change here
loc = '/Users/Adrian/Documents/Foam_Data/ssd0/2013-10-25_09-01-12/000000/';

%// Change here
srcFiles = dir([loc '*.img']); % the folder in which ur images exists

for i = 1:100   % length(srcFiles)

     filename = srcFiles(i).name;

    %// Change here!
    fid = fopen([loc filename]);
    image = fread(fid, 2048*2048, 'uint8=>uint8');
    fclose(fid);
    image = reshape(image, 2048, 2048);
    figure;
    imshow(image);

end

调用
fid
的实际输出是什么?也就是说,当你删除半冒号时输出什么?既然我们已经解决了这个问题,请考虑在这里接受我的答案。这意味着StackOverflow社区不再需要这方面的帮助。祝你好运调用
fid
的实际输出是什么?也就是说,当你删除半冒号时输出什么?既然我们已经解决了这个问题,请考虑在这里接受我的答案。这意味着StackOverflow社区不再需要这方面的帮助。祝你好运谢谢你的回答。我以前试过这个,确实没有任何错误。但是我的图像是暗的,我不知道为什么,因为我可以生成单个图像,但是当我尝试多个图像时,我会得到暗的图像。我会得到一个警告,告诉我图像太大,无法适应屏幕,它只以33%的速度显示。很有趣。你能上传其中一张图片吗?我想让我自己看看出了什么问题。不要认为这与代码有关。这可能是像素的布局方式。这些像素是主要的行还是列布局?谢谢你的回答。我以前试过这个,确实没有任何错误。但是我的图像是暗的,我不知道为什么,因为我可以生成单个图像,但是当我尝试多个图像时,我会得到暗的图像。我会得到一个警告,告诉我图像太大,无法适应屏幕,它只以33%的速度显示。很有趣。你能上传其中一张图片吗?我想让我自己看看出了什么问题。不要认为这与代码有关。这可能是像素的布局方式。这些像素在布局中是主要的行还是列?