将图像分成3*3块

Jay*_*Jay 2 size matlab block image-processing pixels

我有一个矩阵,其尺寸不会是3的倍数,也可能是.我们如何将整个图像分成3*3矩阵的块.(可以忽略不属于3*3倍数的最后一个.此外,3*3矩阵可以保存在数组中.

a=3; b=3; %window size
x=size(f,1)/a; y=size(f,2)/b; %f is the original image
m=a*ones(1,x); n=b*ones(1,y);
I=mat2cell(f,m,n);
Run Code Online (Sandbox Code Playgroud)

Cas*_*lho 5

我从未使用mat2cell来划分矩阵,现在考虑它似乎是一个非常好的主意.由于我在这台计算机上没有MATLAB,我将描述我这样做的方式,这不涉及mat2cell.

忽略最后的列和行很容易:

d = 3; % the dimension of the sub matrix
[x,y] = size(f);

% perform integer division by three
m = floor(x/d);
n = floor(y/d);

% find out how many cols and rows have to be left out
m_rest = mod(x,d);
n_rest = mod(y,d);

% remove the rows and columns that won't fit
new_f = f(1:(end-m_rest), 1:(end-n_rest));

%  this steps you won't have to perform if you use mat2cell
% creates the matrix with (m,n) pages 
new_f = reshape( new_f, [ d m d n ] );
new_f = permute( new_f, [ 1 3 2 4 ] );
Run Code Online (Sandbox Code Playgroud)

现在您可以像这样访问子矩阵:

new_f(:,:,1,1) % returns the 1st one

new_f(:,:,3,2) % returns the one at position [3,2]
Run Code Online (Sandbox Code Playgroud)

如果您想使用mat2cell来做到这一点,您可以执行以下操作:

% after creating new_f, instead of the reshape, permute
cells_f = mat2cell(new_f, d*ones(1,m), d*ones(1,n));
Run Code Online (Sandbox Code Playgroud)

然后您将以不同的方式访问它:

cells_f{1,1}
cells_f{3,2}
Run Code Online (Sandbox Code Playgroud)

我无法测试的单元格方法,因为我在这台PC上没有MATLAB,但如果我能正确回想起mat2cell的用法,它应该可以正常工作.

希望能帮助到你 :)