pixelLabelDatastore来自工作区中的已加载图像

use*_*193 7 matlab deep-learning

我有多个小*.mat文件,每个包含4个输入图像(template{1:4}和第二个通道template2{1:4})和4个输出图像(region_of_interests{1:4}),二进制('掩码')图像来训练深度神经网络.

我基本上遵循Mathworks上的一个示例,它建议使用一个函数(在本例中@matreader)来读取自定义文件格式.

但是......

  1. 使用任何加载函数从一个*.mat文件加载多个图像似乎是不可能的,因为它只允许一个输出,而imageDatastore似乎不允许从工作区加载数据.怎么能实现这一目标?
  2. 同样,似乎无法pixelLabelDatastore从工作空间变量加载.作为一种解决方法,我最终将我的*.mat文件的内容imwrite保存到图像(使用,保存到save_dir),并从那里重新加载(在这种情况下,该函数甚至不允许加载*.mat文件).如何在不将文件重新保存为图像的情况下实现?

在这里,我失败了尝试这样做:


%main script
image_dir = pwd; %location of *.mat files
save_dir  = [pwd '/a/']; %location of saved output masks
imds = imageDatastore(image_dir,'FileExtensions','.mat','ReadFcn',@matreader); %load template (input) images
pxds = pixelLabelDatastore(save_dir,{'nothing','something'},[0 255]);%load region_of_interests (output) image

%etc, etc, go on to train network

%matreader function, save as separate file
function data=matreader(filename)
  in=1; %give up the 3 other images stored in template{1:4}
  load(filename); %loads template and template2, containing 4x input images each
  data=cat(3,template{in},template2{in}); %concatinate 2 template input images in 3rd dimension
end

%generate example data for this question, will save into a file 'example.mat' in workspace
for ind=1:4
  template{ind}=rand([200,400]);
  template2{ind}=rand([200,400]);
  region_of_interests{ind}=rand([200,400])>.5;
end
save('example','template','template2','output')
Run Code Online (Sandbox Code Playgroud)

use*_*005 3

您应该能够使用标准loadsave函数来实现这一点。看看这段代码:

image_dir = pwd;
save_dir = pwd;

imds = imageDatastore(image_dir,'FileExtensions',{'.jpg','.tif'});
pxds = pixelLabelDatastore(save_dir,{'nothing','something'},[0 255]);
save('images.mat','imds', 'pxds')

clear
load('images.mat')  % gives you the variable "imds" and "pxds" directly -> might override previous variables
tmp = load('images.mat'); % saves all variables in a struct, access it via tmp.imds and tmp.pxds
Run Code Online (Sandbox Code Playgroud)

如果您只想选择要加载的变量,请使用:

load('images.mat','imds')        % loads "imds" variable
load('images.mat','pxds')        % loads "pxds" variable
load('images.mat','imds','pxds') % loads both variables
Run Code Online (Sandbox Code Playgroud)

编辑

现在我遇到了问题,但我担心这不是它的工作原理。对象背后的想法Datastore是,如果数据太大而无法作为一个整体放入内存,但每个小部分都足够小以适合内存,则使用它。您可以使用该Datastore对象轻松处理和读取磁盘上的多个文件。这对您来说意味着:只需将图像保存为仅包含一张图像*mat的多个小文件,而不是*.mat一个大文件。

编辑2

是否绝对有必要使用 animageDatastore来完成此任务?如果没有,您可以使用类似以下内容:

image_dir = pwd;
matFiles = dir([image_dir '*.mat']);
for i=1:length(matFiles)
    data = load(matFiles(i).name);
    img = convertMatToImage(data); % write custom function which converts the mat input to your image
    % or something like this:
    % for j=1:4
        % img(:,:,j) = cat(3,template{j},template2{j});
    % end

    % process image
end
Run Code Online (Sandbox Code Playgroud)

另一种选择是在“matreader”中创建一个“图像”,它不仅有2个带,而且简单地将所有带(所有模板)放在彼此之上,提供“数据立方体”,然后在迭代后进行第二步遍历所有小 mat 文件并读取它们,将单个图像从一个更大的数据立方体中分离出来。

看起来像这样:

function data=matreader(filename)
load(filename);
for in=1:4
    data=cat(3,template{in},template2{in}); 
end
end
Run Code Online (Sandbox Code Playgroud)

在你的主文件中,你只需将其分成data4 部分。

我从未测试过它,但也许可以返回单元格而不是矩阵?

function data=matreader(filename)
load(filename);
data = cell(1,4)
for in=1:4
    data{in}=cat(3,template{in},template2{in}); 
end
end
Run Code Online (Sandbox Code Playgroud)

不确定这是否有效。

然而,从这里继续前进的正确方法实际上取决于您计划如何使用来自的图像imds以及是否确实有必要使用imageDatastore.