1

我有一个m.n图像矩阵,如下所示:

images = zeros( m, n, height, width );

这意味着我有m.n给定宽度和高度的图像。然后,在一个 for 循环中;我将这些图像填​​充为:

for i=1:m
    for j=1:n
       images(i,j,:,:) = imread('imagePath');
    end
end

然后,假设我想使用图像(1,1)

image1 = images(1,1,:,:);

我希望这image1size = (h,w)。然而,当我说:

size(image1)

我得到结果:

(1,1,h,w)

问题:

1.

为什么我没有以下结果?

(h,w)

2.

如何重建我的代码以获得我的预期结果?

4

2 回答 2

1

It has to do with how matlab does indexing. When you say

image1 = images(1,1,:,:);

You're telling matlab you want a 4 dimensional array, with first and second dimensions of size 1.

Where as, if you had said:

junk = images(:,:,1,1);
size(junk)
> [m,n]

Matlab treats a matrix of size [m,n] the same as if it were of size [m,n,1] or [m,n,1,1]. Can't do that on the front, thus the need for squeeze as @Junuxx points out. An alternative approach is to do thing as follows:

images = zeros( height, width, m, n );
for i=1:m
    for j=1:n
       images(:,:,m,n) = imread('imagePath');
    end
end
image1 = images(:,:,1,1);
于 2012-06-06T15:00:42.017 回答
1

您可以使用挤压功能来做到这一点:)

image1 = squeeze(image1);
size(image1)

应该给

(h,w)
于 2012-06-06T14:54:48.600 回答