Matlab将图像分割为8x8 2D阵列并将其保存在另一个阵列中

时间:2014-07-07 21:34:54

标签: arrays matlab

下面是我的代码,我只能保存图像的最后一个数组(88位置)。我知道错误是在第11行,但无论我如何尝试我似乎无法使W11成为一个3D阵列。请帮忙吗?

Ie=imread('Untitled-1.014.jpg');  % size 256x160
len = 256/8 ;
wid = 160/8 ;

for m = 1:8
    x1(m) = (len*m);
end

for n = 1:8
    y1(n)= (wid*n);
end

for m = 1:8
    x2(m)= (len*m)-len+1;
end

for n = 1:8
    y2(n)= (wid*n)-wid+1;
end

for m = 1:8
    for n=1:8
        w11 = Ie(x2(m):x1(m),y2(n):y1(n));
    end
end

1 个答案:

答案 0 :(得分:1)

据我了解,您希望在小的8x8块中分割完整的2D图像,并将所有这些块保存在3D阵列中。为此,w11必须从头开始为3D,并且您有子分配元素:

% The image (replaced with random data)
height = 256;
width = 160;        
Ie = rand(height, width);

% Preallocation of smaller 8x8 blocks in 3D
blocksize = 8;
nw = width / blocksize;
nh = height / blocksize;
blockCount = nw*nh;
blocks = zeros(blocksize, blocksize, blockCount);

% Splitting image
index = 1;
sub = (1:blocksize);    
for wi = 1: nw,

    wsub =  sub + (wi-1)*blocksize; % sub indices along width

    for hi = 1: nh,

        hsub = sub + (hi-1)*blocksize; % sub indices along height

        blocks(:, :, index) = Ie(hsub, wsub); % subset assignement
        index = index + 1;

    end
end

然后您可以像这样访问较小的块:

block = blocks(:,:,5); % The 5th block (of size 8x8)

注意:我将块索引作为最后一个维度,以便自动挤压尾随单一维度(这样可以避免一直调用squeeze