Image 从MATLAB中的函数返回4D数组
我试图从MATLAB中的函数调用返回4D图像数据数组。我对MATLAB不是很在行,我不知道从函数返回什么类型的数据。以下是我的功能:Image 从MATLAB中的函数返回4D数组,image,matlab,multidimensional-array,types,return-type,Image,Matlab,Multidimensional Array,Types,Return Type,我试图从MATLAB中的函数调用返回4D图像数据数组。我对MATLAB不是很在行,我不知道从函数返回什么类型的数据。以下是我的功能: function classimg = loadImages(classdir,ext) % create array of all images in directory neg = dir([classdir ext]); % get size of array (to loop through images) numFileN
function classimg = loadImages(classdir,ext)
% create array of all images in directory
neg = dir([classdir ext]);
% get size of array (to loop through images)
numFileNeg = max(size(neg));
% create a 4D array to store our images
classimg = zeros(51,51,3,numFileNeg);
% loop through directory
for i=1:numFileNeg
classimg(:,:,:,i) = imread([myDir neg(i).name]);
end
end
function classimg = loadImages(classdir, ext)
neg = dir(fullfile(classdir, ext));
numFileNeg = numel(neg);
tempImage = imread(fullfile(classdir, neg(1).name));
classimg = zeros(51, 51, 3, numFileNeg, class(tempImage));
classimg(:, :, :, 1) = tempImage;
for i = 2:numFileNeg
classimg(:, :, :, i) = imread(fullfile(classdir, neg(i).name));
end
end
下面是函数调用:
negativeImgs = loadImages("C:\Users\example\Documents\TrainingImages\negatives\","*.jpg");
我找不到有关退货类型的任何在线文档?有人知道这会是什么吗
classimg
已正确填充,因此代码内部工作。您将classimg初始化为一个51x51x3xnumFileNeg零矩阵。使用zeros函数,因此数据类型为double。要清楚地看到这一点,请从命令窗口调用函数,然后键入“whos”以查看classimg的大小和数据类型。,因为您使用初始化classimg
,默认数据类型是,图像数据将从返回的任何数据类型转换为双精度()
如果希望classimg
与图像的数据类型相同(我假设所有图像都具有相同的类型),则可以加载一个图像,获取其类型,并使用该特定类初始化classimg
。以下是如何重写函数:
function classimg = loadImages(classdir,ext)
% create array of all images in directory
neg = dir([classdir ext]);
% get size of array (to loop through images)
numFileNeg = max(size(neg));
% create a 4D array to store our images
classimg = zeros(51,51,3,numFileNeg);
% loop through directory
for i=1:numFileNeg
classimg(:,:,:,i) = imread([myDir neg(i).name]);
end
end
function classimg = loadImages(classdir, ext)
neg = dir(fullfile(classdir, ext));
numFileNeg = numel(neg);
tempImage = imread(fullfile(classdir, neg(1).name));
classimg = zeros(51, 51, 3, numFileNeg, class(tempImage));
classimg(:, :, :, 1) = tempImage;
for i = 2:numFileNeg
classimg(:, :, :, i) = imread(fullfile(classdir, neg(i).name));
end
end
请注意,我还做了一些其他更改。我使用的不是目录名和文件名的串联,因为它可以为您处理任何问题。我还经常得到文件的数量。不清楚你的问题是什么?MATLAB不是静态类型,返回值可以是任何类型。所以只需运行函数并查看返回值!也就是说,AFAIK
imread
将返回一个包含uint8值的MxNx3数组,因此您的返回值很可能是uint8的MxNx3xJ。另外,最好使用numFileNeg=numel(neg)
或numFileNeg=length(neg)
而不是numFileNeg=max(size(neg))