如何在matlab中将图像移动到单元格中?

问题描述 投票:0回答:1

我建立了一个名为N的1 * 5单元格,需要将图像(img)矩阵复制到其中的每个条目中,我该怎么办?这是我提出的但它不起作用....我试图避免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
matlab vectorization cell cell-array
1个回答
0
投票

从我对你的问题的理解,并在循环部分与Cris Luengo达成一致,这就是我的建议。我假设,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

一个小的测试脚本:

% 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

输出:

ans =
   738   738

ans =
   624   624

ans =
   504   504

ans =
   300   300

ans =
   60   60
© www.soinside.com 2019 - 2024. All rights reserved.