[英]How do I move an image into a cell in matlab?
I built a 1*5 cell named N and need to copy an image (img)matrix into each entry in it what should I do? 我建立了一个名为N的1 * 5单元,需要将图像(img)矩阵复制到其中的每个条目中,该怎么办? This is what I came up with but it doesn't work.... I'm trying to avoid for loops so my code will be faster. 这是我想出的,但是它不起作用。...我试图避免for循环,以便我的代码更快。
function newImgs = imresizenew(img,scale) %scale is an array contains the scaling factors to be upplied originaly an entry in a 16*1 cell
N = (cell(length(scale)-1,1))'; %scale is a 1*6 vector array
N(:,:) = mat2cell(img,size(img),1); %Now every entry in N must contain img, but it fails
newImgs =cellfun(@imresize,N,scale,'UniformOutput', false); %newImgs must contain the new resized imgs
end
From what I've understood from your question, and agreeing with Cris Luengo on the loop part, here is what I would suggest. 根据您对问题的理解,并在循环部分上同意Cris Luengo的观点,这就是我的建议。 I assume, that scale(1) = 1
or something like that, because you initialize N = (cell(length(scale) - 1, 1))'
, so I guess one of the values in scale
is not important. 我假设scale(1) = 1
或类似的东西,因为您初始化了N = (cell(length(scale) - 1, 1))'
,所以我猜scale
中的值之一并不重要。
function newImgs = imresizenew(img, scale)
% Initialize cell array.
newImgs = cell(numel(scale) - 1, 1);
% Avoid copying of img and using cellfun by directly filling
% newImgs with properly resized images.
for k = 1:numel(newImgs)
newImgs(k) = imresize(img, scale(k + 1));
end
end
A small test script: 一个小的测试脚本:
% Input
img = rand(600);
scale = [1, 1.23, 1.04, 0.84, 0.5, 0.1];
% Call own function.
newImgs = imresizenew(img, scale);
% Output dimensions.
for k = 1:numel(newImgs)
size(newImgs{k})
end
Output: 输出:
ans =
738 738
ans =
624 624
ans =
504 504
ans =
300 300
ans =
60 60
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.