像素标签数据存储从工作区中加载的图像



我有多个小的 *.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')

您应该能够使用标准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

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

load('images.mat','imds')        % loads "imds" variable
load('images.mat','pxds')        % loads "pxds" variable
load('images.mat','imds','pxds') % loads both variables

编辑

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

编辑 2

是否绝对有必要为此任务使用imageDatastore?如果没有,您可以使用如下所示的内容:

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

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

看起来像这样:

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

在你的主文件中,你必须简单地将data分成4块。

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

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

不确定这是否有效。

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

最新更新