首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将图像分成3*3个块

将图像分成3*3个块
EN

Stack Overflow用户
提问于 2012-04-02 15:54:28
回答 1查看 3.8K关注 0票数 2

我有一个矩阵,它的维数不是3的倍数,也可能是。我们如何将整个图像分成3*3个矩阵的块。(可以忽略不在3*3倍数内的最后一个。此外,3*3矩阵可以保存在数组中。

代码语言:javascript
复制
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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-02 19:26:32

我从来没有用过mat2cell来除矩阵,现在想想,这似乎是一个非常好的想法。因为我在这台计算机上没有MATLAB,所以我将描述我做这件事的方法,这不涉及mat2cell。

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

代码语言:javascript
复制
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 ] );

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

代码语言:javascript
复制
new_f(:,:,1,1) % returns the 1st one

new_f(:,:,3,2) % returns the one at position [3,2]

如果你想使用mat2cell来做这件事,你可以这样做:

代码语言:javascript
复制
% after creating new_f, instead of the reshape, permute
cells_f = mat2cell(new_f, d*ones(1,m), d*ones(1,n));

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

代码语言:javascript
复制
cells_f{1,1}
cells_f{3,2}

cell方法我不能测试,因为我在这台电脑上没有MATLAB,但如果我能正确地回忆起mat2cell的用法,它应该工作得很好。

希望它能有所帮助:)

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9972684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档